| ︙ | | |
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
-
-
-
-
-
+
|
*/
typedef struct {
HANDLE thread; /* Handle to reader or writer thread. */
HANDLE readyEvent; /* Manual-reset event to signal _to_ the main
* thread when the worker thread has finished
* waiting for its normal work to happen. */
HANDLE startEvent; /* Auto-reset event used by the main thread to
* signal when the thread should attempt to do
* its normal work. */
HANDLE stopEvent; /* Auto-reset event used by the main thread to
* signal when the thread should exit. */
TclPipeThreadInfo *TI; /* Thread info structure of writer and reader. */
} ConsoleThreadInfo;
/*
* This structure describes per-instance data for a console based channel.
*/
typedef struct ConsoleInfo {
|
| ︙ | | |
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
-
+
-
+
-
-
+
-
+
-
|
* which events should be reported. */
int flags; /* State flags, see above for a list. */
Tcl_ThreadId threadId; /* Thread to which events should be reported.
* This value is used by the reader/writer
* threads. */
ConsoleThreadInfo writer; /* A specialized thread for handling
* asynchronous writes to the console; the
* waiting starts when a start event is sent,
* waiting starts when a control event is sent,
* and a reset event is sent back to the main
* thread when the write is done. A stop event
* thread when the write is done. */
* is used to terminate the thread. */
ConsoleThreadInfo reader; /* A specialized thread for handling
* asynchronous reads from the console; the
* waiting starts when a start event is sent,
* waiting starts when a control event is sent,
* and a reset event is sent back to the main
* thread when input is available. A stop
* thread when input is available. */
* event is used to terminate the thread. */
DWORD writeError; /* An error caused by the last background
* write. Set to 0 if no error has been
* detected. This word is shared with the
* writer thread so access must be
* synchronized with the writable object. */
char *writeBuf; /* Current background output buffer. Access is
* synchronized with the writable object. */
|
| ︙ | | |
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
-
-
-
-
|
static void ConsoleThreadActionProc(ClientData instanceData,
int action);
static BOOL ReadConsoleBytes(HANDLE hConsole, LPVOID lpBuffer,
DWORD nbytes, LPDWORD nbytesread);
static BOOL WriteConsoleBytes(HANDLE hConsole,
const void *lpBuffer, DWORD nbytes,
LPDWORD nbyteswritten);
static void StartChannelThread(ConsoleInfo *infoPtr,
ConsoleThreadInfo *threadInfoPtr,
LPTHREAD_START_ROUTINE threadProc);
static void StopChannelThread(ConsoleThreadInfo *threadInfoPtr);
/*
* This structure describes the channel type structure for command console
* based IO.
*/
static const Tcl_ChannelType consoleChannelType = {
|
| ︙ | | |
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* StartChannelThread, StopChannelThread --
*
* Helpers that codify how to ask one of the console service threads to
* start and stop.
*
*----------------------------------------------------------------------
*/
static void
StartChannelThread(
ConsoleInfo *infoPtr,
ConsoleThreadInfo *threadInfoPtr,
LPTHREAD_START_ROUTINE threadProc)
{
DWORD id;
threadInfoPtr->readyEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
threadInfoPtr->startEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
threadInfoPtr->stopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
threadInfoPtr->thread = CreateThread(NULL, 256, threadProc, infoPtr, 0,
&id);
SetThreadPriority(threadInfoPtr->thread, THREAD_PRIORITY_HIGHEST);
}
static void
StopChannelThread(
ConsoleThreadInfo *threadInfoPtr)
{
DWORD exitCode = 0;
/*
* The thread may already have closed on it's own. Check it's exit
* code.
*/
GetExitCodeThread(threadInfoPtr->thread, &exitCode);
if (exitCode == STILL_ACTIVE) {
/*
* Set the stop event so that if the reader thread is blocked in
* ConsoleReaderThread on WaitForMultipleEvents, it will exit cleanly.
*/
SetEvent(threadInfoPtr->stopEvent);
/*
* Wait at most 20 milliseconds for the reader thread to close.
*/
if (WaitForSingleObject(threadInfoPtr->thread, 20) == WAIT_TIMEOUT) {
/*
* Forcibly terminate the background thread as a last resort.
* Note that we need to guard against terminating the thread while
* it is in the middle of Tcl_ThreadAlert because it won't be able
* to release the notifier lock.
*/
Tcl_MutexLock(&consoleMutex);
/* BUG: this leaks memory. */
TerminateThread(threadInfoPtr->thread, 0);
Tcl_MutexUnlock(&consoleMutex);
}
}
/*
* Close all the handles associated with the thread, and set the thread
* handle field to NULL to mark that the thread has been cleaned up.
*/
CloseHandle(threadInfoPtr->thread);
CloseHandle(threadInfoPtr->readyEvent);
CloseHandle(threadInfoPtr->startEvent);
CloseHandle(threadInfoPtr->stopEvent);
threadInfoPtr->thread = NULL;
}
/*
*----------------------------------------------------------------------
*
* ConsoleCloseProc --
*
* Closes a console based IO channel.
*
* Results:
* 0 on success, errno otherwise.
*
|
| ︙ | | |
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
|
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
+
+
+
-
+
-
+
+
+
+
-
+
|
/*
* Clean up the background thread if necessary. Note that this must be
* done before we can close the file, since the thread may be blocking
* trying to read from the console.
*/
if (consolePtr->reader.thread) {
TclPipeThreadStop(&consolePtr->reader.TI, consolePtr->reader.thread);
CloseHandle(consolePtr->reader.thread);
CloseHandle(consolePtr->reader.readyEvent);
StopChannelThread(&consolePtr->reader);
consolePtr->reader.thread = NULL;
}
consolePtr->validMask &= ~TCL_READABLE;
/*
* Wait for the writer thread to finish the current buffer, then terminate
* the thread and close the handles. If the channel is nonblocking, there
* should be no pending write operations.
*/
if (consolePtr->writer.thread) {
if (consolePtr->toWrite) {
/*
* We only need to wait if there is something to write. This may
* prevent infinite wait on exit. [Python Bug 216289]
*/
WaitForSingleObject(consolePtr->writer.readyEvent, INFINITE);
WaitForSingleObject(consolePtr->writer.readyEvent, 5000);
}
TclPipeThreadStop(&consolePtr->writer.TI, consolePtr->writer.thread);
CloseHandle(consolePtr->writer.thread);
CloseHandle(consolePtr->writer.readyEvent);
StopChannelThread(&consolePtr->writer);
consolePtr->writer.thread = NULL;
}
consolePtr->validMask &= ~TCL_WRITABLE;
/*
* Don't close the Win32 handle if the handle is a standard channel during
* the thread exit process. Otherwise, one thread may kill the stdio of
* another.
|
| ︙ | | |
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
|
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
|
+
+
-
-
+
+
+
|
int *errorCode) /* Where to store error code. */
{
ConsoleInfo *infoPtr = instanceData;
ConsoleThreadInfo *threadInfo = &infoPtr->writer;
DWORD bytesWritten, timeout;
*errorCode = 0;
/* avoid blocking if pipe-thread exited */
timeout = (infoPtr->flags & CONSOLE_ASYNC) ? 0 : INFINITE;
if (WaitForSingleObject(threadInfo->readyEvent,timeout) == WAIT_TIMEOUT) {
timeout = (infoPtr->flags & CONSOLE_ASYNC) || !TclPipeThreadIsAlive(&threadInfo->TI)
|| TclInExit() || TclInThreadExit() ? 0 : INFINITE;
if (WaitForSingleObject(threadInfo->readyEvent, timeout) == WAIT_TIMEOUT) {
/*
* The writer thread is blocked waiting for a write to complete and
* the channel is in non-blocking mode.
*/
errno = EWOULDBLOCK;
goto error;
|
| ︙ | | |
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
|
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
|
-
+
|
}
infoPtr->writeBufLen = toWrite;
infoPtr->writeBuf = ckalloc(toWrite);
}
memcpy(infoPtr->writeBuf, buf, (size_t) toWrite);
infoPtr->toWrite = toWrite;
ResetEvent(threadInfo->readyEvent);
SetEvent(threadInfo->startEvent);
TclPipeThreadSignal(&threadInfo->TI);
bytesWritten = toWrite;
} else {
/*
* In the blocking case, just try to write the buffer directly. This
* avoids an unnecessary copy.
*/
|
| ︙ | | |
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
|
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
|
-
-
+
+
+
+
-
|
INPUT_RECORD input;
while (1) {
/*
* Synchronize with the reader thread.
*/
timeout = blocking ? INFINITE : 0;
if (WaitForSingleObject(threadInfo->readyEvent,
/* avoid blocking if pipe-thread exited */
timeout = (!blocking || !TclPipeThreadIsAlive(&threadInfo->TI)
|| TclInExit() || TclInThreadExit()) ? 0 : INFINITE;
if (WaitForSingleObject(threadInfo->readyEvent, timeout) == WAIT_TIMEOUT) {
timeout) == WAIT_TIMEOUT) {
/*
* The reader thread is blocked waiting for data and the channel
* is in non-blocking mode.
*/
errno = EWOULDBLOCK;
return -1;
|
| ︙ | | |
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
|
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
|
-
+
|
}
/*
* There wasn't any data available, so reset the thread and try again.
*/
ResetEvent(threadInfo->readyEvent);
SetEvent(threadInfo->startEvent);
TclPipeThreadSignal(&threadInfo->TI);
}
}
/*
*----------------------------------------------------------------------
*
* ConsoleReaderThread --
|
| ︙ | | |
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
|
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
|
+
-
-
-
+
+
+
-
-
-
+
-
-
-
-
-
-
+
-
-
+
-
-
-
+
+
+
+
+
-
-
-
+
+
+
-
-
+
-
-
+
+
|
*----------------------------------------------------------------------
*/
static DWORD WINAPI
ConsoleReaderThread(
LPVOID arg)
{
TclPipeThreadInfo *pipeTI = (TclPipeThreadInfo *)arg;
ConsoleInfo *infoPtr = arg;
HANDLE *handle = infoPtr->handle;
ConsoleThreadInfo *threadInfo = &infoPtr->reader;
ConsoleInfo *infoPtr = NULL; /* access info only after success init/wait */
HANDLE *handle = NULL;
ConsoleThreadInfo *threadInfo = NULL;
DWORD waitResult;
HANDLE wEvents[2];
int done = 0;
/*
* The first event takes precedence.
*/
wEvents[0] = threadInfo->stopEvent;
wEvents[1] = threadInfo->startEvent;
while (!done) {
for (;;) {
/*
* Wait for the main thread to signal before attempting to wait.
* Wait for the main thread to signal before attempting to read.
*/
waitResult = WaitForMultipleObjects(2, wEvents, FALSE, INFINITE);
if (waitResult != (WAIT_OBJECT_0 + 1)) {
if (!TclPipeThreadWaitForSignal(&pipeTI)) {
/* exit */
break;
}
if (!infoPtr) {
/*
* The start event was not signaled. It must be the stop event or
* an error, so exit this thread.
infoPtr = (ConsoleInfo *)pipeTI->clientData;
handle = infoPtr->handle;
threadInfo = &infoPtr->reader;
*/
}
break;
}
/*
* Look for data on the console, but first ignore any events that are
* not KEY_EVENTs.
*/
if (ReadConsoleBytes(handle, infoPtr->buffer, CONSOLE_BUFFER_SIZE,
(LPDWORD) &infoPtr->bytesRead) != FALSE) {
/*
* Data was stored in the buffer.
*/
infoPtr->readFlags |= CONSOLE_BUFFERED;
} else {
DWORD err = GetLastError();
if (err == (DWORD) EOF) {
infoPtr->readFlags = CONSOLE_EOF;
}
done = 1;
}
/*
* Signal the main thread by signalling the readable event and then
* waking up the notifier thread.
*/
|
| ︙ | | |
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
|
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
|
+
+
+
|
*/
Tcl_ThreadAlert(infoPtr->threadId);
}
Tcl_MutexUnlock(&consoleMutex);
}
/* Worker exit, so inform the main thread or free TI-structure (if owned) */
TclPipeThreadExit(&pipeTI);
return 0;
}
/*
*----------------------------------------------------------------------
*
* ConsoleWriterThread --
|
| ︙ | | |
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
|
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
|
+
-
-
-
-
+
+
+
+
-
-
+
-
-
-
-
-
-
+
-
-
+
-
-
+
-
-
-
-
+
-
-
-
+
+
+
+
+
+
|
*----------------------------------------------------------------------
*/
static DWORD WINAPI
ConsoleWriterThread(
LPVOID arg)
{
TclPipeThreadInfo *pipeTI = (TclPipeThreadInfo *)arg;
ConsoleInfo *infoPtr = arg;
HANDLE *handle = infoPtr->handle;
ConsoleThreadInfo *threadInfo = &infoPtr->writer;
DWORD count, toWrite, waitResult;
ConsoleInfo *infoPtr = NULL; /* access info only after success init/wait */
HANDLE *handle = NULL;
ConsoleThreadInfo *threadInfo = NULL;
DWORD count, toWrite;
char *buf;
HANDLE wEvents[2];
int done = 0;
/*
* The first event takes precedence.
*/
wEvents[0] = threadInfo->stopEvent;
wEvents[1] = threadInfo->startEvent;
while (!done) {
for (;;) {
/*
* Wait for the main thread to signal before attempting to write.
*/
if (!TclPipeThreadWaitForSignal(&pipeTI)) {
waitResult = WaitForMultipleObjects(2, wEvents, FALSE, INFINITE);
/* exit */
if (waitResult != (WAIT_OBJECT_0 + 1)) {
/*
* The start event was not signaled. It must be the stop event or
* an error, so exit this thread.
break;
*/
break;
}
if (!infoPtr) {
infoPtr = (ConsoleInfo *)pipeTI->clientData;
handle = infoPtr->handle;
threadInfo = &infoPtr->writer;
}
buf = infoPtr->writeBuf;
toWrite = infoPtr->toWrite;
/*
* Loop until all of the bytes are written or an error occurs.
*/
while (toWrite > 0) {
if (WriteConsoleBytes(handle, buf, (DWORD) toWrite,
&count) == FALSE) {
infoPtr->writeError = GetLastError();
done = 1;
break;
}
toWrite -= count;
buf += count;
}
/*
|
| ︙ | | |
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
|
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
|
+
+
+
|
* it anyway.
*/
Tcl_ThreadAlert(infoPtr->threadId);
}
Tcl_MutexUnlock(&consoleMutex);
}
/* Worker exit, so inform the main thread or free TI-structure (if owned) */
TclPipeThreadExit(&pipeTI);
return 0;
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | |
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
|
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
|
-
+
+
+
+
+
+
-
+
+
+
+
+
+
|
* we only want to catch when complete lines are ready for reading.
*/
GetConsoleMode(infoPtr->handle, &modes);
modes &= ~(ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
modes |= ENABLE_LINE_INPUT;
SetConsoleMode(infoPtr->handle, modes);
StartChannelThread(infoPtr, &infoPtr->reader, ConsoleReaderThread);
infoPtr->reader.readyEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
infoPtr->reader.thread = CreateThread(NULL, 256, ConsoleReaderThread,
TclPipeThreadCreateTI(&infoPtr->reader.TI, infoPtr,
infoPtr->reader.readyEvent), 0, NULL);
SetThreadPriority(infoPtr->reader.thread, THREAD_PRIORITY_HIGHEST);
}
if (permissions & TCL_WRITABLE) {
StartChannelThread(infoPtr, &infoPtr->writer, ConsoleWriterThread);
infoPtr->writer.readyEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
infoPtr->writer.thread = CreateThread(NULL, 256, ConsoleWriterThread,
TclPipeThreadCreateTI(&infoPtr->writer.TI, infoPtr,
infoPtr->writer.readyEvent), 0, NULL);
SetThreadPriority(infoPtr->writer.thread, THREAD_PRIORITY_HIGHEST);
}
/*
* Files have default translation of AUTO and ^Z eof char, which means
* that a ^Z will be accepted as EOF when reading.
*/
|
| ︙ | | |