| ︙ | | |
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
+
+
+
-
-
-
-
-
-
-
-
-
-
|
TclFile writeFile; /* Input from pipe. */
TclFile errorFile; /* Error output from pipe. */
int numPids; /* Number of processes attached to pipe. */
Tcl_Pid *pidPtr; /* Pids of attached processes. */
Tcl_ThreadId threadId; /* Thread to which events should be reported.
* This value is used by the reader/writer
* threads. */
TclPipeThreadInfo *writeTI; /* Thread info of writer and reader, this */
TclPipeThreadInfo *readTI; /* structure owned by corresponding thread. */
HANDLE writeThread; /* Handle to writer thread. */
HANDLE readThread; /* Handle to reader thread. */
HANDLE writable; /* Manual-reset event to signal when the
* writer thread has finished waiting for the
* current buffer to be written. */
HANDLE readable; /* Manual-reset event to signal when the
* reader thread has finished waiting for
* input. */
HANDLE startWriter; /* Auto-reset event used by the main thread to
* signal when the writer thread should
* attempt to write to the pipe. */
HANDLE stopWriter; /* Manual-reset event used to alert the reader
* thread to fall-out and exit */
HANDLE startReader; /* Auto-reset event used by the main thread to
* signal when the reader thread should
* attempt to read from the pipe. */
HANDLE stopReader; /* Manual-reset event used to alert the reader
* thread to fall-out and exit */
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
|
| ︙ | | |
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
|
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
|
-
+
|
if (arg[0] == '\0') {
quote = 1;
} else {
int count;
Tcl_UniChar ch;
for (start = arg; *start != '\0'; start += count) {
count = Tcl_UtfToUniChar(start, &ch);
count = TclUtfToUniChar(start, &ch);
if (Tcl_UniCharIsSpace(ch)) { /* INTL: ISO space. */
quote = 1;
break;
}
}
}
if (quote) {
|
| ︙ | | |
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
|
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
|
-
|
TclFile writeFile, /* If non-null, gives the file for writing. */
TclFile errorFile, /* If non-null, gives the file where errors
* can be read. */
int numPids, /* The number of pids in the pid array. */
Tcl_Pid *pidPtr) /* An array of process identifiers. */
{
char channelName[16 + TCL_INTEGER_SPACE];
DWORD id;
PipeInfo *infoPtr = ckalloc(sizeof(PipeInfo));
PipeInit();
infoPtr->watchMask = 0;
infoPtr->flags = 0;
infoPtr->readFlags = 0;
|
| ︙ | | |
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
|
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
|
-
-
+
-
+
+
-
-
+
-
-
+
+
+
+
+
|
if (readFile != NULL) {
/*
* Start the background reader thread.
*/
infoPtr->readable = CreateEvent(NULL, TRUE, TRUE, NULL);
infoPtr->startReader = CreateEvent(NULL, FALSE, FALSE, NULL);
infoPtr->stopReader = CreateEvent(NULL, TRUE, FALSE, NULL);
infoPtr->readThread = CreateThread(NULL, 256, PipeReaderThread,
TclPipeThreadCreateTI(&infoPtr->readTI, infoPtr, infoPtr->readable),
infoPtr, 0, &id);
0, NULL);
SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST);
infoPtr->validMask |= TCL_READABLE;
} else {
infoPtr->readTI = NULL;
infoPtr->readThread = 0;
}
if (writeFile != NULL) {
/*
* Start the background writer thread.
*/
infoPtr->writable = CreateEvent(NULL, TRUE, TRUE, NULL);
infoPtr->startWriter = CreateEvent(NULL, FALSE, FALSE, NULL);
infoPtr->stopWriter = CreateEvent(NULL, TRUE, FALSE, NULL);
infoPtr->writeThread = CreateThread(NULL, 256, PipeWriterThread,
TclPipeThreadCreateTI(&infoPtr->writeTI, infoPtr, infoPtr->writable),
infoPtr, 0, &id);
SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST);
0, NULL);
SetThreadPriority(infoPtr->writeThread, THREAD_PRIORITY_HIGHEST);
infoPtr->validMask |= TCL_WRITABLE;
} else {
infoPtr->writeTI = NULL;
infoPtr->writeThread = 0;
}
/*
* For backward compatibility with previous versions of Tcl, we use
* "file%d" as the base name for pipes even though it would be more
* natural to use "pipe%d". Use the pointer to keep the channel names
* unique, in case channels share handles (stdin/stdout).
|
| ︙ | | |
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
|
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
|
-
+
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
+
-
-
+
-
+
-
+
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
|
int flags) /* Flags that indicate which side to close. */
{
PipeInfo *pipePtr = (PipeInfo *) instanceData;
Tcl_Channel errChan;
int errorCode, result;
PipeInfo *infoPtr, **nextPtrPtr;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
DWORD exitCode;
int inExit = (TclInExit() || TclInThreadExit());
errorCode = 0;
result = 0;
if ((!flags || flags == TCL_CLOSE_READ) && (pipePtr->readFile != NULL)) {
if ((!flags || flags & TCL_CLOSE_READ) && (pipePtr->readFile != NULL)) {
/*
* 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 pipe.
*/
if (pipePtr->readThread) {
/*
* The thread may already have closed on its own. Check its exit
* code.
*/
GetExitCodeThread(pipePtr->readThread, &exitCode);
TclPipeThreadStop(&pipePtr->readTI, pipePtr->readThread);
if (exitCode == STILL_ACTIVE) {
/*
* Set the stop event so that if the reader thread is blocked
* in PipeReaderThread on WaitForMultipleEvents, it will exit
* cleanly.
*/
SetEvent(pipePtr->stopReader);
/*
* Wait at most 20 milliseconds for the reader thread to
* close.
*/
if (WaitForSingleObject(pipePtr->readThread,
20) == WAIT_TIMEOUT) {
/*
* The thread must be blocked waiting for the pipe to
* become readable in ReadFile(). There isn't a clean way
* to exit the thread from this condition. We should
* terminate the child process instead to get the reader
* thread to fall out of ReadFile with a FALSE. (below) is
* not the correct way to do this, but will stay here
* until a better solution is found.
*
* 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(&pipeMutex);
/* BUG: this leaks memory */
TerminateThread(pipePtr->readThread, 0);
Tcl_MutexUnlock(&pipeMutex);
}
}
CloseHandle(pipePtr->readThread);
CloseHandle(pipePtr->readable);
CloseHandle(pipePtr->startReader);
CloseHandle(pipePtr->stopReader);
pipePtr->readThread = NULL;
}
if (TclpCloseFile(pipePtr->readFile) != 0) {
errorCode = errno;
}
pipePtr->validMask &= ~TCL_READABLE;
pipePtr->readFile = NULL;
}
if ((!flags || flags & TCL_CLOSE_WRITE)
if ((!flags || flags & TCL_CLOSE_WRITE) && (pipePtr->writeFile != NULL)) {
&& (pipePtr->writeFile != NULL)) {
if (pipePtr->writeThread) {
/*
* Wait for the writer thread to finish the current buffer, then
* terminate the thread and close the handles. If the channel is
* nonblocking but blocked during exit, bail out since the worker
* nonblocking or may block during exit, bail out since the worker
* thread is not interruptible and we want TIP#398-fast-exit.
*/
if (TclInExit()
&& (pipePtr->flags & PIPE_ASYNC)) {
if ((pipePtr->flags & PIPE_ASYNC) && inExit) {
/* give it a chance to leave honorably */
SetEvent(pipePtr->stopWriter);
TclPipeThreadStopSignal(&pipePtr->writeTI, pipePtr->writable);
if (WaitForSingleObject(pipePtr->writable, 0) == WAIT_TIMEOUT) {
if (WaitForSingleObject(pipePtr->writable, 20) == WAIT_TIMEOUT) {
return EWOULDBLOCK;
}
} else {
WaitForSingleObject(pipePtr->writable, INFINITE);
WaitForSingleObject(pipePtr->writable, inExit ? 5000 : INFINITE);
}
/*
* The thread may already have closed on it's own. Check its exit
* code.
*/
TclPipeThreadStop(&pipePtr->writeTI, pipePtr->writeThread);
GetExitCodeThread(pipePtr->writeThread, &exitCode);
if (exitCode == STILL_ACTIVE) {
/*
* Set the stop event so that if the reader thread is blocked
* in PipeReaderThread on WaitForMultipleEvents, it will exit
* cleanly.
*/
SetEvent(pipePtr->stopWriter);
/*
* Wait at most 20 milliseconds for the reader thread to
* close.
*/
if (WaitForSingleObject(pipePtr->writeThread,
20) == WAIT_TIMEOUT) {
/*
* The thread must be blocked waiting for the pipe to
* consume input in WriteFile(). There isn't a clean way
* to exit the thread from this condition. We should
* terminate the child process instead to get the writer
* thread to fall out of WriteFile with a FALSE. (below)
* is not the correct way to do this, but will stay here
* until a better solution is found.
*
* 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(&pipeMutex);
/* BUG: this leaks memory */
TerminateThread(pipePtr->writeThread, 0);
Tcl_MutexUnlock(&pipeMutex);
}
}
CloseHandle(pipePtr->writeThread);
CloseHandle(pipePtr->writable);
CloseHandle(pipePtr->startWriter);
CloseHandle(pipePtr->stopWriter);
CloseHandle(pipePtr->writeThread);
pipePtr->writeThread = NULL;
}
if (TclpCloseFile(pipePtr->writeFile) != 0) {
if (errorCode == 0) {
errorCode = errno;
}
}
|
| ︙ | | |
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
|
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
|
-
+
|
nextPtrPtr = &infoPtr->nextPtr, infoPtr = *nextPtrPtr) {
if (infoPtr == (PipeInfo *)pipePtr) {
*nextPtrPtr = infoPtr->nextPtr;
break;
}
}
if ((pipePtr->flags & PIPE_ASYNC) || TclInExit()) {
if ((pipePtr->flags & PIPE_ASYNC) || inExit) {
/*
* If the channel is non-blocking or Tcl is being cleaned up, just
* detach the children PIDs, reap them (important if we are in a
* dynamic load module), and discard the errorFile.
*/
Tcl_DetachPids(pipePtr->numPids, pipePtr->pidPtr);
|
| ︙ | | |
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
|
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
|
+
+
-
+
+
|
int *errorCode) /* Where to store error code. */
{
PipeInfo *infoPtr = (PipeInfo *) instanceData;
WinFile *filePtr = (WinFile*) infoPtr->writeFile;
DWORD bytesWritten, timeout;
*errorCode = 0;
/* avoid blocking if pipe-thread exited */
timeout = (infoPtr->flags & PIPE_ASYNC) ? 0 : INFINITE;
timeout = ((infoPtr->flags & PIPE_ASYNC) || !TclPipeThreadIsAlive(&infoPtr->writeTI)
|| TclInExit() || TclInThreadExit()) ? 0 : INFINITE;
if (WaitForSingleObject(infoPtr->writable, timeout) == WAIT_TIMEOUT) {
/*
* The writer thread is blocked waiting for a write to complete and
* the channel is in non-blocking mode.
*/
errno = EWOULDBLOCK;
|
| ︙ | | |
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
|
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
|
-
+
|
}
infoPtr->writeBufLen = toWrite;
infoPtr->writeBuf = ckalloc(toWrite);
}
memcpy(infoPtr->writeBuf, buf, (size_t) toWrite);
infoPtr->toWrite = toWrite;
ResetEvent(infoPtr->writable);
SetEvent(infoPtr->startWriter);
TclPipeThreadSignal(&infoPtr->writeTI);
bytesWritten = toWrite;
} else {
/*
* In the blocking case, just try to write the buffer directly. This
* avoids an unnecessary copy.
*/
|
| ︙ | | |
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
|
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
|
-
+
+
+
|
HANDLE *handle = ((WinFile *) infoPtr->readFile)->handle;
while (1) {
/*
* Synchronize with the reader thread.
*/
timeout = blocking ? INFINITE : 0;
/* avoid blocking if pipe-thread exited */
timeout = (!blocking || !TclPipeThreadIsAlive(&infoPtr->readTI)
|| TclInExit() || TclInThreadExit()) ? 0 : INFINITE;
if (WaitForSingleObject(infoPtr->readable, timeout) == WAIT_TIMEOUT) {
/*
* The reader thread is blocked waiting for data and the channel
* is in non-blocking mode.
*/
errno = EWOULDBLOCK;
|
| ︙ | | |
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
|
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
|
-
+
|
}
/*
* There wasn't any data available, so reset the thread and try again.
*/
ResetEvent(infoPtr->readable);
SetEvent(infoPtr->startReader);
TclPipeThreadSignal(&infoPtr->readTI);
}
}
/*
*----------------------------------------------------------------------
*
* PipeReaderThread --
|
| ︙ | | |
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
|
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
|
+
-
-
+
+
-
-
-
-
-
+
+
+
-
+
-
-
+
-
-
-
-
-
-
+
+
-
+
|
*----------------------------------------------------------------------
*/
static DWORD WINAPI
PipeReaderThread(
LPVOID arg)
{
TclPipeThreadInfo *pipeTI = (TclPipeThreadInfo *)arg;
PipeInfo *infoPtr = (PipeInfo *)arg;
HANDLE *handle = ((WinFile *) infoPtr->readFile)->handle;
PipeInfo *infoPtr = NULL; /* access info only after success init/wait */
HANDLE handle = NULL;
DWORD count, err;
int done = 0;
HANDLE wEvents[2];
DWORD waitResult;
wEvents[0] = infoPtr->stopReader;
wEvents[1] = infoPtr->startReader;
while (!done) {
/*
* Wait for the main thread to signal before attempting to wait on the
* pipe becoming readable.
*/
if (!TclPipeThreadWaitForSignal(&pipeTI)) {
/* exit */
break;
}
waitResult = WaitForMultipleObjects(2, wEvents, FALSE, INFINITE);
if (waitResult != (WAIT_OBJECT_0 + 1)) {
if (!infoPtr) {
/*
* The start event was not signaled. It might be the stop event or
* an error, so exit.
*/
break;
infoPtr = (PipeInfo *)pipeTI->clientData;
handle = ((WinFile *) infoPtr->readFile)->handle;
}
/*
* Try waiting for 0 bytes. This will block until some data is
* available on NT, but will return immediately on Win 95. So, if no
* data is available after the first read, we block until we can read
* a single byte off of the pipe.
*/
if (ReadFile(handle, NULL, 0, &count, NULL) == FALSE ||
PeekNamedPipe(handle, NULL, 0, NULL, &count, NULL) == FALSE) {
/*
* The error is a result of an EOF condition, so set the EOF bit
* before signalling the main thread.
*/
err = GetLastError();
if (err == ERROR_BROKEN_PIPE) {
infoPtr->readFlags |= PIPE_EOF;
done = 1;
} else if (err == ERROR_INVALID_HANDLE) {
break;
done = 1;
}
} else if (count == 0) {
if (ReadFile(handle, &(infoPtr->extraByte), 1, &count, NULL)
!= FALSE) {
/*
* One byte was consumed as a side effect of waiting for the
* pipe to become readable.
|
| ︙ | | |
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
|
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
|
-
+
-
|
* The error is a result of an EOF condition, so set the
* EOF bit before signalling the main thread.
*/
infoPtr->readFlags |= PIPE_EOF;
done = 1;
} else if (err == ERROR_INVALID_HANDLE) {
break;
done = 1;
}
}
}
/*
* Signal the main thread by signalling the readable event and then
* waking up the notifier thread.
*/
SetEvent(infoPtr->readable);
|
| ︙ | | |
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
|
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
|
+
+
+
+
+
+
|
*/
Tcl_ThreadAlert(infoPtr->threadId);
}
Tcl_MutexUnlock(&pipeMutex);
}
/*
* If state of thread was set to stop, we can sane free info structure,
* otherwise it is shared with main thread, so main thread will own it
*/
TclPipeThreadExit(&pipeTI);
return 0;
}
/*
*----------------------------------------------------------------------
*
* PipeWriterThread --
|
| ︙ | | |
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
|
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
|
+
-
-
+
+
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
-
+
-
-
-
+
-
+
+
+
|
*----------------------------------------------------------------------
*/
static DWORD WINAPI
PipeWriterThread(
LPVOID arg)
{
TclPipeThreadInfo *pipeTI = (TclPipeThreadInfo *)arg;
PipeInfo *infoPtr = (PipeInfo *)arg;
HANDLE *handle = ((WinFile *) infoPtr->writeFile)->handle;
PipeInfo *infoPtr = NULL; /* access info only after success init/wait */
HANDLE handle = NULL;
DWORD count, toWrite;
char *buf;
int done = 0;
HANDLE wEvents[2];
DWORD waitResult;
wEvents[0] = infoPtr->stopWriter;
wEvents[1] = infoPtr->startWriter;
while (!done) {
/*
* 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 might be the stop event or
* an error, so exit.
*/
break;
if (waitResult == WAIT_OBJECT_0) {
SetEvent(infoPtr->writable);
}
}
break;
if (!infoPtr) {
infoPtr = (PipeInfo *)pipeTI->clientData;
handle = ((WinFile *) infoPtr->writeFile)->handle;
}
buf = infoPtr->writeBuf;
toWrite = infoPtr->toWrite;
/*
* Loop until all of the bytes are written or an error occurs.
*/
|
| ︙ | | |
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
|
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
|
+
+
+
+
+
+
|
*/
Tcl_ThreadAlert(infoPtr->threadId);
}
Tcl_MutexUnlock(&pipeMutex);
}
/*
* If state of thread was set to stop, we can sane free info structure,
* otherwise it is shared with main thread, so main thread will own it.
*/
TclPipeThreadExit(&pipeTI);
return 0;
}
/*
*----------------------------------------------------------------------
*
* PipeThreadActionProc --
|
| ︙ | | |
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
|
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
return Tcl_MakeFileChannel((ClientData) handle,
TCL_READABLE|TCL_WRITABLE);
gotError:
TclWinConvertError(GetLastError());
return NULL;
}
/*
*----------------------------------------------------------------------
*
* TclPipeThreadCreateTI --
*
* Creates a thread info structure, can be owned by worker.
*
* Results:
* Pointer to created TI structure.
*
*----------------------------------------------------------------------
*/
TclPipeThreadInfo *
TclPipeThreadCreateTI(
TclPipeThreadInfo **pipeTIPtr,
ClientData clientData,
HANDLE wakeEvent)
{
TclPipeThreadInfo *pipeTI;
#ifndef _PTI_USE_CKALLOC
pipeTI = malloc(sizeof(TclPipeThreadInfo));
#else
pipeTI = ckalloc(sizeof(TclPipeThreadInfo));
#endif
pipeTI->evControl = CreateEvent(NULL, FALSE, FALSE, NULL);
pipeTI->state = PTI_STATE_IDLE;
pipeTI->clientData = clientData;
pipeTI->evWakeUp = wakeEvent;
return (*pipeTIPtr = pipeTI);
}
/*
*----------------------------------------------------------------------
*
* TclPipeThreadWaitForSignal --
*
* Wait for work/stop signals inside pipe worker.
*
* Results:
* 1 if signaled to work, 0 if signaled to stop.
*
* Side effects:
* If this function returns 0, TI-structure pointer given via pipeTIPtr
* may be NULL, so not accessible (can be owned by main thread).
*
*----------------------------------------------------------------------
*/
int
TclPipeThreadWaitForSignal(
TclPipeThreadInfo **pipeTIPtr)
{
TclPipeThreadInfo *pipeTI = *pipeTIPtr;
LONG state;
DWORD waitResult;
HANDLE wakeEvent;
if (!pipeTI) {
return 0;
}
wakeEvent = pipeTI->evWakeUp;
/*
* Wait for the main thread to signal before attempting to do the work.
*/
/* reset work state of thread (idle/waiting) */
if ((state = InterlockedCompareExchange(&pipeTI->state,
PTI_STATE_IDLE, PTI_STATE_WORK)) & (PTI_STATE_STOP|PTI_STATE_END)) {
/* end of work, check the owner of structure */
goto end;
}
/* entering wait */
waitResult = WaitForSingleObject(pipeTI->evControl, INFINITE);
if (waitResult != WAIT_OBJECT_0) {
/*
* The control event was not signaled, so end of work (unexpected
* behaviour, main thread can be dead?).
*/
goto end;
}
/* try to set work state of thread */
if ((state = InterlockedCompareExchange(&pipeTI->state,
PTI_STATE_WORK, PTI_STATE_IDLE)) & (PTI_STATE_STOP|PTI_STATE_END)) {
/* end of work */
goto end;
}
/* signaled to work */
return 1;
end:
/* end of work, check the owner of the TI structure */
if (state != PTI_STATE_STOP) {
*pipeTIPtr = NULL;
} else {
pipeTI->evWakeUp = NULL;
}
if (wakeEvent) {
SetEvent(wakeEvent);
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* TclPipeThreadStopSignal --
*
* Send stop signal to the pipe worker (without waiting).
*
* After calling of this function, TI-structure pointer given via pipeTIPtr
* may be NULL.
*
* Results:
* 1 if signaled (or pipe-thread is down), 0 if pipe thread still working.
*
*----------------------------------------------------------------------
*/
int
TclPipeThreadStopSignal(
TclPipeThreadInfo **pipeTIPtr, HANDLE wakeEvent)
{
TclPipeThreadInfo *pipeTI = *pipeTIPtr;
HANDLE evControl;
int state;
if (!pipeTI) {
return 1;
}
evControl = pipeTI->evControl;
pipeTI->evWakeUp = wakeEvent;
switch (
(state = InterlockedCompareExchange(&pipeTI->state,
PTI_STATE_STOP, PTI_STATE_IDLE))
) {
case PTI_STATE_IDLE:
/* Thread was idle/waiting, notify it goes teardown */
SetEvent(evControl);
*pipeTIPtr = NULL;
case PTI_STATE_DOWN:
return 1;
default:
/*
* Thread works currently, we should try to end it, own the TI structure
* (because of possible sharing the joint structures with thread)
*/
InterlockedExchange(&pipeTI->state, PTI_STATE_END);
break;
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* TclPipeThreadStop --
*
* Send stop signal to the pipe worker and wait for thread completion.
*
* May be combined with TclPipeThreadStopSignal.
*
* After calling of this function, TI-structure pointer given via pipeTIPtr
* is not accessible (owned by pipe worker or released here).
*
* Results:
* None.
*
* Side effects:
* Can terminate pipe worker (and / or stop its synchronous operations).
*
*----------------------------------------------------------------------
*/
void
TclPipeThreadStop(
TclPipeThreadInfo **pipeTIPtr,
HANDLE hThread)
{
TclPipeThreadInfo *pipeTI = *pipeTIPtr;
HANDLE evControl;
int state;
if (!pipeTI) {
return;
}
pipeTI = *pipeTIPtr;
evControl = pipeTI->evControl;
pipeTI->evWakeUp = NULL;
/*
* Try to sane stop the pipe worker, corresponding its current state
*/
switch (
(state = InterlockedCompareExchange(&pipeTI->state,
PTI_STATE_STOP, PTI_STATE_IDLE))
) {
case PTI_STATE_IDLE:
/* Thread was idle/waiting, notify it goes teardown */
SetEvent(evControl);
/* we don't need to wait for it at all, thread frees himself (owns the TI structure) */
pipeTI = NULL;
break;
case PTI_STATE_STOP:
/* already stopped, thread frees himself (owns the TI structure) */
pipeTI = NULL;
break;
case PTI_STATE_DOWN:
/* Thread already down (?), do nothing */
/* we don't need to wait for it, but we should free pipeTI */
hThread = NULL;
break;
/* case PTI_STATE_WORK: */
default:
/*
* Thread works currently, we should try to end it, own the TI structure
* (because of possible sharing the joint structures with thread)
*/
if ((state = InterlockedCompareExchange(&pipeTI->state,
PTI_STATE_END, PTI_STATE_WORK)) == PTI_STATE_DOWN
) {
/* we don't need to wait for it, but we should free pipeTI */
hThread = NULL;
};
break;
}
if (pipeTI && hThread) {
DWORD exitCode;
/*
* The thread may already have closed on its own. Check its exit
* code.
*/
GetExitCodeThread(hThread, &exitCode);
if (exitCode == STILL_ACTIVE) {
int inExit = (TclInExit() || TclInThreadExit());
/*
* Set the stop event so that if the pipe thread is blocked
* somewhere, it may hereafter sane exit cleanly.
*/
SetEvent(evControl);
/*
* Cancel all sync-IO of this thread (may be blocked there).
*/
if (tclWinProcs.cancelSynchronousIo) {
tclWinProcs.cancelSynchronousIo(hThread);
}
/*
* Wait at most 20 milliseconds for the reader thread to
* close (regarding TIP#398-fast-exit).
*/
/* if we want TIP#398-fast-exit. */
if (WaitForSingleObject(hThread, inExit ? 0 : 20) == WAIT_TIMEOUT) {
/*
* The thread must be blocked waiting for the pipe to
* become readable in ReadFile(). There isn't a clean way
* to exit the thread from this condition. We should
* terminate the child process instead to get the reader
* thread to fall out of ReadFile with a FALSE. (below) is
* not the correct way to do this, but will stay here
* until a better solution is found.
*
* 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.
*
* Also note that terminating threads during their initialization or teardown phase
* may result in ntdll.dll's LoaderLock to remain locked indefinitely.
* This causes ntdll.dll's LdrpInitializeThread() to deadlock trying to acquire LoaderLock.
* LdrpInitializeThread() is executed within new threads to perform
* initialization and to execute DllMain() of all loaded dlls.
* As a result, all new threads are deadlocked in their initialization phase and never execute,
* even though CreateThread() reports successful thread creation.
* This results in a very weird process-wide behavior, which is extremely hard to debug.
*
* THREADS SHOULD NEVER BE TERMINATED. Period.
*
* But for now, check if thread is exiting, and if so, let it die peacefully.
*
* Also don't terminate if in exit (otherwise deadlocked in ntdll.dll's).
*/
if ( pipeTI->state != PTI_STATE_DOWN
&& WaitForSingleObject(hThread,
inExit ? 50 : 5000) != WAIT_OBJECT_0
) {
/* BUG: this leaks memory */
if (inExit || !TerminateThread(hThread, 0)) {
/* in exit or terminate fails, just give thread a chance to exit */
if (InterlockedExchange(&pipeTI->state,
PTI_STATE_STOP) != PTI_STATE_DOWN) {
pipeTI = NULL;
}
};
}
}
}
}
*pipeTIPtr = NULL;
if (pipeTI) {
if (pipeTI->evWakeUp) {
SetEvent(pipeTI->evWakeUp);
}
CloseHandle(pipeTI->evControl);
#ifndef _PTI_USE_CKALLOC
free(pipeTI);
#else
ckfree(pipeTI);
#endif
}
}
/*
*----------------------------------------------------------------------
*
* TclPipeThreadExit --
*
* Clean-up for the pipe thread (removes owned TI-structure in worker).
*
* Should be executed on worker exit, to inform the main thread or
* free TI-structure (if owned).
*
* After calling of this function, TI-structure pointer given via pipeTIPtr
* is not accessible (owned by main thread or released here).
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
void
TclPipeThreadExit(
TclPipeThreadInfo **pipeTIPtr)
{
LONG state;
TclPipeThreadInfo *pipeTI = *pipeTIPtr;
/*
* If state of thread was set to stop (exactly), we can sane free its info
* structure, otherwise it is shared with main thread, so main thread will
* own it.
*/
if (!pipeTI) {
return;
}
*pipeTIPtr = NULL;
if ((state = InterlockedExchange(&pipeTI->state,
PTI_STATE_DOWN)) == PTI_STATE_STOP) {
CloseHandle(pipeTI->evControl);
if (pipeTI->evWakeUp) {
SetEvent(pipeTI->evWakeUp);
}
#ifndef _PTI_USE_CKALLOC
free(pipeTI);
#else
ckfree(pipeTI);
/* be sure all subsystems used are finalized */
Tcl_FinalizeThread();
#endif
}
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|