author | unc0rr |
Sun, 28 Sep 2014 00:49:04 +0400 | |
branch | qmlfrontend |
changeset 10428 | 7c25297720f1 |
parent 10426 | 727a154cf784 |
child 10898 | f373838129c2 |
permissions | -rw-r--r-- |
10406 | 1 |
unit uFLIPC; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
2 |
interface |
10418
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
3 |
uses SDLh, uFLTypes; |
10406 | 4 |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
5 |
var msgFrontend, msgEngine: TIPCMessage; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
6 |
mutFrontend, mutEngine: PSDL_mutex; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
7 |
condFrontend, condEngine: PSDL_cond; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
8 |
|
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
9 |
procedure initIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
10 |
procedure freeIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
11 |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
12 |
procedure ipcToEngine(s: shortstring); |
10420 | 13 |
//function ipcReadFromEngine: shortstring; |
14 |
//function ipcCheckFromEngine: boolean; |
|
10406 | 15 |
|
10412 | 16 |
procedure ipcToFrontend(s: shortstring); |
10420 | 17 |
procedure ipcToFrontendRaw(p: pointer; len: Longword); |
10412 | 18 |
function ipcReadFromFrontend: shortstring; |
19 |
function ipcCheckFromFrontend: boolean; |
|
20 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
21 |
procedure registerIPCCallback(p: pointer; f: TIPCCallback); |
10416 | 22 |
|
10406 | 23 |
implementation |
24 |
||
10416 | 25 |
var callbackPointer: pointer; |
26 |
callbackFunction: TIPCCallback; |
|
27 |
callbackListenerThread: PSDL_Thread; |
|
28 |
||
10420 | 29 |
procedure ipcSend(var s: TIPCMessage; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond); |
10412 | 30 |
begin |
31 |
SDL_LockMutex(mut); |
|
10418
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
32 |
|
10412 | 33 |
while (msg.str[0] > #0) or (msg.buf <> nil) do |
34 |
SDL_CondWait(cond, mut); |
|
35 |
||
10420 | 36 |
msg:= s; |
10412 | 37 |
SDL_CondSignal(cond); |
10416 | 38 |
SDL_UnlockMutex(mut); |
10412 | 39 |
end; |
40 |
||
10420 | 41 |
function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): TIPCMessage; |
42 |
var tmp: pointer; |
|
10412 | 43 |
begin |
44 |
SDL_LockMutex(mut); |
|
45 |
while (msg.str[0] = #0) and (msg.buf = nil) do |
|
46 |
SDL_CondWait(cond, mut); |
|
47 |
||
10420 | 48 |
if msg.buf <> nil then |
49 |
begin |
|
50 |
tmp:= msg.buf; |
|
51 |
msg.buf:= GetMem(msg.len); |
|
52 |
Move(tmp^, msg.buf^, msg.len); |
|
53 |
FreeMem(tmp, msg.len) |
|
54 |
end; |
|
55 |
||
56 |
ipcRead:= msg; |
|
10418
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
57 |
|
10412 | 58 |
msg.str[0]:= #0; |
10420 | 59 |
msg.buf:= nil; |
10412 | 60 |
|
61 |
SDL_CondSignal(cond); |
|
62 |
SDL_UnlockMutex(mut) |
|
63 |
end; |
|
64 |
||
65 |
function ipcCheck(var msg: TIPCMessage; mut: PSDL_mutex): boolean; |
|
66 |
begin |
|
67 |
SDL_LockMutex(mut); |
|
68 |
ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil); |
|
69 |
SDL_UnlockMutex(mut) |
|
70 |
end; |
|
71 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
72 |
procedure ipcToEngine(s: shortstring); |
10420 | 73 |
var msg: TIPCMessage; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
74 |
begin |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
75 |
msg.str:= s; |
10420 | 76 |
msg.buf:= nil; |
77 |
ipcSend(msg, msgEngine, mutEngine, condEngine) |
|
10412 | 78 |
end; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
79 |
|
10412 | 80 |
procedure ipcToFrontend(s: shortstring); |
10420 | 81 |
var msg: TIPCMessage; |
10412 | 82 |
begin |
10420 | 83 |
msg.str:= s; |
84 |
msg.buf:= nil; |
|
85 |
ipcSend(msg, msgFrontend, mutFrontend, condFrontend) |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
86 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
87 |
|
10420 | 88 |
procedure ipcToFrontendRaw(p: pointer; len: Longword); |
89 |
var msg: TIPCMessage; |
|
90 |
begin |
|
91 |
msg.str[0]:= #0; |
|
92 |
msg.len:= len; |
|
93 |
msg.buf:= GetMem(len); |
|
94 |
Move(p^, msg.buf^, len); |
|
95 |
ipcSend(msg, msgFrontend, mutFrontend, condFrontend) |
|
96 |
end; |
|
97 |
||
98 |
function ipcReadFromEngine: TIPCMessage; |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
99 |
begin |
10412 | 100 |
ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend) |
101 |
end; |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
102 |
|
10412 | 103 |
function ipcReadFromFrontend: shortstring; |
104 |
begin |
|
10420 | 105 |
ipcReadFromFrontend:= ipcRead(msgEngine, mutEngine, condEngine).str |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
106 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
107 |
|
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
108 |
function ipcCheckFromEngine: boolean; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
109 |
begin |
10412 | 110 |
ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend) |
111 |
end; |
|
112 |
||
113 |
function ipcCheckFromFrontend: boolean; |
|
114 |
begin |
|
115 |
ipcCheckFromFrontend:= ipcCheck(msgEngine, mutEngine) |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
116 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
117 |
|
10416 | 118 |
function listener(p: pointer): Longint; cdecl; export; |
10420 | 119 |
var msg: TIPCMessage; |
10416 | 120 |
begin |
121 |
listener:= 0; |
|
122 |
repeat |
|
10420 | 123 |
msg:= ipcReadFromEngine(); |
124 |
if msg.buf = nil then |
|
125 |
callbackFunction(callbackPointer, @msg.str[1], byte(msg.str[0])) |
|
126 |
else |
|
127 |
begin |
|
128 |
callbackFunction(callbackPointer, msg.buf, msg.len); |
|
129 |
FreeMem(msg.buf, msg.len) |
|
130 |
end |
|
10416 | 131 |
until false |
132 |
end; |
|
133 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
134 |
procedure registerIPCCallback(p: pointer; f: TIPCCallback); |
10416 | 135 |
begin |
136 |
callbackPointer:= p; |
|
137 |
callbackFunction:= f; |
|
138 |
callbackListenerThread:= SDL_CreateThread(@listener{$IFDEF SDL2}, 'ipcListener'{$ENDIF}, nil); |
|
139 |
end; |
|
140 |
||
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
141 |
procedure initIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
142 |
begin |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
143 |
msgFrontend.str:= ''; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
144 |
msgFrontend.buf:= nil; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
145 |
msgEngine.str:= ''; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
146 |
msgEngine.buf:= nil; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
147 |
|
10416 | 148 |
callbackPointer:= nil; |
149 |
callbackListenerThread:= nil; |
|
150 |
||
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
151 |
mutFrontend:= SDL_CreateMutex; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
152 |
mutEngine:= SDL_CreateMutex; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
153 |
condFrontend:= SDL_CreateCond; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
154 |
condEngine:= SDL_CreateCond; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
155 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
156 |
|
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
157 |
procedure freeIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
158 |
begin |
10416 | 159 |
SDL_KillThread(callbackListenerThread); |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
160 |
SDL_DestroyMutex(mutFrontend); |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
161 |
SDL_DestroyMutex(mutEngine); |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
162 |
SDL_DestroyCond(condFrontend); |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
163 |
SDL_DestroyCond(condEngine); |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
164 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
165 |
|
10406 | 166 |
end. |