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