2003-11-20 02:49:41 +08:00
|
|
|
/* bsd_mutex.cc
|
|
|
|
|
|
|
|
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. */
|
|
|
|
#ifdef __OUTSIDE_CYGWIN__
|
|
|
|
#include "woutsup.h"
|
2004-09-15 18:17:53 +08:00
|
|
|
#include <errno.h>
|
2003-11-20 02:49:41 +08:00
|
|
|
#define _KERNEL 1
|
|
|
|
#define __BSD_VISIBLE 1
|
|
|
|
#include <sys/smallprint.h>
|
2004-12-30 23:58:27 +08:00
|
|
|
#include <limits.h>
|
2005-04-06 19:11:07 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/msg.h>
|
|
|
|
#include <sys/sem.h>
|
2003-11-20 02:49:41 +08:00
|
|
|
|
|
|
|
#include "process.h"
|
|
|
|
#include "cygserver_ipc.h"
|
|
|
|
|
|
|
|
/* A BSD kernel global mutex. */
|
|
|
|
struct mtx Giant;
|
|
|
|
|
|
|
|
void
|
|
|
|
mtx_init (mtx *m, const char *name, const void *, int)
|
|
|
|
{
|
|
|
|
m->name = name;
|
|
|
|
m->owner = 0;
|
2005-04-06 19:11:07 +08:00
|
|
|
m->cnt = 0;
|
2003-11-20 02:49:41 +08:00
|
|
|
/* Can't use Windows Mutexes here since Windows Mutexes are only
|
|
|
|
unlockable by the lock owner. */
|
|
|
|
m->h = CreateSemaphore (NULL, 1, 1, NULL);
|
|
|
|
if (!m->h)
|
2014-03-13 00:00:48 +08:00
|
|
|
panic ("couldn't allocate %s mutex, %u\n", name, GetLastError ());
|
2003-11-20 02:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_mtx_lock (mtx *m, DWORD winpid, const char *file, int line)
|
|
|
|
{
|
2017-03-25 00:26:37 +08:00
|
|
|
_debug (file, line, "Try locking mutex %s (%u) (hold: %u)",
|
2005-04-06 19:11:07 +08:00
|
|
|
m->name, winpid, m->owner);
|
2003-11-20 02:49:41 +08:00
|
|
|
if (WaitForSingleObject (m->h, INFINITE) != WAIT_OBJECT_0)
|
2014-03-13 00:00:48 +08:00
|
|
|
_panic (file, line, "wait for %s in %d failed, %u", m->name, winpid,
|
2008-02-07 06:30:38 +08:00
|
|
|
GetLastError ());
|
2003-11-20 02:49:41 +08:00
|
|
|
m->owner = winpid;
|
2017-03-25 00:26:37 +08:00
|
|
|
_debug (file, line, "Locked mutex %s/%u (owner: %u)",
|
2005-04-06 19:11:07 +08:00
|
|
|
m->name, ++m->cnt, winpid);
|
2003-11-20 02:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-04-06 19:11:07 +08:00
|
|
|
mtx_owned (mtx *m, DWORD winpid)
|
2003-11-20 02:49:41 +08:00
|
|
|
{
|
2005-04-06 19:11:07 +08:00
|
|
|
return m->owner == winpid;
|
2003-11-20 02:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-04-06 19:11:07 +08:00
|
|
|
_mtx_assert (mtx *m, int what, DWORD winpid, const char *file, int line)
|
2003-11-20 02:49:41 +08:00
|
|
|
{
|
|
|
|
switch (what)
|
|
|
|
{
|
|
|
|
case MA_OWNED:
|
2005-04-06 19:11:07 +08:00
|
|
|
if (!mtx_owned (m, winpid))
|
2004-07-21 00:06:14 +08:00
|
|
|
_panic (file, line, "Mutex %s not owned", m->name);
|
2003-11-20 02:49:41 +08:00
|
|
|
break;
|
|
|
|
case MA_NOTOWNED:
|
2005-04-06 19:11:07 +08:00
|
|
|
if (mtx_owned (m, winpid))
|
2004-07-21 00:06:14 +08:00
|
|
|
_panic (file, line, "Mutex %s is owned", m->name);
|
2003-11-20 02:49:41 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_mtx_unlock (mtx *m, const char *file, int line)
|
|
|
|
{
|
2005-04-06 19:11:07 +08:00
|
|
|
DWORD owner = m->owner;
|
|
|
|
unsigned long cnt = m->cnt;
|
2003-11-20 02:49:41 +08:00
|
|
|
m->owner = 0;
|
|
|
|
/* Cautiously check if mtx_destroy has been called (shutdown).
|
|
|
|
In that case, m->h is NULL. */
|
|
|
|
if (m->h && !ReleaseSemaphore (m->h, 1, NULL))
|
|
|
|
{
|
2007-02-23 19:43:48 +08:00
|
|
|
/* Check if the semaphore was already on it's max value. */
|
|
|
|
if (GetLastError () != ERROR_TOO_MANY_POSTS)
|
2014-03-13 00:00:48 +08:00
|
|
|
_panic (file, line, "release of mutex %s failed, %u", m->name,
|
2008-02-07 06:30:38 +08:00
|
|
|
GetLastError ());
|
2003-11-20 02:49:41 +08:00
|
|
|
}
|
2017-03-25 00:26:37 +08:00
|
|
|
_debug (file, line, "Unlocked mutex %s/%u (owner: %u)",
|
2005-04-06 19:11:07 +08:00
|
|
|
m->name, cnt, owner);
|
2003-11-20 02:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mtx_destroy (mtx *m)
|
|
|
|
{
|
|
|
|
HANDLE tmp = m->h;
|
|
|
|
m->h = NULL;
|
|
|
|
if (tmp)
|
|
|
|
CloseHandle (tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper functions for msleep/wakeup.
|
|
|
|
*/
|
2004-12-30 23:58:27 +08:00
|
|
|
|
2003-11-20 02:49:41 +08:00
|
|
|
static int
|
|
|
|
win_priority (int priority)
|
|
|
|
{
|
2004-07-19 21:12:00 +08:00
|
|
|
int p = (int)((priority) & PRIO_MASK) - PZERO;
|
2003-11-20 02:49:41 +08:00
|
|
|
/* Generating a valid priority value is a bit tricky. The only valid
|
2007-02-23 19:43:48 +08:00
|
|
|
values on NT4 are -15, -2, -1, 0, 1, 2, 15. */
|
2003-11-20 02:49:41 +08:00
|
|
|
switch (p)
|
|
|
|
{
|
|
|
|
case -15: case -14: case -13: case -12: case -11:
|
|
|
|
return THREAD_PRIORITY_IDLE;
|
|
|
|
case -10: case -9: case -8: case -7: case -6:
|
|
|
|
return THREAD_PRIORITY_LOWEST;
|
|
|
|
case -5: case -4: case -3: case -2: case -1:
|
|
|
|
return THREAD_PRIORITY_BELOW_NORMAL;
|
|
|
|
case 0:
|
|
|
|
return THREAD_PRIORITY_NORMAL;
|
|
|
|
case 1: case 2: case 3: case 4: case 5:
|
|
|
|
return THREAD_PRIORITY_ABOVE_NORMAL;
|
|
|
|
case 6: case 7: case 8: case 9: case 10:
|
|
|
|
return THREAD_PRIORITY_HIGHEST;
|
|
|
|
case 11: case 12: case 13: case 14: case 15:
|
|
|
|
return THREAD_PRIORITY_TIME_CRITICAL;
|
|
|
|
}
|
|
|
|
return THREAD_PRIORITY_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets the thread priority, returns the old priority.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
set_priority (int priority)
|
|
|
|
{
|
|
|
|
int old_prio = GetThreadPriority (GetCurrentThread ());
|
2004-02-07 18:47:39 +08:00
|
|
|
if (!SetThreadPriority (GetCurrentThread (), win_priority (priority)))
|
2003-11-20 02:49:41 +08:00
|
|
|
log (LOG_WARNING,
|
2014-03-13 00:00:48 +08:00
|
|
|
"Warning: Setting thread priority to %d failed with error %u\n",
|
2004-02-07 18:47:39 +08:00
|
|
|
win_priority (priority), GetLastError ());
|
2003-11-20 02:49:41 +08:00
|
|
|
return old_prio;
|
|
|
|
}
|
|
|
|
|
2004-02-07 18:47:39 +08:00
|
|
|
/*
|
|
|
|
* Original description from BSD code:
|
|
|
|
*
|
|
|
|
* General sleep call. Suspends the current process until a wakeup is
|
|
|
|
* performed on the specified identifier. The process will then be made
|
|
|
|
* runnable with the specified priority. Sleeps at most timo/hz seconds
|
|
|
|
* (0 means no timeout). If pri includes PCATCH flag, signals are checked
|
|
|
|
* before and after sleeping, else signals are not checked. Returns 0 if
|
|
|
|
* awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
|
|
|
|
* signal needs to be delivered, ERESTART is returned if the current system
|
|
|
|
* call should be restarted if possible, and EINTR is returned if the system
|
|
|
|
* call should be interrupted by the signal (return EINTR).
|
|
|
|
*
|
|
|
|
* The mutex argument is exited before the caller is suspended, and
|
|
|
|
* entered before msleep returns. If priority includes the PDROP
|
|
|
|
* flag the mutex is not entered before returning.
|
|
|
|
*/
|
|
|
|
static HANDLE msleep_glob_evt;
|
2005-04-09 04:31:43 +08:00
|
|
|
|
|
|
|
class msleep_sync_array
|
|
|
|
{
|
|
|
|
enum msleep_action {
|
|
|
|
MSLEEP_ENTER = 0,
|
|
|
|
MSLEEP_LEAVE,
|
|
|
|
MSLEEP_WAKEUP
|
|
|
|
};
|
|
|
|
|
|
|
|
CRITICAL_SECTION cs;
|
2017-06-20 19:09:07 +08:00
|
|
|
long cnt;
|
|
|
|
long max_cnt;
|
|
|
|
struct msleep_record {
|
|
|
|
void *ident;
|
|
|
|
HANDLE wakeup_evt;
|
|
|
|
LONG threads;
|
|
|
|
} *a;
|
|
|
|
|
|
|
|
int find_ident (void *ident, msleep_action action)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < cnt; ++i)
|
|
|
|
if (a[i].ident == ident)
|
|
|
|
return i;
|
|
|
|
if (i >= max_cnt)
|
|
|
|
panic ("ident %x not found and run out of slots.", ident);
|
|
|
|
if (i >= cnt && action == MSLEEP_LEAVE)
|
|
|
|
panic ("ident %x not found (%d).", ident, action);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE first_entry (int i, void *ident)
|
|
|
|
{
|
|
|
|
debug ("New ident %x, index %d", ident, i);
|
|
|
|
a[i].ident = ident;
|
|
|
|
a[i].wakeup_evt = CreateEvent (NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!a[i].wakeup_evt)
|
|
|
|
panic ("CreateEvent failed: %u", GetLastError ());
|
|
|
|
debug ("i = %d, CreateEvent: %x", i, a[i].wakeup_evt);
|
|
|
|
a[i].threads = 1;
|
|
|
|
++cnt;
|
|
|
|
return a[i].wakeup_evt;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE next_entry (int i)
|
|
|
|
{
|
|
|
|
if (a[i].ident && WaitForSingleObject (a[i].wakeup_evt, 0) != WAIT_OBJECT_0)
|
|
|
|
{
|
|
|
|
++a[i].threads;
|
|
|
|
return a[i].wakeup_evt;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-04-09 04:31:43 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-06-20 19:09:07 +08:00
|
|
|
msleep_sync_array (int count) : cnt (0), max_cnt (count)
|
2005-04-09 04:31:43 +08:00
|
|
|
{
|
|
|
|
InitializeCriticalSection (&cs);
|
2017-06-20 19:09:07 +08:00
|
|
|
if (!(a = new msleep_record[count]))
|
2005-04-09 04:31:43 +08:00
|
|
|
panic ("Allocating msleep records failed: %d", errno);
|
|
|
|
}
|
|
|
|
|
2017-06-20 19:09:07 +08:00
|
|
|
~msleep_sync_array () { delete a; }
|
2014-05-19 17:52:43 +08:00
|
|
|
|
2017-06-20 19:09:07 +08:00
|
|
|
HANDLE enter (void *ident)
|
2005-04-09 04:31:43 +08:00
|
|
|
{
|
2017-06-20 19:09:07 +08:00
|
|
|
HANDLE evt = NULL;
|
|
|
|
while (!evt)
|
2005-04-09 04:31:43 +08:00
|
|
|
{
|
2017-06-20 19:09:07 +08:00
|
|
|
EnterCriticalSection (&cs);
|
|
|
|
int i = find_ident (ident, MSLEEP_ENTER);
|
|
|
|
if (i >= cnt)
|
|
|
|
evt = first_entry (i, ident);
|
|
|
|
else if (!(evt = next_entry (i)))
|
2005-04-09 04:31:43 +08:00
|
|
|
{
|
2017-06-20 19:09:07 +08:00
|
|
|
/* wakeup has been called, so sleep to wait until all
|
|
|
|
formerly waiting threads have left and retry. */
|
|
|
|
LeaveCriticalSection (&cs);
|
|
|
|
Sleep (1L);
|
2005-04-09 04:31:43 +08:00
|
|
|
}
|
|
|
|
}
|
2017-06-20 19:09:07 +08:00
|
|
|
LeaveCriticalSection (&cs);
|
|
|
|
return evt;
|
2005-04-09 04:31:43 +08:00
|
|
|
}
|
|
|
|
|
2017-06-20 19:09:07 +08:00
|
|
|
void leave (void *ident)
|
2005-04-09 04:31:43 +08:00
|
|
|
{
|
2017-06-20 19:09:07 +08:00
|
|
|
EnterCriticalSection (&cs);
|
|
|
|
int i = find_ident (ident, MSLEEP_LEAVE);
|
|
|
|
if (--a[i].threads == 0)
|
|
|
|
{
|
|
|
|
debug ("i = %d, CloseEvent: %x", i, a[i].wakeup_evt);
|
|
|
|
CloseHandle (a[i].wakeup_evt);
|
|
|
|
a[i].ident = NULL;
|
|
|
|
--cnt;
|
|
|
|
if (i < cnt)
|
|
|
|
a[i] = a[cnt];
|
|
|
|
}
|
|
|
|
LeaveCriticalSection (&cs);
|
2005-04-09 04:31:43 +08:00
|
|
|
}
|
|
|
|
|
2017-06-20 19:09:07 +08:00
|
|
|
void wakeup (void *ident)
|
2005-04-09 04:31:43 +08:00
|
|
|
{
|
2017-06-20 19:09:07 +08:00
|
|
|
EnterCriticalSection (&cs);
|
|
|
|
int i = find_ident (ident, MSLEEP_WAKEUP);
|
|
|
|
if (i < cnt && a[i].ident)
|
|
|
|
SetEvent (a[i].wakeup_evt);
|
|
|
|
LeaveCriticalSection (&cs);
|
2005-04-09 04:31:43 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static msleep_sync_array *msleep_sync;
|
2004-02-07 18:47:39 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
msleep_init (void)
|
|
|
|
{
|
2017-06-20 19:09:07 +08:00
|
|
|
extern struct msginfo msginfo;
|
|
|
|
extern struct seminfo seminfo;
|
|
|
|
|
2004-02-07 18:47:39 +08:00
|
|
|
msleep_glob_evt = CreateEvent (NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!msleep_glob_evt)
|
2014-03-13 00:00:48 +08:00
|
|
|
panic ("CreateEvent in msleep_init failed: %u", GetLastError ());
|
2017-06-20 19:09:07 +08:00
|
|
|
int32_t msgmni = support_msgqueues ? msginfo.msgmni : 0;
|
|
|
|
int32_t semmni = support_semaphores ? seminfo.semmni : 0;
|
|
|
|
TUNABLE_INT_FETCH ("kern.ipc.msgmni", &msgmni);
|
|
|
|
TUNABLE_INT_FETCH ("kern.ipc.semmni", &semmni);
|
|
|
|
debug ("Try allocating msgmni (%d) + semmni (%d) msleep records",
|
|
|
|
msgmni, semmni);
|
|
|
|
msleep_sync = new msleep_sync_array (msgmni + semmni);
|
2005-04-09 04:31:43 +08:00
|
|
|
if (!msleep_sync)
|
2005-04-06 19:11:07 +08:00
|
|
|
panic ("Allocating msleep records in msleep_init failed: %d", errno);
|
2004-02-07 18:47:39 +08:00
|
|
|
}
|
|
|
|
|
2003-11-20 02:49:41 +08:00
|
|
|
int
|
2017-06-20 19:09:07 +08:00
|
|
|
_msleep (void *ident, struct mtx *mtx, int priority,
|
2003-11-20 02:49:41 +08:00
|
|
|
const char *wmesg, int timo, struct thread *td)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
2004-12-30 23:58:27 +08:00
|
|
|
|
2017-06-20 19:09:07 +08:00
|
|
|
HANDLE evt = msleep_sync->enter (ident);
|
2004-12-30 23:58:27 +08:00
|
|
|
|
2003-11-20 02:49:41 +08:00
|
|
|
if (mtx)
|
|
|
|
mtx_unlock (mtx);
|
|
|
|
int old_priority = set_priority (priority);
|
2004-02-07 18:47:39 +08:00
|
|
|
HANDLE obj[4] =
|
|
|
|
{
|
2005-04-09 04:31:43 +08:00
|
|
|
evt,
|
2004-02-07 18:47:39 +08:00
|
|
|
msleep_glob_evt,
|
|
|
|
td->client->handle (),
|
2015-01-20 01:42:38 +08:00
|
|
|
td->ipcblk->signal_arrived
|
2004-02-07 18:47:39 +08:00
|
|
|
};
|
2004-02-07 00:25:58 +08:00
|
|
|
/* PCATCH handling. If PCATCH is given and signal_arrived is a valid
|
|
|
|
handle, then it's used in the WaitFor call and EINTR is returned. */
|
|
|
|
int obj_cnt = 3;
|
2015-01-20 01:42:38 +08:00
|
|
|
if ((priority & PCATCH) && obj[3])
|
2004-02-07 00:25:58 +08:00
|
|
|
obj_cnt = 4;
|
|
|
|
switch (WaitForMultipleObjects (obj_cnt, obj, FALSE, timo ?: INFINITE))
|
2003-11-20 02:49:41 +08:00
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0: /* wakeup() has been called. */
|
|
|
|
ret = 0;
|
2005-04-09 04:31:43 +08:00
|
|
|
debug ("msleep wakeup called for %d", td->td_proc->winpid);
|
2003-11-20 02:49:41 +08:00
|
|
|
break;
|
2004-02-07 18:47:39 +08:00
|
|
|
case WAIT_OBJECT_0 + 1: /* Shutdown event (triggered by wakeup_all). */
|
2003-11-20 02:49:41 +08:00
|
|
|
priority |= PDROP;
|
|
|
|
/*FALLTHRU*/
|
2004-02-07 18:47:39 +08:00
|
|
|
case WAIT_OBJECT_0 + 2: /* The dependent process has exited. */
|
2005-04-09 04:31:43 +08:00
|
|
|
debug ("msleep process exit or shutdown for %d", td->td_proc->winpid);
|
2003-11-20 02:49:41 +08:00
|
|
|
ret = EIDRM;
|
|
|
|
break;
|
2004-02-07 00:25:58 +08:00
|
|
|
case WAIT_OBJECT_0 + 3: /* Signal for calling process arrived. */
|
2005-04-09 04:31:43 +08:00
|
|
|
debug ("msleep process got signal for %d", td->td_proc->winpid);
|
2004-02-07 00:25:58 +08:00
|
|
|
ret = EINTR;
|
|
|
|
break;
|
2003-11-20 02:49:41 +08:00
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
ret = EWOULDBLOCK;
|
|
|
|
break;
|
|
|
|
default:
|
2005-04-06 19:11:07 +08:00
|
|
|
/* There's a chance that a process has been terminated before
|
|
|
|
WaitForMultipleObjects has been called. In this case the handles
|
|
|
|
might be invalid. The error code returned is ERROR_INVALID_HANDLE.
|
|
|
|
Since we can trust the values of these handles otherwise, we
|
|
|
|
treat an ERROR_INVALID_HANDLE as a normal process termination and
|
|
|
|
hope for the best. */
|
|
|
|
if (GetLastError () != ERROR_INVALID_HANDLE)
|
2014-03-13 00:00:48 +08:00
|
|
|
panic ("wait in msleep (%s) failed, %u", wmesg, GetLastError ());
|
|
|
|
debug ("wait in msleep (%s) failed for %d, %u", wmesg,
|
2008-02-07 06:30:38 +08:00
|
|
|
td->td_proc->winpid, GetLastError ());
|
2005-04-06 19:11:07 +08:00
|
|
|
ret = EIDRM;
|
2003-11-20 02:49:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2004-12-30 23:58:27 +08:00
|
|
|
|
2017-06-20 19:09:07 +08:00
|
|
|
msleep_sync->leave (ident);
|
|
|
|
|
2003-11-20 02:49:41 +08:00
|
|
|
set_priority (old_priority);
|
2004-12-30 23:58:27 +08:00
|
|
|
|
2004-07-30 22:48:19 +08:00
|
|
|
if (mtx && !(priority & PDROP))
|
2003-11-20 02:49:41 +08:00
|
|
|
mtx_lock (mtx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make all threads sleeping on the specified identifier runnable.
|
|
|
|
*/
|
|
|
|
int
|
2017-06-20 19:09:07 +08:00
|
|
|
wakeup (void *ident)
|
2003-11-20 02:49:41 +08:00
|
|
|
{
|
2017-06-20 19:09:07 +08:00
|
|
|
msleep_sync->wakeup (ident);
|
2003-11-20 02:49:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wakeup all sleeping threads. Only called in the context of cygserver
|
|
|
|
* shutdown.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
wakeup_all (void)
|
|
|
|
{
|
2005-04-09 04:31:43 +08:00
|
|
|
SetEvent (msleep_glob_evt);
|
2003-11-20 02:49:41 +08:00
|
|
|
}
|
|
|
|
#endif /* __OUTSIDE_CYGWIN__ */
|