OSDN Git Service

* select.cc (select_stuff::wait): Temporarily disallow APCS.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / spinlock.h
1 /* spinlock.h: Header file for cygwin time-sensitive synchronization primitive.
2
3    Copyright 2010 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #ifndef _SPINLOCK_H
12 #define _SPINLOCK_H
13
14 #include "ntdll.h"
15
16 #define SPINLOCK_WAIT (15000LL * 10000LL)
17
18 class spinlock
19 {
20   LONG *locker;
21   LONG val;
22   LONG setto;
23   void done (LONG what)
24   {
25     if (locker)
26       {
27         InterlockedExchange (locker, what);
28         locker = NULL;
29       }
30   }
31   long long time ()
32   {
33     LARGE_INTEGER t;
34     if (NtQuerySystemTime (&t) == STATUS_SUCCESS)
35       return get_ll (t);
36     return 0LL;
37   }
38 public:
39   spinlock (LONG& locktest, LONG wanted_val = 1, LONGLONG timeout = SPINLOCK_WAIT):
40     locker (&locktest), setto (wanted_val)
41   {
42     /* Quick test to see if we're already initialized */
43     if ((val = locktest) == wanted_val)
44       locker = NULL;
45     /* Slightly less quick test to see if we are the first cygwin process */
46     else if ((val = InterlockedExchange (locker, -1)) == 0)
47       /* We're armed and dangerous */;
48     else if (val == wanted_val)
49       done (val);       /* This was initialized while we weren't looking */
50     else
51       {
52         long long then = time ();
53         /* Loop waiting for some other process to set locktest to something
54            other than -1, indicating that initialization has finished.  Or,
55            wait a default of 15 seconds for that to happen and, if it doesn't
56            just grab the lock ourselves. */
57         while ((val = InterlockedExchange (locker, -1)) == -1
58                && (time () - then) < timeout)
59           yield ();
60         /* Reset the lock back to wanted_value under the assumption that is
61            what caused the above loop to kick out.  */
62         if (val == -1)
63           val = 0;      /* Timed out.  We'll initialize things ourselves. */
64         else
65           done (val);   /* Put back whatever was there before, assuming that
66                            it is actually wanted_val. */
67       }
68   }
69   ~spinlock () {done (setto);}
70   operator LONG () const {return val;}
71   /* FIXME: This should be handled in a more general fashion, probably by
72      establishing a linked list of spinlocks which are freed on process exit. */
73   void multiple_cygwin_problem (const char *w, unsigned m, unsigned v)
74   {
75     done (val);
76     ::multiple_cygwin_problem (w, m, v);
77   }
78 };
79
80 #endif /*_SPINLOCK_H*/