@@ -156,6 +156,10 @@ typedef struct ReadLoopOptions
156
156
157
157
Dart_Port port ;
158
158
159
+ HANDLE hMutex ;
160
+
161
+ BOOL ackRead ;
162
+
159
163
} ReadLoopOptions ;
160
164
161
165
static DWORD WINAPI read_loop (LPVOID arg )
@@ -168,6 +172,11 @@ static DWORD WINAPI read_loop(LPVOID arg)
168
172
{
169
173
DWORD readlen = 0 ;
170
174
175
+ if (options -> ackRead )
176
+ {
177
+ WaitForSingleObject (options -> hMutex , INFINITE );
178
+ }
179
+
171
180
BOOL ok = ReadFile (options -> fd , buffer , sizeof (buffer ), & readlen , NULL );
172
181
173
182
if (!ok )
@@ -192,12 +201,14 @@ static DWORD WINAPI read_loop(LPVOID arg)
192
201
return 0 ;
193
202
}
194
203
195
- static void start_read_thread (HANDLE fd , Dart_Port port )
204
+ static void start_read_thread (HANDLE fd , Dart_Port port , HANDLE mutex , BOOL ackRead )
196
205
{
197
206
ReadLoopOptions * options = malloc (sizeof (ReadLoopOptions ));
198
207
199
208
options -> fd = fd ;
200
209
options -> port = port ;
210
+ options -> hMutex = mutex ;
211
+ options -> ackRead = ackRead ;
201
212
202
213
DWORD thread_id ;
203
214
@@ -215,6 +226,7 @@ typedef struct WaitExitOptions
215
226
216
227
Dart_Port port ;
217
228
229
+ HANDLE hMutex ;
218
230
} WaitExitOptions ;
219
231
220
232
static DWORD WINAPI wait_exit_thread (LPVOID arg )
@@ -228,18 +240,20 @@ static DWORD WINAPI wait_exit_thread(LPVOID arg)
228
240
GetExitCodeProcess (options -> pid , & exit_code );
229
241
230
242
CloseHandle (options -> pid );
243
+ CloseHandle (options -> hMutex );
231
244
232
245
Dart_PostInteger_DL (options -> port , exit_code );
233
246
234
247
return 0 ;
235
248
}
236
249
237
- static void start_wait_exit_thread (HANDLE pid , Dart_Port port )
250
+ static void start_wait_exit_thread (HANDLE pid , Dart_Port port , HANDLE mutex )
238
251
{
239
252
WaitExitOptions * options = malloc (sizeof (WaitExitOptions ));
240
253
241
254
options -> pid = pid ;
242
255
options -> port = port ;
256
+ options -> hMutex = mutex ;
243
257
244
258
DWORD thread_id ;
245
259
@@ -261,6 +275,10 @@ typedef struct PtyHandle
261
275
262
276
HANDLE hProcess ;
263
277
278
+ BOOL ackRead ;
279
+
280
+ HANDLE hMutex ;
281
+
264
282
} PtyHandle ;
265
283
266
284
char * error_message = NULL ;
@@ -386,9 +404,11 @@ FFI_PLUGIN_EXPORT PtyHandle *pty_create(PtyOptions *options)
386
404
387
405
// CloseHandle(processInfo.hThread);
388
406
389
- start_read_thread ( outputReadSide , options -> stdout_port );
407
+ HANDLE mutex = CreateMutex ( NULL , FALSE, NULL );
390
408
391
- start_wait_exit_thread (processInfo .hProcess , options -> exit_port );
409
+ start_read_thread (outputReadSide , options -> stdout_port , mutex , options -> ackRead );
410
+
411
+ start_wait_exit_thread (processInfo .hProcess , options -> exit_port , mutex );
392
412
393
413
PtyHandle * pty = malloc (sizeof (PtyHandle ));
394
414
@@ -402,6 +422,8 @@ FFI_PLUGIN_EXPORT PtyHandle *pty_create(PtyOptions *options)
402
422
pty -> outputReadSide = outputReadSide ;
403
423
pty -> hPty = hPty ;
404
424
pty -> hProcess = processInfo .hProcess ;
425
+ pty -> ackRead = options -> ackRead ;
426
+ pty -> hMutex = mutex ;
405
427
406
428
return pty ;
407
429
}
@@ -417,6 +439,14 @@ FFI_PLUGIN_EXPORT void pty_write(PtyHandle *handle, char *buffer, int length)
417
439
return ;
418
440
}
419
441
442
+ FFI_PLUGIN_EXPORT void pty_ack_read (PtyHandle * handle )
443
+ {
444
+ if (handle -> ackRead )
445
+ {
446
+ ReleaseMutex (handle -> hMutex );
447
+ }
448
+ }
449
+
420
450
FFI_PLUGIN_EXPORT int pty_resize (PtyHandle * handle , int rows , int cols )
421
451
{
422
452
COORD size ;
0 commit comments