2010-03-14 03:34:35 +08:00
|
|
|
/* spinlock.h: Header file for cygwin time-sensitive synchronization primitive.
|
|
|
|
|
|
|
|
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. */
|
|
|
|
|
|
|
|
#ifndef _SPINLOCK_H
|
|
|
|
#define _SPINLOCK_H
|
|
|
|
|
2010-03-16 05:29:15 +08:00
|
|
|
#include "ntdll.h"
|
|
|
|
|
|
|
|
#define SPINLOCK_WAIT (15000LL * 10000LL)
|
2010-03-14 03:34:35 +08:00
|
|
|
|
|
|
|
class spinlock
|
|
|
|
{
|
|
|
|
LONG *locker;
|
|
|
|
LONG val;
|
2010-03-16 05:29:15 +08:00
|
|
|
LONG setto;
|
|
|
|
void done (LONG what)
|
|
|
|
{
|
|
|
|
if (locker)
|
|
|
|
{
|
|
|
|
InterlockedExchange (locker, what);
|
|
|
|
locker = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long long time ()
|
|
|
|
{
|
|
|
|
LARGE_INTEGER t;
|
|
|
|
if (NtQuerySystemTime (&t) == STATUS_SUCCESS)
|
2018-11-27 00:25:47 +08:00
|
|
|
return t.QuadPart;
|
2010-03-16 05:29:15 +08:00
|
|
|
return 0LL;
|
|
|
|
}
|
2010-03-14 03:34:35 +08:00
|
|
|
public:
|
2010-03-16 05:29:15 +08:00
|
|
|
spinlock (LONG& locktest, LONG wanted_val = 1, LONGLONG timeout = SPINLOCK_WAIT):
|
|
|
|
locker (&locktest), setto (wanted_val)
|
2010-03-14 03:34:35 +08:00
|
|
|
{
|
2010-03-16 05:29:15 +08:00
|
|
|
/* Quick test to see if we're already initialized */
|
|
|
|
if ((val = locktest) == wanted_val)
|
|
|
|
locker = NULL;
|
|
|
|
/* Slightly less quick test to see if we are the first cygwin process */
|
|
|
|
else if ((val = InterlockedExchange (locker, -1)) == 0)
|
|
|
|
/* We're armed and dangerous */;
|
|
|
|
else if (val == wanted_val)
|
|
|
|
done (val); /* This was initialized while we weren't looking */
|
|
|
|
else
|
2010-03-14 03:34:35 +08:00
|
|
|
{
|
2010-03-16 05:29:15 +08:00
|
|
|
long long then = time ();
|
|
|
|
/* Loop waiting for some other process to set locktest to something
|
|
|
|
other than -1, indicating that initialization has finished. Or,
|
|
|
|
wait a default of 15 seconds for that to happen and, if it doesn't
|
|
|
|
just grab the lock ourselves. */
|
|
|
|
while ((val = InterlockedExchange (locker, -1)) == -1
|
|
|
|
&& (time () - then) < timeout)
|
|
|
|
yield ();
|
|
|
|
/* Reset the lock back to wanted_value under the assumption that is
|
|
|
|
what caused the above loop to kick out. */
|
|
|
|
if (val == -1)
|
|
|
|
val = 0; /* Timed out. We'll initialize things ourselves. */
|
|
|
|
else
|
|
|
|
done (val); /* Put back whatever was there before, assuming that
|
|
|
|
it is actually wanted_val. */
|
2010-03-14 03:34:35 +08:00
|
|
|
}
|
|
|
|
}
|
2010-03-16 05:29:15 +08:00
|
|
|
~spinlock () {done (setto);}
|
2013-05-23 00:55:55 +08:00
|
|
|
operator ULONG () const {return (ULONG) val;}
|
2010-03-16 05:29:15 +08:00
|
|
|
/* FIXME: This should be handled in a more general fashion, probably by
|
|
|
|
establishing a linked list of spinlocks which are freed on process exit. */
|
|
|
|
void multiple_cygwin_problem (const char *w, unsigned m, unsigned v)
|
|
|
|
{
|
|
|
|
done (val);
|
|
|
|
::multiple_cygwin_problem (w, m, v);
|
|
|
|
}
|
2010-03-14 03:34:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /*_SPINLOCK_H*/
|