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