1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9
10
11 #if defined(HAVE_MP_SEMAPHORE) && defined(MS_WINDOWS)
12
13 PyDoc_STRVAR(_multiprocessing_SemLock_acquire__doc__,
14 "acquire($self, /, block=True, timeout=None)\n"
15 "--\n"
16 "\n"
17 "Acquire the semaphore/lock.");
18
19 #define _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF \
20 {"acquire", _PyCFunction_CAST(_multiprocessing_SemLock_acquire), METH_FASTCALL|METH_KEYWORDS, _multiprocessing_SemLock_acquire__doc__},
21
22 static PyObject *
23 _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
24 PyObject *timeout_obj);
25
26 static PyObject *
27 _multiprocessing_SemLock_acquire(SemLockObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
28 {
29 PyObject *return_value = NULL;
30 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
31
32 #define NUM_KEYWORDS 2
33 static struct {
34 PyGC_Head _this_is_not_used;
35 PyObject_VAR_HEAD
36 PyObject *ob_item[NUM_KEYWORDS];
37 } _kwtuple = {
38 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
39 .ob_item = { &_Py_ID(block), &_Py_ID(timeout), },
40 };
41 #undef NUM_KEYWORDS
42 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
43
44 #else // !Py_BUILD_CORE
45 # define KWTUPLE NULL
46 #endif // !Py_BUILD_CORE
47
48 static const char * const _keywords[] = {"block", "timeout", NULL};
49 static _PyArg_Parser _parser = {
50 .keywords = _keywords,
51 .fname = "acquire",
52 .kwtuple = KWTUPLE,
53 };
54 #undef KWTUPLE
55 PyObject *argsbuf[2];
56 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
57 int blocking = 1;
58 PyObject *timeout_obj = Py_None;
59
60 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
61 if (!args) {
62 goto exit;
63 }
64 if (!noptargs) {
65 goto skip_optional_pos;
66 }
67 if (args[0]) {
68 blocking = PyObject_IsTrue(args[0]);
69 if (blocking < 0) {
70 goto exit;
71 }
72 if (!--noptargs) {
73 goto skip_optional_pos;
74 }
75 }
76 timeout_obj = args[1];
77 skip_optional_pos:
78 return_value = _multiprocessing_SemLock_acquire_impl(self, blocking, timeout_obj);
79
80 exit:
81 return return_value;
82 }
83
84 #endif /* defined(HAVE_MP_SEMAPHORE) && defined(MS_WINDOWS) */
85
86 #if defined(HAVE_MP_SEMAPHORE) && defined(MS_WINDOWS)
87
88 PyDoc_STRVAR(_multiprocessing_SemLock_release__doc__,
89 "release($self, /)\n"
90 "--\n"
91 "\n"
92 "Release the semaphore/lock.");
93
94 #define _MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF \
95 {"release", (PyCFunction)_multiprocessing_SemLock_release, METH_NOARGS, _multiprocessing_SemLock_release__doc__},
96
97 static PyObject *
98 _multiprocessing_SemLock_release_impl(SemLockObject *self);
99
100 static PyObject *
101 _multiprocessing_SemLock_release(SemLockObject *self, PyObject *Py_UNUSED(ignored))
102 {
103 return _multiprocessing_SemLock_release_impl(self);
104 }
105
106 #endif /* defined(HAVE_MP_SEMAPHORE) && defined(MS_WINDOWS) */
107
108 #if defined(HAVE_MP_SEMAPHORE) && !defined(MS_WINDOWS)
109
110 PyDoc_STRVAR(_multiprocessing_SemLock_acquire__doc__,
111 "acquire($self, /, block=True, timeout=None)\n"
112 "--\n"
113 "\n"
114 "Acquire the semaphore/lock.");
115
116 #define _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF \
117 {"acquire", _PyCFunction_CAST(_multiprocessing_SemLock_acquire), METH_FASTCALL|METH_KEYWORDS, _multiprocessing_SemLock_acquire__doc__},
118
119 static PyObject *
120 _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
121 PyObject *timeout_obj);
122
123 static PyObject *
124 _multiprocessing_SemLock_acquire(SemLockObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
125 {
126 PyObject *return_value = NULL;
127 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
128
129 #define NUM_KEYWORDS 2
130 static struct {
131 PyGC_Head _this_is_not_used;
132 PyObject_VAR_HEAD
133 PyObject *ob_item[NUM_KEYWORDS];
134 } _kwtuple = {
135 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
136 .ob_item = { &_Py_ID(block), &_Py_ID(timeout), },
137 };
138 #undef NUM_KEYWORDS
139 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
140
141 #else // !Py_BUILD_CORE
142 # define KWTUPLE NULL
143 #endif // !Py_BUILD_CORE
144
145 static const char * const _keywords[] = {"block", "timeout", NULL};
146 static _PyArg_Parser _parser = {
147 .keywords = _keywords,
148 .fname = "acquire",
149 .kwtuple = KWTUPLE,
150 };
151 #undef KWTUPLE
152 PyObject *argsbuf[2];
153 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
154 int blocking = 1;
155 PyObject *timeout_obj = Py_None;
156
157 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
158 if (!args) {
159 goto exit;
160 }
161 if (!noptargs) {
162 goto skip_optional_pos;
163 }
164 if (args[0]) {
165 blocking = PyObject_IsTrue(args[0]);
166 if (blocking < 0) {
167 goto exit;
168 }
169 if (!--noptargs) {
170 goto skip_optional_pos;
171 }
172 }
173 timeout_obj = args[1];
174 skip_optional_pos:
175 return_value = _multiprocessing_SemLock_acquire_impl(self, blocking, timeout_obj);
176
177 exit:
178 return return_value;
179 }
180
181 #endif /* defined(HAVE_MP_SEMAPHORE) && !defined(MS_WINDOWS) */
182
183 #if defined(HAVE_MP_SEMAPHORE) && !defined(MS_WINDOWS)
184
185 PyDoc_STRVAR(_multiprocessing_SemLock_release__doc__,
186 "release($self, /)\n"
187 "--\n"
188 "\n"
189 "Release the semaphore/lock.");
190
191 #define _MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF \
192 {"release", (PyCFunction)_multiprocessing_SemLock_release, METH_NOARGS, _multiprocessing_SemLock_release__doc__},
193
194 static PyObject *
195 _multiprocessing_SemLock_release_impl(SemLockObject *self);
196
197 static PyObject *
198 _multiprocessing_SemLock_release(SemLockObject *self, PyObject *Py_UNUSED(ignored))
199 {
200 return _multiprocessing_SemLock_release_impl(self);
201 }
202
203 #endif /* defined(HAVE_MP_SEMAPHORE) && !defined(MS_WINDOWS) */
204
205 #if defined(HAVE_MP_SEMAPHORE)
206
207 static PyObject *
208 _multiprocessing_SemLock_impl(PyTypeObject *type, int kind, int value,
209 int maxvalue, const char *name, int unlink);
210
211 static PyObject *
212 _multiprocessing_SemLock(PyTypeObject *type, PyObject *args, PyObject *kwargs)
213 {
214 PyObject *return_value = NULL;
215 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
216
217 #define NUM_KEYWORDS 5
218 static struct {
219 PyGC_Head _this_is_not_used;
220 PyObject_VAR_HEAD
221 PyObject *ob_item[NUM_KEYWORDS];
222 } _kwtuple = {
223 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
224 .ob_item = { &_Py_ID(kind), &_Py_ID(value), &_Py_ID(maxvalue), &_Py_ID(name), &_Py_ID(unlink), },
225 };
226 #undef NUM_KEYWORDS
227 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
228
229 #else // !Py_BUILD_CORE
230 # define KWTUPLE NULL
231 #endif // !Py_BUILD_CORE
232
233 static const char * const _keywords[] = {"kind", "value", "maxvalue", "name", "unlink", NULL};
234 static _PyArg_Parser _parser = {
235 .keywords = _keywords,
236 .fname = "SemLock",
237 .kwtuple = KWTUPLE,
238 };
239 #undef KWTUPLE
240 PyObject *argsbuf[5];
241 PyObject * const *fastargs;
242 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
243 int kind;
244 int value;
245 int maxvalue;
246 const char *name;
247 int unlink;
248
249 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 5, 5, 0, argsbuf);
250 if (!fastargs) {
251 goto exit;
252 }
253 kind = _PyLong_AsInt(fastargs[0]);
254 if (kind == -1 && PyErr_Occurred()) {
255 goto exit;
256 }
257 value = _PyLong_AsInt(fastargs[1]);
258 if (value == -1 && PyErr_Occurred()) {
259 goto exit;
260 }
261 maxvalue = _PyLong_AsInt(fastargs[2]);
262 if (maxvalue == -1 && PyErr_Occurred()) {
263 goto exit;
264 }
265 if (!PyUnicode_Check(fastargs[3])) {
266 _PyArg_BadArgument("SemLock", "argument 'name'", "str", fastargs[3]);
267 goto exit;
268 }
269 Py_ssize_t name_length;
270 name = PyUnicode_AsUTF8AndSize(fastargs[3], &name_length);
271 if (name == NULL) {
272 goto exit;
273 }
274 if (strlen(name) != (size_t)name_length) {
275 PyErr_SetString(PyExc_ValueError, "embedded null character");
276 goto exit;
277 }
278 unlink = PyObject_IsTrue(fastargs[4]);
279 if (unlink < 0) {
280 goto exit;
281 }
282 return_value = _multiprocessing_SemLock_impl(type, kind, value, maxvalue, name, unlink);
283
284 exit:
285 return return_value;
286 }
287
288 #endif /* defined(HAVE_MP_SEMAPHORE) */
289
290 #if defined(HAVE_MP_SEMAPHORE)
291
292 PyDoc_STRVAR(_multiprocessing_SemLock__rebuild__doc__,
293 "_rebuild($type, handle, kind, maxvalue, name, /)\n"
294 "--\n"
295 "\n");
296
297 #define _MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF \
298 {"_rebuild", _PyCFunction_CAST(_multiprocessing_SemLock__rebuild), METH_FASTCALL|METH_CLASS, _multiprocessing_SemLock__rebuild__doc__},
299
300 static PyObject *
301 _multiprocessing_SemLock__rebuild_impl(PyTypeObject *type, SEM_HANDLE handle,
302 int kind, int maxvalue,
303 const char *name);
304
305 static PyObject *
306 _multiprocessing_SemLock__rebuild(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs)
307 {
308 PyObject *return_value = NULL;
309 SEM_HANDLE handle;
310 int kind;
311 int maxvalue;
312 const char *name;
313
314 if (!_PyArg_ParseStack(args, nargs, ""F_SEM_HANDLE"iiz:_rebuild",
315 &handle, &kind, &maxvalue, &name)) {
316 goto exit;
317 }
318 return_value = _multiprocessing_SemLock__rebuild_impl(type, handle, kind, maxvalue, name);
319
320 exit:
321 return return_value;
322 }
323
324 #endif /* defined(HAVE_MP_SEMAPHORE) */
325
326 #if defined(HAVE_MP_SEMAPHORE)
327
328 PyDoc_STRVAR(_multiprocessing_SemLock__count__doc__,
329 "_count($self, /)\n"
330 "--\n"
331 "\n"
332 "Num of `acquire()`s minus num of `release()`s for this process.");
333
334 #define _MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF \
335 {"_count", (PyCFunction)_multiprocessing_SemLock__count, METH_NOARGS, _multiprocessing_SemLock__count__doc__},
336
337 static PyObject *
338 _multiprocessing_SemLock__count_impl(SemLockObject *self);
339
340 static PyObject *
341 _multiprocessing_SemLock__count(SemLockObject *self, PyObject *Py_UNUSED(ignored))
342 {
343 return _multiprocessing_SemLock__count_impl(self);
344 }
345
346 #endif /* defined(HAVE_MP_SEMAPHORE) */
347
348 #if defined(HAVE_MP_SEMAPHORE)
349
350 PyDoc_STRVAR(_multiprocessing_SemLock__is_mine__doc__,
351 "_is_mine($self, /)\n"
352 "--\n"
353 "\n"
354 "Whether the lock is owned by this thread.");
355
356 #define _MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF \
357 {"_is_mine", (PyCFunction)_multiprocessing_SemLock__is_mine, METH_NOARGS, _multiprocessing_SemLock__is_mine__doc__},
358
359 static PyObject *
360 _multiprocessing_SemLock__is_mine_impl(SemLockObject *self);
361
362 static PyObject *
363 _multiprocessing_SemLock__is_mine(SemLockObject *self, PyObject *Py_UNUSED(ignored))
364 {
365 return _multiprocessing_SemLock__is_mine_impl(self);
366 }
367
368 #endif /* defined(HAVE_MP_SEMAPHORE) */
369
370 #if defined(HAVE_MP_SEMAPHORE)
371
372 PyDoc_STRVAR(_multiprocessing_SemLock__get_value__doc__,
373 "_get_value($self, /)\n"
374 "--\n"
375 "\n"
376 "Get the value of the semaphore.");
377
378 #define _MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF \
379 {"_get_value", (PyCFunction)_multiprocessing_SemLock__get_value, METH_NOARGS, _multiprocessing_SemLock__get_value__doc__},
380
381 static PyObject *
382 _multiprocessing_SemLock__get_value_impl(SemLockObject *self);
383
384 static PyObject *
385 _multiprocessing_SemLock__get_value(SemLockObject *self, PyObject *Py_UNUSED(ignored))
386 {
387 return _multiprocessing_SemLock__get_value_impl(self);
388 }
389
390 #endif /* defined(HAVE_MP_SEMAPHORE) */
391
392 #if defined(HAVE_MP_SEMAPHORE)
393
394 PyDoc_STRVAR(_multiprocessing_SemLock__is_zero__doc__,
395 "_is_zero($self, /)\n"
396 "--\n"
397 "\n"
398 "Return whether semaphore has value zero.");
399
400 #define _MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF \
401 {"_is_zero", (PyCFunction)_multiprocessing_SemLock__is_zero, METH_NOARGS, _multiprocessing_SemLock__is_zero__doc__},
402
403 static PyObject *
404 _multiprocessing_SemLock__is_zero_impl(SemLockObject *self);
405
406 static PyObject *
407 _multiprocessing_SemLock__is_zero(SemLockObject *self, PyObject *Py_UNUSED(ignored))
408 {
409 return _multiprocessing_SemLock__is_zero_impl(self);
410 }
411
412 #endif /* defined(HAVE_MP_SEMAPHORE) */
413
414 #if defined(HAVE_MP_SEMAPHORE)
415
416 PyDoc_STRVAR(_multiprocessing_SemLock__after_fork__doc__,
417 "_after_fork($self, /)\n"
418 "--\n"
419 "\n"
420 "Rezero the net acquisition count after fork().");
421
422 #define _MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF \
423 {"_after_fork", (PyCFunction)_multiprocessing_SemLock__after_fork, METH_NOARGS, _multiprocessing_SemLock__after_fork__doc__},
424
425 static PyObject *
426 _multiprocessing_SemLock__after_fork_impl(SemLockObject *self);
427
428 static PyObject *
429 _multiprocessing_SemLock__after_fork(SemLockObject *self, PyObject *Py_UNUSED(ignored))
430 {
431 return _multiprocessing_SemLock__after_fork_impl(self);
432 }
433
434 #endif /* defined(HAVE_MP_SEMAPHORE) */
435
436 #if defined(HAVE_MP_SEMAPHORE)
437
438 PyDoc_STRVAR(_multiprocessing_SemLock___enter____doc__,
439 "__enter__($self, /)\n"
440 "--\n"
441 "\n"
442 "Enter the semaphore/lock.");
443
444 #define _MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF \
445 {"__enter__", (PyCFunction)_multiprocessing_SemLock___enter__, METH_NOARGS, _multiprocessing_SemLock___enter____doc__},
446
447 static PyObject *
448 _multiprocessing_SemLock___enter___impl(SemLockObject *self);
449
450 static PyObject *
451 _multiprocessing_SemLock___enter__(SemLockObject *self, PyObject *Py_UNUSED(ignored))
452 {
453 return _multiprocessing_SemLock___enter___impl(self);
454 }
455
456 #endif /* defined(HAVE_MP_SEMAPHORE) */
457
458 #if defined(HAVE_MP_SEMAPHORE)
459
460 PyDoc_STRVAR(_multiprocessing_SemLock___exit____doc__,
461 "__exit__($self, exc_type=None, exc_value=None, exc_tb=None, /)\n"
462 "--\n"
463 "\n"
464 "Exit the semaphore/lock.");
465
466 #define _MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF \
467 {"__exit__", _PyCFunction_CAST(_multiprocessing_SemLock___exit__), METH_FASTCALL, _multiprocessing_SemLock___exit____doc__},
468
469 static PyObject *
470 _multiprocessing_SemLock___exit___impl(SemLockObject *self,
471 PyObject *exc_type,
472 PyObject *exc_value, PyObject *exc_tb);
473
474 static PyObject *
475 _multiprocessing_SemLock___exit__(SemLockObject *self, PyObject *const *args, Py_ssize_t nargs)
476 {
477 PyObject *return_value = NULL;
478 PyObject *exc_type = Py_None;
479 PyObject *exc_value = Py_None;
480 PyObject *exc_tb = Py_None;
481
482 if (!_PyArg_CheckPositional("__exit__", nargs, 0, 3)) {
483 goto exit;
484 }
485 if (nargs < 1) {
486 goto skip_optional;
487 }
488 exc_type = args[0];
489 if (nargs < 2) {
490 goto skip_optional;
491 }
492 exc_value = args[1];
493 if (nargs < 3) {
494 goto skip_optional;
495 }
496 exc_tb = args[2];
497 skip_optional:
498 return_value = _multiprocessing_SemLock___exit___impl(self, exc_type, exc_value, exc_tb);
499
500 exit:
501 return return_value;
502 }
503
504 #endif /* defined(HAVE_MP_SEMAPHORE) */
505
506 #ifndef _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF
507 #define _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF
508 #endif /* !defined(_MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF) */
509
510 #ifndef _MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF
511 #define _MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF
512 #endif /* !defined(_MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF) */
513
514 #ifndef _MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF
515 #define _MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF
516 #endif /* !defined(_MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF) */
517
518 #ifndef _MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF
519 #define _MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF
520 #endif /* !defined(_MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF) */
521
522 #ifndef _MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF
523 #define _MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF
524 #endif /* !defined(_MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF) */
525
526 #ifndef _MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF
527 #define _MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF
528 #endif /* !defined(_MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF) */
529
530 #ifndef _MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF
531 #define _MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF
532 #endif /* !defined(_MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF) */
533
534 #ifndef _MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF
535 #define _MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF
536 #endif /* !defined(_MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF) */
537
538 #ifndef _MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF
539 #define _MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF
540 #endif /* !defined(_MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF) */
541
542 #ifndef _MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF
543 #define _MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF
544 #endif /* !defined(_MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF) */
545 /*[clinic end generated code: output=dae57a702cc01512 input=a9049054013a1b77]*/