1 | n/a | |
---|
2 | n/a | /* Thread module */ |
---|
3 | n/a | /* Interface to Sjoerd's portable C thread library */ |
---|
4 | n/a | |
---|
5 | n/a | #include "Python.h" |
---|
6 | n/a | #include "structmember.h" /* offsetof */ |
---|
7 | n/a | |
---|
8 | n/a | #ifndef WITH_THREAD |
---|
9 | n/a | #error "Error! The rest of Python is not compiled with thread support." |
---|
10 | n/a | #error "Rerun configure, adding a --with-threads option." |
---|
11 | n/a | #error "Then run `make clean' followed by `make'." |
---|
12 | n/a | #endif |
---|
13 | n/a | |
---|
14 | n/a | #include "pythread.h" |
---|
15 | n/a | |
---|
16 | n/a | static PyObject *ThreadError; |
---|
17 | n/a | static long nb_threads = 0; |
---|
18 | n/a | static PyObject *str_dict; |
---|
19 | n/a | |
---|
20 | n/a | _Py_IDENTIFIER(stderr); |
---|
21 | n/a | |
---|
22 | n/a | /* Lock objects */ |
---|
23 | n/a | |
---|
24 | n/a | typedef struct { |
---|
25 | n/a | PyObject_HEAD |
---|
26 | n/a | PyThread_type_lock lock_lock; |
---|
27 | n/a | PyObject *in_weakreflist; |
---|
28 | n/a | char locked; /* for sanity checking */ |
---|
29 | n/a | } lockobject; |
---|
30 | n/a | |
---|
31 | n/a | static void |
---|
32 | n/a | lock_dealloc(lockobject *self) |
---|
33 | n/a | { |
---|
34 | n/a | if (self->in_weakreflist != NULL) |
---|
35 | n/a | PyObject_ClearWeakRefs((PyObject *) self); |
---|
36 | n/a | if (self->lock_lock != NULL) { |
---|
37 | n/a | /* Unlock the lock so it's safe to free it */ |
---|
38 | n/a | if (self->locked) |
---|
39 | n/a | PyThread_release_lock(self->lock_lock); |
---|
40 | n/a | PyThread_free_lock(self->lock_lock); |
---|
41 | n/a | } |
---|
42 | n/a | PyObject_Del(self); |
---|
43 | n/a | } |
---|
44 | n/a | |
---|
45 | n/a | /* Helper to acquire an interruptible lock with a timeout. If the lock acquire |
---|
46 | n/a | * is interrupted, signal handlers are run, and if they raise an exception, |
---|
47 | n/a | * PY_LOCK_INTR is returned. Otherwise, PY_LOCK_ACQUIRED or PY_LOCK_FAILURE |
---|
48 | n/a | * are returned, depending on whether the lock can be acquired within the |
---|
49 | n/a | * timeout. |
---|
50 | n/a | */ |
---|
51 | n/a | static PyLockStatus |
---|
52 | n/a | acquire_timed(PyThread_type_lock lock, _PyTime_t timeout) |
---|
53 | n/a | { |
---|
54 | n/a | PyLockStatus r; |
---|
55 | n/a | _PyTime_t endtime = 0; |
---|
56 | n/a | _PyTime_t microseconds; |
---|
57 | n/a | |
---|
58 | n/a | if (timeout > 0) |
---|
59 | n/a | endtime = _PyTime_GetMonotonicClock() + timeout; |
---|
60 | n/a | |
---|
61 | n/a | do { |
---|
62 | n/a | microseconds = _PyTime_AsMicroseconds(timeout, _PyTime_ROUND_CEILING); |
---|
63 | n/a | |
---|
64 | n/a | /* first a simple non-blocking try without releasing the GIL */ |
---|
65 | n/a | r = PyThread_acquire_lock_timed(lock, 0, 0); |
---|
66 | n/a | if (r == PY_LOCK_FAILURE && microseconds != 0) { |
---|
67 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
68 | n/a | r = PyThread_acquire_lock_timed(lock, microseconds, 1); |
---|
69 | n/a | Py_END_ALLOW_THREADS |
---|
70 | n/a | } |
---|
71 | n/a | |
---|
72 | n/a | if (r == PY_LOCK_INTR) { |
---|
73 | n/a | /* Run signal handlers if we were interrupted. Propagate |
---|
74 | n/a | * exceptions from signal handlers, such as KeyboardInterrupt, by |
---|
75 | n/a | * passing up PY_LOCK_INTR. */ |
---|
76 | n/a | if (Py_MakePendingCalls() < 0) { |
---|
77 | n/a | return PY_LOCK_INTR; |
---|
78 | n/a | } |
---|
79 | n/a | |
---|
80 | n/a | /* If we're using a timeout, recompute the timeout after processing |
---|
81 | n/a | * signals, since those can take time. */ |
---|
82 | n/a | if (timeout > 0) { |
---|
83 | n/a | timeout = endtime - _PyTime_GetMonotonicClock(); |
---|
84 | n/a | |
---|
85 | n/a | /* Check for negative values, since those mean block forever. |
---|
86 | n/a | */ |
---|
87 | n/a | if (timeout < 0) { |
---|
88 | n/a | r = PY_LOCK_FAILURE; |
---|
89 | n/a | } |
---|
90 | n/a | } |
---|
91 | n/a | } |
---|
92 | n/a | } while (r == PY_LOCK_INTR); /* Retry if we were interrupted. */ |
---|
93 | n/a | |
---|
94 | n/a | return r; |
---|
95 | n/a | } |
---|
96 | n/a | |
---|
97 | n/a | static int |
---|
98 | n/a | lock_acquire_parse_args(PyObject *args, PyObject *kwds, |
---|
99 | n/a | _PyTime_t *timeout) |
---|
100 | n/a | { |
---|
101 | n/a | char *kwlist[] = {"blocking", "timeout", NULL}; |
---|
102 | n/a | int blocking = 1; |
---|
103 | n/a | PyObject *timeout_obj = NULL; |
---|
104 | n/a | const _PyTime_t unset_timeout = _PyTime_FromSeconds(-1); |
---|
105 | n/a | |
---|
106 | n/a | *timeout = unset_timeout ; |
---|
107 | n/a | |
---|
108 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO:acquire", kwlist, |
---|
109 | n/a | &blocking, &timeout_obj)) |
---|
110 | n/a | return -1; |
---|
111 | n/a | |
---|
112 | n/a | if (timeout_obj |
---|
113 | n/a | && _PyTime_FromSecondsObject(timeout, |
---|
114 | n/a | timeout_obj, _PyTime_ROUND_CEILING) < 0) |
---|
115 | n/a | return -1; |
---|
116 | n/a | |
---|
117 | n/a | if (!blocking && *timeout != unset_timeout ) { |
---|
118 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
119 | n/a | "can't specify a timeout for a non-blocking call"); |
---|
120 | n/a | return -1; |
---|
121 | n/a | } |
---|
122 | n/a | if (*timeout < 0 && *timeout != unset_timeout) { |
---|
123 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
124 | n/a | "timeout value must be positive"); |
---|
125 | n/a | return -1; |
---|
126 | n/a | } |
---|
127 | n/a | if (!blocking) |
---|
128 | n/a | *timeout = 0; |
---|
129 | n/a | else if (*timeout != unset_timeout) { |
---|
130 | n/a | _PyTime_t microseconds; |
---|
131 | n/a | |
---|
132 | n/a | microseconds = _PyTime_AsMicroseconds(*timeout, _PyTime_ROUND_CEILING); |
---|
133 | n/a | if (microseconds >= PY_TIMEOUT_MAX) { |
---|
134 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
135 | n/a | "timeout value is too large"); |
---|
136 | n/a | return -1; |
---|
137 | n/a | } |
---|
138 | n/a | } |
---|
139 | n/a | return 0; |
---|
140 | n/a | } |
---|
141 | n/a | |
---|
142 | n/a | static PyObject * |
---|
143 | n/a | lock_PyThread_acquire_lock(lockobject *self, PyObject *args, PyObject *kwds) |
---|
144 | n/a | { |
---|
145 | n/a | _PyTime_t timeout; |
---|
146 | n/a | PyLockStatus r; |
---|
147 | n/a | |
---|
148 | n/a | if (lock_acquire_parse_args(args, kwds, &timeout) < 0) |
---|
149 | n/a | return NULL; |
---|
150 | n/a | |
---|
151 | n/a | r = acquire_timed(self->lock_lock, timeout); |
---|
152 | n/a | if (r == PY_LOCK_INTR) { |
---|
153 | n/a | return NULL; |
---|
154 | n/a | } |
---|
155 | n/a | |
---|
156 | n/a | if (r == PY_LOCK_ACQUIRED) |
---|
157 | n/a | self->locked = 1; |
---|
158 | n/a | return PyBool_FromLong(r == PY_LOCK_ACQUIRED); |
---|
159 | n/a | } |
---|
160 | n/a | |
---|
161 | n/a | PyDoc_STRVAR(acquire_doc, |
---|
162 | n/a | "acquire(blocking=True, timeout=-1) -> bool\n\ |
---|
163 | n/a | (acquire_lock() is an obsolete synonym)\n\ |
---|
164 | n/a | \n\ |
---|
165 | n/a | Lock the lock. Without argument, this blocks if the lock is already\n\ |
---|
166 | n/a | locked (even by the same thread), waiting for another thread to release\n\ |
---|
167 | n/a | the lock, and return True once the lock is acquired.\n\ |
---|
168 | n/a | With an argument, this will only block if the argument is true,\n\ |
---|
169 | n/a | and the return value reflects whether the lock is acquired.\n\ |
---|
170 | n/a | The blocking operation is interruptible."); |
---|
171 | n/a | |
---|
172 | n/a | static PyObject * |
---|
173 | n/a | lock_PyThread_release_lock(lockobject *self) |
---|
174 | n/a | { |
---|
175 | n/a | /* Sanity check: the lock must be locked */ |
---|
176 | n/a | if (!self->locked) { |
---|
177 | n/a | PyErr_SetString(ThreadError, "release unlocked lock"); |
---|
178 | n/a | return NULL; |
---|
179 | n/a | } |
---|
180 | n/a | |
---|
181 | n/a | PyThread_release_lock(self->lock_lock); |
---|
182 | n/a | self->locked = 0; |
---|
183 | n/a | Py_RETURN_NONE; |
---|
184 | n/a | } |
---|
185 | n/a | |
---|
186 | n/a | PyDoc_STRVAR(release_doc, |
---|
187 | n/a | "release()\n\ |
---|
188 | n/a | (release_lock() is an obsolete synonym)\n\ |
---|
189 | n/a | \n\ |
---|
190 | n/a | Release the lock, allowing another thread that is blocked waiting for\n\ |
---|
191 | n/a | the lock to acquire the lock. The lock must be in the locked state,\n\ |
---|
192 | n/a | but it needn't be locked by the same thread that unlocks it."); |
---|
193 | n/a | |
---|
194 | n/a | static PyObject * |
---|
195 | n/a | lock_locked_lock(lockobject *self) |
---|
196 | n/a | { |
---|
197 | n/a | return PyBool_FromLong((long)self->locked); |
---|
198 | n/a | } |
---|
199 | n/a | |
---|
200 | n/a | PyDoc_STRVAR(locked_doc, |
---|
201 | n/a | "locked() -> bool\n\ |
---|
202 | n/a | (locked_lock() is an obsolete synonym)\n\ |
---|
203 | n/a | \n\ |
---|
204 | n/a | Return whether the lock is in the locked state."); |
---|
205 | n/a | |
---|
206 | n/a | static PyObject * |
---|
207 | n/a | lock_repr(lockobject *self) |
---|
208 | n/a | { |
---|
209 | n/a | return PyUnicode_FromFormat("<%s %s object at %p>", |
---|
210 | n/a | self->locked ? "locked" : "unlocked", Py_TYPE(self)->tp_name, self); |
---|
211 | n/a | } |
---|
212 | n/a | |
---|
213 | n/a | static PyMethodDef lock_methods[] = { |
---|
214 | n/a | {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, |
---|
215 | n/a | METH_VARARGS | METH_KEYWORDS, acquire_doc}, |
---|
216 | n/a | {"acquire", (PyCFunction)lock_PyThread_acquire_lock, |
---|
217 | n/a | METH_VARARGS | METH_KEYWORDS, acquire_doc}, |
---|
218 | n/a | {"release_lock", (PyCFunction)lock_PyThread_release_lock, |
---|
219 | n/a | METH_NOARGS, release_doc}, |
---|
220 | n/a | {"release", (PyCFunction)lock_PyThread_release_lock, |
---|
221 | n/a | METH_NOARGS, release_doc}, |
---|
222 | n/a | {"locked_lock", (PyCFunction)lock_locked_lock, |
---|
223 | n/a | METH_NOARGS, locked_doc}, |
---|
224 | n/a | {"locked", (PyCFunction)lock_locked_lock, |
---|
225 | n/a | METH_NOARGS, locked_doc}, |
---|
226 | n/a | {"__enter__", (PyCFunction)lock_PyThread_acquire_lock, |
---|
227 | n/a | METH_VARARGS | METH_KEYWORDS, acquire_doc}, |
---|
228 | n/a | {"__exit__", (PyCFunction)lock_PyThread_release_lock, |
---|
229 | n/a | METH_VARARGS, release_doc}, |
---|
230 | n/a | {NULL, NULL} /* sentinel */ |
---|
231 | n/a | }; |
---|
232 | n/a | |
---|
233 | n/a | static PyTypeObject Locktype = { |
---|
234 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
235 | n/a | "_thread.lock", /*tp_name*/ |
---|
236 | n/a | sizeof(lockobject), /*tp_size*/ |
---|
237 | n/a | 0, /*tp_itemsize*/ |
---|
238 | n/a | /* methods */ |
---|
239 | n/a | (destructor)lock_dealloc, /*tp_dealloc*/ |
---|
240 | n/a | 0, /*tp_print*/ |
---|
241 | n/a | 0, /*tp_getattr*/ |
---|
242 | n/a | 0, /*tp_setattr*/ |
---|
243 | n/a | 0, /*tp_reserved*/ |
---|
244 | n/a | (reprfunc)lock_repr, /*tp_repr*/ |
---|
245 | n/a | 0, /*tp_as_number*/ |
---|
246 | n/a | 0, /*tp_as_sequence*/ |
---|
247 | n/a | 0, /*tp_as_mapping*/ |
---|
248 | n/a | 0, /*tp_hash*/ |
---|
249 | n/a | 0, /*tp_call*/ |
---|
250 | n/a | 0, /*tp_str*/ |
---|
251 | n/a | 0, /*tp_getattro*/ |
---|
252 | n/a | 0, /*tp_setattro*/ |
---|
253 | n/a | 0, /*tp_as_buffer*/ |
---|
254 | n/a | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
---|
255 | n/a | 0, /*tp_doc*/ |
---|
256 | n/a | 0, /*tp_traverse*/ |
---|
257 | n/a | 0, /*tp_clear*/ |
---|
258 | n/a | 0, /*tp_richcompare*/ |
---|
259 | n/a | offsetof(lockobject, in_weakreflist), /*tp_weaklistoffset*/ |
---|
260 | n/a | 0, /*tp_iter*/ |
---|
261 | n/a | 0, /*tp_iternext*/ |
---|
262 | n/a | lock_methods, /*tp_methods*/ |
---|
263 | n/a | }; |
---|
264 | n/a | |
---|
265 | n/a | /* Recursive lock objects */ |
---|
266 | n/a | |
---|
267 | n/a | typedef struct { |
---|
268 | n/a | PyObject_HEAD |
---|
269 | n/a | PyThread_type_lock rlock_lock; |
---|
270 | n/a | long rlock_owner; |
---|
271 | n/a | unsigned long rlock_count; |
---|
272 | n/a | PyObject *in_weakreflist; |
---|
273 | n/a | } rlockobject; |
---|
274 | n/a | |
---|
275 | n/a | static void |
---|
276 | n/a | rlock_dealloc(rlockobject *self) |
---|
277 | n/a | { |
---|
278 | n/a | if (self->in_weakreflist != NULL) |
---|
279 | n/a | PyObject_ClearWeakRefs((PyObject *) self); |
---|
280 | n/a | /* self->rlock_lock can be NULL if PyThread_allocate_lock() failed |
---|
281 | n/a | in rlock_new() */ |
---|
282 | n/a | if (self->rlock_lock != NULL) { |
---|
283 | n/a | /* Unlock the lock so it's safe to free it */ |
---|
284 | n/a | if (self->rlock_count > 0) |
---|
285 | n/a | PyThread_release_lock(self->rlock_lock); |
---|
286 | n/a | |
---|
287 | n/a | PyThread_free_lock(self->rlock_lock); |
---|
288 | n/a | } |
---|
289 | n/a | Py_TYPE(self)->tp_free(self); |
---|
290 | n/a | } |
---|
291 | n/a | |
---|
292 | n/a | static PyObject * |
---|
293 | n/a | rlock_acquire(rlockobject *self, PyObject *args, PyObject *kwds) |
---|
294 | n/a | { |
---|
295 | n/a | _PyTime_t timeout; |
---|
296 | n/a | long tid; |
---|
297 | n/a | PyLockStatus r = PY_LOCK_ACQUIRED; |
---|
298 | n/a | |
---|
299 | n/a | if (lock_acquire_parse_args(args, kwds, &timeout) < 0) |
---|
300 | n/a | return NULL; |
---|
301 | n/a | |
---|
302 | n/a | tid = PyThread_get_thread_ident(); |
---|
303 | n/a | if (self->rlock_count > 0 && tid == self->rlock_owner) { |
---|
304 | n/a | unsigned long count = self->rlock_count + 1; |
---|
305 | n/a | if (count <= self->rlock_count) { |
---|
306 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
307 | n/a | "Internal lock count overflowed"); |
---|
308 | n/a | return NULL; |
---|
309 | n/a | } |
---|
310 | n/a | self->rlock_count = count; |
---|
311 | n/a | Py_RETURN_TRUE; |
---|
312 | n/a | } |
---|
313 | n/a | r = acquire_timed(self->rlock_lock, timeout); |
---|
314 | n/a | if (r == PY_LOCK_ACQUIRED) { |
---|
315 | n/a | assert(self->rlock_count == 0); |
---|
316 | n/a | self->rlock_owner = tid; |
---|
317 | n/a | self->rlock_count = 1; |
---|
318 | n/a | } |
---|
319 | n/a | else if (r == PY_LOCK_INTR) { |
---|
320 | n/a | return NULL; |
---|
321 | n/a | } |
---|
322 | n/a | |
---|
323 | n/a | return PyBool_FromLong(r == PY_LOCK_ACQUIRED); |
---|
324 | n/a | } |
---|
325 | n/a | |
---|
326 | n/a | PyDoc_STRVAR(rlock_acquire_doc, |
---|
327 | n/a | "acquire(blocking=True) -> bool\n\ |
---|
328 | n/a | \n\ |
---|
329 | n/a | Lock the lock. `blocking` indicates whether we should wait\n\ |
---|
330 | n/a | for the lock to be available or not. If `blocking` is False\n\ |
---|
331 | n/a | and another thread holds the lock, the method will return False\n\ |
---|
332 | n/a | immediately. If `blocking` is True and another thread holds\n\ |
---|
333 | n/a | the lock, the method will wait for the lock to be released,\n\ |
---|
334 | n/a | take it and then return True.\n\ |
---|
335 | n/a | (note: the blocking operation is interruptible.)\n\ |
---|
336 | n/a | \n\ |
---|
337 | n/a | In all other cases, the method will return True immediately.\n\ |
---|
338 | n/a | Precisely, if the current thread already holds the lock, its\n\ |
---|
339 | n/a | internal counter is simply incremented. If nobody holds the lock,\n\ |
---|
340 | n/a | the lock is taken and its internal counter initialized to 1."); |
---|
341 | n/a | |
---|
342 | n/a | static PyObject * |
---|
343 | n/a | rlock_release(rlockobject *self) |
---|
344 | n/a | { |
---|
345 | n/a | long tid = PyThread_get_thread_ident(); |
---|
346 | n/a | |
---|
347 | n/a | if (self->rlock_count == 0 || self->rlock_owner != tid) { |
---|
348 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
349 | n/a | "cannot release un-acquired lock"); |
---|
350 | n/a | return NULL; |
---|
351 | n/a | } |
---|
352 | n/a | if (--self->rlock_count == 0) { |
---|
353 | n/a | self->rlock_owner = 0; |
---|
354 | n/a | PyThread_release_lock(self->rlock_lock); |
---|
355 | n/a | } |
---|
356 | n/a | Py_RETURN_NONE; |
---|
357 | n/a | } |
---|
358 | n/a | |
---|
359 | n/a | PyDoc_STRVAR(rlock_release_doc, |
---|
360 | n/a | "release()\n\ |
---|
361 | n/a | \n\ |
---|
362 | n/a | Release the lock, allowing another thread that is blocked waiting for\n\ |
---|
363 | n/a | the lock to acquire the lock. The lock must be in the locked state,\n\ |
---|
364 | n/a | and must be locked by the same thread that unlocks it; otherwise a\n\ |
---|
365 | n/a | `RuntimeError` is raised.\n\ |
---|
366 | n/a | \n\ |
---|
367 | n/a | Do note that if the lock was acquire()d several times in a row by the\n\ |
---|
368 | n/a | current thread, release() needs to be called as many times for the lock\n\ |
---|
369 | n/a | to be available for other threads."); |
---|
370 | n/a | |
---|
371 | n/a | static PyObject * |
---|
372 | n/a | rlock_acquire_restore(rlockobject *self, PyObject *args) |
---|
373 | n/a | { |
---|
374 | n/a | long owner; |
---|
375 | n/a | unsigned long count; |
---|
376 | n/a | int r = 1; |
---|
377 | n/a | |
---|
378 | n/a | if (!PyArg_ParseTuple(args, "(kl):_acquire_restore", &count, &owner)) |
---|
379 | n/a | return NULL; |
---|
380 | n/a | |
---|
381 | n/a | if (!PyThread_acquire_lock(self->rlock_lock, 0)) { |
---|
382 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
383 | n/a | r = PyThread_acquire_lock(self->rlock_lock, 1); |
---|
384 | n/a | Py_END_ALLOW_THREADS |
---|
385 | n/a | } |
---|
386 | n/a | if (!r) { |
---|
387 | n/a | PyErr_SetString(ThreadError, "couldn't acquire lock"); |
---|
388 | n/a | return NULL; |
---|
389 | n/a | } |
---|
390 | n/a | assert(self->rlock_count == 0); |
---|
391 | n/a | self->rlock_owner = owner; |
---|
392 | n/a | self->rlock_count = count; |
---|
393 | n/a | Py_RETURN_NONE; |
---|
394 | n/a | } |
---|
395 | n/a | |
---|
396 | n/a | PyDoc_STRVAR(rlock_acquire_restore_doc, |
---|
397 | n/a | "_acquire_restore(state) -> None\n\ |
---|
398 | n/a | \n\ |
---|
399 | n/a | For internal use by `threading.Condition`."); |
---|
400 | n/a | |
---|
401 | n/a | static PyObject * |
---|
402 | n/a | rlock_release_save(rlockobject *self) |
---|
403 | n/a | { |
---|
404 | n/a | long owner; |
---|
405 | n/a | unsigned long count; |
---|
406 | n/a | |
---|
407 | n/a | if (self->rlock_count == 0) { |
---|
408 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
409 | n/a | "cannot release un-acquired lock"); |
---|
410 | n/a | return NULL; |
---|
411 | n/a | } |
---|
412 | n/a | |
---|
413 | n/a | owner = self->rlock_owner; |
---|
414 | n/a | count = self->rlock_count; |
---|
415 | n/a | self->rlock_count = 0; |
---|
416 | n/a | self->rlock_owner = 0; |
---|
417 | n/a | PyThread_release_lock(self->rlock_lock); |
---|
418 | n/a | return Py_BuildValue("kl", count, owner); |
---|
419 | n/a | } |
---|
420 | n/a | |
---|
421 | n/a | PyDoc_STRVAR(rlock_release_save_doc, |
---|
422 | n/a | "_release_save() -> tuple\n\ |
---|
423 | n/a | \n\ |
---|
424 | n/a | For internal use by `threading.Condition`."); |
---|
425 | n/a | |
---|
426 | n/a | |
---|
427 | n/a | static PyObject * |
---|
428 | n/a | rlock_is_owned(rlockobject *self) |
---|
429 | n/a | { |
---|
430 | n/a | long tid = PyThread_get_thread_ident(); |
---|
431 | n/a | |
---|
432 | n/a | if (self->rlock_count > 0 && self->rlock_owner == tid) { |
---|
433 | n/a | Py_RETURN_TRUE; |
---|
434 | n/a | } |
---|
435 | n/a | Py_RETURN_FALSE; |
---|
436 | n/a | } |
---|
437 | n/a | |
---|
438 | n/a | PyDoc_STRVAR(rlock_is_owned_doc, |
---|
439 | n/a | "_is_owned() -> bool\n\ |
---|
440 | n/a | \n\ |
---|
441 | n/a | For internal use by `threading.Condition`."); |
---|
442 | n/a | |
---|
443 | n/a | static PyObject * |
---|
444 | n/a | rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
445 | n/a | { |
---|
446 | n/a | rlockobject *self; |
---|
447 | n/a | |
---|
448 | n/a | self = (rlockobject *) type->tp_alloc(type, 0); |
---|
449 | n/a | if (self != NULL) { |
---|
450 | n/a | self->in_weakreflist = NULL; |
---|
451 | n/a | self->rlock_owner = 0; |
---|
452 | n/a | self->rlock_count = 0; |
---|
453 | n/a | |
---|
454 | n/a | self->rlock_lock = PyThread_allocate_lock(); |
---|
455 | n/a | if (self->rlock_lock == NULL) { |
---|
456 | n/a | Py_DECREF(self); |
---|
457 | n/a | PyErr_SetString(ThreadError, "can't allocate lock"); |
---|
458 | n/a | return NULL; |
---|
459 | n/a | } |
---|
460 | n/a | } |
---|
461 | n/a | |
---|
462 | n/a | return (PyObject *) self; |
---|
463 | n/a | } |
---|
464 | n/a | |
---|
465 | n/a | static PyObject * |
---|
466 | n/a | rlock_repr(rlockobject *self) |
---|
467 | n/a | { |
---|
468 | n/a | return PyUnicode_FromFormat("<%s %s object owner=%ld count=%lu at %p>", |
---|
469 | n/a | self->rlock_count ? "locked" : "unlocked", |
---|
470 | n/a | Py_TYPE(self)->tp_name, self->rlock_owner, |
---|
471 | n/a | self->rlock_count, self); |
---|
472 | n/a | } |
---|
473 | n/a | |
---|
474 | n/a | |
---|
475 | n/a | static PyMethodDef rlock_methods[] = { |
---|
476 | n/a | {"acquire", (PyCFunction)rlock_acquire, |
---|
477 | n/a | METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc}, |
---|
478 | n/a | {"release", (PyCFunction)rlock_release, |
---|
479 | n/a | METH_NOARGS, rlock_release_doc}, |
---|
480 | n/a | {"_is_owned", (PyCFunction)rlock_is_owned, |
---|
481 | n/a | METH_NOARGS, rlock_is_owned_doc}, |
---|
482 | n/a | {"_acquire_restore", (PyCFunction)rlock_acquire_restore, |
---|
483 | n/a | METH_VARARGS, rlock_acquire_restore_doc}, |
---|
484 | n/a | {"_release_save", (PyCFunction)rlock_release_save, |
---|
485 | n/a | METH_NOARGS, rlock_release_save_doc}, |
---|
486 | n/a | {"__enter__", (PyCFunction)rlock_acquire, |
---|
487 | n/a | METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc}, |
---|
488 | n/a | {"__exit__", (PyCFunction)rlock_release, |
---|
489 | n/a | METH_VARARGS, rlock_release_doc}, |
---|
490 | n/a | {NULL, NULL} /* sentinel */ |
---|
491 | n/a | }; |
---|
492 | n/a | |
---|
493 | n/a | |
---|
494 | n/a | static PyTypeObject RLocktype = { |
---|
495 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
496 | n/a | "_thread.RLock", /*tp_name*/ |
---|
497 | n/a | sizeof(rlockobject), /*tp_size*/ |
---|
498 | n/a | 0, /*tp_itemsize*/ |
---|
499 | n/a | /* methods */ |
---|
500 | n/a | (destructor)rlock_dealloc, /*tp_dealloc*/ |
---|
501 | n/a | 0, /*tp_print*/ |
---|
502 | n/a | 0, /*tp_getattr*/ |
---|
503 | n/a | 0, /*tp_setattr*/ |
---|
504 | n/a | 0, /*tp_reserved*/ |
---|
505 | n/a | (reprfunc)rlock_repr, /*tp_repr*/ |
---|
506 | n/a | 0, /*tp_as_number*/ |
---|
507 | n/a | 0, /*tp_as_sequence*/ |
---|
508 | n/a | 0, /*tp_as_mapping*/ |
---|
509 | n/a | 0, /*tp_hash*/ |
---|
510 | n/a | 0, /*tp_call*/ |
---|
511 | n/a | 0, /*tp_str*/ |
---|
512 | n/a | 0, /*tp_getattro*/ |
---|
513 | n/a | 0, /*tp_setattro*/ |
---|
514 | n/a | 0, /*tp_as_buffer*/ |
---|
515 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
---|
516 | n/a | 0, /*tp_doc*/ |
---|
517 | n/a | 0, /*tp_traverse*/ |
---|
518 | n/a | 0, /*tp_clear*/ |
---|
519 | n/a | 0, /*tp_richcompare*/ |
---|
520 | n/a | offsetof(rlockobject, in_weakreflist), /*tp_weaklistoffset*/ |
---|
521 | n/a | 0, /*tp_iter*/ |
---|
522 | n/a | 0, /*tp_iternext*/ |
---|
523 | n/a | rlock_methods, /*tp_methods*/ |
---|
524 | n/a | 0, /* tp_members */ |
---|
525 | n/a | 0, /* tp_getset */ |
---|
526 | n/a | 0, /* tp_base */ |
---|
527 | n/a | 0, /* tp_dict */ |
---|
528 | n/a | 0, /* tp_descr_get */ |
---|
529 | n/a | 0, /* tp_descr_set */ |
---|
530 | n/a | 0, /* tp_dictoffset */ |
---|
531 | n/a | 0, /* tp_init */ |
---|
532 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
533 | n/a | rlock_new /* tp_new */ |
---|
534 | n/a | }; |
---|
535 | n/a | |
---|
536 | n/a | static lockobject * |
---|
537 | n/a | newlockobject(void) |
---|
538 | n/a | { |
---|
539 | n/a | lockobject *self; |
---|
540 | n/a | self = PyObject_New(lockobject, &Locktype); |
---|
541 | n/a | if (self == NULL) |
---|
542 | n/a | return NULL; |
---|
543 | n/a | self->lock_lock = PyThread_allocate_lock(); |
---|
544 | n/a | self->locked = 0; |
---|
545 | n/a | self->in_weakreflist = NULL; |
---|
546 | n/a | if (self->lock_lock == NULL) { |
---|
547 | n/a | Py_DECREF(self); |
---|
548 | n/a | PyErr_SetString(ThreadError, "can't allocate lock"); |
---|
549 | n/a | return NULL; |
---|
550 | n/a | } |
---|
551 | n/a | return self; |
---|
552 | n/a | } |
---|
553 | n/a | |
---|
554 | n/a | /* Thread-local objects */ |
---|
555 | n/a | |
---|
556 | n/a | #include "structmember.h" |
---|
557 | n/a | |
---|
558 | n/a | /* Quick overview: |
---|
559 | n/a | |
---|
560 | n/a | We need to be able to reclaim reference cycles as soon as possible |
---|
561 | n/a | (both when a thread is being terminated, or a thread-local object |
---|
562 | n/a | becomes unreachable from user data). Constraints: |
---|
563 | n/a | - it must not be possible for thread-state dicts to be involved in |
---|
564 | n/a | reference cycles (otherwise the cyclic GC will refuse to consider |
---|
565 | n/a | objects referenced from a reachable thread-state dict, even though |
---|
566 | n/a | local_dealloc would clear them) |
---|
567 | n/a | - the death of a thread-state dict must still imply destruction of the |
---|
568 | n/a | corresponding local dicts in all thread-local objects. |
---|
569 | n/a | |
---|
570 | n/a | Our implementation uses small "localdummy" objects in order to break |
---|
571 | n/a | the reference chain. These trivial objects are hashable (using the |
---|
572 | n/a | default scheme of identity hashing) and weakrefable. |
---|
573 | n/a | Each thread-state holds a separate localdummy for each local object |
---|
574 | n/a | (as a /strong reference/), |
---|
575 | n/a | and each thread-local object holds a dict mapping /weak references/ |
---|
576 | n/a | of localdummies to local dicts. |
---|
577 | n/a | |
---|
578 | n/a | Therefore: |
---|
579 | n/a | - only the thread-state dict holds a strong reference to the dummies |
---|
580 | n/a | - only the thread-local object holds a strong reference to the local dicts |
---|
581 | n/a | - only outside objects (application- or library-level) hold strong |
---|
582 | n/a | references to the thread-local objects |
---|
583 | n/a | - as soon as a thread-state dict is destroyed, the weakref callbacks of all |
---|
584 | n/a | dummies attached to that thread are called, and destroy the corresponding |
---|
585 | n/a | local dicts from thread-local objects |
---|
586 | n/a | - as soon as a thread-local object is destroyed, its local dicts are |
---|
587 | n/a | destroyed and its dummies are manually removed from all thread states |
---|
588 | n/a | - the GC can do its work correctly when a thread-local object is dangling, |
---|
589 | n/a | without any interference from the thread-state dicts |
---|
590 | n/a | |
---|
591 | n/a | As an additional optimization, each localdummy holds a borrowed reference |
---|
592 | n/a | to the corresponding localdict. This borrowed reference is only used |
---|
593 | n/a | by the thread-local object which has created the localdummy, which should |
---|
594 | n/a | guarantee that the localdict still exists when accessed. |
---|
595 | n/a | */ |
---|
596 | n/a | |
---|
597 | n/a | typedef struct { |
---|
598 | n/a | PyObject_HEAD |
---|
599 | n/a | PyObject *localdict; /* Borrowed reference! */ |
---|
600 | n/a | PyObject *weakreflist; /* List of weak references to self */ |
---|
601 | n/a | } localdummyobject; |
---|
602 | n/a | |
---|
603 | n/a | static void |
---|
604 | n/a | localdummy_dealloc(localdummyobject *self) |
---|
605 | n/a | { |
---|
606 | n/a | if (self->weakreflist != NULL) |
---|
607 | n/a | PyObject_ClearWeakRefs((PyObject *) self); |
---|
608 | n/a | Py_TYPE(self)->tp_free((PyObject*)self); |
---|
609 | n/a | } |
---|
610 | n/a | |
---|
611 | n/a | static PyTypeObject localdummytype = { |
---|
612 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
613 | n/a | /* tp_name */ "_thread._localdummy", |
---|
614 | n/a | /* tp_basicsize */ sizeof(localdummyobject), |
---|
615 | n/a | /* tp_itemsize */ 0, |
---|
616 | n/a | /* tp_dealloc */ (destructor)localdummy_dealloc, |
---|
617 | n/a | /* tp_print */ 0, |
---|
618 | n/a | /* tp_getattr */ 0, |
---|
619 | n/a | /* tp_setattr */ 0, |
---|
620 | n/a | /* tp_reserved */ 0, |
---|
621 | n/a | /* tp_repr */ 0, |
---|
622 | n/a | /* tp_as_number */ 0, |
---|
623 | n/a | /* tp_as_sequence */ 0, |
---|
624 | n/a | /* tp_as_mapping */ 0, |
---|
625 | n/a | /* tp_hash */ 0, |
---|
626 | n/a | /* tp_call */ 0, |
---|
627 | n/a | /* tp_str */ 0, |
---|
628 | n/a | /* tp_getattro */ 0, |
---|
629 | n/a | /* tp_setattro */ 0, |
---|
630 | n/a | /* tp_as_buffer */ 0, |
---|
631 | n/a | /* tp_flags */ Py_TPFLAGS_DEFAULT, |
---|
632 | n/a | /* tp_doc */ "Thread-local dummy", |
---|
633 | n/a | /* tp_traverse */ 0, |
---|
634 | n/a | /* tp_clear */ 0, |
---|
635 | n/a | /* tp_richcompare */ 0, |
---|
636 | n/a | /* tp_weaklistoffset */ offsetof(localdummyobject, weakreflist) |
---|
637 | n/a | }; |
---|
638 | n/a | |
---|
639 | n/a | |
---|
640 | n/a | typedef struct { |
---|
641 | n/a | PyObject_HEAD |
---|
642 | n/a | PyObject *key; |
---|
643 | n/a | PyObject *args; |
---|
644 | n/a | PyObject *kw; |
---|
645 | n/a | PyObject *weakreflist; /* List of weak references to self */ |
---|
646 | n/a | /* A {localdummy weakref -> localdict} dict */ |
---|
647 | n/a | PyObject *dummies; |
---|
648 | n/a | /* The callback for weakrefs to localdummies */ |
---|
649 | n/a | PyObject *wr_callback; |
---|
650 | n/a | } localobject; |
---|
651 | n/a | |
---|
652 | n/a | /* Forward declaration */ |
---|
653 | n/a | static PyObject *_ldict(localobject *self); |
---|
654 | n/a | static PyObject *_localdummy_destroyed(PyObject *meth_self, PyObject *dummyweakref); |
---|
655 | n/a | |
---|
656 | n/a | /* Create and register the dummy for the current thread. |
---|
657 | n/a | Returns a borrowed reference of the corresponding local dict */ |
---|
658 | n/a | static PyObject * |
---|
659 | n/a | _local_create_dummy(localobject *self) |
---|
660 | n/a | { |
---|
661 | n/a | PyObject *tdict, *ldict = NULL, *wr = NULL; |
---|
662 | n/a | localdummyobject *dummy = NULL; |
---|
663 | n/a | int r; |
---|
664 | n/a | |
---|
665 | n/a | tdict = PyThreadState_GetDict(); |
---|
666 | n/a | if (tdict == NULL) { |
---|
667 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
668 | n/a | "Couldn't get thread-state dictionary"); |
---|
669 | n/a | goto err; |
---|
670 | n/a | } |
---|
671 | n/a | |
---|
672 | n/a | ldict = PyDict_New(); |
---|
673 | n/a | if (ldict == NULL) |
---|
674 | n/a | goto err; |
---|
675 | n/a | dummy = (localdummyobject *) localdummytype.tp_alloc(&localdummytype, 0); |
---|
676 | n/a | if (dummy == NULL) |
---|
677 | n/a | goto err; |
---|
678 | n/a | dummy->localdict = ldict; |
---|
679 | n/a | wr = PyWeakref_NewRef((PyObject *) dummy, self->wr_callback); |
---|
680 | n/a | if (wr == NULL) |
---|
681 | n/a | goto err; |
---|
682 | n/a | |
---|
683 | n/a | /* As a side-effect, this will cache the weakref's hash before the |
---|
684 | n/a | dummy gets deleted */ |
---|
685 | n/a | r = PyDict_SetItem(self->dummies, wr, ldict); |
---|
686 | n/a | if (r < 0) |
---|
687 | n/a | goto err; |
---|
688 | n/a | Py_CLEAR(wr); |
---|
689 | n/a | r = PyDict_SetItem(tdict, self->key, (PyObject *) dummy); |
---|
690 | n/a | if (r < 0) |
---|
691 | n/a | goto err; |
---|
692 | n/a | Py_CLEAR(dummy); |
---|
693 | n/a | |
---|
694 | n/a | Py_DECREF(ldict); |
---|
695 | n/a | return ldict; |
---|
696 | n/a | |
---|
697 | n/a | err: |
---|
698 | n/a | Py_XDECREF(ldict); |
---|
699 | n/a | Py_XDECREF(wr); |
---|
700 | n/a | Py_XDECREF(dummy); |
---|
701 | n/a | return NULL; |
---|
702 | n/a | } |
---|
703 | n/a | |
---|
704 | n/a | static PyObject * |
---|
705 | n/a | local_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
---|
706 | n/a | { |
---|
707 | n/a | localobject *self; |
---|
708 | n/a | PyObject *wr; |
---|
709 | n/a | static PyMethodDef wr_callback_def = { |
---|
710 | n/a | "_localdummy_destroyed", (PyCFunction) _localdummy_destroyed, METH_O |
---|
711 | n/a | }; |
---|
712 | n/a | |
---|
713 | n/a | if (type->tp_init == PyBaseObject_Type.tp_init) { |
---|
714 | n/a | int rc = 0; |
---|
715 | n/a | if (args != NULL) |
---|
716 | n/a | rc = PyObject_IsTrue(args); |
---|
717 | n/a | if (rc == 0 && kw != NULL) |
---|
718 | n/a | rc = PyObject_IsTrue(kw); |
---|
719 | n/a | if (rc != 0) { |
---|
720 | n/a | if (rc > 0) |
---|
721 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
722 | n/a | "Initialization arguments are not supported"); |
---|
723 | n/a | return NULL; |
---|
724 | n/a | } |
---|
725 | n/a | } |
---|
726 | n/a | |
---|
727 | n/a | self = (localobject *)type->tp_alloc(type, 0); |
---|
728 | n/a | if (self == NULL) |
---|
729 | n/a | return NULL; |
---|
730 | n/a | |
---|
731 | n/a | Py_XINCREF(args); |
---|
732 | n/a | self->args = args; |
---|
733 | n/a | Py_XINCREF(kw); |
---|
734 | n/a | self->kw = kw; |
---|
735 | n/a | self->key = PyUnicode_FromFormat("thread.local.%p", self); |
---|
736 | n/a | if (self->key == NULL) |
---|
737 | n/a | goto err; |
---|
738 | n/a | |
---|
739 | n/a | self->dummies = PyDict_New(); |
---|
740 | n/a | if (self->dummies == NULL) |
---|
741 | n/a | goto err; |
---|
742 | n/a | |
---|
743 | n/a | /* We use a weak reference to self in the callback closure |
---|
744 | n/a | in order to avoid spurious reference cycles */ |
---|
745 | n/a | wr = PyWeakref_NewRef((PyObject *) self, NULL); |
---|
746 | n/a | if (wr == NULL) |
---|
747 | n/a | goto err; |
---|
748 | n/a | self->wr_callback = PyCFunction_NewEx(&wr_callback_def, wr, NULL); |
---|
749 | n/a | Py_DECREF(wr); |
---|
750 | n/a | if (self->wr_callback == NULL) |
---|
751 | n/a | goto err; |
---|
752 | n/a | |
---|
753 | n/a | if (_local_create_dummy(self) == NULL) |
---|
754 | n/a | goto err; |
---|
755 | n/a | |
---|
756 | n/a | return (PyObject *)self; |
---|
757 | n/a | |
---|
758 | n/a | err: |
---|
759 | n/a | Py_DECREF(self); |
---|
760 | n/a | return NULL; |
---|
761 | n/a | } |
---|
762 | n/a | |
---|
763 | n/a | static int |
---|
764 | n/a | local_traverse(localobject *self, visitproc visit, void *arg) |
---|
765 | n/a | { |
---|
766 | n/a | Py_VISIT(self->args); |
---|
767 | n/a | Py_VISIT(self->kw); |
---|
768 | n/a | Py_VISIT(self->dummies); |
---|
769 | n/a | return 0; |
---|
770 | n/a | } |
---|
771 | n/a | |
---|
772 | n/a | static int |
---|
773 | n/a | local_clear(localobject *self) |
---|
774 | n/a | { |
---|
775 | n/a | PyThreadState *tstate; |
---|
776 | n/a | Py_CLEAR(self->args); |
---|
777 | n/a | Py_CLEAR(self->kw); |
---|
778 | n/a | Py_CLEAR(self->dummies); |
---|
779 | n/a | Py_CLEAR(self->wr_callback); |
---|
780 | n/a | /* Remove all strong references to dummies from the thread states */ |
---|
781 | n/a | if (self->key |
---|
782 | n/a | && (tstate = PyThreadState_Get()) |
---|
783 | n/a | && tstate->interp) { |
---|
784 | n/a | for(tstate = PyInterpreterState_ThreadHead(tstate->interp); |
---|
785 | n/a | tstate; |
---|
786 | n/a | tstate = PyThreadState_Next(tstate)) |
---|
787 | n/a | if (tstate->dict && |
---|
788 | n/a | PyDict_GetItem(tstate->dict, self->key)) |
---|
789 | n/a | PyDict_DelItem(tstate->dict, self->key); |
---|
790 | n/a | } |
---|
791 | n/a | return 0; |
---|
792 | n/a | } |
---|
793 | n/a | |
---|
794 | n/a | static void |
---|
795 | n/a | local_dealloc(localobject *self) |
---|
796 | n/a | { |
---|
797 | n/a | /* Weakrefs must be invalidated right now, otherwise they can be used |
---|
798 | n/a | from code called below, which is very dangerous since Py_REFCNT(self) == 0 */ |
---|
799 | n/a | if (self->weakreflist != NULL) |
---|
800 | n/a | PyObject_ClearWeakRefs((PyObject *) self); |
---|
801 | n/a | |
---|
802 | n/a | PyObject_GC_UnTrack(self); |
---|
803 | n/a | |
---|
804 | n/a | local_clear(self); |
---|
805 | n/a | Py_XDECREF(self->key); |
---|
806 | n/a | Py_TYPE(self)->tp_free((PyObject*)self); |
---|
807 | n/a | } |
---|
808 | n/a | |
---|
809 | n/a | /* Returns a borrowed reference to the local dict, creating it if necessary */ |
---|
810 | n/a | static PyObject * |
---|
811 | n/a | _ldict(localobject *self) |
---|
812 | n/a | { |
---|
813 | n/a | PyObject *tdict, *ldict, *dummy; |
---|
814 | n/a | |
---|
815 | n/a | tdict = PyThreadState_GetDict(); |
---|
816 | n/a | if (tdict == NULL) { |
---|
817 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
818 | n/a | "Couldn't get thread-state dictionary"); |
---|
819 | n/a | return NULL; |
---|
820 | n/a | } |
---|
821 | n/a | |
---|
822 | n/a | dummy = PyDict_GetItem(tdict, self->key); |
---|
823 | n/a | if (dummy == NULL) { |
---|
824 | n/a | ldict = _local_create_dummy(self); |
---|
825 | n/a | if (ldict == NULL) |
---|
826 | n/a | return NULL; |
---|
827 | n/a | |
---|
828 | n/a | if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init && |
---|
829 | n/a | Py_TYPE(self)->tp_init((PyObject*)self, |
---|
830 | n/a | self->args, self->kw) < 0) { |
---|
831 | n/a | /* we need to get rid of ldict from thread so |
---|
832 | n/a | we create a new one the next time we do an attr |
---|
833 | n/a | access */ |
---|
834 | n/a | PyDict_DelItem(tdict, self->key); |
---|
835 | n/a | return NULL; |
---|
836 | n/a | } |
---|
837 | n/a | } |
---|
838 | n/a | else { |
---|
839 | n/a | assert(Py_TYPE(dummy) == &localdummytype); |
---|
840 | n/a | ldict = ((localdummyobject *) dummy)->localdict; |
---|
841 | n/a | } |
---|
842 | n/a | |
---|
843 | n/a | return ldict; |
---|
844 | n/a | } |
---|
845 | n/a | |
---|
846 | n/a | static int |
---|
847 | n/a | local_setattro(localobject *self, PyObject *name, PyObject *v) |
---|
848 | n/a | { |
---|
849 | n/a | PyObject *ldict; |
---|
850 | n/a | int r; |
---|
851 | n/a | |
---|
852 | n/a | ldict = _ldict(self); |
---|
853 | n/a | if (ldict == NULL) |
---|
854 | n/a | return -1; |
---|
855 | n/a | |
---|
856 | n/a | r = PyObject_RichCompareBool(name, str_dict, Py_EQ); |
---|
857 | n/a | if (r == 1) { |
---|
858 | n/a | PyErr_Format(PyExc_AttributeError, |
---|
859 | n/a | "'%.50s' object attribute '%U' is read-only", |
---|
860 | n/a | Py_TYPE(self)->tp_name, name); |
---|
861 | n/a | return -1; |
---|
862 | n/a | } |
---|
863 | n/a | if (r == -1) |
---|
864 | n/a | return -1; |
---|
865 | n/a | |
---|
866 | n/a | return _PyObject_GenericSetAttrWithDict((PyObject *)self, name, v, ldict); |
---|
867 | n/a | } |
---|
868 | n/a | |
---|
869 | n/a | static PyObject *local_getattro(localobject *, PyObject *); |
---|
870 | n/a | |
---|
871 | n/a | static PyTypeObject localtype = { |
---|
872 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
873 | n/a | /* tp_name */ "_thread._local", |
---|
874 | n/a | /* tp_basicsize */ sizeof(localobject), |
---|
875 | n/a | /* tp_itemsize */ 0, |
---|
876 | n/a | /* tp_dealloc */ (destructor)local_dealloc, |
---|
877 | n/a | /* tp_print */ 0, |
---|
878 | n/a | /* tp_getattr */ 0, |
---|
879 | n/a | /* tp_setattr */ 0, |
---|
880 | n/a | /* tp_reserved */ 0, |
---|
881 | n/a | /* tp_repr */ 0, |
---|
882 | n/a | /* tp_as_number */ 0, |
---|
883 | n/a | /* tp_as_sequence */ 0, |
---|
884 | n/a | /* tp_as_mapping */ 0, |
---|
885 | n/a | /* tp_hash */ 0, |
---|
886 | n/a | /* tp_call */ 0, |
---|
887 | n/a | /* tp_str */ 0, |
---|
888 | n/a | /* tp_getattro */ (getattrofunc)local_getattro, |
---|
889 | n/a | /* tp_setattro */ (setattrofunc)local_setattro, |
---|
890 | n/a | /* tp_as_buffer */ 0, |
---|
891 | n/a | /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
---|
892 | n/a | | Py_TPFLAGS_HAVE_GC, |
---|
893 | n/a | /* tp_doc */ "Thread-local data", |
---|
894 | n/a | /* tp_traverse */ (traverseproc)local_traverse, |
---|
895 | n/a | /* tp_clear */ (inquiry)local_clear, |
---|
896 | n/a | /* tp_richcompare */ 0, |
---|
897 | n/a | /* tp_weaklistoffset */ offsetof(localobject, weakreflist), |
---|
898 | n/a | /* tp_iter */ 0, |
---|
899 | n/a | /* tp_iternext */ 0, |
---|
900 | n/a | /* tp_methods */ 0, |
---|
901 | n/a | /* tp_members */ 0, |
---|
902 | n/a | /* tp_getset */ 0, |
---|
903 | n/a | /* tp_base */ 0, |
---|
904 | n/a | /* tp_dict */ 0, /* internal use */ |
---|
905 | n/a | /* tp_descr_get */ 0, |
---|
906 | n/a | /* tp_descr_set */ 0, |
---|
907 | n/a | /* tp_dictoffset */ 0, |
---|
908 | n/a | /* tp_init */ 0, |
---|
909 | n/a | /* tp_alloc */ 0, |
---|
910 | n/a | /* tp_new */ local_new, |
---|
911 | n/a | /* tp_free */ 0, /* Low-level free-mem routine */ |
---|
912 | n/a | /* tp_is_gc */ 0, /* For PyObject_IS_GC */ |
---|
913 | n/a | }; |
---|
914 | n/a | |
---|
915 | n/a | static PyObject * |
---|
916 | n/a | local_getattro(localobject *self, PyObject *name) |
---|
917 | n/a | { |
---|
918 | n/a | PyObject *ldict, *value; |
---|
919 | n/a | int r; |
---|
920 | n/a | |
---|
921 | n/a | ldict = _ldict(self); |
---|
922 | n/a | if (ldict == NULL) |
---|
923 | n/a | return NULL; |
---|
924 | n/a | |
---|
925 | n/a | r = PyObject_RichCompareBool(name, str_dict, Py_EQ); |
---|
926 | n/a | if (r == 1) { |
---|
927 | n/a | Py_INCREF(ldict); |
---|
928 | n/a | return ldict; |
---|
929 | n/a | } |
---|
930 | n/a | if (r == -1) |
---|
931 | n/a | return NULL; |
---|
932 | n/a | |
---|
933 | n/a | if (Py_TYPE(self) != &localtype) |
---|
934 | n/a | /* use generic lookup for subtypes */ |
---|
935 | n/a | return _PyObject_GenericGetAttrWithDict((PyObject *)self, name, ldict); |
---|
936 | n/a | |
---|
937 | n/a | /* Optimization: just look in dict ourselves */ |
---|
938 | n/a | value = PyDict_GetItem(ldict, name); |
---|
939 | n/a | if (value == NULL) |
---|
940 | n/a | /* Fall back on generic to get __class__ and __dict__ */ |
---|
941 | n/a | return _PyObject_GenericGetAttrWithDict((PyObject *)self, name, ldict); |
---|
942 | n/a | |
---|
943 | n/a | Py_INCREF(value); |
---|
944 | n/a | return value; |
---|
945 | n/a | } |
---|
946 | n/a | |
---|
947 | n/a | /* Called when a dummy is destroyed. */ |
---|
948 | n/a | static PyObject * |
---|
949 | n/a | _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref) |
---|
950 | n/a | { |
---|
951 | n/a | PyObject *obj; |
---|
952 | n/a | localobject *self; |
---|
953 | n/a | assert(PyWeakref_CheckRef(localweakref)); |
---|
954 | n/a | obj = PyWeakref_GET_OBJECT(localweakref); |
---|
955 | n/a | if (obj == Py_None) |
---|
956 | n/a | Py_RETURN_NONE; |
---|
957 | n/a | Py_INCREF(obj); |
---|
958 | n/a | assert(PyObject_TypeCheck(obj, &localtype)); |
---|
959 | n/a | /* If the thread-local object is still alive and not being cleared, |
---|
960 | n/a | remove the corresponding local dict */ |
---|
961 | n/a | self = (localobject *) obj; |
---|
962 | n/a | if (self->dummies != NULL) { |
---|
963 | n/a | PyObject *ldict; |
---|
964 | n/a | ldict = PyDict_GetItem(self->dummies, dummyweakref); |
---|
965 | n/a | if (ldict != NULL) { |
---|
966 | n/a | PyDict_DelItem(self->dummies, dummyweakref); |
---|
967 | n/a | } |
---|
968 | n/a | if (PyErr_Occurred()) |
---|
969 | n/a | PyErr_WriteUnraisable(obj); |
---|
970 | n/a | } |
---|
971 | n/a | Py_DECREF(obj); |
---|
972 | n/a | Py_RETURN_NONE; |
---|
973 | n/a | } |
---|
974 | n/a | |
---|
975 | n/a | /* Module functions */ |
---|
976 | n/a | |
---|
977 | n/a | struct bootstate { |
---|
978 | n/a | PyInterpreterState *interp; |
---|
979 | n/a | PyObject *func; |
---|
980 | n/a | PyObject *args; |
---|
981 | n/a | PyObject *keyw; |
---|
982 | n/a | PyThreadState *tstate; |
---|
983 | n/a | }; |
---|
984 | n/a | |
---|
985 | n/a | static void |
---|
986 | n/a | t_bootstrap(void *boot_raw) |
---|
987 | n/a | { |
---|
988 | n/a | struct bootstate *boot = (struct bootstate *) boot_raw; |
---|
989 | n/a | PyThreadState *tstate; |
---|
990 | n/a | PyObject *res; |
---|
991 | n/a | |
---|
992 | n/a | tstate = boot->tstate; |
---|
993 | n/a | tstate->thread_id = PyThread_get_thread_ident(); |
---|
994 | n/a | _PyThreadState_Init(tstate); |
---|
995 | n/a | PyEval_AcquireThread(tstate); |
---|
996 | n/a | nb_threads++; |
---|
997 | n/a | res = PyEval_CallObjectWithKeywords( |
---|
998 | n/a | boot->func, boot->args, boot->keyw); |
---|
999 | n/a | if (res == NULL) { |
---|
1000 | n/a | if (PyErr_ExceptionMatches(PyExc_SystemExit)) |
---|
1001 | n/a | PyErr_Clear(); |
---|
1002 | n/a | else { |
---|
1003 | n/a | PyObject *file; |
---|
1004 | n/a | PyObject *exc, *value, *tb; |
---|
1005 | n/a | PySys_WriteStderr( |
---|
1006 | n/a | "Unhandled exception in thread started by "); |
---|
1007 | n/a | PyErr_Fetch(&exc, &value, &tb); |
---|
1008 | n/a | file = _PySys_GetObjectId(&PyId_stderr); |
---|
1009 | n/a | if (file != NULL && file != Py_None) |
---|
1010 | n/a | PyFile_WriteObject(boot->func, file, 0); |
---|
1011 | n/a | else |
---|
1012 | n/a | PyObject_Print(boot->func, stderr, 0); |
---|
1013 | n/a | PySys_WriteStderr("\n"); |
---|
1014 | n/a | PyErr_Restore(exc, value, tb); |
---|
1015 | n/a | PyErr_PrintEx(0); |
---|
1016 | n/a | } |
---|
1017 | n/a | } |
---|
1018 | n/a | else |
---|
1019 | n/a | Py_DECREF(res); |
---|
1020 | n/a | Py_DECREF(boot->func); |
---|
1021 | n/a | Py_DECREF(boot->args); |
---|
1022 | n/a | Py_XDECREF(boot->keyw); |
---|
1023 | n/a | PyMem_DEL(boot_raw); |
---|
1024 | n/a | nb_threads--; |
---|
1025 | n/a | PyThreadState_Clear(tstate); |
---|
1026 | n/a | PyThreadState_DeleteCurrent(); |
---|
1027 | n/a | PyThread_exit_thread(); |
---|
1028 | n/a | } |
---|
1029 | n/a | |
---|
1030 | n/a | static PyObject * |
---|
1031 | n/a | thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) |
---|
1032 | n/a | { |
---|
1033 | n/a | PyObject *func, *args, *keyw = NULL; |
---|
1034 | n/a | struct bootstate *boot; |
---|
1035 | n/a | long ident; |
---|
1036 | n/a | |
---|
1037 | n/a | if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3, |
---|
1038 | n/a | &func, &args, &keyw)) |
---|
1039 | n/a | return NULL; |
---|
1040 | n/a | if (!PyCallable_Check(func)) { |
---|
1041 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1042 | n/a | "first arg must be callable"); |
---|
1043 | n/a | return NULL; |
---|
1044 | n/a | } |
---|
1045 | n/a | if (!PyTuple_Check(args)) { |
---|
1046 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1047 | n/a | "2nd arg must be a tuple"); |
---|
1048 | n/a | return NULL; |
---|
1049 | n/a | } |
---|
1050 | n/a | if (keyw != NULL && !PyDict_Check(keyw)) { |
---|
1051 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1052 | n/a | "optional 3rd arg must be a dictionary"); |
---|
1053 | n/a | return NULL; |
---|
1054 | n/a | } |
---|
1055 | n/a | boot = PyMem_NEW(struct bootstate, 1); |
---|
1056 | n/a | if (boot == NULL) |
---|
1057 | n/a | return PyErr_NoMemory(); |
---|
1058 | n/a | boot->interp = PyThreadState_GET()->interp; |
---|
1059 | n/a | boot->func = func; |
---|
1060 | n/a | boot->args = args; |
---|
1061 | n/a | boot->keyw = keyw; |
---|
1062 | n/a | boot->tstate = _PyThreadState_Prealloc(boot->interp); |
---|
1063 | n/a | if (boot->tstate == NULL) { |
---|
1064 | n/a | PyMem_DEL(boot); |
---|
1065 | n/a | return PyErr_NoMemory(); |
---|
1066 | n/a | } |
---|
1067 | n/a | Py_INCREF(func); |
---|
1068 | n/a | Py_INCREF(args); |
---|
1069 | n/a | Py_XINCREF(keyw); |
---|
1070 | n/a | PyEval_InitThreads(); /* Start the interpreter's thread-awareness */ |
---|
1071 | n/a | ident = PyThread_start_new_thread(t_bootstrap, (void*) boot); |
---|
1072 | n/a | if (ident == -1) { |
---|
1073 | n/a | PyErr_SetString(ThreadError, "can't start new thread"); |
---|
1074 | n/a | Py_DECREF(func); |
---|
1075 | n/a | Py_DECREF(args); |
---|
1076 | n/a | Py_XDECREF(keyw); |
---|
1077 | n/a | PyThreadState_Clear(boot->tstate); |
---|
1078 | n/a | PyMem_DEL(boot); |
---|
1079 | n/a | return NULL; |
---|
1080 | n/a | } |
---|
1081 | n/a | return PyLong_FromLong(ident); |
---|
1082 | n/a | } |
---|
1083 | n/a | |
---|
1084 | n/a | PyDoc_STRVAR(start_new_doc, |
---|
1085 | n/a | "start_new_thread(function, args[, kwargs])\n\ |
---|
1086 | n/a | (start_new() is an obsolete synonym)\n\ |
---|
1087 | n/a | \n\ |
---|
1088 | n/a | Start a new thread and return its identifier. The thread will call the\n\ |
---|
1089 | n/a | function with positional arguments from the tuple args and keyword arguments\n\ |
---|
1090 | n/a | taken from the optional dictionary kwargs. The thread exits when the\n\ |
---|
1091 | n/a | function returns; the return value is ignored. The thread will also exit\n\ |
---|
1092 | n/a | when the function raises an unhandled exception; a stack trace will be\n\ |
---|
1093 | n/a | printed unless the exception is SystemExit.\n"); |
---|
1094 | n/a | |
---|
1095 | n/a | static PyObject * |
---|
1096 | n/a | thread_PyThread_exit_thread(PyObject *self) |
---|
1097 | n/a | { |
---|
1098 | n/a | PyErr_SetNone(PyExc_SystemExit); |
---|
1099 | n/a | return NULL; |
---|
1100 | n/a | } |
---|
1101 | n/a | |
---|
1102 | n/a | PyDoc_STRVAR(exit_doc, |
---|
1103 | n/a | "exit()\n\ |
---|
1104 | n/a | (exit_thread() is an obsolete synonym)\n\ |
---|
1105 | n/a | \n\ |
---|
1106 | n/a | This is synonymous to ``raise SystemExit''. It will cause the current\n\ |
---|
1107 | n/a | thread to exit silently unless the exception is caught."); |
---|
1108 | n/a | |
---|
1109 | n/a | static PyObject * |
---|
1110 | n/a | thread_PyThread_interrupt_main(PyObject * self) |
---|
1111 | n/a | { |
---|
1112 | n/a | PyErr_SetInterrupt(); |
---|
1113 | n/a | Py_RETURN_NONE; |
---|
1114 | n/a | } |
---|
1115 | n/a | |
---|
1116 | n/a | PyDoc_STRVAR(interrupt_doc, |
---|
1117 | n/a | "interrupt_main()\n\ |
---|
1118 | n/a | \n\ |
---|
1119 | n/a | Raise a KeyboardInterrupt in the main thread.\n\ |
---|
1120 | n/a | A subthread can use this function to interrupt the main thread." |
---|
1121 | n/a | ); |
---|
1122 | n/a | |
---|
1123 | n/a | static lockobject *newlockobject(void); |
---|
1124 | n/a | |
---|
1125 | n/a | static PyObject * |
---|
1126 | n/a | thread_PyThread_allocate_lock(PyObject *self) |
---|
1127 | n/a | { |
---|
1128 | n/a | return (PyObject *) newlockobject(); |
---|
1129 | n/a | } |
---|
1130 | n/a | |
---|
1131 | n/a | PyDoc_STRVAR(allocate_doc, |
---|
1132 | n/a | "allocate_lock() -> lock object\n\ |
---|
1133 | n/a | (allocate() is an obsolete synonym)\n\ |
---|
1134 | n/a | \n\ |
---|
1135 | n/a | Create a new lock object. See help(type(threading.Lock())) for\n\ |
---|
1136 | n/a | information about locks."); |
---|
1137 | n/a | |
---|
1138 | n/a | static PyObject * |
---|
1139 | n/a | thread_get_ident(PyObject *self) |
---|
1140 | n/a | { |
---|
1141 | n/a | long ident; |
---|
1142 | n/a | ident = PyThread_get_thread_ident(); |
---|
1143 | n/a | if (ident == -1) { |
---|
1144 | n/a | PyErr_SetString(ThreadError, "no current thread ident"); |
---|
1145 | n/a | return NULL; |
---|
1146 | n/a | } |
---|
1147 | n/a | return PyLong_FromLong(ident); |
---|
1148 | n/a | } |
---|
1149 | n/a | |
---|
1150 | n/a | PyDoc_STRVAR(get_ident_doc, |
---|
1151 | n/a | "get_ident() -> integer\n\ |
---|
1152 | n/a | \n\ |
---|
1153 | n/a | Return a non-zero integer that uniquely identifies the current thread\n\ |
---|
1154 | n/a | amongst other threads that exist simultaneously.\n\ |
---|
1155 | n/a | This may be used to identify per-thread resources.\n\ |
---|
1156 | n/a | Even though on some platforms threads identities may appear to be\n\ |
---|
1157 | n/a | allocated consecutive numbers starting at 1, this behavior should not\n\ |
---|
1158 | n/a | be relied upon, and the number should be seen purely as a magic cookie.\n\ |
---|
1159 | n/a | A thread's identity may be reused for another thread after it exits."); |
---|
1160 | n/a | |
---|
1161 | n/a | static PyObject * |
---|
1162 | n/a | thread__count(PyObject *self) |
---|
1163 | n/a | { |
---|
1164 | n/a | return PyLong_FromLong(nb_threads); |
---|
1165 | n/a | } |
---|
1166 | n/a | |
---|
1167 | n/a | PyDoc_STRVAR(_count_doc, |
---|
1168 | n/a | "_count() -> integer\n\ |
---|
1169 | n/a | \n\ |
---|
1170 | n/a | \ |
---|
1171 | n/a | Return the number of currently running Python threads, excluding \n\ |
---|
1172 | n/a | the main thread. The returned number comprises all threads created\n\ |
---|
1173 | n/a | through `start_new_thread()` as well as `threading.Thread`, and not\n\ |
---|
1174 | n/a | yet finished.\n\ |
---|
1175 | n/a | \n\ |
---|
1176 | n/a | This function is meant for internal and specialized purposes only.\n\ |
---|
1177 | n/a | In most applications `threading.enumerate()` should be used instead."); |
---|
1178 | n/a | |
---|
1179 | n/a | static void |
---|
1180 | n/a | release_sentinel(void *wr) |
---|
1181 | n/a | { |
---|
1182 | n/a | /* Tricky: this function is called when the current thread state |
---|
1183 | n/a | is being deleted. Therefore, only simple C code can safely |
---|
1184 | n/a | execute here. */ |
---|
1185 | n/a | PyObject *obj = PyWeakref_GET_OBJECT(wr); |
---|
1186 | n/a | lockobject *lock; |
---|
1187 | n/a | if (obj != Py_None) { |
---|
1188 | n/a | assert(Py_TYPE(obj) == &Locktype); |
---|
1189 | n/a | lock = (lockobject *) obj; |
---|
1190 | n/a | if (lock->locked) { |
---|
1191 | n/a | PyThread_release_lock(lock->lock_lock); |
---|
1192 | n/a | lock->locked = 0; |
---|
1193 | n/a | } |
---|
1194 | n/a | } |
---|
1195 | n/a | /* Deallocating a weakref with a NULL callback only calls |
---|
1196 | n/a | PyObject_GC_Del(), which can't call any Python code. */ |
---|
1197 | n/a | Py_DECREF(wr); |
---|
1198 | n/a | } |
---|
1199 | n/a | |
---|
1200 | n/a | static PyObject * |
---|
1201 | n/a | thread__set_sentinel(PyObject *self) |
---|
1202 | n/a | { |
---|
1203 | n/a | PyObject *wr; |
---|
1204 | n/a | PyThreadState *tstate = PyThreadState_Get(); |
---|
1205 | n/a | lockobject *lock; |
---|
1206 | n/a | |
---|
1207 | n/a | if (tstate->on_delete_data != NULL) { |
---|
1208 | n/a | /* We must support the re-creation of the lock from a |
---|
1209 | n/a | fork()ed child. */ |
---|
1210 | n/a | assert(tstate->on_delete == &release_sentinel); |
---|
1211 | n/a | wr = (PyObject *) tstate->on_delete_data; |
---|
1212 | n/a | tstate->on_delete = NULL; |
---|
1213 | n/a | tstate->on_delete_data = NULL; |
---|
1214 | n/a | Py_DECREF(wr); |
---|
1215 | n/a | } |
---|
1216 | n/a | lock = newlockobject(); |
---|
1217 | n/a | if (lock == NULL) |
---|
1218 | n/a | return NULL; |
---|
1219 | n/a | /* The lock is owned by whoever called _set_sentinel(), but the weakref |
---|
1220 | n/a | hangs to the thread state. */ |
---|
1221 | n/a | wr = PyWeakref_NewRef((PyObject *) lock, NULL); |
---|
1222 | n/a | if (wr == NULL) { |
---|
1223 | n/a | Py_DECREF(lock); |
---|
1224 | n/a | return NULL; |
---|
1225 | n/a | } |
---|
1226 | n/a | tstate->on_delete_data = (void *) wr; |
---|
1227 | n/a | tstate->on_delete = &release_sentinel; |
---|
1228 | n/a | return (PyObject *) lock; |
---|
1229 | n/a | } |
---|
1230 | n/a | |
---|
1231 | n/a | PyDoc_STRVAR(_set_sentinel_doc, |
---|
1232 | n/a | "_set_sentinel() -> lock\n\ |
---|
1233 | n/a | \n\ |
---|
1234 | n/a | Set a sentinel lock that will be released when the current thread\n\ |
---|
1235 | n/a | state is finalized (after it is untied from the interpreter).\n\ |
---|
1236 | n/a | \n\ |
---|
1237 | n/a | This is a private API for the threading module."); |
---|
1238 | n/a | |
---|
1239 | n/a | static PyObject * |
---|
1240 | n/a | thread_stack_size(PyObject *self, PyObject *args) |
---|
1241 | n/a | { |
---|
1242 | n/a | size_t old_size; |
---|
1243 | n/a | Py_ssize_t new_size = 0; |
---|
1244 | n/a | int rc; |
---|
1245 | n/a | |
---|
1246 | n/a | if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size)) |
---|
1247 | n/a | return NULL; |
---|
1248 | n/a | |
---|
1249 | n/a | if (new_size < 0) { |
---|
1250 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1251 | n/a | "size must be 0 or a positive value"); |
---|
1252 | n/a | return NULL; |
---|
1253 | n/a | } |
---|
1254 | n/a | |
---|
1255 | n/a | old_size = PyThread_get_stacksize(); |
---|
1256 | n/a | |
---|
1257 | n/a | rc = PyThread_set_stacksize((size_t) new_size); |
---|
1258 | n/a | if (rc == -1) { |
---|
1259 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1260 | n/a | "size not valid: %zd bytes", |
---|
1261 | n/a | new_size); |
---|
1262 | n/a | return NULL; |
---|
1263 | n/a | } |
---|
1264 | n/a | if (rc == -2) { |
---|
1265 | n/a | PyErr_SetString(ThreadError, |
---|
1266 | n/a | "setting stack size not supported"); |
---|
1267 | n/a | return NULL; |
---|
1268 | n/a | } |
---|
1269 | n/a | |
---|
1270 | n/a | return PyLong_FromSsize_t((Py_ssize_t) old_size); |
---|
1271 | n/a | } |
---|
1272 | n/a | |
---|
1273 | n/a | PyDoc_STRVAR(stack_size_doc, |
---|
1274 | n/a | "stack_size([size]) -> size\n\ |
---|
1275 | n/a | \n\ |
---|
1276 | n/a | Return the thread stack size used when creating new threads. The\n\ |
---|
1277 | n/a | optional size argument specifies the stack size (in bytes) to be used\n\ |
---|
1278 | n/a | for subsequently created threads, and must be 0 (use platform or\n\ |
---|
1279 | n/a | configured default) or a positive integer value of at least 32,768 (32k).\n\ |
---|
1280 | n/a | If changing the thread stack size is unsupported, a ThreadError\n\ |
---|
1281 | n/a | exception is raised. If the specified size is invalid, a ValueError\n\ |
---|
1282 | n/a | exception is raised, and the stack size is unmodified. 32k bytes\n\ |
---|
1283 | n/a | currently the minimum supported stack size value to guarantee\n\ |
---|
1284 | n/a | sufficient stack space for the interpreter itself.\n\ |
---|
1285 | n/a | \n\ |
---|
1286 | n/a | Note that some platforms may have particular restrictions on values for\n\ |
---|
1287 | n/a | the stack size, such as requiring a minimum stack size larger than 32kB or\n\ |
---|
1288 | n/a | requiring allocation in multiples of the system memory page size\n\ |
---|
1289 | n/a | - platform documentation should be referred to for more information\n\ |
---|
1290 | n/a | (4kB pages are common; using multiples of 4096 for the stack size is\n\ |
---|
1291 | n/a | the suggested approach in the absence of more specific information)."); |
---|
1292 | n/a | |
---|
1293 | n/a | static PyMethodDef thread_methods[] = { |
---|
1294 | n/a | {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, |
---|
1295 | n/a | METH_VARARGS, start_new_doc}, |
---|
1296 | n/a | {"start_new", (PyCFunction)thread_PyThread_start_new_thread, |
---|
1297 | n/a | METH_VARARGS, start_new_doc}, |
---|
1298 | n/a | {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, |
---|
1299 | n/a | METH_NOARGS, allocate_doc}, |
---|
1300 | n/a | {"allocate", (PyCFunction)thread_PyThread_allocate_lock, |
---|
1301 | n/a | METH_NOARGS, allocate_doc}, |
---|
1302 | n/a | {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, |
---|
1303 | n/a | METH_NOARGS, exit_doc}, |
---|
1304 | n/a | {"exit", (PyCFunction)thread_PyThread_exit_thread, |
---|
1305 | n/a | METH_NOARGS, exit_doc}, |
---|
1306 | n/a | {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main, |
---|
1307 | n/a | METH_NOARGS, interrupt_doc}, |
---|
1308 | n/a | {"get_ident", (PyCFunction)thread_get_ident, |
---|
1309 | n/a | METH_NOARGS, get_ident_doc}, |
---|
1310 | n/a | {"_count", (PyCFunction)thread__count, |
---|
1311 | n/a | METH_NOARGS, _count_doc}, |
---|
1312 | n/a | {"stack_size", (PyCFunction)thread_stack_size, |
---|
1313 | n/a | METH_VARARGS, stack_size_doc}, |
---|
1314 | n/a | {"_set_sentinel", (PyCFunction)thread__set_sentinel, |
---|
1315 | n/a | METH_NOARGS, _set_sentinel_doc}, |
---|
1316 | n/a | {NULL, NULL} /* sentinel */ |
---|
1317 | n/a | }; |
---|
1318 | n/a | |
---|
1319 | n/a | |
---|
1320 | n/a | /* Initialization function */ |
---|
1321 | n/a | |
---|
1322 | n/a | PyDoc_STRVAR(thread_doc, |
---|
1323 | n/a | "This module provides primitive operations to write multi-threaded programs.\n\ |
---|
1324 | n/a | The 'threading' module provides a more convenient interface."); |
---|
1325 | n/a | |
---|
1326 | n/a | PyDoc_STRVAR(lock_doc, |
---|
1327 | n/a | "A lock object is a synchronization primitive. To create a lock,\n\ |
---|
1328 | n/a | call threading.Lock(). Methods are:\n\ |
---|
1329 | n/a | \n\ |
---|
1330 | n/a | acquire() -- lock the lock, possibly blocking until it can be obtained\n\ |
---|
1331 | n/a | release() -- unlock of the lock\n\ |
---|
1332 | n/a | locked() -- test whether the lock is currently locked\n\ |
---|
1333 | n/a | \n\ |
---|
1334 | n/a | A lock is not owned by the thread that locked it; another thread may\n\ |
---|
1335 | n/a | unlock it. A thread attempting to lock a lock that it has already locked\n\ |
---|
1336 | n/a | will block until another thread unlocks it. Deadlocks may ensue."); |
---|
1337 | n/a | |
---|
1338 | n/a | static struct PyModuleDef threadmodule = { |
---|
1339 | n/a | PyModuleDef_HEAD_INIT, |
---|
1340 | n/a | "_thread", |
---|
1341 | n/a | thread_doc, |
---|
1342 | n/a | -1, |
---|
1343 | n/a | thread_methods, |
---|
1344 | n/a | NULL, |
---|
1345 | n/a | NULL, |
---|
1346 | n/a | NULL, |
---|
1347 | n/a | NULL |
---|
1348 | n/a | }; |
---|
1349 | n/a | |
---|
1350 | n/a | |
---|
1351 | n/a | PyMODINIT_FUNC |
---|
1352 | n/a | PyInit__thread(void) |
---|
1353 | n/a | { |
---|
1354 | n/a | PyObject *m, *d, *v; |
---|
1355 | n/a | double time_max; |
---|
1356 | n/a | double timeout_max; |
---|
1357 | n/a | |
---|
1358 | n/a | /* Initialize types: */ |
---|
1359 | n/a | if (PyType_Ready(&localdummytype) < 0) |
---|
1360 | n/a | return NULL; |
---|
1361 | n/a | if (PyType_Ready(&localtype) < 0) |
---|
1362 | n/a | return NULL; |
---|
1363 | n/a | if (PyType_Ready(&Locktype) < 0) |
---|
1364 | n/a | return NULL; |
---|
1365 | n/a | if (PyType_Ready(&RLocktype) < 0) |
---|
1366 | n/a | return NULL; |
---|
1367 | n/a | |
---|
1368 | n/a | /* Create the module and add the functions */ |
---|
1369 | n/a | m = PyModule_Create(&threadmodule); |
---|
1370 | n/a | if (m == NULL) |
---|
1371 | n/a | return NULL; |
---|
1372 | n/a | |
---|
1373 | n/a | timeout_max = PY_TIMEOUT_MAX / 1000000; |
---|
1374 | n/a | time_max = floor(_PyTime_AsSecondsDouble(_PyTime_MAX)); |
---|
1375 | n/a | timeout_max = Py_MIN(timeout_max, time_max); |
---|
1376 | n/a | |
---|
1377 | n/a | v = PyFloat_FromDouble(timeout_max); |
---|
1378 | n/a | if (!v) |
---|
1379 | n/a | return NULL; |
---|
1380 | n/a | if (PyModule_AddObject(m, "TIMEOUT_MAX", v) < 0) |
---|
1381 | n/a | return NULL; |
---|
1382 | n/a | |
---|
1383 | n/a | /* Add a symbolic constant */ |
---|
1384 | n/a | d = PyModule_GetDict(m); |
---|
1385 | n/a | ThreadError = PyExc_RuntimeError; |
---|
1386 | n/a | Py_INCREF(ThreadError); |
---|
1387 | n/a | |
---|
1388 | n/a | PyDict_SetItemString(d, "error", ThreadError); |
---|
1389 | n/a | Locktype.tp_doc = lock_doc; |
---|
1390 | n/a | Py_INCREF(&Locktype); |
---|
1391 | n/a | PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
---|
1392 | n/a | |
---|
1393 | n/a | Py_INCREF(&RLocktype); |
---|
1394 | n/a | if (PyModule_AddObject(m, "RLock", (PyObject *)&RLocktype) < 0) |
---|
1395 | n/a | return NULL; |
---|
1396 | n/a | |
---|
1397 | n/a | Py_INCREF(&localtype); |
---|
1398 | n/a | if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0) |
---|
1399 | n/a | return NULL; |
---|
1400 | n/a | |
---|
1401 | n/a | nb_threads = 0; |
---|
1402 | n/a | |
---|
1403 | n/a | str_dict = PyUnicode_InternFromString("__dict__"); |
---|
1404 | n/a | if (str_dict == NULL) |
---|
1405 | n/a | return NULL; |
---|
1406 | n/a | |
---|
1407 | n/a | /* Initialize the C thread library */ |
---|
1408 | n/a | PyThread_init_thread(); |
---|
1409 | n/a | return m; |
---|
1410 | n/a | } |
---|