semaphore.c 21 KB

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