2002-08-02 00:20:31 +08:00
|
|
|
/* 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"
|
2002-08-06 00:15:46 +08:00
|
|
|
#include <windows.h>
|
2002-08-29 11:33:50 +08:00
|
|
|
#include <stdlib.h>
|
2002-08-02 00:20:31 +08:00
|
|
|
#include "exceptions.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "cygthread.h"
|
|
|
|
|
|
|
|
#undef CloseHandle
|
|
|
|
|
2002-08-29 11:33:50 +08:00
|
|
|
static cygthread NO_COPY threads[6];
|
2002-08-02 00:20:31 +08:00
|
|
|
#define NTHREADS (sizeof (threads) / sizeof (threads[0]))
|
|
|
|
|
|
|
|
DWORD NO_COPY cygthread::main_thread_id;
|
2002-09-29 10:19:35 +08:00
|
|
|
int NO_COPY cygthread::initialized;
|
2002-08-29 11:33:50 +08:00
|
|
|
|
2002-08-06 00:15:46 +08:00
|
|
|
/* Initial stub called by cygthread constructor. Performs initial
|
|
|
|
per-thread initialization and loops waiting for new thread functions
|
|
|
|
to execute. */
|
2002-08-02 00:20:31 +08:00
|
|
|
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;
|
2002-10-09 13:55:40 +08:00
|
|
|
if (info->arg == cygself)
|
2002-10-14 02:16:33 +08:00
|
|
|
info->ev = info->thread_sync = NULL;
|
2002-10-09 13:55:40 +08:00
|
|
|
else
|
2002-10-14 02:16:33 +08:00
|
|
|
{
|
|
|
|
info->ev = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
|
|
|
info->thread_sync = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
|
|
|
|
}
|
2002-08-02 00:20:31 +08:00
|
|
|
while (1)
|
|
|
|
{
|
2002-10-14 10:37:49 +08:00
|
|
|
if (!info->__name)
|
|
|
|
system_printf ("errnoneous thread activation");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!info->func || initialized < 0)
|
|
|
|
ExitThread (0);
|
2002-08-02 00:20:31 +08:00
|
|
|
|
2002-10-14 10:37:49 +08:00
|
|
|
/* Cygwin threads should not call ExitThread directly */
|
|
|
|
info->func (info->arg == cygself ? info : info->arg);
|
|
|
|
/* ...so the above should always return */
|
2002-08-02 00:20:31 +08:00
|
|
|
|
2002-08-06 13:08:55 +08:00
|
|
|
#ifdef DEBUGGING
|
2002-10-14 10:37:49 +08:00
|
|
|
info->func = NULL; // catch erroneous activation
|
2002-08-06 13:08:55 +08:00
|
|
|
#endif
|
2002-10-14 11:51:44 +08:00
|
|
|
info->__name = NULL;
|
2002-10-14 10:37:49 +08:00
|
|
|
SetEvent (info->ev);
|
|
|
|
}
|
2002-10-14 02:16:33 +08:00
|
|
|
switch (WaitForSingleObject (info->thread_sync, INFINITE))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
2002-10-14 10:37:49 +08:00
|
|
|
// ResetEvent (info->thread_sync);
|
2002-10-14 02:16:33 +08:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
api_fatal ("WFSO failed, %E");
|
|
|
|
break;
|
|
|
|
}
|
2002-08-02 00:20:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-14 02:16:33 +08:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2002-08-06 00:15:46 +08:00
|
|
|
/* This function runs in a secondary thread and starts up a bunch of
|
|
|
|
other suspended threads for use in the cygthread pool. */
|
2002-08-02 00:20:31 +08:00
|
|
|
DWORD WINAPI
|
|
|
|
cygthread::runner (VOID *arg)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < NTHREADS; i++)
|
2002-09-29 10:19:35 +08:00
|
|
|
if (!initialized)
|
|
|
|
threads[i].h = CreateThread (&sec_none_nih, 0, cygthread::stub,
|
|
|
|
&threads[i], CREATE_SUSPENDED,
|
|
|
|
&threads[i].avail);
|
|
|
|
else
|
2002-10-09 12:08:05 +08:00
|
|
|
ExitThread (0);
|
2002-09-29 10:19:35 +08:00
|
|
|
|
|
|
|
initialized ^= 1;
|
2002-10-09 12:08:05 +08:00
|
|
|
ExitThread (0);
|
2002-08-02 00:20:31 +08:00
|
|
|
}
|
|
|
|
|
2002-10-14 02:16:33 +08:00
|
|
|
HANDLE NO_COPY runner_handle;
|
|
|
|
DWORD NO_COPY runner_tid;
|
2002-08-06 00:15:46 +08:00
|
|
|
/* Start things going. Called from dll_crt0_1. */
|
2002-08-02 00:20:31 +08:00
|
|
|
void
|
|
|
|
cygthread::init ()
|
|
|
|
{
|
2002-10-14 02:16:33 +08:00
|
|
|
runner_handle = CreateThread (&sec_none_nih, 0, cygthread::runner, NULL, 0,
|
|
|
|
&runner_tid);
|
|
|
|
if (!runner_handle)
|
2002-08-02 00:20:31 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-08-29 11:33:50 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-08-02 00:20:31 +08:00
|
|
|
void * cygthread::operator
|
|
|
|
new (size_t)
|
|
|
|
{
|
|
|
|
DWORD id;
|
2002-08-06 00:15:46 +08:00
|
|
|
cygthread *info;
|
2002-08-02 00:20:31 +08:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2002-09-29 10:19:35 +08:00
|
|
|
int was_initialized = initialized;
|
|
|
|
if (was_initialized < 0)
|
|
|
|
ExitThread (0);
|
|
|
|
|
2002-08-02 00:20:31 +08:00
|
|
|
/* Search the threads array for an empty slot to use */
|
2002-09-19 11:30:20 +08:00
|
|
|
for (info = threads + NTHREADS - 1; info >= threads; info--)
|
2002-08-02 00:20:31 +08:00
|
|
|
if ((id = (DWORD) InterlockedExchange ((LPLONG) &info->avail, 0)))
|
|
|
|
{
|
|
|
|
info->id = id;
|
2002-08-06 13:08:55 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
if (info->__name)
|
|
|
|
api_fatal ("name not NULL? id %p, i %d", id, info - threads);
|
|
|
|
#endif
|
2002-08-02 00:20:31 +08:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2002-09-29 10:19:35 +08:00
|
|
|
if (was_initialized < 0)
|
|
|
|
ExitThread (0);
|
|
|
|
|
2002-09-19 11:30:20 +08:00
|
|
|
if (!was_initialized)
|
|
|
|
Sleep (0); /* thread_runner is not finished yet. */
|
2002-08-29 11:33:50 +08:00
|
|
|
else
|
2002-09-19 11:30:20 +08:00
|
|
|
{
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
char buf[1024];
|
2002-10-06 09:01:58 +08:00
|
|
|
if (!GetEnvironmentVariable ("CYGWIN_NOFREERANGE_NOCHECK", buf, sizeof (buf)))
|
2002-09-19 11:30:20 +08:00
|
|
|
api_fatal ("Overflowed cygwin thread pool");
|
|
|
|
#endif
|
|
|
|
return freerange ();
|
|
|
|
}
|
2002-08-02 00:20:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cygthread::cygthread (LPTHREAD_START_ROUTINE start, LPVOID param,
|
2002-08-06 13:08:55 +08:00
|
|
|
const char *name): func (start), arg (param)
|
2002-08-02 00:20:31 +08:00
|
|
|
{
|
2002-08-06 13:08:55 +08:00
|
|
|
#ifdef DEBUGGGING
|
|
|
|
if (!__name)
|
|
|
|
api_fatal ("name should never be NULL");
|
|
|
|
#endif
|
|
|
|
thread_printf ("name %s, id %p", name, id);
|
2002-10-14 02:16:33 +08:00
|
|
|
while (!h)
|
2002-08-12 03:31:24 +08:00
|
|
|
#ifndef DEBUGGING
|
2002-08-02 00:20:31 +08:00
|
|
|
Sleep (0);
|
2002-08-12 03:31:24 +08:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
thread_printf ("waiting for %s<%p> to become active", __name, h);
|
|
|
|
Sleep (0);
|
|
|
|
}
|
|
|
|
#endif
|
2002-08-06 13:08:55 +08:00
|
|
|
__name = name; /* Need to set after thread has woken up to
|
|
|
|
ensure that it won't be cleared by exiting
|
|
|
|
thread. */
|
2002-10-14 02:16:33 +08:00
|
|
|
if (thread_sync)
|
|
|
|
SetEvent (thread_sync);
|
|
|
|
else
|
|
|
|
ResumeThread (h);
|
2002-08-02 00:20:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2002-08-06 00:15:46 +08:00
|
|
|
/* Should only be called when the process is exiting since it
|
|
|
|
leaves an open thread slot. */
|
2002-08-02 10:10:24 +08:00
|
|
|
void
|
|
|
|
cygthread::exit_thread ()
|
|
|
|
{
|
2002-08-29 11:33:50 +08:00
|
|
|
if (!is_freerange)
|
|
|
|
SetEvent (*this);
|
2002-08-02 10:10:24 +08:00
|
|
|
ExitThread (0);
|
|
|
|
}
|
|
|
|
|
2002-08-06 00:15:46 +08:00
|
|
|
/* Detach the cygthread from the current thread. Note that the
|
2002-10-14 02:16:33 +08:00
|
|
|
theory is that cygthreads are only associated with one thread.
|
2002-08-06 00:15:46 +08:00
|
|
|
So, there should be no problems with multiple threads doing waits
|
|
|
|
on the one cygthread. */
|
2002-08-02 00:20:31 +08:00
|
|
|
void
|
|
|
|
cygthread::detach ()
|
|
|
|
{
|
2002-08-06 13:08:55 +08:00
|
|
|
if (avail)
|
|
|
|
system_printf ("called detach on available thread %d?", avail);
|
|
|
|
else
|
2002-08-02 00:20:31 +08:00
|
|
|
{
|
|
|
|
DWORD avail = id;
|
2002-08-06 00:15:46 +08:00
|
|
|
/* Checking for __name here is just a minor optimization to avoid
|
|
|
|
an OS call. */
|
|
|
|
if (!__name)
|
2002-08-06 13:08:55 +08:00
|
|
|
thread_printf ("thread id %p returned. No need to wait.", id);
|
2002-08-06 00:15:46 +08:00
|
|
|
else
|
2002-08-02 00:20:31 +08:00
|
|
|
{
|
|
|
|
DWORD res = WaitForSingleObject (*this, INFINITE);
|
2002-08-06 13:08:55 +08:00
|
|
|
thread_printf ("WFSO returns %d, id %p", res, id);
|
2002-08-02 00:20:31 +08:00
|
|
|
}
|
2002-08-29 11:33:50 +08:00
|
|
|
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);
|
|
|
|
}
|
2002-08-02 00:20:31 +08:00
|
|
|
}
|
|
|
|
}
|
2002-09-29 10:19:35 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
cygthread::terminate ()
|
|
|
|
{
|
2002-10-14 02:16:33 +08:00
|
|
|
/* 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);
|
2002-10-14 11:51:44 +08:00
|
|
|
HANDLE hthreads[NTHREADS];
|
|
|
|
int n = 0;
|
2002-10-14 02:16:33 +08:00
|
|
|
for (unsigned i = 0; i < NTHREADS; i++)
|
|
|
|
if (threads[i].h)
|
|
|
|
{
|
2002-10-14 11:51:44 +08:00
|
|
|
hthreads[n++] = threads[i].h;
|
2002-10-14 02:16:33 +08:00
|
|
|
TerminateThread (threads[i].h, 0);
|
|
|
|
}
|
2002-10-14 11:51:44 +08:00
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
(void) WaitForMultipleObjects (n, hthreads, TRUE, INFINITE);
|
|
|
|
while (--n >= 0)
|
|
|
|
CloseHandle (hthreads[n]);
|
|
|
|
}
|
2002-10-14 02:16:33 +08:00
|
|
|
}
|
2002-09-29 10:19:35 +08:00
|
|
|
}
|