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