3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
8 #ifdef __OUTSIDE_CYGWIN__
12 #define __BSD_VISIBLE 1
13 #include <sys/smallprint.h>
20 #include "cygserver_ipc.h"
22 /* A BSD kernel global mutex. */
26 mtx_init (mtx
*m
, const char *name
, const void *, int)
31 /* Can't use Windows Mutexes here since Windows Mutexes are only
32 unlockable by the lock owner. */
33 m
->h
= CreateSemaphore (NULL
, 1, 1, NULL
);
35 panic ("couldn't allocate %s mutex, %u\n", name
, GetLastError ());
39 _mtx_lock (mtx
*m
, DWORD winpid
, const char *file
, int line
)
41 _debug (file
, line
, "Try locking mutex %s (%u) (hold: %u)",
42 m
->name
, winpid
, m
->owner
);
43 if (WaitForSingleObject (m
->h
, INFINITE
) != WAIT_OBJECT_0
)
44 _panic (file
, line
, "wait for %s in %d failed, %u", m
->name
, winpid
,
47 _debug (file
, line
, "Locked mutex %s/%u (owner: %u)",
48 m
->name
, ++m
->cnt
, winpid
);
52 mtx_owned (mtx
*m
, DWORD winpid
)
54 return m
->owner
== winpid
;
58 _mtx_assert (mtx
*m
, int what
, DWORD winpid
, const char *file
, int line
)
63 if (!mtx_owned (m
, winpid
))
64 _panic (file
, line
, "Mutex %s not owned", m
->name
);
67 if (mtx_owned (m
, winpid
))
68 _panic (file
, line
, "Mutex %s is owned", m
->name
);
76 _mtx_unlock (mtx
*m
, const char *file
, int line
)
78 DWORD owner
= m
->owner
;
79 unsigned long cnt
= m
->cnt
;
81 /* Cautiously check if mtx_destroy has been called (shutdown).
82 In that case, m->h is NULL. */
83 if (m
->h
&& !ReleaseSemaphore (m
->h
, 1, NULL
))
85 /* Check if the semaphore was already on it's max value. */
86 if (GetLastError () != ERROR_TOO_MANY_POSTS
)
87 _panic (file
, line
, "release of mutex %s failed, %u", m
->name
,
90 _debug (file
, line
, "Unlocked mutex %s/%u (owner: %u)",
104 * Helper functions for msleep/wakeup.
108 win_priority (int priority
)
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. */
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
;
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
;
130 return THREAD_PRIORITY_NORMAL
;
134 * Sets the thread priority, returns the old priority.
137 set_priority (int priority
)
139 int old_prio
= GetThreadPriority (GetCurrentThread ());
140 if (!SetThreadPriority (GetCurrentThread (), win_priority (priority
)))
142 "Warning: Setting thread priority to %d failed with error %u\n",
143 win_priority (priority
), GetLastError ());
148 * Original description from BSD code:
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).
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.
164 static HANDLE msleep_glob_evt
;
166 class msleep_sync_array
177 struct msleep_record
{
183 int find_ident (void *ident
, msleep_action action
)
186 for (i
= 0; i
< cnt
; ++i
)
187 if (a
[i
].ident
== ident
)
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
);
196 HANDLE
first_entry (int i
, void *ident
)
198 debug ("New ident %x, index %d", ident
, i
);
200 a
[i
].wakeup_evt
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
201 if (!a
[i
].wakeup_evt
)
202 panic ("CreateEvent failed: %u", GetLastError ());
203 debug ("i = %d, CreateEvent: %x", i
, a
[i
].wakeup_evt
);
206 return a
[i
].wakeup_evt
;
209 HANDLE
next_entry (int i
)
211 if (a
[i
].ident
&& WaitForSingleObject (a
[i
].wakeup_evt
, 0) != WAIT_OBJECT_0
)
214 return a
[i
].wakeup_evt
;
221 msleep_sync_array (int count
) : cnt (0), max_cnt (count
)
223 InitializeCriticalSection (&cs
);
224 if (!(a
= new msleep_record
[count
]))
225 panic ("Allocating msleep records failed: %d", errno
);
228 ~msleep_sync_array () { delete a
; }
230 HANDLE
enter (void *ident
)
235 EnterCriticalSection (&cs
);
236 int i
= find_ident (ident
, MSLEEP_ENTER
);
238 evt
= first_entry (i
, ident
);
239 else if (!(evt
= next_entry (i
)))
241 /* wakeup has been called, so sleep to wait until all
242 formerly waiting threads have left and retry. */
243 LeaveCriticalSection (&cs
);
247 LeaveCriticalSection (&cs
);
251 void leave (void *ident
)
253 EnterCriticalSection (&cs
);
254 int i
= find_ident (ident
, MSLEEP_LEAVE
);
255 if (--a
[i
].threads
== 0)
257 debug ("i = %d, CloseEvent: %x", i
, a
[i
].wakeup_evt
);
258 CloseHandle (a
[i
].wakeup_evt
);
264 LeaveCriticalSection (&cs
);
267 void wakeup (void *ident
)
269 EnterCriticalSection (&cs
);
270 int i
= find_ident (ident
, MSLEEP_WAKEUP
);
271 if (i
< cnt
&& a
[i
].ident
)
272 SetEvent (a
[i
].wakeup_evt
);
273 LeaveCriticalSection (&cs
);
277 static msleep_sync_array
*msleep_sync
;
278 extern struct msginfo msginfo
;
279 extern struct seminfo seminfo
;
284 msleep_glob_evt
= CreateEvent (NULL
, TRUE
, FALSE
, NULL
);
285 if (!msleep_glob_evt
)
286 panic ("CreateEvent in msleep_init failed: %u", GetLastError ());
287 int32_t msgmni
= support_msgqueues
? msginfo
.msgmni
: 0;
288 int32_t semmni
= support_semaphores
? seminfo
.semmni
: 0;
289 TUNABLE_INT_FETCH ("kern.ipc.msgmni", &msgmni
);
290 TUNABLE_INT_FETCH ("kern.ipc.semmni", &semmni
);
291 debug ("Try allocating msgmni (%d) + semmni (%d) msleep records",
293 msleep_sync
= new msleep_sync_array (msgmni
+ semmni
);
295 panic ("Allocating msleep records in msleep_init failed: %d", errno
);
299 _msleep (void *ident
, struct mtx
*mtx
, int priority
,
300 const char *wmesg
, int timo
, struct thread
*td
)
304 HANDLE evt
= msleep_sync
->enter (ident
);
308 int old_priority
= set_priority (priority
);
313 td
->client
->handle (),
314 td
->ipcblk
->signal_arrived
316 /* PCATCH handling. If PCATCH is given and signal_arrived is a valid
317 handle, then it's used in the WaitFor call and EINTR is returned. */
319 if ((priority
& PCATCH
) && obj
[3])
321 switch (WaitForMultipleObjects (obj_cnt
, obj
, FALSE
, timo
?: INFINITE
))
323 case WAIT_OBJECT_0
: /* wakeup() has been called. */
325 debug ("msleep wakeup called for %d", td
->td_proc
->winpid
);
327 case WAIT_OBJECT_0
+ 1: /* Shutdown event (triggered by wakeup_all). */
330 case WAIT_OBJECT_0
+ 2: /* The dependent process has exited. */
331 debug ("msleep process exit or shutdown for %d", td
->td_proc
->winpid
);
334 case WAIT_OBJECT_0
+ 3: /* Signal for calling process arrived. */
335 debug ("msleep process got signal for %d", td
->td_proc
->winpid
);
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, %u", wmesg
, GetLastError ());
350 debug ("wait in msleep (%s) failed for %d, %u", wmesg
,
351 td
->td_proc
->winpid
, GetLastError ());
356 msleep_sync
->leave (ident
);
358 set_priority (old_priority
);
360 if (mtx
&& !(priority
& PDROP
))
366 * Make all threads sleeping on the specified identifier runnable.
371 msleep_sync
->wakeup (ident
);
376 * Wakeup all sleeping threads. Only called in the context of cygserver
382 SetEvent (msleep_glob_evt
);
384 #endif /* __OUTSIDE_CYGWIN__ */