Recent

Author Topic: Fiber Library  (Read 7292 times)

Fiji

  • Guest
Fiber Library
« on: July 29, 2014, 11:07:24 am »
I am trying to port this Delphi library to FPC however the problem is assembler. If ported it could run on linux, android etc.

I thought it can be ported to 64 bit just by changing EAX->RAX but I was wrong...

Laksen

  • Hero Member
  • *****
  • Posts: 754
    • J-Software
Re: Fiber Library
« Reply #1 on: July 29, 2014, 01:42:37 pm »
That's some very Windows 32-bit specific code. There won't be an easy way to get that running on Linux or any other processor type

Fiji

  • Guest
Re: Fiber Library
« Reply #2 on: July 29, 2014, 02:10:08 pm »
Well it doesn't use any WINAPI code. Only stack swapping and exception handling.

ttomas

  • Full Member
  • ***
  • Posts: 245
Re: Fiber Library
« Reply #3 on: July 29, 2014, 02:52:14 pm »
This is good starting point for porting to FPC and be cross platform.
Raudus have similar functionality and I think is cross platform (free but not open source)
http://raudus.wikispaces.com/Internals+-+Multithreading
This will be ideal for async TCP/HTTP servers. In OnReceive event you can write something like this:
Code: [Select]
  // Prepare DB Requests
  RaApplication.Application.EnterMultithreadSection;
  try
    // this code will work in working thread and don't block event loop
    ...
    // DB Access and long time processing
  finally
    RaApplication.Application.LeaveMultithreadSection;
  end;
  // Return to main thread (event loop thread)
  // Process Response
or in Fiber DemoForm.pas
Code: [Select]
        try
          // Switch to worker thread

          TFiber.Current.SwitchToWorkerThread;
          Http := TIdHttp.Create(nil);
          Http.AllowCookies := True;
          Http.HandleRedirects := True;
          Http.Get('http://www.cnn.com');
          Http.Free;

        finally
          // Switch back to main thread
          TFiber.Current.SwitchToMainThread;
        end;
« Last Edit: July 29, 2014, 02:57:14 pm by ttomas »

Fiji

  • Guest
Re: Fiber Library
« Reply #4 on: July 29, 2014, 03:50:04 pm »
At this point its only a dream unless some ASM expert steps up.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Fiber Library
« Reply #5 on: July 29, 2014, 04:00:52 pm »
Well it doesn't use any WINAPI code. Only stack swapping and exception handling.
ActiveX? Sure it doesn't use any Windows-ism? It does use WinAPI's threading solution. And x86-ism as well.

Fiji

  • Guest
Re: Fiber Library
« Reply #6 on: July 29, 2014, 04:05:27 pm »
Well no it does not use any ActiveX it seems you haven't even looked at it.
Besides TThread is available on FPC aswell. Only ASM code is the problem.

Code: [Select]
unit DphThreadPool;

interface

uses Windows, SysUtils, Classes, SyncObjs;

unit DphFibers;

interface

uses Windows, Classes, SysUtils{$ifdef threading}, DphThreadPool{$endif};

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Fiber Library
« Reply #7 on: July 29, 2014, 04:08:42 pm »
Well no it does not use any ActiveX it seems you haven't even looked at it.
Besides TThread is available on FPC aswell. Only ASM code is the problem.

Code: [Select]
unit DphThreadPool;

interface

uses Windows, SysUtils, Classes, SyncObjs;

unit DphFibers;

interface

uses Windows, Classes, SysUtils{$ifdef threading}, DphThreadPool{$endif};
Look at the implementation's uses clause. Even if you commented it out, look at those CoInitializeEx, CoUninitialize, CreateSemaphore, CreateEvent and their friends. Haven't looked at it? Perhaps you should dig it deeper first. Try compiling on non-Windows platform, you'll see it immediately.

Fiji

  • Guest
Re: Fiber Library
« Reply #8 on: July 29, 2014, 04:09:43 pm »
CoInitializeEx, CoUninitialize can be removed. The rest are standard sync objects.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Fiber Library
« Reply #9 on: July 29, 2014, 04:14:45 pm »
CoInitializeEx, CoUninitialize can be removed. The rest are standard sync objects.
Certainly no, they don't exist in my Linux. I (and Laksen) told you it's Windows specific, still don't believe? I said try it on a non-Windows OS. You can't see what we, non-Windows users see, if you still develop on Windows box. Cross platform development requires respective target environment to test, either directly or emulated.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Fiber Library
« Reply #10 on: July 29, 2014, 04:27:55 pm »
Note that the pascal code also contains 64-bit unclean code, which needs fixes, also for Delphi 64-bit.

Code: [Select]
  FFiberContext.CESP := Pointer(Cardinal(Stack) + StackSize);
This typecasts a 64-bit type (stack) to a 32-bit type (cardinal).

Some other things:
    1. Also it might be possible to replace the "event" stuff with TEvent, (depending of the set of parameters to createvent is supported by TEvent in some way).
    2. If that doesn't work, there is basic semaphore and event stuff in the FPC threadmanager (the platform dependent part that the OS independent part interfaces to).
    3. The WaitForMultipleObjects is going to be difficult. Classically, Linux doesn't have this kind of primitive for semaphores. (only for fileselectors with Select())
    4. The code uses TThread.Queue() a lot which is only available in trunk versions of FPC.

Note that before porting it to Linux I would first time the linux thread implementation, and see if you can reduce the stack size with other means to reach high thread counts. The dynamics of all thread libraries are different.
« Last Edit: July 29, 2014, 04:33:50 pm by marcov »

Fiji

  • Guest
Re: Fiber Library
« Reply #11 on: July 29, 2014, 04:28:40 pm »
So it can be ported? Without the activex stuff?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Fiber Library
« Reply #12 on: July 29, 2014, 04:34:50 pm »
So it can be ported? Without the activex stuff?

To win64: maybe. Still would require some knowledge.

Not without fully understanding both the current windows implementation and everything related on Linux.

 "Fibers" as a concept is already Windows centric.

P.s. updated the above answer some more after a second look.

Fiji

  • Guest
Re: Fiber Library
« Reply #13 on: July 29, 2014, 04:46:37 pm »
Here is a cross platform solution in C++

https://github.com/mozy/mordor

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Fiber Library
« Reply #14 on: July 29, 2014, 05:09:26 pm »
Here is a cross platform solution in C++

https://github.com/mozy/mordor

Good luck porting that (AND Boost)

 

TinyPortal © 2005-2018