[fpc-pascal] What to use when porting WaitForMultipleObjects to Linux?

Dmitry Boyarintsev skalogryz.lists at gmail.com
Tue May 17 05:34:15 CEST 2016


On Mon, May 16, 2016 at 9:01 AM, Bo Berglund <bo.berglund at gmail.com> wrote:

> The Execute procedure handles everything going on and it acts on event
> signals picked up (I believe) by the WaitForMultipleObjects calls.
>
> What I would like to know is how exactly to handle this in FreePascal
> on Linux....
>

I presume  FConnectEvent, FDisconnectEvent (and others) are of TEvent type
(from FCLs syncobjs)

In this case, I'd think you could implement WaitForMultipleObjects()
yourself, via a simple event waiting loop:

uses ... syncobjs, SysUtils ;

const
  WAIT_TIMEOUT = $102;

function WaitForMultipleObject(cnt : integer; const handle: array of
TEventHandle;
  waitforall: Boolean; timeOutMls: integer): integer;
var
  event : Integer;
  evNum : integer;
  i     : integer;
  ev    : array of boolean;
  wr    : TWaitResult;
  any   : Boolean;
  d     : TDateTime; // please forgive the usage of now() :)
  dm    : integer;
const
  YIELD_TIME=0;
begin
  evNum:=0;
  SetLength(ev, cnt);
  Result:=WAIT_TIMEOUT;
  // create a loop of waiting for each event to fire
  repeat
    any:=false;
    if timeOutMls>0 then d:=now;

    for i:=0 to cnt-1 do
      if not ev[i] then begin
        // refer to synobj sources for .WaitFor() method of TEvent
        wr:=TWaitResult(basiceventWaitFor(0, Handle[i]));
        if wr=wrSignaled then begin
          ev[i]:=true;
          inc(evNum);
          Result:=i;
          if not waitforall then Break;
        end;
      end;

    if not any and (timeOutMls>0) then begin
      // if has timeout, must yeld the time for some time
      // and then repeat the try
      Sleep(YIELD_TIME);
      if timeOutMls>0 then begin
        d:=now-d;
        dm:=round(d*MSecsPerDay);
        dec(timeOutMls, dm);
        if (timeOutMls<0) then timeOutMls:=0;
      end;
    end;
    // btw, actual support for "waitforall" is not needed in this case
  until (evNum>0) and (not waitforall or (cnt=evNum)) or (timeOutMls=0);
  if waitforall and (evNum>0) then Result:=0;
end;

I've not tested this loop, but it should give you an idea of what you could
do.

The fact that you're using the handles of the same type allows you to
implement the loop yourself.

thanks,
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160516/b6c35ccb/attachment.html>


More information about the fpc-pascal mailing list