2000-02-18 03:38:33 +08:00
|
|
|
/* sync.cc: Synchronization functions for cygwin.
|
|
|
|
|
|
|
|
This file implements the methods for controlling the "muto" class
|
|
|
|
which is intended to operate similarly to a mutex but attempts to
|
|
|
|
avoid making expensive calls to the kernel.
|
|
|
|
|
* 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
|
|
|
Copyright 2000, 2001, 2002, 2003, 2004, 2005 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
Written by Christopher Faylor <cgf@cygnus.com>
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "sync.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "security.h"
|
2004-05-16 12:18:50 +08:00
|
|
|
#include "thread.h"
|
|
|
|
#include "cygtls.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-05-18 11:20:01 +08:00
|
|
|
#undef WaitForSingleObject
|
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
DWORD NO_COPY muto::exiting_thread;
|
2005-03-07 04:21:30 +08:00
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
void
|
|
|
|
muto::grab ()
|
|
|
|
{
|
|
|
|
tls = &_my_tls;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Constructor */
|
2002-02-15 05:20:06 +08:00
|
|
|
muto *
|
2002-02-17 12:59:55 +08:00
|
|
|
muto::init (const char *s)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-03-08 13:05:02 +08:00
|
|
|
char *already_exists = (char *) InterlockedExchangePointer (&name, s);
|
|
|
|
if (already_exists)
|
|
|
|
while (!bruteforce)
|
|
|
|
low_priority_sleep (0);
|
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-03-07 04:21:30 +08:00
|
|
|
waiters = -1;
|
|
|
|
/* Create event which is used in the fallback case when blocking is necessary */
|
2005-03-08 13:05:02 +08:00
|
|
|
bruteforce = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
|
|
|
|
if (!bruteforce)
|
|
|
|
api_fatal ("couldn't allocate muto '%s', %E", s);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2005-03-08 13:05:02 +08:00
|
|
|
|
2002-02-15 05:20:06 +08:00
|
|
|
return this;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-02-15 05:20:06 +08:00
|
|
|
#if 0 /* FIXME: Do we need this? mutos aren't destroyed until process exit */
|
2000-03-15 12:49:36 +08:00
|
|
|
/* Destructor (racy?) */
|
2000-02-18 03:38:33 +08:00
|
|
|
muto::~muto ()
|
|
|
|
{
|
2000-03-15 12:49:36 +08:00
|
|
|
while (visits)
|
|
|
|
release ();
|
|
|
|
|
|
|
|
HANDLE h = bruteforce;
|
2001-09-17 11:05:05 +08:00
|
|
|
bruteforce = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Just need to close the event handle */
|
2000-03-15 12:49:36 +08:00
|
|
|
if (h)
|
|
|
|
CloseHandle (h);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2002-02-15 05:20:06 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Acquire the lock. Argument is the number of milliseconds to wait for
|
|
|
|
the lock. Multiple visits from the same thread are allowed and should
|
2000-02-24 10:49:44 +08:00
|
|
|
be handled correctly.
|
|
|
|
|
|
|
|
Note: The goal here is to minimize, as much as possible, calls to the
|
2001-05-05 05:02:15 +08:00
|
|
|
OS. Hence the use of InterlockedIncrement, etc., rather than (much) more
|
2004-03-15 23:50:20 +08:00
|
|
|
expensive OS mutexes. Also note that the only two valid "ms" times are
|
|
|
|
0 and INFINITE. */
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
|
|
|
muto::acquire (DWORD ms)
|
|
|
|
{
|
2004-05-16 12:18:50 +08:00
|
|
|
void *this_tls = &_my_tls;
|
2004-02-12 11:01:58 +08:00
|
|
|
#if 0
|
2004-01-14 23:45:37 +08:00
|
|
|
if (exiting_thread)
|
|
|
|
return this_tid == exiting_thread;
|
2004-02-12 11:01:58 +08:00
|
|
|
#endif
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
if (tls != this_tls)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* Increment the waiters part of the class. Need to do this first to
|
|
|
|
avoid potential races. */
|
2004-03-15 23:50:20 +08:00
|
|
|
LONG was_waiting = ms ? InterlockedIncrement (&waiters) : 0;
|
|
|
|
|
|
|
|
while (was_waiting || InterlockedExchange (&sync, 1) != 0)
|
2005-06-05 12:07:46 +08:00
|
|
|
switch (WaitForSingleObject (bruteforce, ms))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
was_waiting = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0; /* failed. */
|
|
|
|
}
|
2004-03-15 23:50:20 +08:00
|
|
|
|
|
|
|
/* Have to do it this way to avoid a race */
|
|
|
|
if (!ms)
|
|
|
|
InterlockedIncrement (&waiters);
|
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
tls = this_tls; /* register this thread. */
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ++visits; /* Increment visit count. */
|
|
|
|
}
|
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
bool
|
|
|
|
muto::acquired ()
|
|
|
|
{
|
|
|
|
return tls == &_my_tls;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Return the muto lock. Needs to be called once per every acquire. */
|
|
|
|
int
|
|
|
|
muto::release ()
|
|
|
|
{
|
2004-05-16 12:18:50 +08:00
|
|
|
void *this_tls = &_my_tls;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
if (tls != this_tls || !visits)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
SetLastError (ERROR_NOT_OWNER); /* Didn't have the lock. */
|
|
|
|
return 0; /* failed. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Need to check that other thread has not exited, too. */
|
|
|
|
if (!--visits)
|
|
|
|
{
|
2004-05-16 12:18:50 +08:00
|
|
|
tls = 0; /* We were the last unlocker. */
|
2005-07-07 04:05:03 +08:00
|
|
|
InterlockedExchange (&sync, 0); /* Reset trigger. */
|
2000-02-18 03:38:33 +08:00
|
|
|
/* This thread had incremented waiters but had never decremented it.
|
|
|
|
Decrement it now. If it is >= 0 then there are possibly other
|
2004-03-15 23:50:20 +08:00
|
|
|
threads waiting for the lock, so trigger bruteforce. */
|
2001-05-05 05:02:15 +08:00
|
|
|
if (InterlockedDecrement (&waiters) >= 0)
|
2005-07-07 04:05:03 +08:00
|
|
|
SetEvent (bruteforce); /* Wake up one of the waiting threads */
|
2004-05-16 12:18:50 +08:00
|
|
|
else if (*name == '!')
|
|
|
|
{
|
|
|
|
CloseHandle (bruteforce); /* If *name == '!' and there are no
|
|
|
|
other waiters, then this is the
|
|
|
|
last time this muto will ever be
|
|
|
|
used, so close the handle. */
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
bruteforce = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1; /* success. */
|
|
|
|
}
|