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 | unsigned long last_tid; |
18 | int count; |
19 | int maxvalue; |
20 | int kind; |
21 | char *name; |
22 | } SemLockObject; |
23 | |
24 | /*[python input] |
25 | class SEM_HANDLE_converter(CConverter): |
26 | type = "SEM_HANDLE" |
27 | format_unit = '"F_SEM_HANDLE"' |
28 | |
29 | [python start generated code]*/ |
30 | /*[python end generated code: output=da39a3ee5e6b4b0d input=3e0ad43e482d8716]*/ |
31 | |
32 | /*[clinic input] |
33 | module _multiprocessing |
34 | class _multiprocessing.SemLock "SemLockObject *" "&_PyMp_SemLockType" |
35 | [clinic start generated code]*/ |
36 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=935fb41b7d032599]*/ |
37 | |
38 | #include "clinic/semaphore.c.h" |
39 | |
40 | #define ISMINE(o) (o->count > 0 && PyThread_get_thread_ident() == o->last_tid) |
41 | |
42 | |
43 | #ifdef MS_WINDOWS |
44 | |
45 | /* |
46 | * Windows definitions |
47 | */ |
48 | |
49 | #define SEM_FAILED NULL |
50 | |
51 | #define SEM_CLEAR_ERROR() SetLastError(0) |
52 | #define SEM_GET_LAST_ERROR() GetLastError() |
53 | #define SEM_CREATE(name, val, max) CreateSemaphore(NULL, val, max, NULL) |
54 | #define SEM_CLOSE(sem) (CloseHandle(sem) ? 0 : -1) |
55 | #define SEM_GETVALUE(sem, pval) _GetSemaphoreValue(sem, pval) |
56 | #define SEM_UNLINK(name) 0 |
57 | |
58 | static int |
59 | _GetSemaphoreValue(HANDLE handle, long *value) |
60 | { |
61 | long previous; |
62 | |
63 | switch (WaitForSingleObjectEx(handle, 0, FALSE)) { |
64 | case WAIT_OBJECT_0: |
65 | if (!ReleaseSemaphore(handle, 1, &previous)) |
66 | return MP_STANDARD_ERROR; |
67 | *value = previous + 1; |
68 | return 0; |
69 | case WAIT_TIMEOUT: |
70 | *value = 0; |
71 | return 0; |
72 | default: |
73 | return MP_STANDARD_ERROR; |
74 | } |
75 | } |
76 | |
77 | /*[clinic input] |
78 | _multiprocessing.SemLock.acquire |
79 | |
80 | block as blocking: bool(accept={int}) = True |
81 | timeout as timeout_obj: object = None |
82 | |
83 | Acquire the semaphore/lock. |
84 | [clinic start generated code]*/ |
85 | |
86 | static PyObject * |
87 | _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking, |
88 | PyObject *timeout_obj) |
89 | /*[clinic end generated code: output=f9998f0b6b0b0872 input=86f05662cf753eb4]*/ |
90 | { |
91 | double timeout; |
92 | DWORD res, full_msecs, nhandles; |
93 | HANDLE handles[2], sigint_event; |
94 | |
95 | /* calculate timeout */ |
96 | if (!blocking) { |
97 | full_msecs = 0; |
98 | } else if (timeout_obj == Py_None) { |
99 | full_msecs = INFINITE; |
100 | } else { |
101 | timeout = PyFloat_AsDouble(timeout_obj); |
102 | if (PyErr_Occurred()) |
103 | return NULL; |
104 | timeout *= 1000.0; /* convert to millisecs */ |
105 | if (timeout < 0.0) { |
106 | timeout = 0.0; |
107 | } else if (timeout >= 0.5 * INFINITE) { /* 25 days */ |
108 | PyErr_SetString(PyExc_OverflowError, |
109 | "timeout is too large" ); |
110 | return NULL; |
111 | } |
112 | full_msecs = (DWORD)(timeout + 0.5); |
113 | } |
114 | |
115 | /* check whether we already own the lock */ |
116 | if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) { |
117 | ++self->count; |
118 | Py_RETURN_TRUE; |
119 | } |
120 | |
121 | /* check whether we can acquire without releasing the GIL and blocking */ |
122 | if (WaitForSingleObjectEx(self->handle, 0, FALSE) == WAIT_OBJECT_0) { |
123 | self->last_tid = GetCurrentThreadId(); |
124 | ++self->count; |
125 | Py_RETURN_TRUE; |
126 | } |
127 | |
128 | /* prepare list of handles */ |
129 | nhandles = 0; |
130 | handles[nhandles++] = self->handle; |
131 | if (_PyOS_IsMainThread()) { |
132 | sigint_event = _PyOS_SigintEvent(); |
133 | assert(sigint_event != NULL); |
134 | handles[nhandles++] = sigint_event; |
135 | } |
136 | else { |
137 | sigint_event = NULL; |
138 | } |
139 | |
140 | /* do the wait */ |
141 | Py_BEGIN_ALLOW_THREADS |
142 | if (sigint_event != NULL) |
143 | ResetEvent(sigint_event); |
144 | res = WaitForMultipleObjectsEx(nhandles, handles, FALSE, full_msecs, FALSE); |
145 | Py_END_ALLOW_THREADS |
146 | |
147 | /* handle result */ |
148 | switch (res) { |
149 | case WAIT_TIMEOUT: |
150 | Py_RETURN_FALSE; |
151 | case WAIT_OBJECT_0 + 0: |
152 | self->last_tid = GetCurrentThreadId(); |
153 | ++self->count; |
154 | Py_RETURN_TRUE; |
155 | case WAIT_OBJECT_0 + 1: |
156 | errno = EINTR; |
157 | return PyErr_SetFromErrno(PyExc_OSError); |
158 | case WAIT_FAILED: |
159 | return PyErr_SetFromWindowsErr(0); |
160 | default: |
161 | PyErr_Format(PyExc_RuntimeError, "WaitForSingleObject() or " |
162 | "WaitForMultipleObjects() gave unrecognized " |
163 | "value %u" , res); |
164 | return NULL; |
165 | } |
166 | } |
167 | |
168 | /*[clinic input] |
169 | _multiprocessing.SemLock.release |
170 | |
171 | Release the semaphore/lock. |
172 | [clinic start generated code]*/ |
173 | |
174 | static PyObject * |
175 | _multiprocessing_SemLock_release_impl(SemLockObject *self) |
176 | /*[clinic end generated code: output=b22f53ba96b0d1db input=ba7e63a961885d3d]*/ |
177 | { |
178 | if (self->kind == RECURSIVE_MUTEX) { |
179 | if (!ISMINE(self)) { |
180 | PyErr_SetString(PyExc_AssertionError, "attempt to " |
181 | "release recursive lock not owned " |
182 | "by thread" ); |
183 | return NULL; |
184 | } |
185 | if (self->count > 1) { |
186 | --self->count; |
187 | Py_RETURN_NONE; |
188 | } |
189 | assert(self->count == 1); |
190 | } |
191 | |
192 | if (!ReleaseSemaphore(self->handle, 1, NULL)) { |
193 | if (GetLastError() == ERROR_TOO_MANY_POSTS) { |
194 | PyErr_SetString(PyExc_ValueError, "semaphore or lock " |
195 | "released too many times" ); |
196 | return NULL; |
197 | } else { |
198 | return PyErr_SetFromWindowsErr(0); |
199 | } |
200 | } |
201 | |
202 | --self->count; |
203 | Py_RETURN_NONE; |
204 | } |
205 | |
206 | #else /* !MS_WINDOWS */ |
207 | |
208 | /* |
209 | * Unix definitions |
210 | */ |
211 | |
212 | #define SEM_CLEAR_ERROR() |
213 | #define SEM_GET_LAST_ERROR() 0 |
214 | #define SEM_CREATE(name, val, max) sem_open(name, O_CREAT | O_EXCL, 0600, val) |
215 | #define SEM_CLOSE(sem) sem_close(sem) |
216 | #define SEM_GETVALUE(sem, pval) sem_getvalue(sem, pval) |
217 | #define SEM_UNLINK(name) sem_unlink(name) |
218 | |
219 | /* OS X 10.4 defines SEM_FAILED as -1 instead of (sem_t *)-1; this gives |
220 | compiler warnings, and (potentially) undefined behaviour. */ |
221 | #ifdef __APPLE__ |
222 | # undef SEM_FAILED |
223 | # define SEM_FAILED ((sem_t *)-1) |
224 | #endif |
225 | |
226 | #ifndef HAVE_SEM_UNLINK |
227 | # define sem_unlink(name) 0 |
228 | #endif |
229 | |
230 | #ifndef HAVE_SEM_TIMEDWAIT |
231 | # define sem_timedwait(sem,deadline) sem_timedwait_save(sem,deadline,_save) |
232 | |
233 | static int |
234 | sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save) |
235 | { |
236 | int res; |
237 | unsigned long delay, difference; |
238 | struct timeval now, tvdeadline, tvdelay; |
239 | |
240 | errno = 0; |
241 | tvdeadline.tv_sec = deadline->tv_sec; |
242 | tvdeadline.tv_usec = deadline->tv_nsec / 1000; |
243 | |
244 | for (delay = 0 ; ; delay += 1000) { |
245 | /* poll */ |
246 | if (sem_trywait(sem) == 0) |
247 | return 0; |
248 | else if (errno != EAGAIN) |
249 | return MP_STANDARD_ERROR; |
250 | |
251 | /* get current time */ |
252 | if (gettimeofday(&now, NULL) < 0) |
253 | return MP_STANDARD_ERROR; |
254 | |
255 | /* check for timeout */ |
256 | if (tvdeadline.tv_sec < now.tv_sec || |
257 | (tvdeadline.tv_sec == now.tv_sec && |
258 | tvdeadline.tv_usec <= now.tv_usec)) { |
259 | errno = ETIMEDOUT; |
260 | return MP_STANDARD_ERROR; |
261 | } |
262 | |
263 | /* calculate how much time is left */ |
264 | difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 + |
265 | (tvdeadline.tv_usec - now.tv_usec); |
266 | |
267 | /* check delay not too long -- maximum is 20 msecs */ |
268 | if (delay > 20000) |
269 | delay = 20000; |
270 | if (delay > difference) |
271 | delay = difference; |
272 | |
273 | /* sleep */ |
274 | tvdelay.tv_sec = delay / 1000000; |
275 | tvdelay.tv_usec = delay % 1000000; |
276 | if (select(0, NULL, NULL, NULL, &tvdelay) < 0) |
277 | return MP_STANDARD_ERROR; |
278 | |
279 | /* check for signals */ |
280 | Py_BLOCK_THREADS |
281 | res = PyErr_CheckSignals(); |
282 | Py_UNBLOCK_THREADS |
283 | |
284 | if (res) { |
285 | errno = EINTR; |
286 | return MP_EXCEPTION_HAS_BEEN_SET; |
287 | } |
288 | } |
289 | } |
290 | |
291 | #endif /* !HAVE_SEM_TIMEDWAIT */ |
292 | |
293 | /*[clinic input] |
294 | _multiprocessing.SemLock.acquire |
295 | |
296 | block as blocking: bool(accept={int}) = True |
297 | timeout as timeout_obj: object = None |
298 | |
299 | Acquire the semaphore/lock. |
300 | [clinic start generated code]*/ |
301 | |
302 | static PyObject * |
303 | _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking, |
304 | PyObject *timeout_obj) |
305 | /*[clinic end generated code: output=f9998f0b6b0b0872 input=86f05662cf753eb4]*/ |
306 | { |
307 | int res, err = 0; |
308 | struct timespec deadline = {0}; |
309 | |
310 | if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) { |
311 | ++self->count; |
312 | Py_RETURN_TRUE; |
313 | } |
314 | |
315 | int use_deadline = (timeout_obj != Py_None); |
316 | if (use_deadline) { |
317 | double timeout = PyFloat_AsDouble(timeout_obj); |
318 | if (PyErr_Occurred()) { |
319 | return NULL; |
320 | } |
321 | if (timeout < 0.0) { |
322 | timeout = 0.0; |
323 | } |
324 | |
325 | struct timeval now; |
326 | if (gettimeofday(&now, NULL) < 0) { |
327 | PyErr_SetFromErrno(PyExc_OSError); |
328 | return NULL; |
329 | } |
330 | long sec = (long) timeout; |
331 | long nsec = (long) (1e9 * (timeout - sec) + 0.5); |
332 | deadline.tv_sec = now.tv_sec + sec; |
333 | deadline.tv_nsec = now.tv_usec * 1000 + nsec; |
334 | deadline.tv_sec += (deadline.tv_nsec / 1000000000); |
335 | deadline.tv_nsec %= 1000000000; |
336 | } |
337 | |
338 | /* Check whether we can acquire without releasing the GIL and blocking */ |
339 | do { |
340 | res = sem_trywait(self->handle); |
341 | err = errno; |
342 | } while (res < 0 && errno == EINTR && !PyErr_CheckSignals()); |
343 | errno = err; |
344 | |
345 | if (res < 0 && errno == EAGAIN && blocking) { |
346 | /* Couldn't acquire immediately, need to block */ |
347 | do { |
348 | Py_BEGIN_ALLOW_THREADS |
349 | if (!use_deadline) { |
350 | res = sem_wait(self->handle); |
351 | } |
352 | else { |
353 | res = sem_timedwait(self->handle, &deadline); |
354 | } |
355 | Py_END_ALLOW_THREADS |
356 | err = errno; |
357 | if (res == MP_EXCEPTION_HAS_BEEN_SET) |
358 | break; |
359 | } while (res < 0 && errno == EINTR && !PyErr_CheckSignals()); |
360 | } |
361 | |
362 | if (res < 0) { |
363 | errno = err; |
364 | if (errno == EAGAIN || errno == ETIMEDOUT) |
365 | Py_RETURN_FALSE; |
366 | else if (errno == EINTR) |
367 | return NULL; |
368 | else |
369 | return PyErr_SetFromErrno(PyExc_OSError); |
370 | } |
371 | |
372 | ++self->count; |
373 | self->last_tid = PyThread_get_thread_ident(); |
374 | |
375 | Py_RETURN_TRUE; |
376 | } |
377 | |
378 | /*[clinic input] |
379 | _multiprocessing.SemLock.release |
380 | |
381 | Release the semaphore/lock. |
382 | [clinic start generated code]*/ |
383 | |
384 | static PyObject * |
385 | _multiprocessing_SemLock_release_impl(SemLockObject *self) |
386 | /*[clinic end generated code: output=b22f53ba96b0d1db input=ba7e63a961885d3d]*/ |
387 | { |
388 | if (self->kind == RECURSIVE_MUTEX) { |
389 | if (!ISMINE(self)) { |
390 | PyErr_SetString(PyExc_AssertionError, "attempt to " |
391 | "release recursive lock not owned " |
392 | "by thread" ); |
393 | return NULL; |
394 | } |
395 | if (self->count > 1) { |
396 | --self->count; |
397 | Py_RETURN_NONE; |
398 | } |
399 | assert(self->count == 1); |
400 | } else { |
401 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
402 | /* We will only check properly the maxvalue == 1 case */ |
403 | if (self->maxvalue == 1) { |
404 | /* make sure that already locked */ |
405 | if (sem_trywait(self->handle) < 0) { |
406 | if (errno != EAGAIN) { |
407 | PyErr_SetFromErrno(PyExc_OSError); |
408 | return NULL; |
409 | } |
410 | /* it is already locked as expected */ |
411 | } else { |
412 | /* it was not locked so undo wait and raise */ |
413 | if (sem_post(self->handle) < 0) { |
414 | PyErr_SetFromErrno(PyExc_OSError); |
415 | return NULL; |
416 | } |
417 | PyErr_SetString(PyExc_ValueError, "semaphore " |
418 | "or lock released too many " |
419 | "times" ); |
420 | return NULL; |
421 | } |
422 | } |
423 | #else |
424 | int sval; |
425 | |
426 | /* This check is not an absolute guarantee that the semaphore |
427 | does not rise above maxvalue. */ |
428 | if (sem_getvalue(self->handle, &sval) < 0) { |
429 | return PyErr_SetFromErrno(PyExc_OSError); |
430 | } else if (sval >= self->maxvalue) { |
431 | PyErr_SetString(PyExc_ValueError, "semaphore or lock " |
432 | "released too many times" ); |
433 | return NULL; |
434 | } |
435 | #endif |
436 | } |
437 | |
438 | if (sem_post(self->handle) < 0) |
439 | return PyErr_SetFromErrno(PyExc_OSError); |
440 | |
441 | --self->count; |
442 | Py_RETURN_NONE; |
443 | } |
444 | |
445 | #endif /* !MS_WINDOWS */ |
446 | |
447 | /* |
448 | * All platforms |
449 | */ |
450 | |
451 | static PyObject * |
452 | newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue, |
453 | char *name) |
454 | { |
455 | SemLockObject *self; |
456 | |
457 | self = PyObject_New(SemLockObject, type); |
458 | if (!self) |
459 | return NULL; |
460 | self->handle = handle; |
461 | self->kind = kind; |
462 | self->count = 0; |
463 | self->last_tid = 0; |
464 | self->maxvalue = maxvalue; |
465 | self->name = name; |
466 | return (PyObject*)self; |
467 | } |
468 | |
469 | /*[clinic input] |
470 | @classmethod |
471 | _multiprocessing.SemLock.__new__ |
472 | |
473 | kind: int |
474 | value: int |
475 | maxvalue: int |
476 | name: str |
477 | unlink: bool(accept={int}) |
478 | |
479 | [clinic start generated code]*/ |
480 | |
481 | static PyObject * |
482 | _multiprocessing_SemLock_impl(PyTypeObject *type, int kind, int value, |
483 | int maxvalue, const char *name, int unlink) |
484 | /*[clinic end generated code: output=30727e38f5f7577a input=b378c3ee27d3a0fa]*/ |
485 | { |
486 | SEM_HANDLE handle = SEM_FAILED; |
487 | PyObject *result; |
488 | char *name_copy = NULL; |
489 | |
490 | if (kind != RECURSIVE_MUTEX && kind != SEMAPHORE) { |
491 | PyErr_SetString(PyExc_ValueError, "unrecognized kind" ); |
492 | return NULL; |
493 | } |
494 | |
495 | if (!unlink) { |
496 | name_copy = PyMem_Malloc(strlen(name) + 1); |
497 | if (name_copy == NULL) { |
498 | return PyErr_NoMemory(); |
499 | } |
500 | strcpy(name_copy, name); |
501 | } |
502 | |
503 | SEM_CLEAR_ERROR(); |
504 | handle = SEM_CREATE(name, value, maxvalue); |
505 | /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */ |
506 | if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0) |
507 | goto failure; |
508 | |
509 | if (unlink && SEM_UNLINK(name) < 0) |
510 | goto failure; |
511 | |
512 | result = newsemlockobject(type, handle, kind, maxvalue, name_copy); |
513 | if (!result) |
514 | goto failure; |
515 | |
516 | return result; |
517 | |
518 | failure: |
519 | if (handle != SEM_FAILED) |
520 | SEM_CLOSE(handle); |
521 | PyMem_Free(name_copy); |
522 | if (!PyErr_Occurred()) { |
523 | _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
524 | } |
525 | return NULL; |
526 | } |
527 | |
528 | /*[clinic input] |
529 | @classmethod |
530 | _multiprocessing.SemLock._rebuild |
531 | |
532 | handle: SEM_HANDLE |
533 | kind: int |
534 | maxvalue: int |
535 | name: str(accept={str, NoneType}) |
536 | / |
537 | |
538 | [clinic start generated code]*/ |
539 | |
540 | static PyObject * |
541 | _multiprocessing_SemLock__rebuild_impl(PyTypeObject *type, SEM_HANDLE handle, |
542 | int kind, int maxvalue, |
543 | const char *name) |
544 | /*[clinic end generated code: output=2aaee14f063f3bd9 input=f7040492ac6d9962]*/ |
545 | { |
546 | char *name_copy = NULL; |
547 | |
548 | if (name != NULL) { |
549 | name_copy = PyMem_Malloc(strlen(name) + 1); |
550 | if (name_copy == NULL) |
551 | return PyErr_NoMemory(); |
552 | strcpy(name_copy, name); |
553 | } |
554 | |
555 | #ifndef MS_WINDOWS |
556 | if (name != NULL) { |
557 | handle = sem_open(name, 0); |
558 | if (handle == SEM_FAILED) { |
559 | PyMem_Free(name_copy); |
560 | return PyErr_SetFromErrno(PyExc_OSError); |
561 | } |
562 | } |
563 | #endif |
564 | |
565 | return newsemlockobject(type, handle, kind, maxvalue, name_copy); |
566 | } |
567 | |
568 | static void |
569 | semlock_dealloc(SemLockObject* self) |
570 | { |
571 | if (self->handle != SEM_FAILED) |
572 | SEM_CLOSE(self->handle); |
573 | PyMem_Free(self->name); |
574 | PyObject_Free(self); |
575 | } |
576 | |
577 | /*[clinic input] |
578 | _multiprocessing.SemLock._count |
579 | |
580 | Num of `acquire()`s minus num of `release()`s for this process. |
581 | [clinic start generated code]*/ |
582 | |
583 | static PyObject * |
584 | _multiprocessing_SemLock__count_impl(SemLockObject *self) |
585 | /*[clinic end generated code: output=5ba8213900e517bb input=36fc59b1cd1025ab]*/ |
586 | { |
587 | return PyLong_FromLong((long)self->count); |
588 | } |
589 | |
590 | /*[clinic input] |
591 | _multiprocessing.SemLock._is_mine |
592 | |
593 | Whether the lock is owned by this thread. |
594 | [clinic start generated code]*/ |
595 | |
596 | static PyObject * |
597 | _multiprocessing_SemLock__is_mine_impl(SemLockObject *self) |
598 | /*[clinic end generated code: output=92dc98863f4303be input=a96664cb2f0093ba]*/ |
599 | { |
600 | /* only makes sense for a lock */ |
601 | return PyBool_FromLong(ISMINE(self)); |
602 | } |
603 | |
604 | /*[clinic input] |
605 | _multiprocessing.SemLock._get_value |
606 | |
607 | Get the value of the semaphore. |
608 | [clinic start generated code]*/ |
609 | |
610 | static PyObject * |
611 | _multiprocessing_SemLock__get_value_impl(SemLockObject *self) |
612 | /*[clinic end generated code: output=64bc1b89bda05e36 input=cb10f9a769836203]*/ |
613 | { |
614 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
615 | PyErr_SetNone(PyExc_NotImplementedError); |
616 | return NULL; |
617 | #else |
618 | int sval; |
619 | if (SEM_GETVALUE(self->handle, &sval) < 0) |
620 | return _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
621 | /* some posix implementations use negative numbers to indicate |
622 | the number of waiting threads */ |
623 | if (sval < 0) |
624 | sval = 0; |
625 | return PyLong_FromLong((long)sval); |
626 | #endif |
627 | } |
628 | |
629 | /*[clinic input] |
630 | _multiprocessing.SemLock._is_zero |
631 | |
632 | Return whether semaphore has value zero. |
633 | [clinic start generated code]*/ |
634 | |
635 | static PyObject * |
636 | _multiprocessing_SemLock__is_zero_impl(SemLockObject *self) |
637 | /*[clinic end generated code: output=815d4c878c806ed7 input=294a446418d31347]*/ |
638 | { |
639 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
640 | if (sem_trywait(self->handle) < 0) { |
641 | if (errno == EAGAIN) |
642 | Py_RETURN_TRUE; |
643 | return _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
644 | } else { |
645 | if (sem_post(self->handle) < 0) |
646 | return _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
647 | Py_RETURN_FALSE; |
648 | } |
649 | #else |
650 | int sval; |
651 | if (SEM_GETVALUE(self->handle, &sval) < 0) |
652 | return _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
653 | return PyBool_FromLong((long)sval == 0); |
654 | #endif |
655 | } |
656 | |
657 | /*[clinic input] |
658 | _multiprocessing.SemLock._after_fork |
659 | |
660 | Rezero the net acquisition count after fork(). |
661 | [clinic start generated code]*/ |
662 | |
663 | static PyObject * |
664 | _multiprocessing_SemLock__after_fork_impl(SemLockObject *self) |
665 | /*[clinic end generated code: output=718bb27914c6a6c1 input=190991008a76621e]*/ |
666 | { |
667 | self->count = 0; |
668 | Py_RETURN_NONE; |
669 | } |
670 | |
671 | /*[clinic input] |
672 | _multiprocessing.SemLock.__enter__ |
673 | |
674 | Enter the semaphore/lock. |
675 | [clinic start generated code]*/ |
676 | |
677 | static PyObject * |
678 | _multiprocessing_SemLock___enter___impl(SemLockObject *self) |
679 | /*[clinic end generated code: output=beeb2f07c858511f input=c5e27d594284690b]*/ |
680 | { |
681 | return _multiprocessing_SemLock_acquire_impl(self, 1, Py_None); |
682 | } |
683 | |
684 | /*[clinic input] |
685 | _multiprocessing.SemLock.__exit__ |
686 | |
687 | exc_type: object = None |
688 | exc_value: object = None |
689 | exc_tb: object = None |
690 | / |
691 | |
692 | Exit the semaphore/lock. |
693 | [clinic start generated code]*/ |
694 | |
695 | static PyObject * |
696 | _multiprocessing_SemLock___exit___impl(SemLockObject *self, |
697 | PyObject *exc_type, |
698 | PyObject *exc_value, PyObject *exc_tb) |
699 | /*[clinic end generated code: output=3b37c1a9f8b91a03 input=7d644b64a89903f8]*/ |
700 | { |
701 | return _multiprocessing_SemLock_release_impl(self); |
702 | } |
703 | |
704 | /* |
705 | * Semaphore methods |
706 | */ |
707 | |
708 | static PyMethodDef semlock_methods[] = { |
709 | _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF |
710 | _MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF |
711 | _MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF |
712 | _MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF |
713 | _MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF |
714 | _MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF |
715 | _MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF |
716 | _MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF |
717 | _MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF |
718 | _MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF |
719 | {NULL} |
720 | }; |
721 | |
722 | /* |
723 | * Member table |
724 | */ |
725 | |
726 | static PyMemberDef semlock_members[] = { |
727 | {"handle" , T_SEM_HANDLE, offsetof(SemLockObject, handle), READONLY, |
728 | "" }, |
729 | {"kind" , T_INT, offsetof(SemLockObject, kind), READONLY, |
730 | "" }, |
731 | {"maxvalue" , T_INT, offsetof(SemLockObject, maxvalue), READONLY, |
732 | "" }, |
733 | {"name" , T_STRING, offsetof(SemLockObject, name), READONLY, |
734 | "" }, |
735 | {NULL} |
736 | }; |
737 | |
738 | /* |
739 | * Semaphore type |
740 | */ |
741 | |
742 | PyTypeObject _PyMp_SemLockType = { |
743 | PyVarObject_HEAD_INIT(NULL, 0) |
744 | /* tp_name */ "_multiprocessing.SemLock" , |
745 | /* tp_basicsize */ sizeof(SemLockObject), |
746 | /* tp_itemsize */ 0, |
747 | /* tp_dealloc */ (destructor)semlock_dealloc, |
748 | /* tp_vectorcall_offset */ 0, |
749 | /* tp_getattr */ 0, |
750 | /* tp_setattr */ 0, |
751 | /* tp_as_async */ 0, |
752 | /* tp_repr */ 0, |
753 | /* tp_as_number */ 0, |
754 | /* tp_as_sequence */ 0, |
755 | /* tp_as_mapping */ 0, |
756 | /* tp_hash */ 0, |
757 | /* tp_call */ 0, |
758 | /* tp_str */ 0, |
759 | /* tp_getattro */ 0, |
760 | /* tp_setattro */ 0, |
761 | /* tp_as_buffer */ 0, |
762 | /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
763 | /* tp_doc */ "Semaphore/Mutex type" , |
764 | /* tp_traverse */ 0, |
765 | /* tp_clear */ 0, |
766 | /* tp_richcompare */ 0, |
767 | /* tp_weaklistoffset */ 0, |
768 | /* tp_iter */ 0, |
769 | /* tp_iternext */ 0, |
770 | /* tp_methods */ semlock_methods, |
771 | /* tp_members */ semlock_members, |
772 | /* tp_getset */ 0, |
773 | /* tp_base */ 0, |
774 | /* tp_dict */ 0, |
775 | /* tp_descr_get */ 0, |
776 | /* tp_descr_set */ 0, |
777 | /* tp_dictoffset */ 0, |
778 | /* tp_init */ 0, |
779 | /* tp_alloc */ 0, |
780 | /* tp_new */ _multiprocessing_SemLock, |
781 | }; |
782 | |
783 | /* |
784 | * Function to unlink semaphore names |
785 | */ |
786 | |
787 | PyObject * |
788 | _PyMp_sem_unlink(const char *name) |
789 | { |
790 | if (SEM_UNLINK(name) < 0) { |
791 | _PyMp_SetError(NULL, MP_STANDARD_ERROR); |
792 | return NULL; |
793 | } |
794 | |
795 | Py_RETURN_NONE; |
796 | } |
797 | |