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