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