author | sheepluva |
Sun, 01 May 2016 21:09:45 +0200 | |
branch | qmlfrontend |
changeset 11792 | bcba7938ccb5 |
parent 11465 | 9f2fb0031ef0 |
child 12863 | 0c6fb706f747 |
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 |
procedure initIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
6 |
procedure freeIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
7 |
|
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
8 |
procedure ipcToEngine(s: shortstring); |
10898 | 9 |
procedure ipcToEngineRaw(p: pointer; len: Longword); |
11457
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
10 |
procedure ipcSetEngineBarrier(); |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
11 |
procedure ipcRemoveBarrierFromEngineQueue(); |
10420 | 12 |
//function ipcReadFromEngine: shortstring; |
13 |
//function ipcCheckFromEngine: boolean; |
|
10406 | 14 |
|
10935 | 15 |
procedure ipcToNet(s: shortstring); |
16 |
procedure ipcToNetRaw(p: pointer; len: Longword); |
|
17 |
||
10412 | 18 |
procedure ipcToFrontend(s: shortstring); |
10420 | 19 |
procedure ipcToFrontendRaw(p: pointer; len: Longword); |
11459 | 20 |
function ipcReadFromFrontend: TIPCMessage; |
10412 | 21 |
function ipcCheckFromFrontend: boolean; |
22 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
23 |
procedure registerIPCCallback(p: pointer; f: TIPCCallback); |
10935 | 24 |
procedure registerNetCallback(p: pointer; f: TIPCCallback); |
10416 | 25 |
|
10406 | 26 |
implementation |
27 |
||
10935 | 28 |
var callbackPointerF: pointer; |
29 |
callbackFunctionF: TIPCCallback; |
|
30 |
callbackListenerThreadF: PSDL_Thread; |
|
31 |
callbackPointerN: pointer; |
|
32 |
callbackFunctionN: TIPCCallback; |
|
33 |
callbackListenerThreadN: PSDL_Thread; |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
34 |
queueFrontend, queueEngine, queueNet: PIPCQueue; |
10416 | 35 |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
36 |
procedure ipcSend(var s: TIPCMessage; queue: PIPCQueue); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
37 |
var pmsg: PIPCMessage; |
10412 | 38 |
begin |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
39 |
SDL_LockMutex(queue^.mut); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
40 |
|
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
41 |
s.next:= nil; |
11457
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
42 |
s.barrier:= 0; |
10418
091d2c0216c3
Move away from passing shortstrings into C code, now IPC works
unc0rr
parents:
10416
diff
changeset
|
43 |
|
11464
30397f91571c
Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents:
11459
diff
changeset
|
44 |
if (queue^.msg.next = nil) and (queue^.msg.str[0] = #0) and (queue^.msg.buf = nil) and (queue^.msg.barrier = 0) then |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
45 |
begin |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
46 |
queue^.msg:= s; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
47 |
end else |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
48 |
begin |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
49 |
new(pmsg); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
50 |
pmsg^:= s; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
51 |
queue^.last^.next:= pmsg; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
52 |
queue^.last:= pmsg; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
53 |
end; |
11465 | 54 |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
55 |
SDL_CondSignal(queue^.cond); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
56 |
SDL_UnlockMutex(queue^.mut); |
10412 | 57 |
end; |
58 |
||
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
59 |
function ipcRead(queue: PIPCQueue): TIPCMessage; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
60 |
var pmsg: PIPCMessage; |
10412 | 61 |
begin |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
62 |
SDL_LockMutex(queue^.mut); |
11457
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
63 |
while ((queue^.msg.str[0] = #0) and (queue^.msg.buf = nil)) |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
64 |
and ((queue^.msg.barrier > 0) or (queue^.msg.next = nil) or ((queue^.msg.next^.barrier > 0) and (queue^.msg.next^.str[0] = #0) and (queue^.msg.next^.buf = nil))) do |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
65 |
SDL_CondWait(queue^.cond, queue^.mut); |
10412 | 66 |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
67 |
if (queue^.msg.str[0] <> #0) or (queue^.msg.buf <> nil) then |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
68 |
begin |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
69 |
ipcRead:= queue^.msg; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
70 |
queue^.msg.str[0]:= #0; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
71 |
queue^.msg.buf:= nil; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
72 |
end else |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
73 |
begin |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
74 |
pmsg:= queue^.msg.next; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
75 |
ipcRead:= pmsg^; |
11457
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
76 |
if pmsg^.barrier > 0 then |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
77 |
begin |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
78 |
pmsg^.str[0]:= #0; |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
79 |
pmsg^.buf:= nil |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
80 |
end else |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
81 |
begin |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
82 |
queue^.msg.next:= pmsg^.next; |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
83 |
if queue^.msg.next = nil then queue^.last:= @queue^.msg; |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
84 |
dispose(pmsg) |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
85 |
end |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
86 |
end; |
10420 | 87 |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
88 |
SDL_UnlockMutex(queue^.mut) |
10412 | 89 |
end; |
90 |
||
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
91 |
function ipcCheck(queue: PIPCQueue): boolean; |
10412 | 92 |
begin |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
93 |
SDL_LockMutex(queue^.mut); |
11457
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
94 |
ipcCheck:= (queue^.msg.str[0] > #0) or (queue^.msg.buf <> nil) or |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
95 |
((queue^.msg.barrier = 0) and (queue^.msg.next <> nil) and ((queue^.msg.next^.barrier = 0) or (queue^.msg.next^.str[0] <> #0) or (queue^.msg.next^.buf <> nil))); |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
96 |
SDL_UnlockMutex(queue^.mut) |
10412 | 97 |
end; |
98 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
99 |
procedure ipcToEngine(s: shortstring); |
10420 | 100 |
var msg: TIPCMessage; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
101 |
begin |
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
102 |
msg.str:= s; |
10420 | 103 |
msg.buf:= nil; |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
104 |
ipcSend(msg, queueEngine) |
10412 | 105 |
end; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
106 |
|
10412 | 107 |
procedure ipcToFrontend(s: shortstring); |
10420 | 108 |
var msg: TIPCMessage; |
10412 | 109 |
begin |
10420 | 110 |
msg.str:= s; |
111 |
msg.buf:= nil; |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
112 |
ipcSend(msg, queueFrontend) |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
113 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
114 |
|
11457
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
115 |
procedure ipcSetEngineBarrier(); |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
116 |
begin |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
117 |
SDL_LockMutex(queueEngine^.mut); |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
118 |
|
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
119 |
inc(queueEngine^.last^.barrier); |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
120 |
|
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
121 |
SDL_UnlockMutex(queueEngine^.mut); |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
122 |
end; |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
123 |
|
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
124 |
procedure ipcRemoveBarrierFromEngineQueue(); |
11456 | 125 |
var pmsg, t: PIPCMessage; |
126 |
q: PIPCQueue; |
|
127 |
begin |
|
128 |
q:= queueEngine; |
|
129 |
||
130 |
SDL_LockMutex(q^.mut); |
|
131 |
||
132 |
pmsg:= @q^.msg; |
|
133 |
while pmsg <> nil do |
|
134 |
begin |
|
135 |
t:= pmsg^.next; |
|
11457
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
136 |
q^.msg.next:= t; |
11456 | 137 |
|
11464
30397f91571c
Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents:
11459
diff
changeset
|
138 |
pmsg^.str[0]:= #0; |
11456 | 139 |
if pmsg^.buf <> nil then |
11464
30397f91571c
Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents:
11459
diff
changeset
|
140 |
begin |
11456 | 141 |
FreeMem(pmsg^.buf, pmsg^.len); |
11464
30397f91571c
Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents:
11459
diff
changeset
|
142 |
pmsg^.buf:= nil |
30397f91571c
Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents:
11459
diff
changeset
|
143 |
end; |
11456 | 144 |
|
145 |
if pmsg <> @q^.msg then |
|
11457
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
146 |
if pmsg^.barrier = 0 then |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
147 |
dispose(pmsg) |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
148 |
else |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
149 |
if pmsg^.barrier = 1 then |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
150 |
begin |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
151 |
dispose(pmsg); |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
152 |
t:= nil |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
153 |
end else |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
154 |
begin |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
155 |
dec(pmsg^.barrier); |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
156 |
q^.msg.next:= pmsg; |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
157 |
t:= nil |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
158 |
end |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
159 |
else |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
160 |
if pmsg^.barrier > 0 then |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
161 |
begin |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
162 |
dec(pmsg^.barrier); |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
163 |
t:= nil |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
164 |
end; |
78860824b5a5
Introduce barriers between messages to different engine instances
unc0rr
parents:
11456
diff
changeset
|
165 |
|
11456 | 166 |
pmsg:= t |
167 |
end; |
|
11465 | 168 |
|
11464
30397f91571c
Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents:
11459
diff
changeset
|
169 |
if q^.msg.next = nil then q^.last:= @q^.msg; |
11456 | 170 |
|
171 |
q^.msg.str[0]:= #0; |
|
172 |
q^.msg.buf:= nil; |
|
173 |
||
174 |
SDL_UnlockMutex(q^.mut); |
|
175 |
end; |
|
176 |
||
10935 | 177 |
procedure ipcToNet(s: shortstring); |
178 |
var msg: TIPCMessage; |
|
179 |
begin |
|
180 |
msg.str:= s; |
|
181 |
msg.buf:= nil; |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
182 |
ipcSend(msg, queueNet) |
10935 | 183 |
end; |
184 |
||
10898 | 185 |
procedure ipcToEngineRaw(p: pointer; len: Longword); |
186 |
var msg: TIPCMessage; |
|
187 |
begin |
|
188 |
msg.str[0]:= #0; |
|
189 |
msg.len:= len; |
|
190 |
msg.buf:= GetMem(len); |
|
191 |
Move(p^, msg.buf^, len); |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
192 |
ipcSend(msg, queueEngine) |
10898 | 193 |
end; |
194 |
||
10420 | 195 |
procedure ipcToFrontendRaw(p: pointer; len: Longword); |
196 |
var msg: TIPCMessage; |
|
197 |
begin |
|
198 |
msg.str[0]:= #0; |
|
199 |
msg.len:= len; |
|
200 |
msg.buf:= GetMem(len); |
|
201 |
Move(p^, msg.buf^, len); |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
202 |
ipcSend(msg, queueFrontend) |
10420 | 203 |
end; |
204 |
||
10935 | 205 |
procedure ipcToNetRaw(p: pointer; len: Longword); |
206 |
var msg: TIPCMessage; |
|
207 |
begin |
|
208 |
msg.str[0]:= #0; |
|
209 |
msg.len:= len; |
|
210 |
msg.buf:= GetMem(len); |
|
211 |
Move(p^, msg.buf^, len); |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
212 |
ipcSend(msg, queueNet) |
10935 | 213 |
end; |
214 |
||
10420 | 215 |
function ipcReadFromEngine: TIPCMessage; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
216 |
begin |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
217 |
ipcReadFromEngine:= ipcRead(queueFrontend) |
10412 | 218 |
end; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
219 |
|
11459 | 220 |
function ipcReadFromFrontend: TIPCMessage; |
10412 | 221 |
begin |
11459 | 222 |
ipcReadFromFrontend:= ipcRead(queueEngine) |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
223 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
224 |
|
10935 | 225 |
function ipcReadToNet: TIPCMessage; |
226 |
begin |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
227 |
ipcReadToNet:= ipcRead(queueNet) |
10935 | 228 |
end; |
229 |
||
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
230 |
function ipcCheckFromEngine: boolean; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
231 |
begin |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
232 |
ipcCheckFromEngine:= ipcCheck(queueFrontend) |
10412 | 233 |
end; |
234 |
||
235 |
function ipcCheckFromFrontend: boolean; |
|
236 |
begin |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
237 |
ipcCheckFromFrontend:= ipcCheck(queueEngine) |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
238 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
239 |
|
10933 | 240 |
function engineListener(p: pointer): Longint; cdecl; export; |
10420 | 241 |
var msg: TIPCMessage; |
10416 | 242 |
begin |
10933 | 243 |
engineListener:= 0; |
10416 | 244 |
repeat |
10420 | 245 |
msg:= ipcReadFromEngine(); |
246 |
if msg.buf = nil then |
|
10935 | 247 |
callbackFunctionF(callbackPointerF, @msg.str[1], byte(msg.str[0])) |
10420 | 248 |
else |
249 |
begin |
|
10935 | 250 |
callbackFunctionF(callbackPointerF, msg.buf, msg.len); |
251 |
FreeMem(msg.buf, msg.len) |
|
252 |
end |
|
253 |
until false |
|
254 |
end; |
|
255 |
||
256 |
function netListener(p: pointer): Longint; cdecl; export; |
|
257 |
var msg: TIPCMessage; |
|
258 |
begin |
|
259 |
netListener:= 0; |
|
260 |
repeat |
|
261 |
msg:= ipcReadToNet(); |
|
262 |
if msg.buf = nil then |
|
263 |
callbackFunctionN(callbackPointerN, @msg.str[1], byte(msg.str[0])) |
|
264 |
else |
|
265 |
begin |
|
266 |
callbackFunctionN(callbackPointerN, msg.buf, msg.len); |
|
10420 | 267 |
FreeMem(msg.buf, msg.len) |
268 |
end |
|
10416 | 269 |
until false |
270 |
end; |
|
271 |
||
10428
7c25297720f1
More refactoring: move PoC preview getting code into flib
unc0rr
parents:
10426
diff
changeset
|
272 |
procedure registerIPCCallback(p: pointer; f: TIPCCallback); |
10416 | 273 |
begin |
10935 | 274 |
callbackPointerF:= p; |
275 |
callbackFunctionF:= f; |
|
11408 | 276 |
callbackListenerThreadF:= SDL_CreateThread(@engineListener, 'engineListener', nil); |
10935 | 277 |
end; |
278 |
||
279 |
procedure registerNetCallback(p: pointer; f: TIPCCallback); |
|
280 |
begin |
|
281 |
callbackPointerN:= p; |
|
282 |
callbackFunctionN:= f; |
|
11408 | 283 |
callbackListenerThreadN:= SDL_CreateThread(@netListener, 'netListener', nil); |
10416 | 284 |
end; |
285 |
||
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
286 |
function createQueue: PIPCQueue; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
287 |
var q: PIPCQueue; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
288 |
begin |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
289 |
new(q); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
290 |
q^.msg.str:= ''; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
291 |
q^.msg.buf:= nil; |
11465 | 292 |
q^.msg.barrier:= 0; |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
293 |
q^.mut:= SDL_CreateMutex; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
294 |
q^.cond:= SDL_CreateCond; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
295 |
q^.msg.next:= nil; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
296 |
q^.last:= @q^.msg; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
297 |
createQueue:= q |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
298 |
end; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
299 |
|
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
300 |
procedure destroyQueue(queue: PIPCQueue); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
301 |
begin |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
302 |
SDL_DestroyCond(queue^.cond); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
303 |
SDL_DestroyMutex(queue^.mut); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
304 |
dispose(queue); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
305 |
end; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
306 |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
307 |
procedure initIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
308 |
begin |
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
309 |
queueFrontend:= createQueue; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
310 |
queueEngine:= createQueue; |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
311 |
queueNet:= createQueue; |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
312 |
|
10935 | 313 |
callbackPointerF:= nil; |
314 |
callbackListenerThreadF:= nil; |
|
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
315 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
316 |
|
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
317 |
procedure freeIPC; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
318 |
begin |
11408 | 319 |
//FIXME SDL_KillThread(callbackListenerThreadF); |
320 |
//FIXME SDL_KillThread(callbackListenerThreadN); |
|
11455
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
321 |
destroyQueue(queueFrontend); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
322 |
destroyQueue(queueEngine); |
0c75fa9ce340
- Use queues instead of single buffer to communicate between threads
unc0rr
parents:
11423
diff
changeset
|
323 |
destroyQueue(queueNet); |
10410
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
324 |
end; |
669bfa55cd70
Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents:
10406
diff
changeset
|
325 |
|
10406 | 326 |
end. |