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