2002-08-02 00:20:31 +08:00
|
|
|
/* cygthread.cc
|
|
|
|
|
2013-01-21 12:38:31 +08:00
|
|
|
Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009,
|
2015-11-27 21:39:11 +08:00
|
|
|
2010, 2011, 2012, 2013, 2015 Red Hat, Inc.
|
2002-08-02 00:20:31 +08: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-08 00:15:45 +08:00
|
|
|
#include "miscfuncs.h"
|
2002-08-29 11:33:50 +08:00
|
|
|
#include <stdlib.h>
|
2002-12-11 12:00:04 +08:00
|
|
|
#include "sigproc.h"
|
2003-12-03 13:21:55 +08:00
|
|
|
#include "cygtls.h"
|
2011-04-29 16:27:11 +08:00
|
|
|
#include "ntdll.h"
|
2002-08-02 00:20:31 +08:00
|
|
|
|
|
|
|
#undef CloseHandle
|
|
|
|
|
2011-09-21 12:33:57 +08:00
|
|
|
static cygthread NO_COPY threads[64];
|
2002-08-02 00:20:31 +08:00
|
|
|
#define NTHREADS (sizeof (threads) / sizeof (threads[0]))
|
|
|
|
|
|
|
|
DWORD NO_COPY cygthread::main_thread_id;
|
2002-10-23 04:16:31 +08:00
|
|
|
bool NO_COPY cygthread::exiting;
|
2002-08-29 11:33:50 +08:00
|
|
|
|
2005-10-18 07:27:00 +08: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-02 05:31:24 +08:00
|
|
|
ev = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
2005-10-18 07:27:00 +08:00
|
|
|
pass_arg = alloca (arglen);
|
|
|
|
memcpy (pass_arg, arg, arglen);
|
|
|
|
SetEvent (ev);
|
|
|
|
}
|
|
|
|
if (issimplestub)
|
|
|
|
{
|
|
|
|
/* Wait for main thread to assign 'h' */
|
|
|
|
while (!h)
|
2010-03-13 07:13:48 +08:00
|
|
|
yield ();
|
2005-10-18 07:27:00 +08:00
|
|
|
if (ev)
|
|
|
|
CloseHandle (ev);
|
|
|
|
ev = h;
|
|
|
|
}
|
|
|
|
/* Cygwin threads should not call ExitThread directly */
|
|
|
|
func (pass_arg);
|
|
|
|
/* ...so the above should always return */
|
|
|
|
}
|
|
|
|
|
2002-08-06 00:15:46 +08: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 09:57:38 +08:00
|
|
|
per-thread initialization and loops waiting for another thread function
|
2002-08-06 00:15:46 +08:00
|
|
|
to execute. */
|
2002-08-02 00:20:31 +08:00
|
|
|
DWORD WINAPI
|
|
|
|
cygthread::stub (VOID *arg)
|
2003-12-03 13:21:55 +08:00
|
|
|
{
|
2002-08-02 00:20:31 +08:00
|
|
|
cygthread *info = (cygthread *) arg;
|
2004-11-26 12:15:10 +08:00
|
|
|
_my_tls._ctinfo = info;
|
2002-10-09 13:55:40 +08:00
|
|
|
if (info->arg == cygself)
|
2003-04-10 13:27:34 +08:00
|
|
|
{
|
|
|
|
if (info->ev)
|
|
|
|
{
|
|
|
|
CloseHandle (info->ev);
|
|
|
|
CloseHandle (info->thread_sync);
|
|
|
|
}
|
2004-01-14 23:45:37 +08:00
|
|
|
info->ev = info->thread_sync = info->stack_ptr = NULL;
|
2003-04-10 13:27:34 +08:00
|
|
|
}
|
2002-10-09 13:55:40 +08:00
|
|
|
else
|
2002-10-14 02:16:33 +08:00
|
|
|
{
|
2003-04-11 10:16:17 +08:00
|
|
|
info->stack_ptr = &arg;
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("thread '%s', id %y, stack_ptr %p", info->name (), info->id, info->stack_ptr);
|
2003-04-10 13:27:34 +08:00
|
|
|
if (!info->ev)
|
|
|
|
{
|
2006-03-02 05:31:24 +08:00
|
|
|
info->ev = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
2003-04-10 13:27:34 +08:00
|
|
|
info->thread_sync = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
|
|
|
|
}
|
2002-10-14 02:16:33 +08:00
|
|
|
}
|
2004-01-14 23:45:37 +08:00
|
|
|
|
2002-08-02 00:20:31 +08:00
|
|
|
while (1)
|
|
|
|
{
|
2004-12-23 02:12:30 +08: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 10:37:49 +08:00
|
|
|
else
|
|
|
|
{
|
2005-10-18 07:27:00 +08:00
|
|
|
info->callfunc (false);
|
2002-08-02 00:20:31 +08: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 09:57:38 +08:00
|
|
|
HANDLE notify = info->notify_detached;
|
2005-02-02 00:49:13 +08:00
|
|
|
/* If func is NULL, the above function has set that to indicate
|
2004-12-23 22:57:08 +08: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 09:57:38 +08:00
|
|
|
what it is doing. */
|
2004-12-23 22:57:08 +08:00
|
|
|
if (!info->func)
|
|
|
|
info->release (false);
|
|
|
|
else
|
|
|
|
{
|
2002-08-06 13:08:55 +08:00
|
|
|
#ifdef DEBUGGING
|
2004-12-23 22:57:08 +08:00
|
|
|
info->func = NULL; // catch erroneous activation
|
|
|
|
info->__oldname = info->__name;
|
2002-08-06 13:08:55 +08:00
|
|
|
#endif
|
2004-12-23 22:57:08 +08: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 09:57:38 +08:00
|
|
|
if (notify)
|
|
|
|
SetEvent (notify);
|
2002-10-14 10:37:49 +08:00
|
|
|
}
|
2002-10-14 02:16:33 +08:00
|
|
|
switch (WaitForSingleObject (info->thread_sync, INFINITE))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
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)
|
2003-12-03 13:21:55 +08:00
|
|
|
{
|
2002-10-14 02:16:33 +08:00
|
|
|
cygthread *info = (cygthread *) arg;
|
2004-11-26 12:15:10 +08:00
|
|
|
_my_tls._ctinfo = info;
|
2003-04-11 10:16:17 +08:00
|
|
|
info->stack_ptr = &arg;
|
2011-08-26 03:37:39 +08:00
|
|
|
HANDLE notify = info->notify_detached;
|
2005-10-18 07:27:00 +08:00
|
|
|
info->callfunc (true);
|
2011-08-26 03:37:39 +08:00
|
|
|
if (notify)
|
|
|
|
SetEvent (notify);
|
2003-12-14 15:09:22 +08:00
|
|
|
return 0;
|
2002-10-14 02:16:33 +08:00
|
|
|
}
|
|
|
|
|
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 ()
|
|
|
|
{
|
|
|
|
main_thread_id = GetCurrentThreadId ();
|
|
|
|
}
|
|
|
|
|
2002-10-23 04:16:31 +08:00
|
|
|
cygthread *
|
2002-08-29 11:33:50 +08:00
|
|
|
cygthread::freerange ()
|
|
|
|
{
|
|
|
|
cygthread *self = (cygthread *) calloc (1, sizeof (*self));
|
|
|
|
self->is_freerange = true;
|
2003-11-08 02:21:05 +08:00
|
|
|
self->inuse = 1;
|
2002-08-29 11:33:50 +08:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2002-08-02 00:20:31 +08:00
|
|
|
void * cygthread::operator
|
|
|
|
new (size_t)
|
|
|
|
{
|
2002-08-06 00:15:46 +08:00
|
|
|
cygthread *info;
|
2002-08-02 00:20:31 +08:00
|
|
|
|
2002-10-23 04:16:31 +08:00
|
|
|
/* Search the threads array for an empty slot to use */
|
|
|
|
for (info = threads; info < threads + NTHREADS; info++)
|
2003-04-10 13:27:34 +08:00
|
|
|
if (!InterlockedExchange (&info->inuse, 1))
|
2002-10-23 04:16:31 +08:00
|
|
|
{
|
2003-04-05 02:52:11 +08:00
|
|
|
/* available */
|
2002-08-06 13:08:55 +08:00
|
|
|
#ifdef DEBUGGING
|
2002-10-23 04:16:31 +08:00
|
|
|
if (info->__name)
|
2013-04-23 17:44:36 +08:00
|
|
|
api_fatal ("name not NULL? %s, id %y, i %ld", info->__name, info->id, info - threads);
|
2002-08-06 13:08:55 +08:00
|
|
|
#endif
|
2002-10-23 04:16:31 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2002-09-29 10:19:35 +08:00
|
|
|
|
2002-09-19 11:30:20 +08:00
|
|
|
#ifdef DEBUGGING
|
2005-09-22 03:29:50 +08:00
|
|
|
if (!getenv ("CYGWIN_FREERANGE_NOCHECK"))
|
|
|
|
api_fatal ("overflowed cygwin thread pool");
|
2003-11-08 02:21:05 +08:00
|
|
|
else
|
2005-09-22 03:29:50 +08:00
|
|
|
thread_printf ("overflowed cygwin thread pool");
|
2002-09-19 11:30:20 +08:00
|
|
|
#endif
|
2002-10-23 04:16:31 +08:00
|
|
|
|
|
|
|
info = freerange (); /* exhausted thread pool */
|
|
|
|
|
|
|
|
out:
|
|
|
|
return info;
|
2002-08-02 00:20:31 +08:00
|
|
|
}
|
|
|
|
|
2011-07-31 04:50:23 +08:00
|
|
|
/* This function is called via QueueUserAPC. Apparently creating threads
|
|
|
|
asynchronously is a huge performance win on Win64. */
|
|
|
|
void CALLBACK
|
|
|
|
cygthread::async_create (ULONG_PTR arg)
|
|
|
|
{
|
|
|
|
cygthread *that = (cygthread *) arg;
|
|
|
|
that->create ();
|
|
|
|
::SetThreadPriority (that->h, THREAD_PRIORITY_HIGHEST);
|
|
|
|
that->zap_h ();
|
|
|
|
}
|
|
|
|
|
2010-07-31 02:04:22 +08:00
|
|
|
void
|
|
|
|
cygthread::create ()
|
2002-08-02 00:20:31 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
thread_printf ("name %s, id %y, this %p", __name, id, this);
|
2005-10-18 07:27:00 +08:00
|
|
|
HANDLE htobe;
|
2003-04-10 13:27:34 +08:00
|
|
|
if (h)
|
2002-08-12 03:31:24 +08:00
|
|
|
{
|
2005-10-18 07:27:00 +08:00
|
|
|
if (ev)
|
|
|
|
ResetEvent (ev);
|
2003-04-10 13:27:34 +08:00
|
|
|
while (!thread_sync)
|
2010-03-13 07:13:48 +08:00
|
|
|
yield ();
|
2003-04-10 13:27:34 +08:00
|
|
|
SetEvent (thread_sync);
|
2013-04-23 17:44:36 +08:00
|
|
|
thread_printf ("activated name '%s', thread_sync %p for id %y", __name, thread_sync, id);
|
2005-10-18 07:27:00 +08:00
|
|
|
htobe = h;
|
2002-08-12 03:31:24 +08:00
|
|
|
}
|
2002-10-21 09:00:58 +08:00
|
|
|
else
|
2003-04-10 13:27:34 +08:00
|
|
|
{
|
2003-04-11 10:16:17 +08:00
|
|
|
stack_ptr = NULL;
|
2005-10-18 07:27:00 +08:00
|
|
|
htobe = CreateThread (&sec_none_nih, 0, is_freerange ? simplestub : stub,
|
|
|
|
this, 0, &id);
|
|
|
|
if (!htobe)
|
2013-04-23 17:44:36 +08:00
|
|
|
api_fatal ("CreateThread failed for %s - %p<%y>, %E", __name, h, id);
|
|
|
|
thread_printf ("created name '%s', thread %p, id %y", __name, h, id);
|
2004-12-23 23:36:59 +08:00
|
|
|
#ifdef DEBUGGING
|
2004-12-23 02:12:30 +08:00
|
|
|
terminated = false;
|
2004-12-23 23:36:59 +08:00
|
|
|
#endif
|
2003-04-10 13:27:34 +08:00
|
|
|
}
|
2005-10-18 07:27:00 +08:00
|
|
|
|
2010-07-31 02:04:22 +08:00
|
|
|
if (arglen)
|
2005-10-18 07:27:00 +08:00
|
|
|
{
|
|
|
|
while (!ev)
|
2010-03-13 07:13:48 +08:00
|
|
|
yield ();
|
2005-10-18 07:27:00 +08:00
|
|
|
WaitForSingleObject (ev, INFINITE);
|
|
|
|
ResetEvent (ev);
|
|
|
|
}
|
|
|
|
h = htobe;
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-25 05:38:17 +08:00
|
|
|
if (res)
|
|
|
|
/* ok */;
|
|
|
|
else if (!_main_tls)
|
|
|
|
res = "main";
|
|
|
|
else
|
2002-08-02 00:20:31 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
__small_sprintf (_my_tls.locals.unknown_thread_name, "unknown (%y)", tid);
|
2005-05-19 07:30:02 +08:00
|
|
|
res = _my_tls.locals.unknown_thread_name;
|
2002-08-02 00:20:31 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
cygthread::operator
|
|
|
|
HANDLE ()
|
|
|
|
{
|
|
|
|
while (!ev)
|
2010-03-13 07:13:48 +08:00
|
|
|
yield ();
|
2002-08-02 00:20:31 +08:00
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
|
2004-12-23 00:19:27 +08:00
|
|
|
void
|
2004-12-23 02:12:30 +08:00
|
|
|
cygthread::release (bool nuke_h)
|
2004-12-23 00:19:27 +08:00
|
|
|
{
|
2004-12-23 02:12:30 +08:00
|
|
|
if (nuke_h)
|
|
|
|
h = NULL;
|
2004-12-23 23:36:59 +08:00
|
|
|
#ifdef DEBUGGING
|
2004-12-23 02:12:30 +08:00
|
|
|
__oldname = __name;
|
2005-01-02 10:22:25 +08:00
|
|
|
debug_printf ("released thread '%s'", __oldname);
|
2004-12-23 23:36:59 +08:00
|
|
|
#endif
|
2004-12-23 00:19:27 +08:00
|
|
|
__name = NULL;
|
2004-12-23 22:57:08 +08:00
|
|
|
func = NULL;
|
2006-02-28 09:27:41 +08:00
|
|
|
/* Must be last */
|
2004-12-23 22:57:08 +08: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-23 00:19:27 +08:00
|
|
|
}
|
|
|
|
|
2002-12-11 12:00:04 +08:00
|
|
|
/* Forcibly terminate a thread. */
|
2005-02-06 13:04:34 +08:00
|
|
|
bool
|
2002-12-11 12:00:04 +08:00
|
|
|
cygthread::terminate_thread ()
|
|
|
|
{
|
2005-02-06 13:04:34 +08:00
|
|
|
bool terminated = true;
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("thread '%s', id %y, inuse %d, stack_ptr %p", __name, id, inuse, stack_ptr);
|
2004-12-28 09:27:26 +08:00
|
|
|
while (inuse && !stack_ptr)
|
2010-03-13 07:13:48 +08:00
|
|
|
yield ();
|
2004-12-28 09:27:26 +08:00
|
|
|
|
2004-12-23 02:12:30 +08:00
|
|
|
if (!inuse)
|
2005-02-11 23:24:15 +08:00
|
|
|
goto force_notterminated;
|
2005-02-14 02:17:29 +08:00
|
|
|
|
2005-07-07 04:05:03 +08:00
|
|
|
TerminateThread (h, 0);
|
|
|
|
WaitForSingleObject (h, INFINITE);
|
2003-04-10 13:27:34 +08:00
|
|
|
CloseHandle (h);
|
|
|
|
|
2005-02-11 23:24:15 +08:00
|
|
|
if (!inuse || exiting)
|
|
|
|
goto force_notterminated;
|
|
|
|
|
2011-04-29 16:27:11 +08:00
|
|
|
if (ev && !(terminated = !IsEventSignalled (ev)))
|
2006-03-02 05:31:24 +08:00
|
|
|
ResetEvent (ev);
|
2004-12-23 00:59:03 +08:00
|
|
|
|
2013-03-30 01:00:36 +08:00
|
|
|
if (!wincap.terminate_thread_frees_stack ())
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION m;
|
|
|
|
memset (&m, 0, sizeof (m));
|
|
|
|
VirtualQuery (stack_ptr, &m, sizeof m);
|
|
|
|
|
|
|
|
if (!m.RegionSize)
|
|
|
|
system_printf ("m.RegionSize 0? stack_ptr %p", stack_ptr);
|
|
|
|
else if (!VirtualFree (m.AllocationBase, 0, MEM_RELEASE))
|
|
|
|
debug_printf ("VirtualFree of allocation base %p<%p> failed, %E",
|
|
|
|
stack_ptr, m.AllocationBase);
|
|
|
|
}
|
2002-12-11 12:00:04 +08:00
|
|
|
|
2003-06-03 05:22:02 +08:00
|
|
|
if (is_freerange)
|
|
|
|
free (this);
|
|
|
|
else
|
2006-03-02 05:31:24 +08:00
|
|
|
{
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
terminated = true;
|
|
|
|
#endif
|
|
|
|
release (true);
|
|
|
|
}
|
2005-02-14 02:17:29 +08:00
|
|
|
|
2005-02-11 23:24:15 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
force_notterminated:
|
|
|
|
terminated = false;
|
|
|
|
out:
|
2005-02-06 13:04:34 +08:00
|
|
|
return terminated;
|
2002-12-11 12:00:04 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2003-04-05 02:52:11 +08:00
|
|
|
So, there should be never be multiple threads doing waits
|
|
|
|
on the same cygthread. */
|
2002-12-14 12:01:32 +08:00
|
|
|
bool
|
|
|
|
cygthread::detach (HANDLE sigwait)
|
2002-08-02 00:20:31 +08:00
|
|
|
{
|
2002-12-14 12:01:32 +08:00
|
|
|
bool signalled = false;
|
2005-02-11 23:24:15 +08:00
|
|
|
bool thread_was_reset = false;
|
2003-04-10 13:27:34 +08:00
|
|
|
if (!inuse)
|
2013-04-23 17:44:36 +08:00
|
|
|
system_printf ("called detach but inuse %d, thread %y?", inuse, id);
|
2002-08-06 13:08:55 +08:00
|
|
|
else
|
2002-08-02 00:20:31 +08:00
|
|
|
{
|
2002-12-11 12:00:04 +08:00
|
|
|
DWORD res;
|
|
|
|
|
2002-12-14 12:01:32 +08: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 09:57:38 +08: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-29 03:31:23 +08:00
|
|
|
res = WaitForSingleObject (*this, INFINITE);
|
2002-12-11 12:00:04 +08:00
|
|
|
else
|
|
|
|
{
|
2005-02-11 23:24:15 +08:00
|
|
|
/* Lower our priority and give priority to the read thread */
|
|
|
|
HANDLE hth = GetCurrentThread ();
|
|
|
|
LONG prio = GetThreadPriority (hth);
|
2005-07-07 04:05:03 +08:00
|
|
|
::SetThreadPriority (hth, THREAD_PRIORITY_BELOW_NORMAL);
|
2005-02-11 23:24:15 +08:00
|
|
|
|
2012-06-19 08:38:02 +08:00
|
|
|
HANDLE w4[2];
|
|
|
|
unsigned n = 2;
|
2005-02-11 23:24:15 +08:00
|
|
|
DWORD howlong = INFINITE;
|
2012-06-19 08:38:02 +08:00
|
|
|
w4[0] = sigwait;
|
2015-11-27 21:39:11 +08:00
|
|
|
wait_signal_arrived here (w4[1]);
|
2005-02-11 23:24:15 +08:00
|
|
|
/* For a description of the below loop see the end of this file */
|
|
|
|
for (int i = 0; i < 2; i++)
|
2012-06-19 08:38:02 +08:00
|
|
|
switch (res = WaitForMultipleObjects (n, w4, FALSE, howlong))
|
2005-02-11 23:24:15 +08:00
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
if (n == 1)
|
|
|
|
howlong = 50;
|
|
|
|
break;
|
2012-06-19 08:38:02 +08:00
|
|
|
case WAIT_OBJECT_0 + 1:
|
2005-02-11 23:24:15 +08:00
|
|
|
n = 1;
|
|
|
|
if (i--)
|
|
|
|
howlong = 50;
|
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!exiting)
|
2012-06-19 08:38:02 +08: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:
|
|
|
|
system_printf ("%s handle %p is bad", (j ? "signal_arrived" : "semaphore"), w4[j]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
api_fatal ("exiting on fatal error");
|
|
|
|
}
|
2005-02-11 23:24:15 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* WAIT_OBJECT_0 means that the thread successfully read something,
|
|
|
|
so wait for the cygthread to "terminate". */
|
2003-05-10 01:56:58 +08:00
|
|
|
if (res == WAIT_OBJECT_0)
|
2005-07-07 04:05:03 +08:00
|
|
|
WaitForSingleObject (*this, INFINITE);
|
2005-02-06 13:04:34 +08:00
|
|
|
else
|
2002-12-11 12:00:04 +08:00
|
|
|
{
|
2005-02-11 23:24:15 +08:00
|
|
|
/* Thread didn't terminate on its own, so maybe we have to
|
|
|
|
do it. */
|
2005-02-06 13:04:34 +08:00
|
|
|
signalled = terminate_thread ();
|
2005-02-11 23:24:15 +08: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 12:00:04 +08:00
|
|
|
}
|
2005-07-07 04:05:03 +08:00
|
|
|
::SetThreadPriority (hth, prio);
|
2002-12-11 12:00:04 +08:00
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
thread_printf ("%s returns %d, id %y", sigwait ? "WFMO" : "WFSO",
|
2002-12-11 12:00:04 +08:00
|
|
|
res, id);
|
2002-10-21 09:00:58 +08:00
|
|
|
|
2005-02-11 23:24:15 +08:00
|
|
|
if (thread_was_reset)
|
2002-12-11 12:00:04 +08:00
|
|
|
/* already handled */;
|
|
|
|
else if (is_freerange)
|
2002-08-29 11:33:50 +08:00
|
|
|
{
|
|
|
|
CloseHandle (h);
|
|
|
|
free (this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-10-21 09:00:58 +08:00
|
|
|
ResetEvent (*this);
|
2003-04-10 13:27:34 +08:00
|
|
|
/* Mark the thread as available by setting inuse to zero */
|
2005-07-07 04:05:03 +08:00
|
|
|
InterlockedExchange (&inuse, 0);
|
2002-08-29 11:33:50 +08:00
|
|
|
}
|
2002-08-02 00:20:31 +08:00
|
|
|
}
|
2002-12-14 12:01:32 +08:00
|
|
|
return signalled;
|
2002-08-02 00:20:31 +08:00
|
|
|
}
|
2002-09-29 10:19:35 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
cygthread::terminate ()
|
|
|
|
{
|
2002-10-23 04:16:31 +08:00
|
|
|
exiting = 1;
|
2002-09-29 10:19:35 +08:00
|
|
|
}
|
2005-02-11 23:24:15 +08: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
|
|
|
|
*/
|