]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygserver/bsd_mutex.cc
* bsd_helper.cc: Drop unnecessary security.h include.
[newlib-cygwin.git] / winsup / cygserver / bsd_mutex.cc
1 /* bsd_mutex.cc
2
3 Copyright 2003, 2004, 2005, 2007 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 #ifdef __OUTSIDE_CYGWIN__
11 #include "woutsup.h"
12 #include <errno.h>
13 #define _KERNEL 1
14 #define __BSD_VISIBLE 1
15 #include <sys/smallprint.h>
16 #include <limits.h>
17 #include <stdlib.h>
18 #include <sys/msg.h>
19 #include <sys/sem.h>
20
21 #include "process.h"
22 #include "cygserver_ipc.h"
23
24 /* A BSD kernel global mutex. */
25 struct mtx Giant;
26
27 void
28 mtx_init (mtx *m, const char *name, const void *, int)
29 {
30 m->name = name;
31 m->owner = 0;
32 m->cnt = 0;
33 /* Can't use Windows Mutexes here since Windows Mutexes are only
34 unlockable by the lock owner. */
35 m->h = CreateSemaphore (NULL, 1, 1, NULL);
36 if (!m->h)
37 panic ("couldn't allocate %s mutex, %E\n", name);
38 }
39
40 void
41 _mtx_lock (mtx *m, DWORD winpid, const char *file, int line)
42 {
43 _log (file, line, LOG_DEBUG, "Try locking mutex %s (%u) (hold: %u)",
44 m->name, winpid, m->owner);
45 if (WaitForSingleObject (m->h, INFINITE) != WAIT_OBJECT_0)
46 _panic (file, line, "wait for %s in %d failed, %E", m->name, winpid);
47 m->owner = winpid;
48 _log (file, line, LOG_DEBUG, "Locked mutex %s/%u (%u)",
49 m->name, ++m->cnt, winpid);
50 }
51
52 int
53 mtx_owned (mtx *m, DWORD winpid)
54 {
55 return m->owner == winpid;
56 }
57
58 void
59 _mtx_assert (mtx *m, int what, DWORD winpid, const char *file, int line)
60 {
61 switch (what)
62 {
63 case MA_OWNED:
64 if (!mtx_owned (m, winpid))
65 _panic (file, line, "Mutex %s not owned", m->name);
66 break;
67 case MA_NOTOWNED:
68 if (mtx_owned (m, winpid))
69 _panic (file, line, "Mutex %s is owned", m->name);
70 break;
71 default:
72 break;
73 }
74 }
75
76 void
77 _mtx_unlock (mtx *m, const char *file, int line)
78 {
79 DWORD owner = m->owner;
80 unsigned long cnt = m->cnt;
81 m->owner = 0;
82 /* Cautiously check if mtx_destroy has been called (shutdown).
83 In that case, m->h is NULL. */
84 if (m->h && !ReleaseSemaphore (m->h, 1, NULL))
85 {
86 /* Check if the semaphore was already on it's max value. */
87 if (GetLastError () != ERROR_TOO_MANY_POSTS)
88 _panic (file, line, "release of mutex %s failed, %E", m->name);
89 }
90 _log (file, line, LOG_DEBUG, "Unlocked mutex %s/%u (owner: %u)",
91 m->name, cnt, owner);
92 }
93
94 void
95 mtx_destroy (mtx *m)
96 {
97 HANDLE tmp = m->h;
98 m->h = NULL;
99 if (tmp)
100 CloseHandle (tmp);
101 }
102
103 /*
104 * Helper functions for msleep/wakeup.
105 */
106
107 static int
108 win_priority (int priority)
109 {
110 int p = (int)((priority) & PRIO_MASK) - PZERO;
111 /* Generating a valid priority value is a bit tricky. The only valid
112 values on NT4 are -15, -2, -1, 0, 1, 2, 15. */
113 switch (p)
114 {
115 case -15: case -14: case -13: case -12: case -11:
116 return THREAD_PRIORITY_IDLE;
117 case -10: case -9: case -8: case -7: case -6:
118 return THREAD_PRIORITY_LOWEST;
119 case -5: case -4: case -3: case -2: case -1:
120 return THREAD_PRIORITY_BELOW_NORMAL;
121 case 0:
122 return THREAD_PRIORITY_NORMAL;
123 case 1: case 2: case 3: case 4: case 5:
124 return THREAD_PRIORITY_ABOVE_NORMAL;
125 case 6: case 7: case 8: case 9: case 10:
126 return THREAD_PRIORITY_HIGHEST;
127 case 11: case 12: case 13: case 14: case 15:
128 return THREAD_PRIORITY_TIME_CRITICAL;
129 }
130 return THREAD_PRIORITY_NORMAL;
131 }
132
133 /*
134 * Sets the thread priority, returns the old priority.
135 */
136 static int
137 set_priority (int priority)
138 {
139 int old_prio = GetThreadPriority (GetCurrentThread ());
140 if (!SetThreadPriority (GetCurrentThread (), win_priority (priority)))
141 log (LOG_WARNING,
142 "Warning: Setting thread priority to %d failed with error %lu\n",
143 win_priority (priority), GetLastError ());
144 return old_prio;
145 }
146
147 /*
148 * Original description from BSD code:
149 *
150 * General sleep call. Suspends the current process until a wakeup is
151 * performed on the specified identifier. The process will then be made
152 * runnable with the specified priority. Sleeps at most timo/hz seconds
153 * (0 means no timeout). If pri includes PCATCH flag, signals are checked
154 * before and after sleeping, else signals are not checked. Returns 0 if
155 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
156 * signal needs to be delivered, ERESTART is returned if the current system
157 * call should be restarted if possible, and EINTR is returned if the system
158 * call should be interrupted by the signal (return EINTR).
159 *
160 * The mutex argument is exited before the caller is suspended, and
161 * entered before msleep returns. If priority includes the PDROP
162 * flag the mutex is not entered before returning.
163 */
164 static HANDLE msleep_glob_evt;
165
166 class msleep_sync_array
167 {
168 enum msleep_action {
169 MSLEEP_ENTER = 0,
170 MSLEEP_LEAVE,
171 MSLEEP_WAKEUP
172 };
173
174 CRITICAL_SECTION cs;
175 long cnt;
176 long max_cnt;
177 struct msleep_record {
178 void *ident;
179 HANDLE wakeup_evt;
180 LONG threads;
181 } *a;
182
183 int find_ident (void *ident, msleep_action action)
184 {
185 int i;
186 for (i = 0; i < cnt; ++i)
187 if (a[i].ident == ident)
188 return i;
189 if (i >= max_cnt)
190 panic ("ident %x not found and run out of slots.", ident);
191 if (i >= cnt && action == MSLEEP_LEAVE)
192 panic ("ident %x not found (%d).", ident, action);
193 return i;
194 }
195
196 HANDLE first_entry (int i, void *ident)
197 {
198 debug ("New ident %x, index %d", ident, i);
199 a[i].ident = ident;
200 a[i].wakeup_evt = CreateEvent (NULL, TRUE, FALSE, NULL);
201 if (!a[i].wakeup_evt)
202 panic ("CreateEvent failed: %E");
203 debug ("i = %d, CreateEvent: %x", i, a[i].wakeup_evt);
204 a[i].threads = 1;
205 ++cnt;
206 return a[i].wakeup_evt;
207 }
208
209 HANDLE next_entry (int i)
210 {
211 if (a[i].ident && WaitForSingleObject (a[i].wakeup_evt, 0) != WAIT_OBJECT_0)
212 {
213 ++a[i].threads;
214 return a[i].wakeup_evt;
215 }
216 return NULL;
217 }
218
219 public:
220
221 msleep_sync_array (int count) : cnt (0), max_cnt (count)
222 {
223 InitializeCriticalSection (&cs);
224 if (!(a = new msleep_record[count]))
225 panic ("Allocating msleep records failed: %d", errno);
226 }
227
228 HANDLE enter (void *ident)
229 {
230 HANDLE evt = NULL;
231 while (!evt)
232 {
233 EnterCriticalSection (&cs);
234 int i = find_ident (ident, MSLEEP_ENTER);
235 if (i >= cnt)
236 evt = first_entry (i, ident);
237 else if (!(evt = next_entry (i)))
238 {
239 /* wakeup has been called, so sleep to wait until all
240 formerly waiting threads have left and retry. */
241 LeaveCriticalSection (&cs);
242 Sleep (1L);
243 }
244 }
245 LeaveCriticalSection (&cs);
246 return evt;
247 }
248
249 void leave (void *ident)
250 {
251 EnterCriticalSection (&cs);
252 int i = find_ident (ident, MSLEEP_LEAVE);
253 if (--a[i].threads == 0)
254 {
255 debug ("i = %d, CloseEvent: %x", i, a[i].wakeup_evt);
256 CloseHandle (a[i].wakeup_evt);
257 a[i].ident = NULL;
258 --cnt;
259 if (i < cnt)
260 a[i] = a[cnt];
261 }
262 LeaveCriticalSection (&cs);
263 }
264
265 void wakeup (void *ident)
266 {
267 EnterCriticalSection (&cs);
268 int i = find_ident (ident, MSLEEP_WAKEUP);
269 if (i < cnt && a[i].ident)
270 SetEvent (a[i].wakeup_evt);
271 LeaveCriticalSection (&cs);
272 }
273 };
274
275 static msleep_sync_array *msleep_sync;
276
277 void
278 msleep_init (void)
279 {
280 extern struct msginfo msginfo;
281 extern struct seminfo seminfo;
282
283 msleep_glob_evt = CreateEvent (NULL, TRUE, FALSE, NULL);
284 if (!msleep_glob_evt)
285 panic ("CreateEvent in msleep_init failed: %E");
286 long msgmni = support_msgqueues ? msginfo.msgmni : 0;
287 long semmni = support_semaphores ? seminfo.semmni : 0;
288 TUNABLE_INT_FETCH ("kern.ipc.msgmni", &msgmni);
289 TUNABLE_INT_FETCH ("kern.ipc.semmni", &semmni);
290 debug ("Try allocating msgmni (%d) + semmni (%d) msleep records",
291 msgmni, semmni);
292 msleep_sync = new msleep_sync_array (msgmni + semmni);
293 if (!msleep_sync)
294 panic ("Allocating msleep records in msleep_init failed: %d", errno);
295 }
296
297 int
298 _msleep (void *ident, struct mtx *mtx, int priority,
299 const char *wmesg, int timo, struct thread *td)
300 {
301 int ret = -1;
302
303 HANDLE evt = msleep_sync->enter (ident);
304
305 if (mtx)
306 mtx_unlock (mtx);
307 int old_priority = set_priority (priority);
308 HANDLE obj[4] =
309 {
310 evt,
311 msleep_glob_evt,
312 td->client->handle (),
313 td->client->signal_arrived ()
314 };
315 /* PCATCH handling. If PCATCH is given and signal_arrived is a valid
316 handle, then it's used in the WaitFor call and EINTR is returned. */
317 int obj_cnt = 3;
318 if ((priority & PCATCH)
319 && td->client->signal_arrived () != INVALID_HANDLE_VALUE)
320 obj_cnt = 4;
321 switch (WaitForMultipleObjects (obj_cnt, obj, FALSE, timo ?: INFINITE))
322 {
323 case WAIT_OBJECT_0: /* wakeup() has been called. */
324 ret = 0;
325 debug ("msleep wakeup called for %d", td->td_proc->winpid);
326 break;
327 case WAIT_OBJECT_0 + 1: /* Shutdown event (triggered by wakeup_all). */
328 priority |= PDROP;
329 /*FALLTHRU*/
330 case WAIT_OBJECT_0 + 2: /* The dependent process has exited. */
331 debug ("msleep process exit or shutdown for %d", td->td_proc->winpid);
332 ret = EIDRM;
333 break;
334 case WAIT_OBJECT_0 + 3: /* Signal for calling process arrived. */
335 debug ("msleep process got signal for %d", td->td_proc->winpid);
336 ret = EINTR;
337 break;
338 case WAIT_TIMEOUT:
339 ret = EWOULDBLOCK;
340 break;
341 default:
342 /* There's a chance that a process has been terminated before
343 WaitForMultipleObjects has been called. In this case the handles
344 might be invalid. The error code returned is ERROR_INVALID_HANDLE.
345 Since we can trust the values of these handles otherwise, we
346 treat an ERROR_INVALID_HANDLE as a normal process termination and
347 hope for the best. */
348 if (GetLastError () != ERROR_INVALID_HANDLE)
349 panic ("wait in msleep (%s) failed, %E", wmesg);
350 debug ("wait in msleep (%s) failed for %d, %E", wmesg,
351 td->td_proc->winpid);
352 ret = EIDRM;
353 break;
354 }
355
356 msleep_sync->leave (ident);
357
358 set_priority (old_priority);
359
360 if (mtx && !(priority & PDROP))
361 mtx_lock (mtx);
362 return ret;
363 }
364
365 /*
366 * Make all threads sleeping on the specified identifier runnable.
367 */
368 int
369 wakeup (void *ident)
370 {
371 msleep_sync->wakeup (ident);
372 return 0;
373 }
374
375 /*
376 * Wakeup all sleeping threads. Only called in the context of cygserver
377 * shutdown.
378 */
379 void
380 wakeup_all (void)
381 {
382 SetEvent (msleep_glob_evt);
383 }
384 #endif /* __OUTSIDE_CYGWIN__ */
This page took 0.054544 seconds and 5 git commands to generate.