hedgewars/uFLIPC.pas
branchqmlfrontend
changeset 10410 669bfa55cd70
parent 10406 b5fd52ac760f
child 10412 9a8d4efcf3fa
equal deleted inserted replaced
10408:5fdc42ef5bcc 10410:669bfa55cd70
     1 unit uFLIPC;
     1 unit uFLIPC;
       
     2 interface
       
     3 uses SDLh;
     2 
     4 
     3 interface
     5 type TIPCMessage = record
       
     6                    str: shortstring;
       
     7                    len: Longword;
       
     8                    buf: Pointer
       
     9                end;
       
    10 
       
    11 var msgFrontend, msgEngine: TIPCMessage;
       
    12     mutFrontend, mutEngine: PSDL_mutex;
       
    13     condFrontend, condEngine: PSDL_cond;
       
    14 
       
    15 procedure initIPC;
       
    16 procedure freeIPC;
       
    17 
       
    18 procedure ipcToEngine(s: shortstring);
       
    19 function  ipcReadFromEngine: shortstring;
       
    20 function  ipcCheckFromEngine: boolean;
     4 
    21 
     5 implementation
    22 implementation
     6 
    23 
       
    24 procedure ipcToEngine(s: shortstring);
       
    25 begin
       
    26     SDL_LockMutex(mutEngine);
       
    27     while (msgEngine.str[0] > #0) or (msgEngine.buf <> nil) do
       
    28         SDL_CondWait(condEngine, mutEngine);
       
    29 
       
    30     msgEngine.str:= s;
       
    31     SDL_CondSignal(condEngine);
       
    32     SDL_UnlockMutex(mutEngine)
       
    33 end;
       
    34 
       
    35 function ipcReadFromEngine: shortstring;
       
    36 begin
       
    37     SDL_LockMutex(mutFrontend);
       
    38     while (msgFrontend.str[0] = #0) and (msgFrontend.buf = nil) do
       
    39         SDL_CondWait(condFrontend, mutFrontend);
       
    40 
       
    41     ipcReadFromEngine:= msgFrontend.str;
       
    42     msgFrontend.str[0]:= #0;
       
    43     if msgFrontend.buf <> nil then
       
    44     begin
       
    45         FreeMem(msgFrontend.buf, msgFrontend.len);
       
    46         msgFrontend.buf:= nil
       
    47     end;
       
    48 
       
    49     SDL_CondSignal(condFrontend);
       
    50     SDL_UnlockMutex(mutFrontend)
       
    51 end;
       
    52 
       
    53 function ipcCheckFromEngine: boolean;
       
    54 begin
       
    55     SDL_LockMutex(mutFrontend);
       
    56     ipcCheckFromEngine:= (msgFrontend.str[0] > #0) or (msgFrontend.buf <> nil);
       
    57     SDL_UnlockMutex(mutFrontend)
       
    58 end;
       
    59 
       
    60 procedure initIPC;
       
    61 begin
       
    62     msgFrontend.str:= '';
       
    63     msgFrontend.buf:= nil;
       
    64     msgEngine.str:= '';
       
    65     msgEngine.buf:= nil;
       
    66 
       
    67     mutFrontend:= SDL_CreateMutex;
       
    68     mutEngine:= SDL_CreateMutex;
       
    69     condFrontend:= SDL_CreateCond;
       
    70     condEngine:= SDL_CreateCond;
       
    71 end;
       
    72 
       
    73 procedure freeIPC;
       
    74 begin
       
    75     SDL_DestroyMutex(mutFrontend);
       
    76     SDL_DestroyMutex(mutEngine);
       
    77     SDL_DestroyCond(condFrontend);
       
    78     SDL_DestroyCond(condEngine);
       
    79 end;
       
    80 
     7 end.
    81 end.