2000-02-17 20:38:33 +01: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.
|
|
|
|
|
2004-02-09 05:04:24 +01:00
|
|
|
Copyright 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.
|
2000-02-17 20:38:33 +01: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-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "sync.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include "security.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2000-05-18 05:20:01 +02:00
|
|
|
#undef WaitForSingleObject
|
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
DWORD NO_COPY muto::exiting_thread;
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Constructor */
|
2002-02-14 22:20:06 +01:00
|
|
|
muto *
|
2002-02-17 05:59:55 +01:00
|
|
|
muto::init (const char *s)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-02-14 22:20:06 +01:00
|
|
|
waiters = -1;
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Create event which is used in the fallback case when blocking is necessary */
|
2002-02-17 05:59:55 +01:00
|
|
|
if (!(bruteforce = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL)))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
DWORD oerr = GetLastError ();
|
|
|
|
SetLastError (oerr);
|
2002-02-14 22:20:06 +01:00
|
|
|
return NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2000-03-15 05:49:36 +01:00
|
|
|
name = s;
|
2002-02-14 22:20:06 +01:00
|
|
|
return this;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2002-02-14 22:20:06 +01:00
|
|
|
#if 0 /* FIXME: Do we need this? mutos aren't destroyed until process exit */
|
2000-03-15 05:49:36 +01:00
|
|
|
/* Destructor (racy?) */
|
2000-02-17 20:38:33 +01:00
|
|
|
muto::~muto ()
|
|
|
|
{
|
2000-03-15 05:49:36 +01:00
|
|
|
while (visits)
|
|
|
|
release ();
|
|
|
|
|
|
|
|
HANDLE h = bruteforce;
|
2001-09-17 05:05:05 +02:00
|
|
|
bruteforce = NULL;
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Just need to close the event handle */
|
2000-03-15 05:49:36 +01:00
|
|
|
if (h)
|
|
|
|
CloseHandle (h);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2002-02-14 22:20:06 +01:00
|
|
|
#endif
|
2000-02-17 20:38:33 +01: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 03:49:44 +01:00
|
|
|
be handled correctly.
|
|
|
|
|
|
|
|
Note: The goal here is to minimize, as much as possible, calls to the
|
2001-05-04 23:02:15 +02:00
|
|
|
OS. Hence the use of InterlockedIncrement, etc., rather than (much) more
|
2004-03-15 16:50:20 +01:00
|
|
|
expensive OS mutexes. Also note that the only two valid "ms" times are
|
|
|
|
0 and INFINITE. */
|
2000-02-17 20:38:33 +01:00
|
|
|
int
|
|
|
|
muto::acquire (DWORD ms)
|
|
|
|
{
|
|
|
|
DWORD this_tid = GetCurrentThreadId ();
|
2004-02-12 04:01:58 +01:00
|
|
|
#if 0
|
2004-01-14 16:45:37 +01:00
|
|
|
if (exiting_thread)
|
|
|
|
return this_tid == exiting_thread;
|
2004-02-12 04:01:58 +01:00
|
|
|
#endif
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (tid != this_tid)
|
|
|
|
{
|
|
|
|
/* Increment the waiters part of the class. Need to do this first to
|
|
|
|
avoid potential races. */
|
2004-03-15 16:50:20 +01:00
|
|
|
LONG was_waiting = ms ? InterlockedIncrement (&waiters) : 0;
|
|
|
|
|
|
|
|
while (was_waiting || InterlockedExchange (&sync, 1) != 0)
|
|
|
|
{
|
|
|
|
switch (WaitForSingleObject (bruteforce, ms))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
was_waiting = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0; /* failed. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Have to do it this way to avoid a race */
|
|
|
|
if (!ms)
|
|
|
|
InterlockedIncrement (&waiters);
|
|
|
|
|
|
|
|
tid = this_tid; /* register this thread. */
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ++visits; /* Increment visit count. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the muto lock. Needs to be called once per every acquire. */
|
|
|
|
int
|
|
|
|
muto::release ()
|
|
|
|
{
|
|
|
|
DWORD this_tid = GetCurrentThreadId ();
|
|
|
|
|
|
|
|
if (tid != this_tid || !visits)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2000-02-24 07:45:32 +01:00
|
|
|
tid = 0; /* We were the last unlocker. */
|
2001-05-04 23:02:15 +02:00
|
|
|
(void) InterlockedExchange (&sync, 0); /* Reset trigger. */
|
2000-02-17 20:38:33 +01: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 16:50:20 +01:00
|
|
|
threads waiting for the lock, so trigger bruteforce. */
|
2001-05-04 23:02:15 +02:00
|
|
|
if (InterlockedDecrement (&waiters) >= 0)
|
2000-02-17 20:38:33 +01:00
|
|
|
(void) SetEvent (bruteforce); /* Wake up one of the waiting threads */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1; /* success. */
|
|
|
|
}
|
2000-09-07 03:18:37 +02:00
|
|
|
|
2004-01-14 16:45:37 +01:00
|
|
|
bool
|
|
|
|
muto::acquired ()
|
|
|
|
{
|
|
|
|
return tid == GetCurrentThreadId ();
|
|
|
|
}
|
|
|
|
|
2000-09-07 03:18:37 +02:00
|
|
|
/* Call only when we're exiting. This is not thread safe. */
|
|
|
|
void
|
|
|
|
muto::reset ()
|
|
|
|
{
|
|
|
|
visits = sync = tid = 0;
|
2001-05-04 23:02:15 +02:00
|
|
|
InterlockedExchange (&waiters, -1);
|
2000-09-07 03:18:37 +02:00
|
|
|
if (bruteforce)
|
|
|
|
{
|
|
|
|
CloseHandle (bruteforce);
|
|
|
|
bruteforce = CreateEvent (&sec_none_nih, FALSE, FALSE, name);
|
|
|
|
}
|
|
|
|
}
|