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.
|
|
|
|
|
2000-02-24 10:49:44 +08:00
|
|
|
Copyright 2000 Cygnus Solutions.
|
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"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-02-26 09:11:54 +08:00
|
|
|
muto NO_COPY muto_start;
|
2000-02-25 03:54:01 +08:00
|
|
|
|
2000-05-18 11:20:01 +08:00
|
|
|
#undef WaitForSingleObject
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Constructor */
|
2000-05-17 13:49:51 +08:00
|
|
|
muto::muto (int inh, const char *s) : sync (0), visits(0), waiters(-1), tid (0), next (NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* Create event which is used in the fallback case when blocking is necessary */
|
|
|
|
if (!(bruteforce = CreateEvent (inh ? &sec_all_nih : &sec_none_nih, FALSE, FALSE, name)))
|
|
|
|
{
|
|
|
|
DWORD oerr = GetLastError ();
|
|
|
|
SetLastError (oerr);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-15 12:49:36 +08:00
|
|
|
name = s;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
h = 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
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 04:39:38 +08:00
|
|
|
OS. Hence the use of ilockincr, etc., rather than (much) more
|
2000-02-24 10:49:44 +08:00
|
|
|
expensive OS mutexes. */
|
2000-02-18 03:38:33 +08:00
|
|
|
int
|
|
|
|
muto::acquire (DWORD ms)
|
|
|
|
{
|
|
|
|
DWORD this_tid = GetCurrentThreadId ();
|
|
|
|
|
|
|
|
if (tid != this_tid)
|
|
|
|
{
|
|
|
|
/* Increment the waiters part of the class. Need to do this first to
|
|
|
|
avoid potential races. */
|
2001-05-05 04:39:38 +08:00
|
|
|
LONG was_waiting = ilockincr (&waiters);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* This is deceptively simple. Basically, it allows multiple attempts to
|
|
|
|
lock the same muto to succeed without attempting to manipulate sync.
|
|
|
|
If the muto is already locked then this thread will wait for ms until
|
|
|
|
it is signalled by muto::release. Then it will attempt to grab the
|
2000-02-24 10:49:44 +08:00
|
|
|
sync field. If it succeeds, then this thread owns the muto.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
There is a pathological condition where a thread times out waiting for
|
|
|
|
bruteforce but the release code triggers the bruteforce event. In this
|
|
|
|
case, it is possible for a thread which is going to wait for bruteforce
|
|
|
|
to wake up immediately. It will then attempt to grab sync but will fail
|
|
|
|
and go back to waiting. */
|
2001-05-05 04:39:38 +08:00
|
|
|
while (tid != this_tid && (was_waiting || ilockexch (&sync, 1) != 0))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
switch (WaitForSingleObject (bruteforce, ms))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
2000-10-20 01:57:28 +08:00
|
|
|
goto gotit;
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
default:
|
2001-05-05 04:39:38 +08:00
|
|
|
ilockdecr (&waiters);
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0; /* failed. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-20 01:57:28 +08:00
|
|
|
gotit:
|
2000-02-18 03:38:33 +08:00
|
|
|
tid = this_tid; /* register this thread. */
|
|
|
|
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 14:45:32 +08:00
|
|
|
tid = 0; /* We were the last unlocker. */
|
2001-05-05 04:39:38 +08:00
|
|
|
(void) ilockexch (&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
|
|
|
|
threads waiting for the lock, so trigger bruteforce. */
|
2001-05-05 04:39:38 +08:00
|
|
|
if (ilockdecr (&waiters) >= 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
(void) SetEvent (bruteforce); /* Wake up one of the waiting threads */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1; /* success. */
|
|
|
|
}
|
2000-09-07 09:18:37 +08:00
|
|
|
|
|
|
|
/* Call only when we're exiting. This is not thread safe. */
|
|
|
|
void
|
|
|
|
muto::reset ()
|
|
|
|
{
|
|
|
|
visits = sync = tid = 0;
|
2001-05-05 04:39:38 +08:00
|
|
|
ilockexch (&waiters, -1);
|
2000-09-07 09:18:37 +08:00
|
|
|
if (bruteforce)
|
|
|
|
{
|
|
|
|
CloseHandle (bruteforce);
|
|
|
|
bruteforce = CreateEvent (&sec_none_nih, FALSE, FALSE, name);
|
|
|
|
}
|
|
|
|
}
|