1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/* cygthread.cc
Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <windows.h>
#include <stdlib.h>
#include "exceptions.h"
#include "security.h"
#include "cygthread.h"
#undef CloseHandle
static cygthread NO_COPY threads[9];
#define NTHREADS (sizeof (threads) / sizeof (threads[0]))
DWORD NO_COPY cygthread::main_thread_id;
int NO_COPY cygthread::initialized;
/* Initial stub called by cygthread constructor. Performs initial
per-thread initialization and loops waiting for new thread functions
to execute. */
DWORD WINAPI
cygthread::stub (VOID *arg)
{
DECLARE_TLS_STORAGE;
exception_list except_entry;
/* Initialize this thread's ability to respond to things like
SIGSEGV or SIGFPE. */
init_exceptions (&except_entry);
cygthread *info = (cygthread *) arg;
if (info->arg == cygself)
info->ev = info->thread_sync = NULL;
else
{
info->ev = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
info->thread_sync = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
}
while (1)
{
if (!info->__name)
system_printf ("erroneous thread activation");
else
{
if (!info->func || initialized < 0)
ExitThread (0);
/* Cygwin threads should not call ExitThread directly */
info->func (info->arg == cygself ? info : info->arg);
/* ...so the above should always return */
#ifdef DEBUGGING
info->func = NULL; // catch erroneous activation
#endif
info->__name = NULL;
SetEvent (info->ev);
}
switch (WaitForSingleObject (info->thread_sync, INFINITE))
{
case WAIT_OBJECT_0:
// ResetEvent (info->thread_sync);
continue;
default:
api_fatal ("WFSO failed, %E");
break;
}
}
}
/* Overflow stub called by cygthread constructor. Calls specified function
and then exits the thread. */
DWORD WINAPI
cygthread::simplestub (VOID *arg)
{
DECLARE_TLS_STORAGE;
exception_list except_entry;
/* Initialize this thread's ability to respond to things like
SIGSEGV or SIGFPE. */
init_exceptions (&except_entry);
cygthread *info = (cygthread *) arg;
info->func (info->arg == cygself ? info : info->arg);
ExitThread (0);
}
/* This function runs in a secondary thread and starts up a bunch of
other suspended threads for use in the cygthread pool. */
DWORD WINAPI
cygthread::runner (VOID *arg)
{
for (unsigned i = 0; i < NTHREADS; i++)
if (!initialized)
threads[i].h = CreateThread (&sec_none_nih, 0, cygthread::stub,
&threads[i], CREATE_SUSPENDED,
&threads[i].avail);
else
ExitThread (0);
initialized ^= 1;
ExitThread (0);
}
HANDLE NO_COPY runner_handle;
DWORD NO_COPY runner_tid;
/* Start things going. Called from dll_crt0_1. */
void
cygthread::init ()
{
runner_handle = CreateThread (&sec_none_nih, 0, cygthread::runner, NULL, 0,
&runner_tid);
if (!runner_handle)
api_fatal ("can't start thread_runner, %E");
main_thread_id = GetCurrentThreadId ();
}
bool
cygthread::is ()
{
DWORD tid = GetCurrentThreadId ();
for (DWORD i = 0; i < NTHREADS; i++)
if (threads[i].id == tid)
return 1;
return 0;
}
void *
cygthread::freerange ()
{
cygthread *self = (cygthread *) calloc (1, sizeof (*self));
self->is_freerange = true;
self->h = CreateThread (&sec_none_nih, 0, cygthread::simplestub, self,
CREATE_SUSPENDED, &self->id);
self->ev = self->h;
return self;
}
void * cygthread::operator
new (size_t)
{
DWORD id;
cygthread *info;
for (;;)
{
int was_initialized = initialized;
if (was_initialized < 0)
ExitThread (0);
/* Search the threads array for an empty slot to use */
for (info = threads + NTHREADS - 1; info >= threads; info--)
if ((id = (DWORD) InterlockedExchange ((LPLONG) &info->avail, 0)))
{
info->id = id;
#ifdef DEBUGGING
if (info->__name)
api_fatal ("name not NULL? id %p, i %d", id, info - threads);
#endif
return info;
}
if (was_initialized < 0)
ExitThread (0);
if (!was_initialized)
Sleep (0); /* thread_runner is not finished yet. */
else
{
#ifdef DEBUGGING
char buf[1024];
if (!GetEnvironmentVariable ("CYGWIN_NOFREERANGE_NOCHECK", buf, sizeof (buf)))
api_fatal ("Overflowed cygwin thread pool");
#endif
return freerange ();
}
}
}
cygthread::cygthread (LPTHREAD_START_ROUTINE start, LPVOID param,
const char *name): func (start), arg (param)
{
#ifdef DEBUGGGING
if (!__name)
api_fatal ("name should never be NULL");
#endif
thread_printf ("name %s, id %p", name, id);
while (!h)
#ifndef DEBUGGING
Sleep (0);
#else
{
thread_printf ("waiting for %s<%p> to become active", __name, h);
Sleep (0);
}
#endif
__name = name; /* Need to set after thread has woken up to
ensure that it won't be cleared by exiting
thread. */
if (thread_sync)
SetEvent (thread_sync);
else
ResumeThread (h);
}
/* Return the symbolic name of the current thread for debugging.
*/
const char *
cygthread::name (DWORD tid)
{
const char *res = NULL;
if (!tid)
tid = GetCurrentThreadId ();
if (tid == main_thread_id)
return "main";
for (DWORD i = 0; i < NTHREADS; i++)
if (threads[i].id == tid)
{
res = threads[i].__name ?: "exiting thread";
break;
}
if (!res)
{
static char buf[30] NO_COPY = {0};
__small_sprintf (buf, "unknown (%p)", tid);
res = buf;
}
return res;
}
cygthread::operator
HANDLE ()
{
while (!ev)
Sleep (0);
return ev;
}
/* Should only be called when the process is exiting since it
leaves an open thread slot. */
void
cygthread::exit_thread ()
{
if (!is_freerange)
SetEvent (*this);
ExitThread (0);
}
/* Detach the cygthread from the current thread. Note that the
theory is that cygthreads are only associated with one thread.
So, there should be no problems with multiple threads doing waits
on the one cygthread. */
void
cygthread::detach ()
{
if (avail)
system_printf ("called detach on available thread %d?", avail);
else
{
DWORD avail = id;
/* Checking for __name here is just a minor optimization to avoid
an OS call. */
if (!__name)
thread_printf ("thread id %p returned. No need to wait.", id);
else
{
DWORD res = WaitForSingleObject (*this, INFINITE);
thread_printf ("WFSO returns %d, id %p", res, id);
}
if (is_freerange)
{
CloseHandle (h);
free (this);
}
else
{
ResetEvent (*this);
id = 0;
__name = NULL;
/* Mark the thread as available by setting avail to non-zero */
(void) InterlockedExchange ((LPLONG) &this->avail, avail);
}
}
}
void
cygthread::terminate ()
{
/* Wow. All of this seems to be necessary or (on Windows 9x at least) the
process will sometimes deadlock if there are suspended threads. I assume
that something funky is happening like a suspended thread being created
while the process is exiting or something. In particular, it seems like
the WaitForSingleObjects are necessary since it appears that the
TerminateThread call may happen asynchronously, i.e., when TerminateThread
returns, the thread may not yet have terminated. */
if (runner_handle && initialized >= 0)
{
/* Don't care about detaching (or attaching) threads now */
if (cygwin_hmodule && !DisableThreadLibraryCalls (cygwin_hmodule))
system_printf ("DisableThreadLibraryCalls (%p) failed, %E",
cygwin_hmodule);
initialized = -1;
(void) TerminateThread (runner_handle, 0);
(void) WaitForSingleObject (runner_handle, INFINITE);
(void) CloseHandle (runner_handle);
HANDLE hthreads[NTHREADS];
int n = 0;
for (unsigned i = 0; i < NTHREADS; i++)
if (threads[i].h)
{
hthreads[n] = threads[i].h;
threads[i].h = NULL;
TerminateThread (hthreads[n++], 0);
}
if (n)
{
(void) WaitForMultipleObjects (n, hthreads, TRUE, INFINITE);
while (--n >= 0)
CloseHandle (hthreads[n]);
}
}
}
|