Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * A type which wraps a semaphore |
| 3 | * |
| 4 | * semaphore.c |
| 5 | * |
Richard Oudkerk | 3e268aa | 2012-04-30 12:13:55 +0100 | [diff] [blame] | 6 | * Copyright (c) 2006-2008, R Oudkerk |
| 7 | * Licensed to PSF under a Contributor Agreement. |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "multiprocessing.h" |
| 11 | |
| 12 | enum { RECURSIVE_MUTEX, SEMAPHORE }; |
| 13 | |
| 14 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 15 | PyObject_HEAD |
| 16 | SEM_HANDLE handle; |
| 17 | long last_tid; |
| 18 | int count; |
| 19 | int maxvalue; |
| 20 | int kind; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 21 | } SemLockObject; |
| 22 | |
| 23 | #define ISMINE(o) (o->count > 0 && PyThread_get_thread_ident() == o->last_tid) |
| 24 | |
| 25 | |
| 26 | #ifdef MS_WINDOWS |
| 27 | |
| 28 | /* |
| 29 | * Windows definitions |
| 30 | */ |
| 31 | |
| 32 | #define SEM_FAILED NULL |
| 33 | |
| 34 | #define SEM_CLEAR_ERROR() SetLastError(0) |
| 35 | #define SEM_GET_LAST_ERROR() GetLastError() |
| 36 | #define SEM_CREATE(name, val, max) CreateSemaphore(NULL, val, max, NULL) |
| 37 | #define SEM_CLOSE(sem) (CloseHandle(sem) ? 0 : -1) |
| 38 | #define SEM_GETVALUE(sem, pval) _GetSemaphoreValue(sem, pval) |
| 39 | #define SEM_UNLINK(name) 0 |
| 40 | |
| 41 | static int |
| 42 | _GetSemaphoreValue(HANDLE handle, long *value) |
| 43 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | long previous; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 45 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | switch (WaitForSingleObject(handle, 0)) { |
| 47 | case WAIT_OBJECT_0: |
| 48 | if (!ReleaseSemaphore(handle, 1, &previous)) |
| 49 | return MP_STANDARD_ERROR; |
| 50 | *value = previous + 1; |
| 51 | return 0; |
| 52 | case WAIT_TIMEOUT: |
| 53 | *value = 0; |
| 54 | return 0; |
| 55 | default: |
| 56 | return MP_STANDARD_ERROR; |
| 57 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static PyObject * |
| 61 | semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds) |
| 62 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | int blocking = 1; |
| 64 | double timeout; |
| 65 | PyObject *timeout_obj = Py_None; |
Antoine Pitrou | 6dd381e | 2011-11-21 21:26:56 +0100 | [diff] [blame] | 66 | DWORD res, full_msecs, nhandles; |
| 67 | HANDLE handles[2], sigint_event; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 68 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | static char *kwlist[] = {"block", "timeout", NULL}; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 70 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist, |
| 72 | &blocking, &timeout_obj)) |
| 73 | return NULL; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 74 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | /* calculate timeout */ |
| 76 | if (!blocking) { |
| 77 | full_msecs = 0; |
| 78 | } else if (timeout_obj == Py_None) { |
| 79 | full_msecs = INFINITE; |
| 80 | } else { |
| 81 | timeout = PyFloat_AsDouble(timeout_obj); |
| 82 | if (PyErr_Occurred()) |
| 83 | return NULL; |
| 84 | timeout *= 1000.0; /* convert to millisecs */ |
| 85 | if (timeout < 0.0) { |
| 86 | timeout = 0.0; |
| 87 | } else if (timeout >= 0.5 * INFINITE) { /* 25 days */ |
| 88 | PyErr_SetString(PyExc_OverflowError, |
| 89 | "timeout is too large"); |
| 90 | return NULL; |
| 91 | } |
| 92 | full_msecs = (DWORD)(timeout + 0.5); |
| 93 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 94 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | /* check whether we already own the lock */ |
| 96 | if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) { |
| 97 | ++self->count; |
| 98 | Py_RETURN_TRUE; |
| 99 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 100 | |
Antoine Pitrou | 6dd381e | 2011-11-21 21:26:56 +0100 | [diff] [blame] | 101 | /* check whether we can acquire without releasing the GIL and blocking */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | if (WaitForSingleObject(self->handle, 0) == WAIT_OBJECT_0) { |
| 103 | self->last_tid = GetCurrentThreadId(); |
| 104 | ++self->count; |
| 105 | Py_RETURN_TRUE; |
| 106 | } |
| 107 | |
Antoine Pitrou | 6dd381e | 2011-11-21 21:26:56 +0100 | [diff] [blame] | 108 | /* prepare list of handles */ |
| 109 | nhandles = 0; |
| 110 | handles[nhandles++] = self->handle; |
| 111 | if (_PyOS_IsMainThread()) { |
| 112 | sigint_event = _PyOS_SigintEvent(); |
| 113 | assert(sigint_event != NULL); |
| 114 | handles[nhandles++] = sigint_event; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Antoine Pitrou | 6dd381e | 2011-11-21 21:26:56 +0100 | [diff] [blame] | 117 | /* do the wait */ |
| 118 | Py_BEGIN_ALLOW_THREADS |
| 119 | if (sigint_event != NULL) |
| 120 | ResetEvent(sigint_event); |
| 121 | res = WaitForMultipleObjects(nhandles, handles, FALSE, full_msecs); |
| 122 | Py_END_ALLOW_THREADS |
| 123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | /* handle result */ |
| 125 | switch (res) { |
| 126 | case WAIT_TIMEOUT: |
| 127 | Py_RETURN_FALSE; |
Antoine Pitrou | 6dd381e | 2011-11-21 21:26:56 +0100 | [diff] [blame] | 128 | case WAIT_OBJECT_0 + 0: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | self->last_tid = GetCurrentThreadId(); |
| 130 | ++self->count; |
| 131 | Py_RETURN_TRUE; |
Antoine Pitrou | 6dd381e | 2011-11-21 21:26:56 +0100 | [diff] [blame] | 132 | case WAIT_OBJECT_0 + 1: |
| 133 | errno = EINTR; |
| 134 | return PyErr_SetFromErrno(PyExc_IOError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | case WAIT_FAILED: |
| 136 | return PyErr_SetFromWindowsErr(0); |
| 137 | default: |
| 138 | PyErr_Format(PyExc_RuntimeError, "WaitForSingleObject() or " |
| 139 | "WaitForMultipleObjects() gave unrecognized " |
| 140 | "value %d", res); |
| 141 | return NULL; |
| 142 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static PyObject * |
| 146 | semlock_release(SemLockObject *self, PyObject *args) |
| 147 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | if (self->kind == RECURSIVE_MUTEX) { |
| 149 | if (!ISMINE(self)) { |
| 150 | PyErr_SetString(PyExc_AssertionError, "attempt to " |
| 151 | "release recursive lock not owned " |
| 152 | "by thread"); |
| 153 | return NULL; |
| 154 | } |
| 155 | if (self->count > 1) { |
| 156 | --self->count; |
| 157 | Py_RETURN_NONE; |
| 158 | } |
| 159 | assert(self->count == 1); |
| 160 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | if (!ReleaseSemaphore(self->handle, 1, NULL)) { |
| 163 | if (GetLastError() == ERROR_TOO_MANY_POSTS) { |
| 164 | PyErr_SetString(PyExc_ValueError, "semaphore or lock " |
| 165 | "released too many times"); |
| 166 | return NULL; |
| 167 | } else { |
| 168 | return PyErr_SetFromWindowsErr(0); |
| 169 | } |
| 170 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | --self->count; |
| 173 | Py_RETURN_NONE; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | #else /* !MS_WINDOWS */ |
| 177 | |
| 178 | /* |
| 179 | * Unix definitions |
| 180 | */ |
| 181 | |
| 182 | #define SEM_CLEAR_ERROR() |
| 183 | #define SEM_GET_LAST_ERROR() 0 |
| 184 | #define SEM_CREATE(name, val, max) sem_open(name, O_CREAT | O_EXCL, 0600, val) |
| 185 | #define SEM_CLOSE(sem) sem_close(sem) |
| 186 | #define SEM_GETVALUE(sem, pval) sem_getvalue(sem, pval) |
| 187 | #define SEM_UNLINK(name) sem_unlink(name) |
| 188 | |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 189 | #ifndef HAVE_SEM_UNLINK |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 190 | # define sem_unlink(name) 0 |
| 191 | #endif |
| 192 | |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 193 | #ifndef HAVE_SEM_TIMEDWAIT |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 194 | # define sem_timedwait(sem,deadline) sem_timedwait_save(sem,deadline,_save) |
| 195 | |
Richard Oudkerk | 8fb9f4c | 2012-10-07 18:08:47 +0100 | [diff] [blame] | 196 | static int |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 197 | sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save) |
| 198 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | int res; |
| 200 | unsigned long delay, difference; |
| 201 | struct timeval now, tvdeadline, tvdelay; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 | errno = 0; |
| 204 | tvdeadline.tv_sec = deadline->tv_sec; |
| 205 | tvdeadline.tv_usec = deadline->tv_nsec / 1000; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | for (delay = 0 ; ; delay += 1000) { |
| 208 | /* poll */ |
| 209 | if (sem_trywait(sem) == 0) |
| 210 | return 0; |
| 211 | else if (errno != EAGAIN) |
| 212 | return MP_STANDARD_ERROR; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | /* get current time */ |
| 215 | if (gettimeofday(&now, NULL) < 0) |
| 216 | return MP_STANDARD_ERROR; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | /* check for timeout */ |
| 219 | if (tvdeadline.tv_sec < now.tv_sec || |
| 220 | (tvdeadline.tv_sec == now.tv_sec && |
| 221 | tvdeadline.tv_usec <= now.tv_usec)) { |
| 222 | errno = ETIMEDOUT; |
| 223 | return MP_STANDARD_ERROR; |
| 224 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | /* calculate how much time is left */ |
| 227 | difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 + |
| 228 | (tvdeadline.tv_usec - now.tv_usec); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | /* check delay not too long -- maximum is 20 msecs */ |
| 231 | if (delay > 20000) |
| 232 | delay = 20000; |
| 233 | if (delay > difference) |
| 234 | delay = difference; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | /* sleep */ |
| 237 | tvdelay.tv_sec = delay / 1000000; |
| 238 | tvdelay.tv_usec = delay % 1000000; |
| 239 | if (select(0, NULL, NULL, NULL, &tvdelay) < 0) |
| 240 | return MP_STANDARD_ERROR; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | /* check for signals */ |
| 243 | Py_BLOCK_THREADS |
| 244 | res = PyErr_CheckSignals(); |
| 245 | Py_UNBLOCK_THREADS |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | if (res) { |
| 248 | errno = EINTR; |
| 249 | return MP_EXCEPTION_HAS_BEEN_SET; |
| 250 | } |
| 251 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | #endif /* !HAVE_SEM_TIMEDWAIT */ |
| 255 | |
| 256 | static PyObject * |
| 257 | semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds) |
| 258 | { |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 259 | int blocking = 1, res, err = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | double timeout; |
| 261 | PyObject *timeout_obj = Py_None; |
| 262 | struct timespec deadline = {0}; |
| 263 | struct timeval now; |
| 264 | long sec, nsec; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | static char *kwlist[] = {"block", "timeout", NULL}; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist, |
| 269 | &blocking, &timeout_obj)) |
| 270 | return NULL; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) { |
| 273 | ++self->count; |
| 274 | Py_RETURN_TRUE; |
| 275 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | if (timeout_obj != Py_None) { |
| 278 | timeout = PyFloat_AsDouble(timeout_obj); |
| 279 | if (PyErr_Occurred()) |
| 280 | return NULL; |
| 281 | if (timeout < 0.0) |
| 282 | timeout = 0.0; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | if (gettimeofday(&now, NULL) < 0) { |
| 285 | PyErr_SetFromErrno(PyExc_OSError); |
| 286 | return NULL; |
| 287 | } |
| 288 | sec = (long) timeout; |
| 289 | nsec = (long) (1e9 * (timeout - sec) + 0.5); |
| 290 | deadline.tv_sec = now.tv_sec + sec; |
| 291 | deadline.tv_nsec = now.tv_usec * 1000 + nsec; |
| 292 | deadline.tv_sec += (deadline.tv_nsec / 1000000000); |
| 293 | deadline.tv_nsec %= 1000000000; |
| 294 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | do { |
| 297 | Py_BEGIN_ALLOW_THREADS |
| 298 | if (blocking && timeout_obj == Py_None) |
| 299 | res = sem_wait(self->handle); |
| 300 | else if (!blocking) |
| 301 | res = sem_trywait(self->handle); |
| 302 | else |
| 303 | res = sem_timedwait(self->handle, &deadline); |
| 304 | Py_END_ALLOW_THREADS |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 305 | err = errno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | if (res == MP_EXCEPTION_HAS_BEEN_SET) |
| 307 | break; |
| 308 | } while (res < 0 && errno == EINTR && !PyErr_CheckSignals()); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | if (res < 0) { |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 311 | errno = err; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | if (errno == EAGAIN || errno == ETIMEDOUT) |
| 313 | Py_RETURN_FALSE; |
| 314 | else if (errno == EINTR) |
| 315 | return NULL; |
| 316 | else |
| 317 | return PyErr_SetFromErrno(PyExc_OSError); |
| 318 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | ++self->count; |
| 321 | self->last_tid = PyThread_get_thread_ident(); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | Py_RETURN_TRUE; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static PyObject * |
| 327 | semlock_release(SemLockObject *self, PyObject *args) |
| 328 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | if (self->kind == RECURSIVE_MUTEX) { |
| 330 | if (!ISMINE(self)) { |
| 331 | PyErr_SetString(PyExc_AssertionError, "attempt to " |
| 332 | "release recursive lock not owned " |
| 333 | "by thread"); |
| 334 | return NULL; |
| 335 | } |
| 336 | if (self->count > 1) { |
| 337 | --self->count; |
| 338 | Py_RETURN_NONE; |
| 339 | } |
| 340 | assert(self->count == 1); |
| 341 | } else { |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 342 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | /* We will only check properly the maxvalue == 1 case */ |
| 344 | if (self->maxvalue == 1) { |
| 345 | /* make sure that already locked */ |
| 346 | if (sem_trywait(self->handle) < 0) { |
| 347 | if (errno != EAGAIN) { |
| 348 | PyErr_SetFromErrno(PyExc_OSError); |
| 349 | return NULL; |
| 350 | } |
| 351 | /* it is already locked as expected */ |
| 352 | } else { |
| 353 | /* it was not locked so undo wait and raise */ |
| 354 | if (sem_post(self->handle) < 0) { |
| 355 | PyErr_SetFromErrno(PyExc_OSError); |
| 356 | return NULL; |
| 357 | } |
| 358 | PyErr_SetString(PyExc_ValueError, "semaphore " |
| 359 | "or lock released too many " |
| 360 | "times"); |
| 361 | return NULL; |
| 362 | } |
| 363 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 364 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | int sval; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | /* This check is not an absolute guarantee that the semaphore |
| 368 | does not rise above maxvalue. */ |
| 369 | if (sem_getvalue(self->handle, &sval) < 0) { |
| 370 | return PyErr_SetFromErrno(PyExc_OSError); |
| 371 | } else if (sval >= self->maxvalue) { |
| 372 | PyErr_SetString(PyExc_ValueError, "semaphore or lock " |
| 373 | "released too many times"); |
| 374 | return NULL; |
| 375 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 376 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | if (sem_post(self->handle) < 0) |
| 380 | return PyErr_SetFromErrno(PyExc_OSError); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | --self->count; |
| 383 | Py_RETURN_NONE; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | #endif /* !MS_WINDOWS */ |
| 387 | |
| 388 | /* |
| 389 | * All platforms |
| 390 | */ |
| 391 | |
| 392 | static PyObject * |
| 393 | newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue) |
| 394 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | SemLockObject *self; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | self = PyObject_New(SemLockObject, type); |
| 398 | if (!self) |
| 399 | return NULL; |
| 400 | self->handle = handle; |
| 401 | self->kind = kind; |
| 402 | self->count = 0; |
| 403 | self->last_tid = 0; |
| 404 | self->maxvalue = maxvalue; |
| 405 | return (PyObject*)self; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static PyObject * |
| 409 | semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | char buffer[256]; |
| 412 | SEM_HANDLE handle = SEM_FAILED; |
| 413 | int kind, maxvalue, value; |
| 414 | PyObject *result; |
| 415 | static char *kwlist[] = {"kind", "value", "maxvalue", NULL}; |
| 416 | static int counter = 0; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "iii", kwlist, |
| 419 | &kind, &value, &maxvalue)) |
| 420 | return NULL; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | if (kind != RECURSIVE_MUTEX && kind != SEMAPHORE) { |
| 423 | PyErr_SetString(PyExc_ValueError, "unrecognized kind"); |
| 424 | return NULL; |
| 425 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 426 | |
Georg Brandl | c7f4af4 | 2010-10-18 05:06:18 +0000 | [diff] [blame] | 427 | PyOS_snprintf(buffer, sizeof(buffer), "/mp%ld-%d", (long)getpid(), counter++); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | SEM_CLEAR_ERROR(); |
| 430 | handle = SEM_CREATE(buffer, value, maxvalue); |
| 431 | /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */ |
| 432 | if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0) |
| 433 | goto failure; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 434 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | if (SEM_UNLINK(buffer) < 0) |
| 436 | goto failure; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | result = newsemlockobject(type, handle, kind, maxvalue); |
| 439 | if (!result) |
| 440 | goto failure; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | return result; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 443 | |
| 444 | failure: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | if (handle != SEM_FAILED) |
| 446 | SEM_CLOSE(handle); |
Richard Oudkerk | 8fb9f4c | 2012-10-07 18:08:47 +0100 | [diff] [blame] | 447 | _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | return NULL; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static PyObject * |
| 452 | semlock_rebuild(PyTypeObject *type, PyObject *args) |
| 453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | SEM_HANDLE handle; |
| 455 | int kind, maxvalue; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | if (!PyArg_ParseTuple(args, F_SEM_HANDLE "ii", |
| 458 | &handle, &kind, &maxvalue)) |
| 459 | return NULL; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | return newsemlockobject(type, handle, kind, maxvalue); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static void |
| 465 | semlock_dealloc(SemLockObject* self) |
| 466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | if (self->handle != SEM_FAILED) |
| 468 | SEM_CLOSE(self->handle); |
| 469 | PyObject_Del(self); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static PyObject * |
| 473 | semlock_count(SemLockObject *self) |
| 474 | { |
Antoine Pitrou | 2341f9b | 2011-05-09 20:55:03 +0200 | [diff] [blame] | 475 | return PyLong_FromLong((long)self->count); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static PyObject * |
| 479 | semlock_ismine(SemLockObject *self) |
| 480 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | /* only makes sense for a lock */ |
| 482 | return PyBool_FromLong(ISMINE(self)); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static PyObject * |
| 486 | semlock_getvalue(SemLockObject *self) |
| 487 | { |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 488 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | PyErr_SetNone(PyExc_NotImplementedError); |
| 490 | return NULL; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 491 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | int sval; |
| 493 | if (SEM_GETVALUE(self->handle, &sval) < 0) |
Richard Oudkerk | 8fb9f4c | 2012-10-07 18:08:47 +0100 | [diff] [blame] | 494 | return _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | /* some posix implementations use negative numbers to indicate |
| 496 | the number of waiting threads */ |
| 497 | if (sval < 0) |
| 498 | sval = 0; |
Antoine Pitrou | 2341f9b | 2011-05-09 20:55:03 +0200 | [diff] [blame] | 499 | return PyLong_FromLong((long)sval); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 500 | #endif |
| 501 | } |
| 502 | |
| 503 | static PyObject * |
| 504 | semlock_iszero(SemLockObject *self) |
| 505 | { |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 506 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | if (sem_trywait(self->handle) < 0) { |
| 508 | if (errno == EAGAIN) |
| 509 | Py_RETURN_TRUE; |
Richard Oudkerk | 8fb9f4c | 2012-10-07 18:08:47 +0100 | [diff] [blame] | 510 | return _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 511 | } else { |
| 512 | if (sem_post(self->handle) < 0) |
Richard Oudkerk | 8fb9f4c | 2012-10-07 18:08:47 +0100 | [diff] [blame] | 513 | return _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | Py_RETURN_FALSE; |
| 515 | } |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 516 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | int sval; |
| 518 | if (SEM_GETVALUE(self->handle, &sval) < 0) |
Richard Oudkerk | 8fb9f4c | 2012-10-07 18:08:47 +0100 | [diff] [blame] | 519 | return _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | return PyBool_FromLong((long)sval == 0); |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 521 | #endif |
| 522 | } |
| 523 | |
| 524 | static PyObject * |
| 525 | semlock_afterfork(SemLockObject *self) |
| 526 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 527 | self->count = 0; |
| 528 | Py_RETURN_NONE; |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* |
| 532 | * Semaphore methods |
| 533 | */ |
| 534 | |
| 535 | static PyMethodDef semlock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | {"acquire", (PyCFunction)semlock_acquire, METH_VARARGS | METH_KEYWORDS, |
| 537 | "acquire the semaphore/lock"}, |
| 538 | {"release", (PyCFunction)semlock_release, METH_NOARGS, |
| 539 | "release the semaphore/lock"}, |
Jesse Noller | f8d0085 | 2009-03-31 03:25:07 +0000 | [diff] [blame] | 540 | {"__enter__", (PyCFunction)semlock_acquire, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | "enter the semaphore/lock"}, |
| 542 | {"__exit__", (PyCFunction)semlock_release, METH_VARARGS, |
| 543 | "exit the semaphore/lock"}, |
| 544 | {"_count", (PyCFunction)semlock_count, METH_NOARGS, |
| 545 | "num of `acquire()`s minus num of `release()`s for this process"}, |
| 546 | {"_is_mine", (PyCFunction)semlock_ismine, METH_NOARGS, |
| 547 | "whether the lock is owned by this thread"}, |
| 548 | {"_get_value", (PyCFunction)semlock_getvalue, METH_NOARGS, |
| 549 | "get the value of the semaphore"}, |
| 550 | {"_is_zero", (PyCFunction)semlock_iszero, METH_NOARGS, |
| 551 | "returns whether semaphore has value zero"}, |
| 552 | {"_rebuild", (PyCFunction)semlock_rebuild, METH_VARARGS | METH_CLASS, |
| 553 | ""}, |
| 554 | {"_after_fork", (PyCFunction)semlock_afterfork, METH_NOARGS, |
| 555 | "rezero the net acquisition count after fork()"}, |
| 556 | {NULL} |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | /* |
| 560 | * Member table |
| 561 | */ |
| 562 | |
| 563 | static PyMemberDef semlock_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | {"handle", T_SEM_HANDLE, offsetof(SemLockObject, handle), READONLY, |
| 565 | ""}, |
| 566 | {"kind", T_INT, offsetof(SemLockObject, kind), READONLY, |
| 567 | ""}, |
| 568 | {"maxvalue", T_INT, offsetof(SemLockObject, maxvalue), READONLY, |
| 569 | ""}, |
| 570 | {NULL} |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 571 | }; |
| 572 | |
| 573 | /* |
| 574 | * Semaphore type |
| 575 | */ |
| 576 | |
Richard Oudkerk | 8fb9f4c | 2012-10-07 18:08:47 +0100 | [diff] [blame] | 577 | PyTypeObject _PyMp_SemLockType = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | PyVarObject_HEAD_INIT(NULL, 0) |
| 579 | /* tp_name */ "_multiprocessing.SemLock", |
| 580 | /* tp_basicsize */ sizeof(SemLockObject), |
| 581 | /* tp_itemsize */ 0, |
| 582 | /* tp_dealloc */ (destructor)semlock_dealloc, |
| 583 | /* tp_print */ 0, |
| 584 | /* tp_getattr */ 0, |
| 585 | /* tp_setattr */ 0, |
| 586 | /* tp_reserved */ 0, |
| 587 | /* tp_repr */ 0, |
| 588 | /* tp_as_number */ 0, |
| 589 | /* tp_as_sequence */ 0, |
| 590 | /* tp_as_mapping */ 0, |
| 591 | /* tp_hash */ 0, |
| 592 | /* tp_call */ 0, |
| 593 | /* tp_str */ 0, |
| 594 | /* tp_getattro */ 0, |
| 595 | /* tp_setattro */ 0, |
| 596 | /* tp_as_buffer */ 0, |
| 597 | /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 598 | /* tp_doc */ "Semaphore/Mutex type", |
| 599 | /* tp_traverse */ 0, |
| 600 | /* tp_clear */ 0, |
| 601 | /* tp_richcompare */ 0, |
| 602 | /* tp_weaklistoffset */ 0, |
| 603 | /* tp_iter */ 0, |
| 604 | /* tp_iternext */ 0, |
| 605 | /* tp_methods */ semlock_methods, |
| 606 | /* tp_members */ semlock_members, |
| 607 | /* tp_getset */ 0, |
| 608 | /* tp_base */ 0, |
| 609 | /* tp_dict */ 0, |
| 610 | /* tp_descr_get */ 0, |
| 611 | /* tp_descr_set */ 0, |
| 612 | /* tp_dictoffset */ 0, |
| 613 | /* tp_init */ 0, |
| 614 | /* tp_alloc */ 0, |
| 615 | /* tp_new */ semlock_new, |
Benjamin Peterson | e711caf | 2008-06-11 16:44:04 +0000 | [diff] [blame] | 616 | }; |