Debian Codegraph @

Modules/_billiard/semaphore.c

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