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