2002-08-01 18:20:31 +02:00
|
|
|
/* cygthread.cc
|
|
|
|
|
2003-01-10 13:32:49 +01:00
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
|
2002-08-01 18:20:31 +02:00
|
|
|
|
|
|
|
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-05 18:15:46 +02:00
|
|
|
#include <windows.h>
|
2002-08-29 05:33:50 +02:00
|
|
|
#include <stdlib.h>
|
2002-08-01 18:20:31 +02:00
|
|
|
#include "exceptions.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "cygthread.h"
|
2002-10-22 22:16:31 +02:00
|
|
|
#include "sync.h"
|
2002-12-11 05:00:04 +01:00
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "sigproc.h"
|
2003-12-03 06:21:55 +01:00
|
|
|
#include "thread.h"
|
|
|
|
#include "cygtls.h"
|
2002-08-01 18:20:31 +02:00
|
|
|
|
|
|
|
#undef CloseHandle
|
|
|
|
|
2004-01-19 06:46:54 +01:00
|
|
|
static cygthread NO_COPY threads[32];
|
2002-08-01 18:20:31 +02:00
|
|
|
#define NTHREADS (sizeof (threads) / sizeof (threads[0]))
|
|
|
|
|
|
|
|
DWORD NO_COPY cygthread::main_thread_id;
|
2002-10-22 22:16:31 +02:00
|
|
|
bool NO_COPY cygthread::exiting;
|
2002-08-29 05:33:50 +02:00
|
|
|
|
2002-08-05 18:15:46 +02:00
|
|
|
/* Initial stub called by cygthread constructor. Performs initial
|
|
|
|
per-thread initialization and loops waiting for new thread functions
|
|
|
|
to execute. */
|
2002-08-01 18:20:31 +02:00
|
|
|
DWORD WINAPI
|
|
|
|
cygthread::stub (VOID *arg)
|
2003-12-03 06:21:55 +01:00
|
|
|
{
|
2002-08-01 18:20:31 +02:00
|
|
|
cygthread *info = (cygthread *) arg;
|
2002-10-09 07:55:40 +02:00
|
|
|
if (info->arg == cygself)
|
2003-04-10 07:27:34 +02:00
|
|
|
{
|
|
|
|
if (info->ev)
|
|
|
|
{
|
|
|
|
CloseHandle (info->ev);
|
|
|
|
CloseHandle (info->thread_sync);
|
|
|
|
}
|
2004-01-14 16:45:37 +01:00
|
|
|
info->ev = info->thread_sync = info->stack_ptr = NULL;
|
2003-04-10 07:27:34 +02:00
|
|
|
}
|
2002-10-09 07:55:40 +02:00
|
|
|
else
|
2002-10-13 20:16:33 +02:00
|
|
|
{
|
2003-04-11 04:16:17 +02:00
|
|
|
info->stack_ptr = &arg;
|
2003-04-10 07:27:34 +02:00
|
|
|
if (!info->ev)
|
|
|
|
{
|
|
|
|
info->ev = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
|
|
|
info->thread_sync = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
|
|
|
|
}
|
2002-10-13 20:16:33 +02:00
|
|
|
}
|
2004-01-14 16:45:37 +01:00
|
|
|
|
2002-08-01 18:20:31 +02:00
|
|
|
while (1)
|
|
|
|
{
|
2002-10-14 04:37:49 +02:00
|
|
|
if (!info->__name)
|
2002-10-20 20:17:43 +02:00
|
|
|
system_printf ("erroneous thread activation");
|
2002-10-14 04:37:49 +02:00
|
|
|
else
|
|
|
|
{
|
2002-10-22 22:16:31 +02:00
|
|
|
if (!info->func || exiting)
|
2003-12-14 08:09:22 +01:00
|
|
|
return 0;
|
2002-08-01 18:20:31 +02:00
|
|
|
|
2002-10-14 04:37:49 +02:00
|
|
|
/* Cygwin threads should not call ExitThread directly */
|
|
|
|
info->func (info->arg == cygself ? info : info->arg);
|
|
|
|
/* ...so the above should always return */
|
2002-08-01 18:20:31 +02:00
|
|
|
|
2002-08-06 07:08:55 +02:00
|
|
|
#ifdef DEBUGGING
|
2002-10-14 04:37:49 +02:00
|
|
|
info->func = NULL; // catch erroneous activation
|
2002-08-06 07:08:55 +02:00
|
|
|
#endif
|
2002-10-14 05:51:44 +02:00
|
|
|
info->__name = NULL;
|
2002-10-14 04:37:49 +02:00
|
|
|
SetEvent (info->ev);
|
|
|
|
}
|
2002-10-13 20:16:33 +02:00
|
|
|
switch (WaitForSingleObject (info->thread_sync, INFINITE))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
api_fatal ("WFSO failed, %E");
|
|
|
|
break;
|
|
|
|
}
|
2002-08-01 18:20:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-13 20:16:33 +02:00
|
|
|
/* Overflow stub called by cygthread constructor. Calls specified function
|
|
|
|
and then exits the thread. */
|
|
|
|
DWORD WINAPI
|
|
|
|
cygthread::simplestub (VOID *arg)
|
2003-12-03 06:21:55 +01:00
|
|
|
{
|
2002-10-13 20:16:33 +02:00
|
|
|
cygthread *info = (cygthread *) arg;
|
2003-04-11 04:16:17 +02:00
|
|
|
info->stack_ptr = &arg;
|
2003-12-05 06:29:17 +01:00
|
|
|
info->ev = info->h;
|
2002-10-13 20:16:33 +02:00
|
|
|
info->func (info->arg == cygself ? info : info->arg);
|
2003-12-14 08:09:22 +01:00
|
|
|
return 0;
|
2002-10-13 20:16:33 +02:00
|
|
|
}
|
|
|
|
|
2002-08-05 18:15:46 +02:00
|
|
|
/* Start things going. Called from dll_crt0_1. */
|
2002-08-01 18:20:31 +02:00
|
|
|
void
|
|
|
|
cygthread::init ()
|
|
|
|
{
|
|
|
|
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-10-22 22:16:31 +02:00
|
|
|
cygthread *
|
2002-08-29 05:33:50 +02:00
|
|
|
cygthread::freerange ()
|
|
|
|
{
|
|
|
|
cygthread *self = (cygthread *) calloc (1, sizeof (*self));
|
|
|
|
self->is_freerange = true;
|
2003-11-07 19:21:05 +01:00
|
|
|
self->inuse = 1;
|
2002-08-29 05:33:50 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2002-08-01 18:20:31 +02:00
|
|
|
void * cygthread::operator
|
|
|
|
new (size_t)
|
|
|
|
{
|
2002-08-05 18:15:46 +02:00
|
|
|
cygthread *info;
|
2002-08-01 18:20:31 +02:00
|
|
|
|
2002-10-22 22:16:31 +02:00
|
|
|
/* Search the threads array for an empty slot to use */
|
|
|
|
for (info = threads; info < threads + NTHREADS; info++)
|
2003-04-10 07:27:34 +02:00
|
|
|
if (!InterlockedExchange (&info->inuse, 1))
|
2002-10-22 22:16:31 +02:00
|
|
|
{
|
2003-04-04 20:52:11 +02:00
|
|
|
/* available */
|
2002-08-06 07:08:55 +02:00
|
|
|
#ifdef DEBUGGING
|
2002-10-22 22:16:31 +02:00
|
|
|
if (info->__name)
|
2003-04-04 20:52:11 +02:00
|
|
|
api_fatal ("name not NULL? id %p, i %d", info->id, info - threads);
|
2002-08-06 07:08:55 +02:00
|
|
|
#endif
|
2002-10-22 22:16:31 +02:00
|
|
|
goto out;
|
|
|
|
}
|
2002-09-29 04:19:35 +02:00
|
|
|
|
2002-09-19 05:30:20 +02:00
|
|
|
#ifdef DEBUGGING
|
2002-10-22 22:16:31 +02:00
|
|
|
char buf[1024];
|
2003-04-04 20:52:11 +02:00
|
|
|
if (!GetEnvironmentVariable ("CYGWIN_FREERANGE_NOCHECK", buf, sizeof (buf)))
|
2002-10-22 22:16:31 +02:00
|
|
|
api_fatal ("Overflowed cygwin thread pool");
|
2003-11-07 19:21:05 +01:00
|
|
|
else
|
|
|
|
thread_printf ("Overflowed cygwin thread pool");
|
2002-09-19 05:30:20 +02:00
|
|
|
#endif
|
2002-10-22 22:16:31 +02:00
|
|
|
|
|
|
|
info = freerange (); /* exhausted thread pool */
|
|
|
|
|
|
|
|
out:
|
|
|
|
return info;
|
2002-08-01 18:20:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cygthread::cygthread (LPTHREAD_START_ROUTINE start, LPVOID param,
|
2003-04-10 07:27:34 +02:00
|
|
|
const char *name): __name (name),
|
|
|
|
func (start), arg (param)
|
2002-08-01 18:20:31 +02:00
|
|
|
{
|
2002-08-06 07:08:55 +02:00
|
|
|
thread_printf ("name %s, id %p", name, id);
|
2003-04-10 07:27:34 +02:00
|
|
|
if (h)
|
2002-08-11 21:31:24 +02:00
|
|
|
{
|
2003-04-10 07:27:34 +02:00
|
|
|
while (!thread_sync)
|
|
|
|
low_priority_sleep (0);
|
|
|
|
SetEvent (thread_sync);
|
|
|
|
thread_printf ("activated thread_sync %p", thread_sync);
|
2002-08-11 21:31:24 +02:00
|
|
|
}
|
2002-10-21 03:00:58 +02:00
|
|
|
else
|
2003-04-10 07:27:34 +02:00
|
|
|
{
|
2003-04-11 04:16:17 +02:00
|
|
|
stack_ptr = NULL;
|
2003-04-10 07:27:34 +02:00
|
|
|
h = CreateThread (&sec_none_nih, 0, is_freerange ? simplestub : stub,
|
|
|
|
this, 0, &id);
|
|
|
|
if (!h)
|
|
|
|
api_fatal ("thread handle not set - %p<%p>, %E", h, id);
|
|
|
|
thread_printf ("created thread %p", h);
|
|
|
|
}
|
2002-08-01 18:20:31 +02: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)
|
2002-11-13 20:36:12 +01:00
|
|
|
low_priority_sleep (0);
|
2002-08-01 18:20:31 +02:00
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
|
2002-08-05 18:15:46 +02:00
|
|
|
/* Should only be called when the process is exiting since it
|
|
|
|
leaves an open thread slot. */
|
2002-08-02 04:10:24 +02:00
|
|
|
void
|
|
|
|
cygthread::exit_thread ()
|
|
|
|
{
|
2002-08-29 05:33:50 +02:00
|
|
|
if (!is_freerange)
|
|
|
|
SetEvent (*this);
|
2002-08-02 04:10:24 +02:00
|
|
|
ExitThread (0);
|
|
|
|
}
|
|
|
|
|
2002-12-11 05:00:04 +01:00
|
|
|
/* Forcibly terminate a thread. */
|
|
|
|
void
|
|
|
|
cygthread::terminate_thread ()
|
|
|
|
{
|
|
|
|
if (!is_freerange)
|
2003-04-10 07:27:34 +02:00
|
|
|
{
|
|
|
|
ResetEvent (*this);
|
|
|
|
ResetEvent (thread_sync);
|
|
|
|
}
|
2002-12-11 05:00:04 +01:00
|
|
|
(void) TerminateThread (h, 0);
|
|
|
|
(void) WaitForSingleObject (h, INFINITE);
|
2003-04-10 07:27:34 +02:00
|
|
|
CloseHandle (h);
|
|
|
|
|
2003-04-11 04:16:17 +02:00
|
|
|
while (!stack_ptr)
|
|
|
|
low_priority_sleep (0);
|
2002-12-11 05:00:04 +01:00
|
|
|
|
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
memset (&m, 0, sizeof (m));
|
|
|
|
(void) VirtualQuery (stack_ptr, &m, sizeof m);
|
|
|
|
|
2003-04-11 04:16:17 +02:00
|
|
|
if (!m.RegionSize)
|
|
|
|
system_printf ("m.RegionSize 0? stack_ptr %p", stack_ptr);
|
|
|
|
else if (!VirtualFree (m.AllocationBase, 0, MEM_RELEASE))
|
2003-06-05 21:18:33 +02:00
|
|
|
debug_printf ("VirtualFree of allocation base %p<%p> failed, %E",
|
2003-04-11 04:16:17 +02:00
|
|
|
stack_ptr, m.AllocationBase);
|
2002-12-11 05:00:04 +01:00
|
|
|
|
2003-06-02 23:22:02 +02:00
|
|
|
if (is_freerange)
|
|
|
|
free (this);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
h = NULL;
|
|
|
|
__name = NULL;
|
|
|
|
stack_ptr = NULL;
|
|
|
|
(void) InterlockedExchange (&inuse, 0); /* No longer in use */
|
|
|
|
}
|
2002-12-11 05:00:04 +01:00
|
|
|
}
|
|
|
|
|
2002-08-05 18:15:46 +02:00
|
|
|
/* Detach the cygthread from the current thread. Note that the
|
2002-10-13 20:16:33 +02:00
|
|
|
theory is that cygthreads are only associated with one thread.
|
2003-04-04 20:52:11 +02:00
|
|
|
So, there should be never be multiple threads doing waits
|
|
|
|
on the same cygthread. */
|
2002-12-14 05:01:32 +01:00
|
|
|
bool
|
|
|
|
cygthread::detach (HANDLE sigwait)
|
2002-08-01 18:20:31 +02:00
|
|
|
{
|
2002-12-14 05:01:32 +01:00
|
|
|
bool signalled = false;
|
2003-04-10 07:27:34 +02:00
|
|
|
if (!inuse)
|
|
|
|
system_printf ("called detach but inuse %d, thread %p?", inuse, id);
|
2002-08-06 07:08:55 +02:00
|
|
|
else
|
2002-08-01 18:20:31 +02:00
|
|
|
{
|
2002-12-11 05:00:04 +01:00
|
|
|
DWORD res;
|
|
|
|
|
2002-12-14 05:01:32 +01:00
|
|
|
if (!sigwait)
|
2002-12-11 05:00:04 +01:00
|
|
|
res = WaitForSingleObject (*this, INFINITE);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HANDLE w4[2];
|
2003-05-09 19:56:58 +02:00
|
|
|
w4[0] = *this;
|
|
|
|
w4[1] = signal_arrived;
|
2002-12-14 05:01:32 +01:00
|
|
|
res = WaitForSingleObject (sigwait, INFINITE);
|
|
|
|
if (res != WAIT_OBJECT_0)
|
2003-02-27 16:09:31 +01:00
|
|
|
system_printf ("WFSO sigwait %p failed, res %u, %E", sigwait, res);
|
2002-12-11 05:00:04 +01:00
|
|
|
res = WaitForMultipleObjects (2, w4, FALSE, INFINITE);
|
2003-05-09 19:56:58 +02:00
|
|
|
if (res == WAIT_OBJECT_0)
|
2002-12-14 05:01:32 +01:00
|
|
|
/* nothing */;
|
2003-05-09 19:56:58 +02:00
|
|
|
else if (WaitForSingleObject (sigwait, 0) == WAIT_OBJECT_0)
|
2002-12-14 05:01:32 +01:00
|
|
|
res = WaitForSingleObject (*this, INFINITE);
|
2003-05-09 19:56:58 +02:00
|
|
|
else if ((res = WaitForSingleObject (*this, 0)) != WAIT_OBJECT_0)
|
2002-12-11 05:00:04 +01:00
|
|
|
{
|
2003-04-04 20:52:11 +02:00
|
|
|
signalled = true;
|
2002-12-11 05:00:04 +01:00
|
|
|
terminate_thread ();
|
2002-12-25 18:43:37 +01:00
|
|
|
set_sig_errno (EINTR); /* caller should be dealing with return
|
2002-12-11 05:00:04 +01:00
|
|
|
values. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-14 05:01:32 +01:00
|
|
|
thread_printf ("%s returns %d, id %p", sigwait ? "WFMO" : "WFSO",
|
2002-12-11 05:00:04 +01:00
|
|
|
res, id);
|
2002-10-21 03:00:58 +02:00
|
|
|
|
2003-04-04 20:52:11 +02:00
|
|
|
if (signalled)
|
2002-12-11 05:00:04 +01:00
|
|
|
/* already handled */;
|
|
|
|
else if (is_freerange)
|
2002-08-29 05:33:50 +02:00
|
|
|
{
|
|
|
|
CloseHandle (h);
|
|
|
|
free (this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-10-21 03:00:58 +02:00
|
|
|
ResetEvent (*this);
|
2003-04-10 07:27:34 +02:00
|
|
|
/* Mark the thread as available by setting inuse to zero */
|
|
|
|
(void) InterlockedExchange (&inuse, 0);
|
2002-08-29 05:33:50 +02:00
|
|
|
}
|
2002-08-01 18:20:31 +02:00
|
|
|
}
|
2002-12-14 05:01:32 +01:00
|
|
|
return signalled;
|
2002-08-01 18:20:31 +02:00
|
|
|
}
|
2002-09-29 04:19:35 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
cygthread::terminate ()
|
|
|
|
{
|
2002-10-22 22:16:31 +02:00
|
|
|
exiting = 1;
|
2002-09-29 04:19:35 +02:00
|
|
|
}
|