2002-08-01 18:20:31 +02: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-05 18:15:46 +02:00
|
|
|
#include <windows.h>
|
2002-08-01 18:20:31 +02:00
|
|
|
#include "exceptions.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "cygthread.h"
|
|
|
|
|
|
|
|
#undef CloseHandle
|
|
|
|
|
|
|
|
static cygthread NO_COPY threads[8];
|
|
|
|
#define NTHREADS (sizeof (threads) / sizeof (threads[0]))
|
|
|
|
|
|
|
|
static HANDLE NO_COPY hthreads[NTHREADS];
|
|
|
|
|
|
|
|
DWORD NO_COPY cygthread::main_thread_id;
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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-08-05 18:15:46 +02:00
|
|
|
info->ev = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
2002-08-01 18:20:31 +02:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (!info->func)
|
|
|
|
ExitThread (0);
|
|
|
|
|
2002-08-06 07:08:55 +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
|
|
|
|
info->func = NULL; // catch erroneous activation
|
|
|
|
#endif
|
2002-08-01 18:20:31 +02:00
|
|
|
SetEvent (info->ev);
|
2002-08-05 18:15:46 +02:00
|
|
|
info->__name = NULL;
|
2002-08-01 18:20:31 +02:00
|
|
|
SuspendThread (info->h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-05 18:15:46 +02: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-01 18:20:31 +02:00
|
|
|
DWORD WINAPI
|
|
|
|
cygthread::runner (VOID *arg)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < NTHREADS; i++)
|
|
|
|
hthreads[i] = threads[i].h =
|
|
|
|
CreateThread (&sec_none_nih, 0, cygthread::stub, &threads[i],
|
|
|
|
CREATE_SUSPENDED, &threads[i].avail);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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 ()
|
|
|
|
{
|
|
|
|
DWORD tid;
|
|
|
|
HANDLE h = CreateThread (&sec_none_nih, 0, cygthread::runner, NULL, 0, &tid);
|
|
|
|
if (!h)
|
|
|
|
api_fatal ("can't start thread_runner, %E");
|
|
|
|
CloseHandle (h);
|
|
|
|
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::operator
|
|
|
|
new (size_t)
|
|
|
|
{
|
|
|
|
DWORD id;
|
2002-08-05 18:15:46 +02:00
|
|
|
cygthread *info;
|
2002-08-01 18:20:31 +02:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Search the threads array for an empty slot to use */
|
|
|
|
for (info = threads; info < threads + NTHREADS; info++)
|
|
|
|
if ((id = (DWORD) InterlockedExchange ((LPLONG) &info->avail, 0)))
|
|
|
|
{
|
|
|
|
info->id = id;
|
2002-08-06 07:08:55 +02:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
if (info->__name)
|
|
|
|
api_fatal ("name not NULL? id %p, i %d", id, info - threads);
|
|
|
|
#endif
|
2002-08-01 18:20:31 +02:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thread_runner may not be finished yet. */
|
|
|
|
Sleep (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cygthread::cygthread (LPTHREAD_START_ROUTINE start, LPVOID param,
|
2002-08-06 07:08:55 +02:00
|
|
|
const char *name): func (start), arg (param)
|
2002-08-01 18:20:31 +02:00
|
|
|
{
|
2002-08-06 07:08:55 +02:00
|
|
|
#ifdef DEBUGGGING
|
|
|
|
if (!__name)
|
|
|
|
api_fatal ("name should never be NULL");
|
|
|
|
#endif
|
|
|
|
thread_printf ("name %s, id %p", name, id);
|
2002-08-11 21:31:24 +02:00
|
|
|
while (!h || ResumeThread (h) != 1)
|
|
|
|
#ifndef DEBUGGING
|
2002-08-01 18:20:31 +02:00
|
|
|
Sleep (0);
|
2002-08-11 21:31:24 +02:00
|
|
|
#else
|
|
|
|
{
|
|
|
|
thread_printf ("waiting for %s<%p> to become active", __name, h);
|
|
|
|
Sleep (0);
|
|
|
|
}
|
|
|
|
#endif
|
2002-08-06 07:08:55 +02:00
|
|
|
__name = name; /* Need to set after thread has woken up to
|
|
|
|
ensure that it won't be cleared by exiting
|
|
|
|
thread. */
|
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)
|
|
|
|
Sleep (0);
|
|
|
|
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-06 07:08:55 +02:00
|
|
|
SetEvent (*this);
|
2002-08-02 04:10:24 +02:00
|
|
|
ExitThread (0);
|
|
|
|
}
|
|
|
|
|
2002-08-05 18:15:46 +02:00
|
|
|
/* Detach the cygthread from the current thread. Note that the
|
|
|
|
theory is that cygthread's are only associated with one thread.
|
|
|
|
So, there should be no problems with multiple threads doing waits
|
|
|
|
on the one cygthread. */
|
2002-08-01 18:20:31 +02:00
|
|
|
void
|
|
|
|
cygthread::detach ()
|
|
|
|
{
|
2002-08-06 07:08:55 +02:00
|
|
|
if (avail)
|
|
|
|
system_printf ("called detach on available thread %d?", avail);
|
|
|
|
else
|
2002-08-01 18:20:31 +02:00
|
|
|
{
|
|
|
|
DWORD avail = id;
|
2002-08-05 18:15:46 +02:00
|
|
|
/* Checking for __name here is just a minor optimization to avoid
|
|
|
|
an OS call. */
|
|
|
|
if (!__name)
|
2002-08-06 07:08:55 +02:00
|
|
|
thread_printf ("thread id %p returned. No need to wait.", id);
|
2002-08-05 18:15:46 +02:00
|
|
|
else
|
2002-08-01 18:20:31 +02:00
|
|
|
{
|
|
|
|
DWORD res = WaitForSingleObject (*this, INFINITE);
|
2002-08-06 07:08:55 +02:00
|
|
|
thread_printf ("WFSO returns %d, id %p", res, id);
|
2002-08-01 18:20:31 +02:00
|
|
|
}
|
2002-08-05 18:15:46 +02:00
|
|
|
ResetEvent (*this);
|
2002-08-01 18:20:31 +02:00
|
|
|
id = 0;
|
2002-08-06 07:08:55 +02:00
|
|
|
__name = NULL;
|
2002-08-05 18:15:46 +02:00
|
|
|
/* Mark the thread as available by setting avail to non-zero */
|
2002-08-01 18:20:31 +02:00
|
|
|
(void) InterlockedExchange ((LPLONG) &this->avail, avail);
|
|
|
|
}
|
|
|
|
}
|