| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * A type which wraps a semaphore | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * semaphore.c | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2012-04-30 12:13:55 +01:00
										 |  |  |  * Copyright (c) 2006-2008, R Oudkerk | 
					
						
							|  |  |  |  * Licensed to PSF under a Contributor Agreement. | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "multiprocessing.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-02 17:51:19 +02:00
										 |  |  | #ifdef HAVE_SYS_TIME_H
 | 
					
						
							|  |  |  | #  include <sys/time.h>           // gettimeofday()
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 11:36:10 +02:00
										 |  |  | #ifdef HAVE_MP_SEMAPHORE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 00:06:14 -08:00
										 |  |  | // These match the values in Lib/multiprocessing/synchronize.py
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | enum { RECURSIVE_MUTEX, SEMAPHORE }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject_HEAD | 
					
						
							|  |  |  |     SEM_HANDLE handle; | 
					
						
							| 
									
										
										
										
											2017-03-23 15:48:39 +02:00
										 |  |  |     unsigned long last_tid; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int count; | 
					
						
							|  |  |  |     int maxvalue; | 
					
						
							|  |  |  |     int kind; | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |     char *name; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } SemLockObject; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-20 21:43:55 +01:00
										 |  |  | #define _SemLockObject_CAST(op) ((SemLockObject *)(op))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[python input]
 | 
					
						
							|  |  |  | class SEM_HANDLE_converter(CConverter): | 
					
						
							|  |  |  |     type = "SEM_HANDLE" | 
					
						
							|  |  |  |     format_unit = '"F_SEM_HANDLE"' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [python start generated code]*/ | 
					
						
							|  |  |  | /*[python end generated code: output=da39a3ee5e6b4b0d input=3e0ad43e482d8716]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | module _multiprocessing | 
					
						
							|  |  |  | class _multiprocessing.SemLock "SemLockObject *" "&_PyMp_SemLockType" | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=935fb41b7d032599]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "clinic/semaphore.c.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | #define ISMINE(o) (o->count > 0 && PyThread_get_thread_ident() == o->last_tid)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef MS_WINDOWS
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Windows definitions | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define SEM_FAILED NULL
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define SEM_CLEAR_ERROR() SetLastError(0)
 | 
					
						
							|  |  |  | #define SEM_GET_LAST_ERROR() GetLastError()
 | 
					
						
							|  |  |  | #define SEM_CREATE(name, val, max) CreateSemaphore(NULL, val, max, NULL)
 | 
					
						
							|  |  |  | #define SEM_CLOSE(sem) (CloseHandle(sem) ? 0 : -1)
 | 
					
						
							|  |  |  | #define SEM_GETVALUE(sem, pval) _GetSemaphoreValue(sem, pval)
 | 
					
						
							|  |  |  | #define SEM_UNLINK(name) 0
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-03-22 23:44:56 +01:00
										 |  |  | _GetSemaphoreValue(HANDLE handle, int *value) | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     long previous; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-25 14:25:48 +01:00
										 |  |  |     switch (WaitForSingleObjectEx(handle, 0, FALSE)) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     case WAIT_OBJECT_0: | 
					
						
							|  |  |  |         if (!ReleaseSemaphore(handle, 1, &previous)) | 
					
						
							|  |  |  |             return MP_STANDARD_ERROR; | 
					
						
							|  |  |  |         *value = previous + 1; | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     case WAIT_TIMEOUT: | 
					
						
							|  |  |  |         *value = 0; | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         return MP_STANDARD_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2024-04-04 14:09:38 -04:00
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing.SemLock.acquire | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-03 21:52:21 +02:00
										 |  |  |     block as blocking: bool = True | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  |     timeout as timeout_obj: object = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Acquire the semaphore/lock. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking, | 
					
						
							|  |  |  |                                       PyObject *timeout_obj) | 
					
						
							| 
									
										
										
										
											2024-04-04 14:09:38 -04:00
										 |  |  | /*[clinic end generated code: output=f9998f0b6b0b0872 input=079ca779975f3ad6]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     double timeout; | 
					
						
							| 
									
										
										
										
											2011-11-21 21:26:56 +01:00
										 |  |  |     DWORD res, full_msecs, nhandles; | 
					
						
							|  |  |  |     HANDLE handles[2], sigint_event; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* calculate timeout */ | 
					
						
							|  |  |  |     if (!blocking) { | 
					
						
							|  |  |  |         full_msecs = 0; | 
					
						
							|  |  |  |     } else if (timeout_obj == Py_None) { | 
					
						
							|  |  |  |         full_msecs = INFINITE; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         timeout = PyFloat_AsDouble(timeout_obj); | 
					
						
							|  |  |  |         if (PyErr_Occurred()) | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         timeout *= 1000.0;      /* convert to millisecs */ | 
					
						
							|  |  |  |         if (timeout < 0.0) { | 
					
						
							|  |  |  |             timeout = 0.0; | 
					
						
							|  |  |  |         } else if (timeout >= 0.5 * INFINITE) { /* 25 days */ | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_OverflowError, | 
					
						
							|  |  |  |                             "timeout is too large"); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         full_msecs = (DWORD)(timeout + 0.5); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* check whether we already own the lock */ | 
					
						
							|  |  |  |     if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) { | 
					
						
							|  |  |  |         ++self->count; | 
					
						
							|  |  |  |         Py_RETURN_TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-21 21:26:56 +01:00
										 |  |  |     /* check whether we can acquire without releasing the GIL and blocking */ | 
					
						
							| 
									
										
										
										
											2013-01-25 14:25:48 +01:00
										 |  |  |     if (WaitForSingleObjectEx(self->handle, 0, FALSE) == WAIT_OBJECT_0) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         self->last_tid = GetCurrentThreadId(); | 
					
						
							|  |  |  |         ++self->count; | 
					
						
							|  |  |  |         Py_RETURN_TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-21 21:26:56 +01:00
										 |  |  |     /* prepare list of handles */ | 
					
						
							|  |  |  |     nhandles = 0; | 
					
						
							|  |  |  |     handles[nhandles++] = self->handle; | 
					
						
							|  |  |  |     if (_PyOS_IsMainThread()) { | 
					
						
							|  |  |  |         sigint_event = _PyOS_SigintEvent(); | 
					
						
							|  |  |  |         assert(sigint_event != NULL); | 
					
						
							|  |  |  |         handles[nhandles++] = sigint_event; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-07-23 07:13:14 +03:00
										 |  |  |     else { | 
					
						
							|  |  |  |         sigint_event = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-21 21:26:56 +01:00
										 |  |  |     /* do the wait */ | 
					
						
							|  |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |     if (sigint_event != NULL) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         ResetEvent(sigint_event); | 
					
						
							| 
									
										
										
										
											2013-01-25 14:29:13 +01:00
										 |  |  |     res = WaitForMultipleObjectsEx(nhandles, handles, FALSE, full_msecs, FALSE); | 
					
						
							| 
									
										
										
										
											2011-11-21 21:26:56 +01:00
										 |  |  |     Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* handle result */ | 
					
						
							|  |  |  |     switch (res) { | 
					
						
							|  |  |  |     case WAIT_TIMEOUT: | 
					
						
							|  |  |  |         Py_RETURN_FALSE; | 
					
						
							| 
									
										
										
										
											2011-11-21 21:26:56 +01:00
										 |  |  |     case WAIT_OBJECT_0 + 0: | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         self->last_tid = GetCurrentThreadId(); | 
					
						
							|  |  |  |         ++self->count; | 
					
						
							|  |  |  |         Py_RETURN_TRUE; | 
					
						
							| 
									
										
										
										
											2011-11-21 21:26:56 +01:00
										 |  |  |     case WAIT_OBJECT_0 + 1: | 
					
						
							|  |  |  |         errno = EINTR; | 
					
						
							| 
									
										
										
										
											2017-04-16 10:46:38 +03:00
										 |  |  |         return PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     case WAIT_FAILED: | 
					
						
							|  |  |  |         return PyErr_SetFromWindowsErr(0); | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         PyErr_Format(PyExc_RuntimeError, "WaitForSingleObject() or " | 
					
						
							|  |  |  |                      "WaitForMultipleObjects() gave unrecognized " | 
					
						
							| 
									
										
										
										
											2019-03-13 22:59:55 +02:00
										 |  |  |                      "value %u", res); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2024-04-04 14:09:38 -04:00
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing.SemLock.release | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Release the semaphore/lock. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock_release_impl(SemLockObject *self) | 
					
						
							| 
									
										
										
										
											2024-04-04 14:09:38 -04:00
										 |  |  | /*[clinic end generated code: output=b22f53ba96b0d1db input=9bd62d3645e7a531]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (self->kind == RECURSIVE_MUTEX) { | 
					
						
							|  |  |  |         if (!ISMINE(self)) { | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_AssertionError, "attempt to " | 
					
						
							|  |  |  |                             "release recursive lock not owned " | 
					
						
							|  |  |  |                             "by thread"); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (self->count > 1) { | 
					
						
							|  |  |  |             --self->count; | 
					
						
							|  |  |  |             Py_RETURN_NONE; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         assert(self->count == 1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!ReleaseSemaphore(self->handle, 1, NULL)) { | 
					
						
							|  |  |  |         if (GetLastError() == ERROR_TOO_MANY_POSTS) { | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_ValueError, "semaphore or lock " | 
					
						
							|  |  |  |                             "released too many times"); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return PyErr_SetFromWindowsErr(0); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     --self->count; | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else /* !MS_WINDOWS */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Unix definitions | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define SEM_CLEAR_ERROR()
 | 
					
						
							|  |  |  | #define SEM_GET_LAST_ERROR() 0
 | 
					
						
							|  |  |  | #define SEM_CREATE(name, val, max) sem_open(name, O_CREAT | O_EXCL, 0600, val)
 | 
					
						
							|  |  |  | #define SEM_CLOSE(sem) sem_close(sem)
 | 
					
						
							|  |  |  | #define SEM_GETVALUE(sem, pval) sem_getvalue(sem, pval)
 | 
					
						
							|  |  |  | #define SEM_UNLINK(name) sem_unlink(name)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-01 17:29:44 +00:00
										 |  |  | /* OS X 10.4 defines SEM_FAILED as -1 instead of (sem_t *)-1;  this gives
 | 
					
						
							|  |  |  |    compiler warnings, and (potentially) undefined behaviour. */ | 
					
						
							|  |  |  | #ifdef __APPLE__
 | 
					
						
							|  |  |  | #  undef SEM_FAILED
 | 
					
						
							|  |  |  | #  define SEM_FAILED ((sem_t *)-1)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
  Issue 5619: Pass MS CRT debug flags into subprocesses
........
  r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
  Fix multiprocessing.event to match the new threading.Event API
........
  r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
  issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
  r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
  Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
  r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
  Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
											
										 
											2009-04-05 21:24:58 +00:00
										 |  |  | #ifndef HAVE_SEM_UNLINK
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | #  define sem_unlink(name) 0
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
  Issue 5619: Pass MS CRT debug flags into subprocesses
........
  r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
  Fix multiprocessing.event to match the new threading.Event API
........
  r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
  issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
  r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
  Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
  r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
  Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
											
										 
											2009-04-05 21:24:58 +00:00
										 |  |  | #ifndef HAVE_SEM_TIMEDWAIT
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | #  define sem_timedwait(sem,deadline) sem_timedwait_save(sem,deadline,_save)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-07 18:08:47 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int res; | 
					
						
							|  |  |  |     unsigned long delay, difference; | 
					
						
							|  |  |  |     struct timeval now, tvdeadline, tvdelay; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     errno = 0; | 
					
						
							|  |  |  |     tvdeadline.tv_sec = deadline->tv_sec; | 
					
						
							|  |  |  |     tvdeadline.tv_usec = deadline->tv_nsec / 1000; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (delay = 0 ; ; delay += 1000) { | 
					
						
							|  |  |  |         /* poll */ | 
					
						
							|  |  |  |         if (sem_trywait(sem) == 0) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         else if (errno != EAGAIN) | 
					
						
							|  |  |  |             return MP_STANDARD_ERROR; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* get current time */ | 
					
						
							|  |  |  |         if (gettimeofday(&now, NULL) < 0) | 
					
						
							|  |  |  |             return MP_STANDARD_ERROR; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* check for timeout */ | 
					
						
							|  |  |  |         if (tvdeadline.tv_sec < now.tv_sec || | 
					
						
							|  |  |  |             (tvdeadline.tv_sec == now.tv_sec && | 
					
						
							|  |  |  |              tvdeadline.tv_usec <= now.tv_usec)) { | 
					
						
							|  |  |  |             errno = ETIMEDOUT; | 
					
						
							|  |  |  |             return MP_STANDARD_ERROR; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* calculate how much time is left */ | 
					
						
							|  |  |  |         difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 + | 
					
						
							|  |  |  |             (tvdeadline.tv_usec - now.tv_usec); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* check delay not too long -- maximum is 20 msecs */ | 
					
						
							|  |  |  |         if (delay > 20000) | 
					
						
							|  |  |  |             delay = 20000; | 
					
						
							|  |  |  |         if (delay > difference) | 
					
						
							|  |  |  |             delay = difference; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* sleep */ | 
					
						
							|  |  |  |         tvdelay.tv_sec = delay / 1000000; | 
					
						
							|  |  |  |         tvdelay.tv_usec = delay % 1000000; | 
					
						
							|  |  |  |         if (select(0, NULL, NULL, NULL, &tvdelay) < 0) | 
					
						
							|  |  |  |             return MP_STANDARD_ERROR; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* check for signals */ | 
					
						
							|  |  |  |         Py_BLOCK_THREADS | 
					
						
							|  |  |  |         res = PyErr_CheckSignals(); | 
					
						
							|  |  |  |         Py_UNBLOCK_THREADS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (res) { | 
					
						
							|  |  |  |             errno = EINTR; | 
					
						
							|  |  |  |             return MP_EXCEPTION_HAS_BEEN_SET; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* !HAVE_SEM_TIMEDWAIT */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2024-04-04 14:09:38 -04:00
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing.SemLock.acquire | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-03 21:52:21 +02:00
										 |  |  |     block as blocking: bool = True | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  |     timeout as timeout_obj: object = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Acquire the semaphore/lock. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking, | 
					
						
							|  |  |  |                                       PyObject *timeout_obj) | 
					
						
							| 
									
										
										
										
											2024-04-04 14:09:38 -04:00
										 |  |  | /*[clinic end generated code: output=f9998f0b6b0b0872 input=079ca779975f3ad6]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  |     int res, err = 0; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     struct timespec deadline = {0}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) { | 
					
						
							|  |  |  |         ++self->count; | 
					
						
							|  |  |  |         Py_RETURN_TRUE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 01:25:59 +01:00
										 |  |  |     int use_deadline = (timeout_obj != Py_None); | 
					
						
							|  |  |  |     if (use_deadline) { | 
					
						
							|  |  |  |         double timeout = PyFloat_AsDouble(timeout_obj); | 
					
						
							|  |  |  |         if (PyErr_Occurred()) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2020-02-01 01:25:59 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (timeout < 0.0) { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |             timeout = 0.0; | 
					
						
							| 
									
										
										
										
											2020-02-01 01:25:59 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 01:25:59 +01:00
										 |  |  |         struct timeval now; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (gettimeofday(&now, NULL) < 0) { | 
					
						
							|  |  |  |             PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-01 01:25:59 +01:00
										 |  |  |         long sec = (long) timeout; | 
					
						
							|  |  |  |         long nsec = (long) (1e9 * (timeout - sec) + 0.5); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         deadline.tv_sec = now.tv_sec + sec; | 
					
						
							|  |  |  |         deadline.tv_nsec = now.tv_usec * 1000 + nsec; | 
					
						
							|  |  |  |         deadline.tv_sec += (deadline.tv_nsec / 1000000000); | 
					
						
							|  |  |  |         deadline.tv_nsec %= 1000000000; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-22 13:10:46 +02:00
										 |  |  |     /* Check whether we can acquire without releasing the GIL and blocking */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     do { | 
					
						
							| 
									
										
										
										
											2017-10-22 13:10:46 +02:00
										 |  |  |         res = sem_trywait(self->handle); | 
					
						
							| 
									
										
										
										
											2011-12-16 12:28:32 +01:00
										 |  |  |         err = errno; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } while (res < 0 && errno == EINTR && !PyErr_CheckSignals()); | 
					
						
							| 
									
										
										
										
											2017-10-22 13:10:46 +02:00
										 |  |  |     errno = err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (res < 0 && errno == EAGAIN && blocking) { | 
					
						
							|  |  |  |         /* Couldn't acquire immediately, need to block */ | 
					
						
							|  |  |  |         do { | 
					
						
							|  |  |  |             Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2020-02-01 01:25:59 +01:00
										 |  |  |             if (!use_deadline) { | 
					
						
							| 
									
										
										
										
											2017-10-22 13:10:46 +02:00
										 |  |  |                 res = sem_wait(self->handle); | 
					
						
							| 
									
										
										
										
											2017-10-23 13:57:51 -07:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							| 
									
										
										
										
											2017-10-22 13:10:46 +02:00
										 |  |  |                 res = sem_timedwait(self->handle, &deadline); | 
					
						
							| 
									
										
										
										
											2017-10-23 13:57:51 -07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-10-22 13:10:46 +02:00
										 |  |  |             Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |             err = errno; | 
					
						
							|  |  |  |             if (res == MP_EXCEPTION_HAS_BEEN_SET) | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } while (res < 0 && errno == EINTR && !PyErr_CheckSignals()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (res < 0) { | 
					
						
							| 
									
										
										
										
											2011-12-16 12:28:32 +01:00
										 |  |  |         errno = err; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         if (errno == EAGAIN || errno == ETIMEDOUT) | 
					
						
							|  |  |  |             Py_RETURN_FALSE; | 
					
						
							|  |  |  |         else if (errno == EINTR) | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             return PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ++self->count; | 
					
						
							|  |  |  |     self->last_tid = PyThread_get_thread_ident(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_RETURN_TRUE; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2024-04-04 14:09:38 -04:00
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing.SemLock.release | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Release the semaphore/lock. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock_release_impl(SemLockObject *self) | 
					
						
							| 
									
										
										
										
											2024-04-04 14:09:38 -04:00
										 |  |  | /*[clinic end generated code: output=b22f53ba96b0d1db input=9bd62d3645e7a531]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (self->kind == RECURSIVE_MUTEX) { | 
					
						
							|  |  |  |         if (!ISMINE(self)) { | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_AssertionError, "attempt to " | 
					
						
							|  |  |  |                             "release recursive lock not owned " | 
					
						
							|  |  |  |                             "by thread"); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (self->count > 1) { | 
					
						
							|  |  |  |             --self->count; | 
					
						
							|  |  |  |             Py_RETURN_NONE; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         assert(self->count == 1); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
											
												Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
  Issue 5619: Pass MS CRT debug flags into subprocesses
........
  r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
  Fix multiprocessing.event to match the new threading.Event API
........
  r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
  issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
  r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
  Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
  r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
  Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
											
										 
											2009-04-05 21:24:58 +00:00
										 |  |  | #ifdef HAVE_BROKEN_SEM_GETVALUE
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         /* We will only check properly the maxvalue == 1 case */ | 
					
						
							|  |  |  |         if (self->maxvalue == 1) { | 
					
						
							|  |  |  |             /* make sure that already locked */ | 
					
						
							|  |  |  |             if (sem_trywait(self->handle) < 0) { | 
					
						
							|  |  |  |                 if (errno != EAGAIN) { | 
					
						
							|  |  |  |                     PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |                     return NULL; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 /* it is already locked as expected */ | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 /* it was not locked so undo wait and raise  */ | 
					
						
							|  |  |  |                 if (sem_post(self->handle) < 0) { | 
					
						
							|  |  |  |                     PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |                     return NULL; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 PyErr_SetString(PyExc_ValueError, "semaphore " | 
					
						
							|  |  |  |                                 "or lock released too many " | 
					
						
							|  |  |  |                                 "times"); | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         int sval; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* This check is not an absolute guarantee that the semaphore
 | 
					
						
							|  |  |  |            does not rise above maxvalue. */ | 
					
						
							|  |  |  |         if (sem_getvalue(self->handle, &sval) < 0) { | 
					
						
							|  |  |  |             return PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							|  |  |  |         } else if (sval >= self->maxvalue) { | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_ValueError, "semaphore or lock " | 
					
						
							|  |  |  |                             "released too many times"); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (sem_post(self->handle) < 0) | 
					
						
							|  |  |  |         return PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     --self->count; | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* !MS_WINDOWS */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * All platforms | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  | newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue, | 
					
						
							|  |  |  |                  char *name) | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:42:36 +05:30
										 |  |  |     SemLockObject *self = (SemLockObject *)type->tp_alloc(type, 0); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!self) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     self->handle = handle; | 
					
						
							|  |  |  |     self->kind = kind; | 
					
						
							|  |  |  |     self->count = 0; | 
					
						
							|  |  |  |     self->last_tid = 0; | 
					
						
							|  |  |  |     self->maxvalue = maxvalue; | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |     self->name = name; | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return (PyObject*)self; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @classmethod | 
					
						
							|  |  |  | _multiprocessing.SemLock.__new__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     kind: int | 
					
						
							|  |  |  |     value: int | 
					
						
							|  |  |  |     maxvalue: int | 
					
						
							|  |  |  |     name: str | 
					
						
							| 
									
										
										
										
											2022-12-03 21:52:21 +02:00
										 |  |  |     unlink: bool | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock_impl(PyTypeObject *type, int kind, int value, | 
					
						
							|  |  |  |                               int maxvalue, const char *name, int unlink) | 
					
						
							| 
									
										
										
										
											2022-12-03 21:52:21 +02:00
										 |  |  | /*[clinic end generated code: output=30727e38f5f7577a input=fdaeb69814471c5b]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     SEM_HANDLE handle = SEM_FAILED; | 
					
						
							|  |  |  |     PyObject *result; | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  |     char *name_copy = NULL; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (kind != RECURSIVE_MUTEX && kind != SEMAPHORE) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_ValueError, "unrecognized kind"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |     if (!unlink) { | 
					
						
							|  |  |  |         name_copy = PyMem_Malloc(strlen(name) + 1); | 
					
						
							| 
									
										
										
										
											2018-12-07 03:11:30 -07:00
										 |  |  |         if (name_copy == NULL) { | 
					
						
							|  |  |  |             return PyErr_NoMemory(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |         strcpy(name_copy, name); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     SEM_CLEAR_ERROR(); | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |     handle = SEM_CREATE(name, value, maxvalue); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */ | 
					
						
							|  |  |  |     if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0) | 
					
						
							|  |  |  |         goto failure; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |     if (unlink && SEM_UNLINK(name) < 0) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         goto failure; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |     result = newsemlockobject(type, handle, kind, maxvalue, name_copy); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!result) | 
					
						
							|  |  |  |         goto failure; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   failure: | 
					
						
							| 
									
										
										
										
											2018-12-07 03:11:30 -07:00
										 |  |  |     if (!PyErr_Occurred()) { | 
					
						
							|  |  |  |         _PyMp_SetError(NULL, MP_STANDARD_ERROR); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-27 00:35:06 +03:00
										 |  |  |     if (handle != SEM_FAILED) | 
					
						
							|  |  |  |         SEM_CLOSE(handle); | 
					
						
							|  |  |  |     PyMem_Free(name_copy); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @classmethod | 
					
						
							|  |  |  | _multiprocessing.SemLock._rebuild | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     handle: SEM_HANDLE | 
					
						
							|  |  |  |     kind: int | 
					
						
							|  |  |  |     maxvalue: int | 
					
						
							|  |  |  |     name: str(accept={str, NoneType}) | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock__rebuild_impl(PyTypeObject *type, SEM_HANDLE handle, | 
					
						
							|  |  |  |                                        int kind, int maxvalue, | 
					
						
							|  |  |  |                                        const char *name) | 
					
						
							|  |  |  | /*[clinic end generated code: output=2aaee14f063f3bd9 input=f7040492ac6d9962]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  |     char *name_copy = NULL; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |     if (name != NULL) { | 
					
						
							|  |  |  |         name_copy = PyMem_Malloc(strlen(name) + 1); | 
					
						
							|  |  |  |         if (name_copy == NULL) | 
					
						
							|  |  |  |             return PyErr_NoMemory(); | 
					
						
							|  |  |  |         strcpy(name_copy, name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef MS_WINDOWS
 | 
					
						
							|  |  |  |     if (name != NULL) { | 
					
						
							|  |  |  |         handle = sem_open(name, 0); | 
					
						
							|  |  |  |         if (handle == SEM_FAILED) { | 
					
						
							| 
									
										
										
										
											2023-08-27 00:35:06 +03:00
										 |  |  |             PyErr_SetFromErrno(PyExc_OSError); | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |             PyMem_Free(name_copy); | 
					
						
							| 
									
										
										
										
											2023-08-27 00:35:06 +03:00
										 |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return newsemlockobject(type, handle, kind, maxvalue, name_copy); | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2025-01-20 21:43:55 +01:00
										 |  |  | semlock_dealloc(PyObject *op) | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-20 21:43:55 +01:00
										 |  |  |     SemLockObject *self = _SemLockObject_CAST(op); | 
					
						
							| 
									
										
										
										
											2022-07-21 00:56:01 +05:30
										 |  |  |     PyTypeObject *tp = Py_TYPE(self); | 
					
						
							|  |  |  |     PyObject_GC_UnTrack(self); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (self->handle != SEM_FAILED) | 
					
						
							|  |  |  |         SEM_CLOSE(self->handle); | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |     PyMem_Free(self->name); | 
					
						
							| 
									
										
										
										
											2022-07-21 00:56:01 +05:30
										 |  |  |     tp->tp_free(self); | 
					
						
							|  |  |  |     Py_DECREF(tp); | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2024-04-04 14:09:38 -04:00
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing.SemLock._count | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Num of `acquire()`s minus num of `release()`s for this process. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock__count_impl(SemLockObject *self) | 
					
						
							| 
									
										
										
										
											2024-04-04 14:09:38 -04:00
										 |  |  | /*[clinic end generated code: output=5ba8213900e517bb input=9fa6e0b321b16935]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-05-09 20:55:03 +02:00
										 |  |  |     return PyLong_FromLong((long)self->count); | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _multiprocessing.SemLock._is_mine | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Whether the lock is owned by this thread. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock__is_mine_impl(SemLockObject *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=92dc98863f4303be input=a96664cb2f0093ba]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* only makes sense for a lock */ | 
					
						
							|  |  |  |     return PyBool_FromLong(ISMINE(self)); | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _multiprocessing.SemLock._get_value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Get the value of the semaphore. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock__get_value_impl(SemLockObject *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=64bc1b89bda05e36 input=cb10f9a769836203]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
											
												Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
  Issue 5619: Pass MS CRT debug flags into subprocesses
........
  r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
  Fix multiprocessing.event to match the new threading.Event API
........
  r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
  issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
  r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
  Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
  r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
  Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
											
										 
											2009-04-05 21:24:58 +00:00
										 |  |  | #ifdef HAVE_BROKEN_SEM_GETVALUE
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyErr_SetNone(PyExc_NotImplementedError); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int sval; | 
					
						
							|  |  |  |     if (SEM_GETVALUE(self->handle, &sval) < 0) | 
					
						
							| 
									
										
										
										
											2012-10-07 18:08:47 +01:00
										 |  |  |         return _PyMp_SetError(NULL, MP_STANDARD_ERROR); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     /* some posix implementations use negative numbers to indicate
 | 
					
						
							|  |  |  |        the number of waiting threads */ | 
					
						
							|  |  |  |     if (sval < 0) | 
					
						
							|  |  |  |         sval = 0; | 
					
						
							| 
									
										
										
										
											2011-05-09 20:55:03 +02:00
										 |  |  |     return PyLong_FromLong((long)sval); | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _multiprocessing.SemLock._is_zero | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return whether semaphore has value zero. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock__is_zero_impl(SemLockObject *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=815d4c878c806ed7 input=294a446418d31347]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
											
												Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
  Issue 5619: Pass MS CRT debug flags into subprocesses
........
  r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
  Fix multiprocessing.event to match the new threading.Event API
........
  r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
  issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
  r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
  Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
  r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
  Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
											
										 
											2009-04-05 21:24:58 +00:00
										 |  |  | #ifdef HAVE_BROKEN_SEM_GETVALUE
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (sem_trywait(self->handle) < 0) { | 
					
						
							|  |  |  |         if (errno == EAGAIN) | 
					
						
							|  |  |  |             Py_RETURN_TRUE; | 
					
						
							| 
									
										
										
										
											2012-10-07 18:08:47 +01:00
										 |  |  |         return _PyMp_SetError(NULL, MP_STANDARD_ERROR); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         if (sem_post(self->handle) < 0) | 
					
						
							| 
									
										
										
										
											2012-10-07 18:08:47 +01:00
										 |  |  |             return _PyMp_SetError(NULL, MP_STANDARD_ERROR); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         Py_RETURN_FALSE; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     int sval; | 
					
						
							|  |  |  |     if (SEM_GETVALUE(self->handle, &sval) < 0) | 
					
						
							| 
									
										
										
										
											2012-10-07 18:08:47 +01:00
										 |  |  |         return _PyMp_SetError(NULL, MP_STANDARD_ERROR); | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     return PyBool_FromLong((long)sval == 0); | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _multiprocessing.SemLock._after_fork | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Rezero the net acquisition count after fork(). | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing_SemLock__after_fork_impl(SemLockObject *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=718bb27914c6a6c1 input=190991008a76621e]*/ | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     self->count = 0; | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2024-05-09 09:05:52 -07:00
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing.SemLock.__enter__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Enter the semaphore/lock. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _multiprocessing_SemLock___enter___impl(SemLockObject *self) | 
					
						
							| 
									
										
										
										
											2024-05-09 09:05:52 -07:00
										 |  |  | /*[clinic end generated code: output=beeb2f07c858511f input=d35c9860992ee790]*/ | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     return _multiprocessing_SemLock_acquire_impl(self, 1, Py_None); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							| 
									
										
										
										
											2024-05-09 09:05:52 -07:00
										 |  |  | @critical_section | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _multiprocessing.SemLock.__exit__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     exc_type: object = None | 
					
						
							|  |  |  |     exc_value: object = None | 
					
						
							|  |  |  |     exc_tb: object = None | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Exit the semaphore/lock. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _multiprocessing_SemLock___exit___impl(SemLockObject *self, | 
					
						
							|  |  |  |                                        PyObject *exc_type, | 
					
						
							|  |  |  |                                        PyObject *exc_value, PyObject *exc_tb) | 
					
						
							| 
									
										
										
										
											2024-05-09 09:05:52 -07:00
										 |  |  | /*[clinic end generated code: output=3b37c1a9f8b91a03 input=1610c8cc3e0e337e]*/ | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     return _multiprocessing_SemLock_release_impl(self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 00:56:01 +05:30
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-01-20 21:43:55 +01:00
										 |  |  | semlock_traverse(PyObject *s, visitproc visit, void *arg) | 
					
						
							| 
									
										
										
										
											2022-07-21 00:56:01 +05:30
										 |  |  | { | 
					
						
							|  |  |  |     Py_VISIT(Py_TYPE(s)); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Semaphore methods | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef semlock_methods[] = { | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  |     _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF | 
					
						
							|  |  |  |     _MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF | 
					
						
							|  |  |  |     _MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF | 
					
						
							|  |  |  |     _MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF | 
					
						
							|  |  |  |     _MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF | 
					
						
							|  |  |  |     _MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF | 
					
						
							|  |  |  |     _MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF | 
					
						
							|  |  |  |     _MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF | 
					
						
							|  |  |  |     _MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF | 
					
						
							|  |  |  |     _MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     {NULL} | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Member table | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMemberDef semlock_members[] = { | 
					
						
							| 
									
										
										
										
											2023-07-25 15:28:30 +02:00
										 |  |  |     {"handle", T_SEM_HANDLE, offsetof(SemLockObject, handle), Py_READONLY, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |      ""}, | 
					
						
							| 
									
										
										
										
											2023-07-25 15:28:30 +02:00
										 |  |  |     {"kind", Py_T_INT, offsetof(SemLockObject, kind), Py_READONLY, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |      ""}, | 
					
						
							| 
									
										
										
										
											2023-07-25 15:28:30 +02:00
										 |  |  |     {"maxvalue", Py_T_INT, offsetof(SemLockObject, maxvalue), Py_READONLY, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |      ""}, | 
					
						
							| 
									
										
										
										
											2023-07-25 15:28:30 +02:00
										 |  |  |     {"name", Py_T_STRING, offsetof(SemLockObject, name), Py_READONLY, | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  |      ""}, | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     {NULL} | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Semaphore type | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 00:56:01 +05:30
										 |  |  | static PyType_Slot _PyMp_SemLockType_slots[] = { | 
					
						
							|  |  |  |     {Py_tp_dealloc, semlock_dealloc}, | 
					
						
							|  |  |  |     {Py_tp_getattro, PyObject_GenericGetAttr}, | 
					
						
							|  |  |  |     {Py_tp_setattro, PyObject_GenericSetAttr}, | 
					
						
							|  |  |  |     {Py_tp_methods, semlock_methods}, | 
					
						
							|  |  |  |     {Py_tp_members, semlock_members}, | 
					
						
							|  |  |  |     {Py_tp_alloc, PyType_GenericAlloc}, | 
					
						
							|  |  |  |     {Py_tp_new, _multiprocessing_SemLock}, | 
					
						
							|  |  |  |     {Py_tp_traverse, semlock_traverse}, | 
					
						
							|  |  |  |     {Py_tp_free, PyObject_GC_Del}, | 
					
						
							|  |  |  |     {Py_tp_doc, (void *)PyDoc_STR("Semaphore/Mutex type")}, | 
					
						
							|  |  |  |     {0, 0}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyType_Spec _PyMp_SemLockType_spec = { | 
					
						
							|  |  |  |     .name = "_multiprocessing.SemLock", | 
					
						
							|  |  |  |     .basicsize = sizeof(SemLockObject), | 
					
						
							|  |  |  |     .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | | 
					
						
							|  |  |  |               Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE), | 
					
						
							|  |  |  |     .slots = _PyMp_SemLockType_slots, | 
					
						
							| 
									
										
										
										
											2008-06-11 16:44:04 +00:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Function to unlink semaphore names | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2020-07-12 10:11:11 -06:00
										 |  |  | _PyMp_sem_unlink(const char *name) | 
					
						
							| 
									
										
										
										
											2013-08-14 15:35:41 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (SEM_UNLINK(name) < 0) { | 
					
						
							|  |  |  |         _PyMp_SetError(NULL, MP_STANDARD_ERROR); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_RETURN_NONE; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-29 11:36:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif // HAVE_MP_SEMAPHORE
 |