2002-08-01 18:20:31 +02:00
|
|
|
/* cygthread.cc
|
|
|
|
|
2009-01-03 06:12:22 +01:00
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009
|
|
|
|
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"
|
2008-04-07 18:15:45 +02:00
|
|
|
#include "miscfuncs.h"
|
2002-08-29 05:33:50 +02:00
|
|
|
#include <stdlib.h>
|
2002-12-11 05:00:04 +01:00
|
|
|
#include "sigproc.h"
|
2003-12-03 06:21:55 +01:00
|
|
|
#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
|
|
|
|
2005-10-18 01:27:00 +02:00
|
|
|
void
|
|
|
|
cygthread::callfunc (bool issimplestub)
|
|
|
|
{
|
|
|
|
void *pass_arg;
|
|
|
|
if (arg == cygself)
|
|
|
|
pass_arg = this;
|
|
|
|
else if (!arglen)
|
|
|
|
pass_arg = arg;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (issimplestub)
|
2006-03-01 22:31:24 +01:00
|
|
|
ev = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
2005-10-18 01:27:00 +02:00
|
|
|
pass_arg = alloca (arglen);
|
|
|
|
memcpy (pass_arg, arg, arglen);
|
|
|
|
SetEvent (ev);
|
|
|
|
}
|
|
|
|
if (issimplestub)
|
|
|
|
{
|
|
|
|
/* Wait for main thread to assign 'h' */
|
|
|
|
while (!h)
|
|
|
|
low_priority_sleep (0);
|
|
|
|
if (ev)
|
|
|
|
CloseHandle (ev);
|
|
|
|
ev = h;
|
|
|
|
}
|
|
|
|
/* Cygwin threads should not call ExitThread directly */
|
|
|
|
func (pass_arg);
|
|
|
|
/* ...so the above should always return */
|
|
|
|
}
|
|
|
|
|
2002-08-05 18:15:46 +02:00
|
|
|
/* Initial stub called by cygthread constructor. Performs initial
|
* wininfo.h (wininfo::timer_active): Delete.
(wininfo::itv): Ditto.
(wininfo::start_time): Ditto.
(wininfo::window_started): Ditto.
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(wininfo::wininfo): Ditto.
(wininfo::lock): New method.
(wininfo::release): Ditto.
* window.cc: Use new lock/acquire wininfo methods throughout.
(wininfo::wininfo): Delete
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(getitimer): Ditto.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
(wininfo::lock): Define new function.
(wininfo::release): Ditto.
(wininfo::process): Delete WM_TIMER handling.
* timer.cc (struct timetracker): Delete it, flags. Add it_interval,
interval_us, sleepto_us, running, init_muto(), syncthread, and gettime().
(ttstart): Make NO_COPY.
(lock_timer_tracker): New class.
(timer_tracker::timer_tracker): Distinguish ttstart case.
(timer_tracker::~timer_tracker): New destructor. Clean out events, and reset
magic.
(timer_tracker::init_muto): New method.
(to_us): Round up as per POSIX.
(timer_thread): Reorganize to match timer_tracker::settime and
timer_tracker::gettime. Call sig_send without wait. Call auto_release.
(timer_tracker::settime): Reorganize logic to avoid race. Call gettime to
recover old value.
(timer_tracker::gettime): New method.
(timer_create): Properly set errno on invalid timerid. Use new
lock_timer_tracker method.
(timer_delete): Ditto. Simplify code slightly.
(timer_gettime): New function.
(fixup_timers_after_fork): Reinit ttstart.
(getitimer): New implementation.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
* cygwin.din: Export timer_gettime.
* winsup.h: Remove has has_visible_window_station declaration.
* Makefile.in (DLL_OFILES): Add lsearch.o.
* cygthread.h (cygthread::notify_detached): New element.
(cygthread::cygthread): Take optional fourth argument signifying event to
signal on thread completion.
* cygthread.cc (cygthread::stub): Signal notify_detached event, if it exists.
(cygthread::cygthread): Initialize notify_detached from fourth argument.
(cygthread::detach): Wait for notify_detached field is present.
* lsearch.cc: New file.
* search.h: Ditto.
* include/cygwin/version.h: Bump API minor number to 126.
* cygwin.din: Export lsearch, lfind.
2005-03-27 03:57:38 +02:00
|
|
|
per-thread initialization and loops waiting for another thread function
|
2002-08-05 18:15:46 +02:00
|
|
|
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;
|
2004-11-26 05:15:10 +01:00
|
|
|
_my_tls._ctinfo = info;
|
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;
|
2004-12-28 02:27:26 +01:00
|
|
|
debug_printf ("thread '%s', id %p, stack_ptr %p", info->name (), info->id, info->stack_ptr);
|
2003-04-10 07:27:34 +02:00
|
|
|
if (!info->ev)
|
|
|
|
{
|
2006-03-01 22:31:24 +01:00
|
|
|
info->ev = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
2003-04-10 07:27:34 +02:00
|
|
|
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)
|
|
|
|
{
|
2004-12-22 19:12:30 +01:00
|
|
|
if (!info->__name)
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
system_printf ("erroneous thread activation, name is NULL prev thread name = '%s'", info->__oldname);
|
|
|
|
#else
|
|
|
|
system_printf ("erroneous thread activation, name is NULL");
|
|
|
|
#endif
|
2002-10-14 04:37:49 +02:00
|
|
|
else
|
|
|
|
{
|
2005-01-02 03:22:25 +01:00
|
|
|
if (exiting)
|
|
|
|
{
|
|
|
|
info->inuse = false; // FIXME: Do we need this?
|
|
|
|
return 0;
|
|
|
|
}
|
2002-08-01 18:20:31 +02:00
|
|
|
|
2005-10-18 01:27:00 +02:00
|
|
|
info->callfunc (false);
|
2002-08-01 18:20:31 +02:00
|
|
|
|
* wininfo.h (wininfo::timer_active): Delete.
(wininfo::itv): Ditto.
(wininfo::start_time): Ditto.
(wininfo::window_started): Ditto.
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(wininfo::wininfo): Ditto.
(wininfo::lock): New method.
(wininfo::release): Ditto.
* window.cc: Use new lock/acquire wininfo methods throughout.
(wininfo::wininfo): Delete
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(getitimer): Ditto.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
(wininfo::lock): Define new function.
(wininfo::release): Ditto.
(wininfo::process): Delete WM_TIMER handling.
* timer.cc (struct timetracker): Delete it, flags. Add it_interval,
interval_us, sleepto_us, running, init_muto(), syncthread, and gettime().
(ttstart): Make NO_COPY.
(lock_timer_tracker): New class.
(timer_tracker::timer_tracker): Distinguish ttstart case.
(timer_tracker::~timer_tracker): New destructor. Clean out events, and reset
magic.
(timer_tracker::init_muto): New method.
(to_us): Round up as per POSIX.
(timer_thread): Reorganize to match timer_tracker::settime and
timer_tracker::gettime. Call sig_send without wait. Call auto_release.
(timer_tracker::settime): Reorganize logic to avoid race. Call gettime to
recover old value.
(timer_tracker::gettime): New method.
(timer_create): Properly set errno on invalid timerid. Use new
lock_timer_tracker method.
(timer_delete): Ditto. Simplify code slightly.
(timer_gettime): New function.
(fixup_timers_after_fork): Reinit ttstart.
(getitimer): New implementation.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
* cygwin.din: Export timer_gettime.
* winsup.h: Remove has has_visible_window_station declaration.
* Makefile.in (DLL_OFILES): Add lsearch.o.
* cygthread.h (cygthread::notify_detached): New element.
(cygthread::cygthread): Take optional fourth argument signifying event to
signal on thread completion.
* cygthread.cc (cygthread::stub): Signal notify_detached event, if it exists.
(cygthread::cygthread): Initialize notify_detached from fourth argument.
(cygthread::detach): Wait for notify_detached field is present.
* lsearch.cc: New file.
* search.h: Ditto.
* include/cygwin/version.h: Bump API minor number to 126.
* cygwin.din: Export lsearch, lfind.
2005-03-27 03:57:38 +02:00
|
|
|
HANDLE notify = info->notify_detached;
|
2005-02-01 17:49:13 +01:00
|
|
|
/* If func is NULL, the above function has set that to indicate
|
2004-12-23 15:57:08 +01:00
|
|
|
that it doesn't want to alert anyone with a SetEvent and should
|
|
|
|
just be marked as no longer inuse. Hopefully the function knows
|
* wininfo.h (wininfo::timer_active): Delete.
(wininfo::itv): Ditto.
(wininfo::start_time): Ditto.
(wininfo::window_started): Ditto.
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(wininfo::wininfo): Ditto.
(wininfo::lock): New method.
(wininfo::release): Ditto.
* window.cc: Use new lock/acquire wininfo methods throughout.
(wininfo::wininfo): Delete
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(getitimer): Ditto.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
(wininfo::lock): Define new function.
(wininfo::release): Ditto.
(wininfo::process): Delete WM_TIMER handling.
* timer.cc (struct timetracker): Delete it, flags. Add it_interval,
interval_us, sleepto_us, running, init_muto(), syncthread, and gettime().
(ttstart): Make NO_COPY.
(lock_timer_tracker): New class.
(timer_tracker::timer_tracker): Distinguish ttstart case.
(timer_tracker::~timer_tracker): New destructor. Clean out events, and reset
magic.
(timer_tracker::init_muto): New method.
(to_us): Round up as per POSIX.
(timer_thread): Reorganize to match timer_tracker::settime and
timer_tracker::gettime. Call sig_send without wait. Call auto_release.
(timer_tracker::settime): Reorganize logic to avoid race. Call gettime to
recover old value.
(timer_tracker::gettime): New method.
(timer_create): Properly set errno on invalid timerid. Use new
lock_timer_tracker method.
(timer_delete): Ditto. Simplify code slightly.
(timer_gettime): New function.
(fixup_timers_after_fork): Reinit ttstart.
(getitimer): New implementation.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
* cygwin.din: Export timer_gettime.
* winsup.h: Remove has has_visible_window_station declaration.
* Makefile.in (DLL_OFILES): Add lsearch.o.
* cygthread.h (cygthread::notify_detached): New element.
(cygthread::cygthread): Take optional fourth argument signifying event to
signal on thread completion.
* cygthread.cc (cygthread::stub): Signal notify_detached event, if it exists.
(cygthread::cygthread): Initialize notify_detached from fourth argument.
(cygthread::detach): Wait for notify_detached field is present.
* lsearch.cc: New file.
* search.h: Ditto.
* include/cygwin/version.h: Bump API minor number to 126.
* cygwin.din: Export lsearch, lfind.
2005-03-27 03:57:38 +02:00
|
|
|
what it is doing. */
|
2004-12-23 15:57:08 +01:00
|
|
|
if (!info->func)
|
|
|
|
info->release (false);
|
|
|
|
else
|
|
|
|
{
|
2002-08-06 07:08:55 +02:00
|
|
|
#ifdef DEBUGGING
|
2004-12-23 15:57:08 +01:00
|
|
|
info->func = NULL; // catch erroneous activation
|
|
|
|
info->__oldname = info->__name;
|
2002-08-06 07:08:55 +02:00
|
|
|
#endif
|
2004-12-23 15:57:08 +01:00
|
|
|
info->__name = NULL;
|
|
|
|
SetEvent (info->ev);
|
|
|
|
}
|
* wininfo.h (wininfo::timer_active): Delete.
(wininfo::itv): Ditto.
(wininfo::start_time): Ditto.
(wininfo::window_started): Ditto.
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(wininfo::wininfo): Ditto.
(wininfo::lock): New method.
(wininfo::release): Ditto.
* window.cc: Use new lock/acquire wininfo methods throughout.
(wininfo::wininfo): Delete
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(getitimer): Ditto.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
(wininfo::lock): Define new function.
(wininfo::release): Ditto.
(wininfo::process): Delete WM_TIMER handling.
* timer.cc (struct timetracker): Delete it, flags. Add it_interval,
interval_us, sleepto_us, running, init_muto(), syncthread, and gettime().
(ttstart): Make NO_COPY.
(lock_timer_tracker): New class.
(timer_tracker::timer_tracker): Distinguish ttstart case.
(timer_tracker::~timer_tracker): New destructor. Clean out events, and reset
magic.
(timer_tracker::init_muto): New method.
(to_us): Round up as per POSIX.
(timer_thread): Reorganize to match timer_tracker::settime and
timer_tracker::gettime. Call sig_send without wait. Call auto_release.
(timer_tracker::settime): Reorganize logic to avoid race. Call gettime to
recover old value.
(timer_tracker::gettime): New method.
(timer_create): Properly set errno on invalid timerid. Use new
lock_timer_tracker method.
(timer_delete): Ditto. Simplify code slightly.
(timer_gettime): New function.
(fixup_timers_after_fork): Reinit ttstart.
(getitimer): New implementation.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
* cygwin.din: Export timer_gettime.
* winsup.h: Remove has has_visible_window_station declaration.
* Makefile.in (DLL_OFILES): Add lsearch.o.
* cygthread.h (cygthread::notify_detached): New element.
(cygthread::cygthread): Take optional fourth argument signifying event to
signal on thread completion.
* cygthread.cc (cygthread::stub): Signal notify_detached event, if it exists.
(cygthread::cygthread): Initialize notify_detached from fourth argument.
(cygthread::detach): Wait for notify_detached field is present.
* lsearch.cc: New file.
* search.h: Ditto.
* include/cygwin/version.h: Bump API minor number to 126.
* cygwin.din: Export lsearch, lfind.
2005-03-27 03:57:38 +02:00
|
|
|
if (notify)
|
|
|
|
SetEvent (notify);
|
2002-10-14 04:37:49 +02:00
|
|
|
}
|
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;
|
2004-11-26 05:15:10 +01:00
|
|
|
_my_tls._ctinfo = info;
|
2003-04-11 04:16:17 +02:00
|
|
|
info->stack_ptr = &arg;
|
2005-10-18 01:27:00 +02:00
|
|
|
info->callfunc (true);
|
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 ();
|
|
|
|
}
|
|
|
|
|
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)
|
2005-01-29 06:39:07 +01:00
|
|
|
api_fatal ("name not NULL? %s, id %p, i %d", info->__name, 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
|
2005-09-21 21:29:50 +02:00
|
|
|
if (!getenv ("CYGWIN_FREERANGE_NOCHECK"))
|
|
|
|
api_fatal ("overflowed cygwin thread pool");
|
2003-11-07 19:21:05 +01:00
|
|
|
else
|
2005-09-21 21:29:50 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-10-18 01:27:00 +02:00
|
|
|
cygthread::cygthread (LPTHREAD_START_ROUTINE start, size_t n, void *param,
|
* wininfo.h (wininfo::timer_active): Delete.
(wininfo::itv): Ditto.
(wininfo::start_time): Ditto.
(wininfo::window_started): Ditto.
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(wininfo::wininfo): Ditto.
(wininfo::lock): New method.
(wininfo::release): Ditto.
* window.cc: Use new lock/acquire wininfo methods throughout.
(wininfo::wininfo): Delete
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(getitimer): Ditto.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
(wininfo::lock): Define new function.
(wininfo::release): Ditto.
(wininfo::process): Delete WM_TIMER handling.
* timer.cc (struct timetracker): Delete it, flags. Add it_interval,
interval_us, sleepto_us, running, init_muto(), syncthread, and gettime().
(ttstart): Make NO_COPY.
(lock_timer_tracker): New class.
(timer_tracker::timer_tracker): Distinguish ttstart case.
(timer_tracker::~timer_tracker): New destructor. Clean out events, and reset
magic.
(timer_tracker::init_muto): New method.
(to_us): Round up as per POSIX.
(timer_thread): Reorganize to match timer_tracker::settime and
timer_tracker::gettime. Call sig_send without wait. Call auto_release.
(timer_tracker::settime): Reorganize logic to avoid race. Call gettime to
recover old value.
(timer_tracker::gettime): New method.
(timer_create): Properly set errno on invalid timerid. Use new
lock_timer_tracker method.
(timer_delete): Ditto. Simplify code slightly.
(timer_gettime): New function.
(fixup_timers_after_fork): Reinit ttstart.
(getitimer): New implementation.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
* cygwin.din: Export timer_gettime.
* winsup.h: Remove has has_visible_window_station declaration.
* Makefile.in (DLL_OFILES): Add lsearch.o.
* cygthread.h (cygthread::notify_detached): New element.
(cygthread::cygthread): Take optional fourth argument signifying event to
signal on thread completion.
* cygthread.cc (cygthread::stub): Signal notify_detached event, if it exists.
(cygthread::cygthread): Initialize notify_detached from fourth argument.
(cygthread::detach): Wait for notify_detached field is present.
* lsearch.cc: New file.
* search.h: Ditto.
* include/cygwin/version.h: Bump API minor number to 126.
* cygwin.din: Export lsearch, lfind.
2005-03-27 03:57:38 +02:00
|
|
|
const char *name, HANDLE notify)
|
2005-10-18 01:27:00 +02:00
|
|
|
: __name (name), func (start), arglen (n), arg (param), notify_detached (notify)
|
2002-08-01 18:20:31 +02:00
|
|
|
{
|
2002-08-06 07:08:55 +02:00
|
|
|
thread_printf ("name %s, id %p", name, id);
|
2005-10-18 01:27:00 +02:00
|
|
|
HANDLE htobe;
|
2003-04-10 07:27:34 +02:00
|
|
|
if (h)
|
2002-08-11 21:31:24 +02:00
|
|
|
{
|
2005-10-18 01:27:00 +02:00
|
|
|
if (ev)
|
|
|
|
ResetEvent (ev);
|
2003-04-10 07:27:34 +02:00
|
|
|
while (!thread_sync)
|
|
|
|
low_priority_sleep (0);
|
|
|
|
SetEvent (thread_sync);
|
2005-01-02 03:22:25 +01:00
|
|
|
thread_printf ("activated name '%s', thread_sync %p for thread %p", name, thread_sync, id);
|
2005-10-18 01:27:00 +02:00
|
|
|
htobe = h;
|
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;
|
2005-10-18 01:27:00 +02:00
|
|
|
htobe = CreateThread (&sec_none_nih, 0, is_freerange ? simplestub : stub,
|
|
|
|
this, 0, &id);
|
|
|
|
if (!htobe)
|
2005-09-12 23:19:07 +02:00
|
|
|
api_fatal ("CreateThread failed for %s - %p<%p>, %E", name, h, id);
|
2005-01-02 03:22:25 +01:00
|
|
|
thread_printf ("created name '%s', thread %p, id %p", name, h, id);
|
2004-12-23 16:36:59 +01:00
|
|
|
#ifdef DEBUGGING
|
2004-12-22 19:12:30 +01:00
|
|
|
terminated = false;
|
2004-12-23 16:36:59 +01:00
|
|
|
#endif
|
2003-04-10 07:27:34 +02:00
|
|
|
}
|
2005-10-18 01:27:00 +02:00
|
|
|
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
while (!ev)
|
|
|
|
low_priority_sleep (0);
|
|
|
|
WaitForSingleObject (ev, INFINITE);
|
|
|
|
ResetEvent (ev);
|
|
|
|
}
|
|
|
|
h = htobe;
|
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)
|
|
|
|
{
|
2005-05-19 01:30:02 +02:00
|
|
|
__small_sprintf (_my_tls.locals.unknown_thread_name, "unknown (%p)", tid);
|
|
|
|
res = _my_tls.locals.unknown_thread_name;
|
2002-08-01 18:20:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-12-22 17:19:27 +01:00
|
|
|
void
|
2004-12-22 19:12:30 +01:00
|
|
|
cygthread::release (bool nuke_h)
|
2004-12-22 17:19:27 +01:00
|
|
|
{
|
2004-12-22 19:12:30 +01:00
|
|
|
if (nuke_h)
|
|
|
|
h = NULL;
|
2004-12-23 16:36:59 +01:00
|
|
|
#ifdef DEBUGGING
|
2004-12-22 19:12:30 +01:00
|
|
|
__oldname = __name;
|
2005-01-02 03:22:25 +01:00
|
|
|
debug_printf ("released thread '%s'", __oldname);
|
2004-12-23 16:36:59 +01:00
|
|
|
#endif
|
2004-12-22 17:19:27 +01:00
|
|
|
__name = NULL;
|
2004-12-23 15:57:08 +01:00
|
|
|
func = NULL;
|
2006-02-28 02:27:41 +01:00
|
|
|
/* Must be last */
|
2004-12-23 15:57:08 +01:00
|
|
|
if (!InterlockedExchange (&inuse, 0))
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
api_fatal ("released a thread that was not inuse");
|
|
|
|
#else
|
|
|
|
system_printf ("released a thread that was not inuse");
|
|
|
|
#endif
|
2004-12-22 17:19:27 +01:00
|
|
|
}
|
|
|
|
|
2002-12-11 05:00:04 +01:00
|
|
|
/* Forcibly terminate a thread. */
|
2005-02-06 06:04:34 +01:00
|
|
|
bool
|
2002-12-11 05:00:04 +01:00
|
|
|
cygthread::terminate_thread ()
|
|
|
|
{
|
2005-02-06 06:04:34 +01:00
|
|
|
bool terminated = true;
|
2006-06-03 23:44:01 +02:00
|
|
|
debug_printf ("thread '%s', id %p, inuse %d, stack_ptr %p", __name, id, inuse, stack_ptr);
|
2004-12-28 02:27:26 +01:00
|
|
|
while (inuse && !stack_ptr)
|
|
|
|
low_priority_sleep (0);
|
|
|
|
|
2004-12-22 19:12:30 +01:00
|
|
|
if (!inuse)
|
2005-02-11 16:24:15 +01:00
|
|
|
goto force_notterminated;
|
2005-02-13 19:17:29 +01:00
|
|
|
|
2005-07-06 22:05:03 +02:00
|
|
|
TerminateThread (h, 0);
|
|
|
|
WaitForSingleObject (h, INFINITE);
|
2003-04-10 07:27:34 +02:00
|
|
|
CloseHandle (h);
|
|
|
|
|
2005-02-11 16:24:15 +01:00
|
|
|
if (!inuse || exiting)
|
|
|
|
goto force_notterminated;
|
|
|
|
|
2006-03-01 22:31:24 +01:00
|
|
|
if (ev && !(terminated = WaitForSingleObject (ev, 0) != WAIT_OBJECT_0))
|
|
|
|
ResetEvent (ev);
|
2004-12-22 17:59:03 +01:00
|
|
|
|
2002-12-11 05:00:04 +01:00
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
memset (&m, 0, sizeof (m));
|
2005-07-06 22:05:03 +02:00
|
|
|
VirtualQuery (stack_ptr, &m, sizeof m);
|
2002-12-11 05:00:04 +01:00
|
|
|
|
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
|
2006-03-01 22:31:24 +01:00
|
|
|
{
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
terminated = true;
|
|
|
|
#endif
|
|
|
|
release (true);
|
|
|
|
}
|
2005-02-13 19:17:29 +01:00
|
|
|
|
2005-02-11 16:24:15 +01:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
force_notterminated:
|
|
|
|
terminated = false;
|
|
|
|
out:
|
2005-02-06 06:04:34 +01:00
|
|
|
return terminated;
|
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;
|
2005-02-11 16:24:15 +01:00
|
|
|
bool thread_was_reset = 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)
|
* wininfo.h (wininfo::timer_active): Delete.
(wininfo::itv): Ditto.
(wininfo::start_time): Ditto.
(wininfo::window_started): Ditto.
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(wininfo::wininfo): Ditto.
(wininfo::lock): New method.
(wininfo::release): Ditto.
* window.cc: Use new lock/acquire wininfo methods throughout.
(wininfo::wininfo): Delete
(wininfo::getitimer): Ditto.
(wininfo::setitimer): Ditto.
(getitimer): Ditto.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
(wininfo::lock): Define new function.
(wininfo::release): Ditto.
(wininfo::process): Delete WM_TIMER handling.
* timer.cc (struct timetracker): Delete it, flags. Add it_interval,
interval_us, sleepto_us, running, init_muto(), syncthread, and gettime().
(ttstart): Make NO_COPY.
(lock_timer_tracker): New class.
(timer_tracker::timer_tracker): Distinguish ttstart case.
(timer_tracker::~timer_tracker): New destructor. Clean out events, and reset
magic.
(timer_tracker::init_muto): New method.
(to_us): Round up as per POSIX.
(timer_thread): Reorganize to match timer_tracker::settime and
timer_tracker::gettime. Call sig_send without wait. Call auto_release.
(timer_tracker::settime): Reorganize logic to avoid race. Call gettime to
recover old value.
(timer_tracker::gettime): New method.
(timer_create): Properly set errno on invalid timerid. Use new
lock_timer_tracker method.
(timer_delete): Ditto. Simplify code slightly.
(timer_gettime): New function.
(fixup_timers_after_fork): Reinit ttstart.
(getitimer): New implementation.
(setitimer): Ditto.
(ualarm): Ditto.
(alarm): Ditto.
* cygwin.din: Export timer_gettime.
* winsup.h: Remove has has_visible_window_station declaration.
* Makefile.in (DLL_OFILES): Add lsearch.o.
* cygthread.h (cygthread::notify_detached): New element.
(cygthread::cygthread): Take optional fourth argument signifying event to
signal on thread completion.
* cygthread.cc (cygthread::stub): Signal notify_detached event, if it exists.
(cygthread::cygthread): Initialize notify_detached from fourth argument.
(cygthread::detach): Wait for notify_detached field is present.
* lsearch.cc: New file.
* search.h: Ditto.
* include/cygwin/version.h: Bump API minor number to 126.
* cygwin.din: Export lsearch, lfind.
2005-03-27 03:57:38 +02:00
|
|
|
/* If the caller specified a special handle for notification, wait for that.
|
|
|
|
This assumes that the thread in question is auto releasing. */
|
2005-03-28 21:31:23 +02:00
|
|
|
res = WaitForSingleObject (*this, INFINITE);
|
2002-12-11 05:00:04 +01:00
|
|
|
else
|
|
|
|
{
|
2005-02-11 16:24:15 +01:00
|
|
|
/* Lower our priority and give priority to the read thread */
|
|
|
|
HANDLE hth = GetCurrentThread ();
|
|
|
|
LONG prio = GetThreadPriority (hth);
|
2005-07-06 22:05:03 +02:00
|
|
|
::SetThreadPriority (hth, THREAD_PRIORITY_BELOW_NORMAL);
|
2005-02-11 16:24:15 +01:00
|
|
|
|
2002-12-11 05:00:04 +01:00
|
|
|
HANDLE w4[2];
|
2005-02-11 16:24:15 +01:00
|
|
|
unsigned n = 2;
|
|
|
|
DWORD howlong = INFINITE;
|
|
|
|
w4[0] = sigwait;
|
2003-05-09 19:56:58 +02:00
|
|
|
w4[1] = signal_arrived;
|
2005-02-11 16:24:15 +01:00
|
|
|
/* For a description of the below loop see the end of this file */
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
switch (res = WaitForMultipleObjects (n, w4, FALSE, howlong))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
if (n == 1)
|
|
|
|
howlong = 50;
|
|
|
|
break;
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
n = 1;
|
|
|
|
if (i--)
|
|
|
|
howlong = 50;
|
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!exiting)
|
2005-06-10 01:48:08 +02:00
|
|
|
{
|
|
|
|
system_printf ("WFMO failed waiting for cygthread '%s', %E", __name);
|
|
|
|
for (unsigned j = 0; j < n; j++)
|
|
|
|
switch (WaitForSingleObject (w4[j], 0))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
break;
|
|
|
|
default:
|
2005-06-16 00:50:31 +02:00
|
|
|
system_printf ("%s handle %p is bad", (j ? "signal_arrived" : "semaphore"), w4[j]);
|
2005-06-10 01:48:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
api_fatal ("exiting on fatal error");
|
|
|
|
}
|
2005-02-11 16:24:15 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* WAIT_OBJECT_0 means that the thread successfully read something,
|
|
|
|
so wait for the cygthread to "terminate". */
|
2003-05-09 19:56:58 +02:00
|
|
|
if (res == WAIT_OBJECT_0)
|
2005-07-06 22:05:03 +02:00
|
|
|
WaitForSingleObject (*this, INFINITE);
|
2005-02-06 06:04:34 +01:00
|
|
|
else
|
2002-12-11 05:00:04 +01:00
|
|
|
{
|
2005-02-11 16:24:15 +01:00
|
|
|
/* Thread didn't terminate on its own, so maybe we have to
|
|
|
|
do it. */
|
2005-02-06 06:04:34 +01:00
|
|
|
signalled = terminate_thread ();
|
2005-02-11 16:24:15 +01:00
|
|
|
/* Possibly the thread completed *just* before it was
|
|
|
|
terminated. Detect this. If this happened then the
|
|
|
|
read was not terminated on a signal. */
|
|
|
|
if (WaitForSingleObject (sigwait, 0) == WAIT_OBJECT_0)
|
|
|
|
signalled = false;
|
|
|
|
if (signalled)
|
|
|
|
set_sig_errno (EINTR);
|
|
|
|
thread_was_reset = true;
|
2002-12-11 05:00:04 +01:00
|
|
|
}
|
2005-07-06 22:05:03 +02:00
|
|
|
::SetThreadPriority (hth, prio);
|
2002-12-11 05:00:04 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2005-02-11 16:24:15 +01:00
|
|
|
if (thread_was_reset)
|
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 */
|
2005-07-06 22:05:03 +02:00
|
|
|
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
|
|
|
}
|
2005-02-11 16:24:15 +01:00
|
|
|
|
|
|
|
/* The below is an explanation of synchronization loop in cygthread::detach.
|
|
|
|
The intent is that the loop will always try hard to wait for both
|
|
|
|
synchronization events from the reader thread but will exit with
|
|
|
|
res == WAIT_TIMEOUT if a signal occurred and the reader thread is
|
|
|
|
still blocked.
|
|
|
|
|
|
|
|
case 0 - no signal
|
|
|
|
|
|
|
|
i == 0 (howlong == INFINITE)
|
|
|
|
W0 activated
|
|
|
|
howlong not set because n != 1
|
|
|
|
just loop
|
|
|
|
|
|
|
|
i == 1 (howlong == INFINITE)
|
|
|
|
W0 activated
|
|
|
|
howlong not set because n != 1
|
|
|
|
just loop (to exit loop) - no signal
|
|
|
|
|
|
|
|
i == 2 (howlong == INFINITE)
|
|
|
|
exit loop
|
|
|
|
|
|
|
|
case 1 - signal before thread initialized
|
|
|
|
|
|
|
|
i == 0 (howlong == INFINITE)
|
|
|
|
WO + 1 activated
|
|
|
|
n set to 1
|
|
|
|
howlong untouched because i-- == 0
|
|
|
|
loop
|
|
|
|
|
|
|
|
i == 0 (howlong == INFINITE)
|
|
|
|
W0 must be activated
|
|
|
|
howlong set to 50 because n == 1
|
|
|
|
|
|
|
|
i == 1 (howlong == 50)
|
|
|
|
W0 activated
|
|
|
|
loop (to exit loop) - no signal
|
|
|
|
|
|
|
|
WAIT_TIMEOUT activated
|
|
|
|
signal potentially detected
|
|
|
|
loop (to exit loop)
|
|
|
|
|
|
|
|
i == 2 (howlong == 50)
|
|
|
|
exit loop
|
|
|
|
|
|
|
|
case 2 - signal after thread initialized
|
|
|
|
|
|
|
|
i == 0 (howlong == INFINITE)
|
|
|
|
W0 activated
|
|
|
|
howlong not set because n != 1
|
|
|
|
loop
|
|
|
|
|
|
|
|
i == 1 (howlong == INFINITE)
|
|
|
|
W0 + 1 activated
|
|
|
|
n set to 1
|
|
|
|
howlong set to 50 because i-- != 0
|
|
|
|
loop
|
|
|
|
|
|
|
|
i == 1 (howlong == 50)
|
|
|
|
W0 activated
|
|
|
|
loop (to exit loop) - no signal
|
|
|
|
|
|
|
|
WAIT_TIMEOUT activated
|
|
|
|
loop (to exit loop) - signal
|
|
|
|
|
|
|
|
i == 2 (howlong == 50)
|
|
|
|
exit loop
|
|
|
|
*/
|