]>
sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/sync.cc
1 /* sync.cc: Synchronization functions for cygwin.
3 This file implements the methods for controlling the "muto" class
4 which is intended to operate similarly to a mutex but attempts to
5 avoid making expensive calls to the kernel.
7 Copyright 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.
9 Written by Christopher Faylor <cgf@cygnus.com>
11 This file is part of Cygwin.
13 This software is a copyrighted work licensed under the terms of the
14 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
26 #undef WaitForSingleObject
28 DWORD NO_COPY
muto::exiting_thread
;
32 muto::init (const char *s
)
35 /* Create event which is used in the fallback case when blocking is necessary */
36 if (!(bruteforce
= CreateEvent (&sec_none_nih
, FALSE
, FALSE
, NULL
)))
38 DWORD oerr
= GetLastError ();
46 #if 0 /* FIXME: Do we need this? mutos aren't destroyed until process exit */
47 /* Destructor (racy?) */
53 HANDLE h
= bruteforce
;
55 /* Just need to close the event handle */
61 /* Acquire the lock. Argument is the number of milliseconds to wait for
62 the lock. Multiple visits from the same thread are allowed and should
65 Note: The goal here is to minimize, as much as possible, calls to the
66 OS. Hence the use of InterlockedIncrement, etc., rather than (much) more
67 expensive OS mutexes. */
69 muto::acquire (DWORD ms
)
71 DWORD this_tid
= GetCurrentThreadId ();
74 return this_tid
== exiting_thread
;
79 /* Increment the waiters part of the class. Need to do this first to
80 avoid potential races. */
81 LONG was_waiting
= InterlockedIncrement (&waiters
);
83 /* This is deceptively simple. Basically, it allows multiple attempts to
84 lock the same muto to succeed without attempting to manipulate sync.
85 If the muto is already locked then this thread will wait for ms until
86 it is signalled by muto::release. Then it will attempt to grab the
87 sync field. If it succeeds, then this thread owns the muto.
89 There is a pathological condition where a thread times out waiting for
90 bruteforce but the release code triggers the bruteforce event. In this
91 case, it is possible for a thread which is going to wait for bruteforce
92 to wake up immediately. It will then attempt to grab sync but will fail
93 and go back to waiting. */
94 if (tid
!= this_tid
&& (was_waiting
|| InterlockedExchange (&sync
, 1) != 0))
96 switch (WaitForSingleObject (bruteforce
, ms
))
102 InterlockedDecrement (&waiters
);
103 return 0; /* failed. */
109 tid
= this_tid
; /* register this thread. */
110 return ++visits
; /* Increment visit count. */
113 /* Return the muto lock. Needs to be called once per every acquire. */
117 DWORD this_tid
= GetCurrentThreadId ();
119 if (tid
!= this_tid
|| !visits
)
121 SetLastError (ERROR_NOT_OWNER
); /* Didn't have the lock. */
122 return 0; /* failed. */
125 /* FIXME: Need to check that other thread has not exited, too. */
128 tid
= 0; /* We were the last unlocker. */
129 (void) InterlockedExchange (&sync
, 0); /* Reset trigger. */
130 /* This thread had incremented waiters but had never decremented it.
131 Decrement it now. If it is >= 0 then there are possibly other
132 threads waiting for the lock, so trigger bruteforce. */
133 if (InterlockedDecrement (&waiters
) >= 0)
134 (void) SetEvent (bruteforce
); /* Wake up one of the waiting threads */
137 return 1; /* success. */
143 return tid
== GetCurrentThreadId ();
146 /* Call only when we're exiting. This is not thread safe. */
150 visits
= sync
= tid
= 0;
151 InterlockedExchange (&waiters
, -1);
154 CloseHandle (bruteforce
);
155 bruteforce
= CreateEvent (&sec_none_nih
, FALSE
, FALSE
, name
);
This page took 0.044452 seconds and 6 git commands to generate.