semaphore.c 21 KB

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