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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | owner = self->rlock_owner; |
| 418 | count = self->rlock_count; |
| 419 | self->rlock_count = 0; |
| 420 | self->rlock_owner = 0; |
| 421 | PyThread_release_lock(self->rlock_lock); |
| 422 | return Py_BuildValue("kl", count, owner); |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | PyDoc_STRVAR(rlock_release_save_doc, |
| 426 | "_release_save() -> tuple\n\ |
| 427 | \n\ |
| 428 | For internal use by `threading.Condition`."); |
| 429 | |
| 430 | |
| 431 | static PyObject * |
| 432 | rlock_is_owned(rlockobject *self) |
| 433 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | long tid = PyThread_get_thread_ident(); |
| 435 | |
| 436 | if (self->rlock_count > 0 && self->rlock_owner == tid) { |
| 437 | Py_RETURN_TRUE; |
| 438 | } |
| 439 | Py_RETURN_FALSE; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | PyDoc_STRVAR(rlock_is_owned_doc, |
| 443 | "_is_owned() -> bool\n\ |
| 444 | \n\ |
| 445 | For internal use by `threading.Condition`."); |
| 446 | |
| 447 | static PyObject * |
| 448 | rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 449 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | rlockobject *self; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | self = (rlockobject *) type->tp_alloc(type, 0); |
| 453 | if (self != NULL) { |
| 454 | self->rlock_lock = PyThread_allocate_lock(); |
| 455 | if (self->rlock_lock == NULL) { |
| 456 | type->tp_free(self); |
| 457 | PyErr_SetString(ThreadError, "can't allocate lock"); |
| 458 | return NULL; |
| 459 | } |
| 460 | self->in_weakreflist = NULL; |
| 461 | self->rlock_owner = 0; |
| 462 | self->rlock_count = 0; |
| 463 | } |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 464 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | return (PyObject *) self; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static PyObject * |
| 469 | rlock_repr(rlockobject *self) |
| 470 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | return PyUnicode_FromFormat("<%s owner=%ld count=%lu>", |
| 472 | Py_TYPE(self)->tp_name, self->rlock_owner, self->rlock_count); |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | |
| 476 | static PyMethodDef rlock_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | {"acquire", (PyCFunction)rlock_acquire, |
| 478 | METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc}, |
| 479 | {"release", (PyCFunction)rlock_release, |
| 480 | METH_NOARGS, rlock_release_doc}, |
| 481 | {"_is_owned", (PyCFunction)rlock_is_owned, |
| 482 | METH_NOARGS, rlock_is_owned_doc}, |
| 483 | {"_acquire_restore", (PyCFunction)rlock_acquire_restore, |
| 484 | METH_O, rlock_acquire_restore_doc}, |
| 485 | {"_release_save", (PyCFunction)rlock_release_save, |
| 486 | METH_NOARGS, rlock_release_save_doc}, |
| 487 | {"__enter__", (PyCFunction)rlock_acquire, |
| 488 | METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc}, |
| 489 | {"__exit__", (PyCFunction)rlock_release, |
| 490 | METH_VARARGS, rlock_release_doc}, |
| 491 | {NULL, NULL} /* sentinel */ |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | |
| 495 | static PyTypeObject RLocktype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 497 | "_thread.RLock", /*tp_name*/ |
| 498 | sizeof(rlockobject), /*tp_size*/ |
| 499 | 0, /*tp_itemsize*/ |
| 500 | /* methods */ |
| 501 | (destructor)rlock_dealloc, /*tp_dealloc*/ |
| 502 | 0, /*tp_print*/ |
| 503 | 0, /*tp_getattr*/ |
| 504 | 0, /*tp_setattr*/ |
| 505 | 0, /*tp_reserved*/ |
| 506 | (reprfunc)rlock_repr, /*tp_repr*/ |
| 507 | 0, /*tp_as_number*/ |
| 508 | 0, /*tp_as_sequence*/ |
| 509 | 0, /*tp_as_mapping*/ |
| 510 | 0, /*tp_hash*/ |
| 511 | 0, /*tp_call*/ |
| 512 | 0, /*tp_str*/ |
| 513 | 0, /*tp_getattro*/ |
| 514 | 0, /*tp_setattro*/ |
| 515 | 0, /*tp_as_buffer*/ |
| 516 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 517 | 0, /*tp_doc*/ |
| 518 | 0, /*tp_traverse*/ |
| 519 | 0, /*tp_clear*/ |
| 520 | 0, /*tp_richcompare*/ |
| 521 | offsetof(rlockobject, in_weakreflist), /*tp_weaklistoffset*/ |
| 522 | 0, /*tp_iter*/ |
| 523 | 0, /*tp_iternext*/ |
| 524 | rlock_methods, /*tp_methods*/ |
| 525 | 0, /* tp_members */ |
| 526 | 0, /* tp_getset */ |
| 527 | 0, /* tp_base */ |
| 528 | 0, /* tp_dict */ |
| 529 | 0, /* tp_descr_get */ |
| 530 | 0, /* tp_descr_set */ |
| 531 | 0, /* tp_dictoffset */ |
| 532 | 0, /* tp_init */ |
| 533 | PyType_GenericAlloc, /* tp_alloc */ |
| 534 | rlock_new /* tp_new */ |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 535 | }; |
| 536 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 537 | static lockobject * |
| 538 | newlockobject(void) |
| 539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | lockobject *self; |
| 541 | self = PyObject_New(lockobject, &Locktype); |
| 542 | if (self == NULL) |
| 543 | return NULL; |
| 544 | self->lock_lock = PyThread_allocate_lock(); |
| 545 | self->in_weakreflist = NULL; |
| 546 | if (self->lock_lock == NULL) { |
| 547 | Py_DECREF(self); |
| 548 | PyErr_SetString(ThreadError, "can't allocate lock"); |
| 549 | return NULL; |
| 550 | } |
| 551 | return self; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 554 | /* Thread-local objects */ |
| 555 | |
| 556 | #include "structmember.h" |
| 557 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 558 | /* Quick overview: |
| 559 | |
| 560 | We need to be able to reclaim reference cycles as soon as possible |
| 561 | (both when a thread is being terminated, or a thread-local object |
| 562 | becomes unreachable from user data). Constraints: |
| 563 | - it must not be possible for thread-state dicts to be involved in |
| 564 | reference cycles (otherwise the cyclic GC will refuse to consider |
| 565 | objects referenced from a reachable thread-state dict, even though |
| 566 | local_dealloc would clear them) |
| 567 | - the death of a thread-state dict must still imply destruction of the |
| 568 | corresponding local dicts in all thread-local objects. |
| 569 | |
| 570 | Our implementation uses small "localdummy" objects in order to break |
| 571 | the reference chain. These trivial objects are hashable (using the |
| 572 | default scheme of identity hashing) and weakrefable. |
| 573 | Each thread-state holds a separate localdummy for each local object |
| 574 | (as a /strong reference/), |
| 575 | and each thread-local object holds a dict mapping /weak references/ |
| 576 | of localdummies to local dicts. |
| 577 | |
| 578 | Therefore: |
| 579 | - only the thread-state dict holds a strong reference to the dummies |
| 580 | - only the thread-local object holds a strong reference to the local dicts |
| 581 | - only outside objects (application- or library-level) hold strong |
| 582 | references to the thread-local objects |
| 583 | - as soon as a thread-state dict is destroyed, the weakref callbacks of all |
| 584 | dummies attached to that thread are called, and destroy the corresponding |
| 585 | local dicts from thread-local objects |
| 586 | - as soon as a thread-local object is destroyed, its local dicts are |
| 587 | destroyed and its dummies are manually removed from all thread states |
| 588 | - the GC can do its work correctly when a thread-local object is dangling, |
| 589 | without any interference from the thread-state dicts |
| 590 | |
| 591 | As an additional optimization, each localdummy holds a borrowed reference |
| 592 | to the corresponding localdict. This borrowed reference is only used |
| 593 | by the thread-local object which has created the localdummy, which should |
| 594 | guarantee that the localdict still exists when accessed. |
| 595 | */ |
| 596 | |
| 597 | typedef struct { |
| 598 | PyObject_HEAD |
| 599 | PyObject *localdict; /* Borrowed reference! */ |
| 600 | PyObject *weakreflist; /* List of weak references to self */ |
| 601 | } localdummyobject; |
| 602 | |
| 603 | static void |
| 604 | localdummy_dealloc(localdummyobject *self) |
| 605 | { |
| 606 | if (self->weakreflist != NULL) |
| 607 | PyObject_ClearWeakRefs((PyObject *) self); |
| 608 | Py_TYPE(self)->tp_free((PyObject*)self); |
| 609 | } |
| 610 | |
| 611 | static PyTypeObject localdummytype = { |
| 612 | PyVarObject_HEAD_INIT(NULL, 0) |
| 613 | /* tp_name */ "_thread._localdummy", |
| 614 | /* tp_basicsize */ sizeof(localdummyobject), |
| 615 | /* tp_itemsize */ 0, |
| 616 | /* tp_dealloc */ (destructor)localdummy_dealloc, |
| 617 | /* tp_print */ 0, |
| 618 | /* tp_getattr */ 0, |
| 619 | /* tp_setattr */ 0, |
| 620 | /* tp_reserved */ 0, |
| 621 | /* tp_repr */ 0, |
| 622 | /* tp_as_number */ 0, |
| 623 | /* tp_as_sequence */ 0, |
| 624 | /* tp_as_mapping */ 0, |
| 625 | /* tp_hash */ 0, |
| 626 | /* tp_call */ 0, |
| 627 | /* tp_str */ 0, |
| 628 | /* tp_getattro */ 0, |
| 629 | /* tp_setattro */ 0, |
| 630 | /* tp_as_buffer */ 0, |
| 631 | /* tp_flags */ Py_TPFLAGS_DEFAULT, |
| 632 | /* tp_doc */ "Thread-local dummy", |
| 633 | /* tp_traverse */ 0, |
| 634 | /* tp_clear */ 0, |
| 635 | /* tp_richcompare */ 0, |
| 636 | /* tp_weaklistoffset */ offsetof(localdummyobject, weakreflist) |
| 637 | }; |
| 638 | |
| 639 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 640 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | PyObject_HEAD |
| 642 | PyObject *key; |
| 643 | PyObject *args; |
| 644 | PyObject *kw; |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 645 | PyObject *weakreflist; /* List of weak references to self */ |
| 646 | /* A {localdummy weakref -> localdict} dict */ |
| 647 | PyObject *dummies; |
| 648 | /* The callback for weakrefs to localdummies */ |
| 649 | PyObject *wr_callback; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 650 | } localobject; |
| 651 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 652 | /* Forward declaration */ |
| 653 | static PyObject *_ldict(localobject *self); |
| 654 | static PyObject *_localdummy_destroyed(PyObject *meth_self, PyObject *dummyweakref); |
| 655 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 656 | /* Create and register the dummy for the current thread. |
| 657 | Returns a borrowed reference of the corresponding local dict */ |
| 658 | static PyObject * |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 659 | _local_create_dummy(localobject *self) |
| 660 | { |
| 661 | PyObject *tdict, *ldict = NULL, *wr = NULL; |
| 662 | localdummyobject *dummy = NULL; |
| 663 | int r; |
| 664 | |
| 665 | tdict = PyThreadState_GetDict(); |
| 666 | if (tdict == NULL) { |
| 667 | PyErr_SetString(PyExc_SystemError, |
| 668 | "Couldn't get thread-state dictionary"); |
| 669 | goto err; |
| 670 | } |
| 671 | |
| 672 | ldict = PyDict_New(); |
| 673 | if (ldict == NULL) |
| 674 | goto err; |
| 675 | dummy = (localdummyobject *) localdummytype.tp_alloc(&localdummytype, 0); |
| 676 | if (dummy == NULL) |
| 677 | goto err; |
| 678 | dummy->localdict = ldict; |
| 679 | wr = PyWeakref_NewRef((PyObject *) dummy, self->wr_callback); |
| 680 | if (wr == NULL) |
| 681 | goto err; |
| 682 | |
| 683 | /* As a side-effect, this will cache the weakref's hash before the |
| 684 | dummy gets deleted */ |
| 685 | r = PyDict_SetItem(self->dummies, wr, ldict); |
| 686 | if (r < 0) |
| 687 | goto err; |
| 688 | Py_CLEAR(wr); |
| 689 | r = PyDict_SetItem(tdict, self->key, (PyObject *) dummy); |
| 690 | if (r < 0) |
| 691 | goto err; |
| 692 | Py_CLEAR(dummy); |
| 693 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 694 | Py_DECREF(ldict); |
| 695 | return ldict; |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 696 | |
| 697 | err: |
| 698 | Py_XDECREF(ldict); |
| 699 | Py_XDECREF(wr); |
| 700 | Py_XDECREF(dummy); |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 701 | return NULL; |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 704 | static PyObject * |
| 705 | local_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | localobject *self; |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 708 | PyObject *wr; |
| 709 | static PyMethodDef wr_callback_def = { |
| 710 | "_localdummy_destroyed", (PyCFunction) _localdummy_destroyed, METH_O |
| 711 | }; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | if (type->tp_init == PyBaseObject_Type.tp_init |
| 714 | && ((args && PyObject_IsTrue(args)) |
| 715 | || (kw && PyObject_IsTrue(kw)))) { |
| 716 | PyErr_SetString(PyExc_TypeError, |
| 717 | "Initialization arguments are not supported"); |
| 718 | return NULL; |
| 719 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | self = (localobject *)type->tp_alloc(type, 0); |
| 722 | if (self == NULL) |
| 723 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | Py_XINCREF(args); |
| 726 | self->args = args; |
| 727 | Py_XINCREF(kw); |
| 728 | self->kw = kw; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | self->key = PyUnicode_FromFormat("thread.local.%p", self); |
| 730 | if (self->key == NULL) |
| 731 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 732 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 733 | self->dummies = PyDict_New(); |
| 734 | if (self->dummies == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 736 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 737 | /* We use a weak reference to self in the callback closure |
| 738 | in order to avoid spurious reference cycles */ |
| 739 | wr = PyWeakref_NewRef((PyObject *) self, NULL); |
| 740 | if (wr == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | goto err; |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 742 | self->wr_callback = PyCFunction_New(&wr_callback_def, wr); |
| 743 | Py_DECREF(wr); |
| 744 | if (self->wr_callback == NULL) |
| 745 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 746 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 747 | if (_local_create_dummy(self) == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 750 | return (PyObject *)self; |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 751 | |
| 752 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | Py_DECREF(self); |
| 754 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | static int |
| 758 | local_traverse(localobject *self, visitproc visit, void *arg) |
| 759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | Py_VISIT(self->args); |
| 761 | Py_VISIT(self->kw); |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 762 | Py_VISIT(self->dummies); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | return 0; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | static int |
| 767 | local_clear(localobject *self) |
| 768 | { |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 769 | PyThreadState *tstate; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | Py_CLEAR(self->args); |
| 771 | Py_CLEAR(self->kw); |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 772 | Py_CLEAR(self->dummies); |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 773 | Py_CLEAR(self->wr_callback); |
| 774 | /* Remove all strong references to dummies from the thread states */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | if (self->key |
| 776 | && (tstate = PyThreadState_Get()) |
| 777 | && tstate->interp) { |
| 778 | for(tstate = PyInterpreterState_ThreadHead(tstate->interp); |
| 779 | tstate; |
| 780 | tstate = PyThreadState_Next(tstate)) |
| 781 | if (tstate->dict && |
| 782 | PyDict_GetItem(tstate->dict, self->key)) |
| 783 | PyDict_DelItem(tstate->dict, self->key); |
| 784 | } |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 785 | return 0; |
| 786 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 787 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 788 | static void |
| 789 | local_dealloc(localobject *self) |
| 790 | { |
| 791 | /* Weakrefs must be invalidated right now, otherwise they can be used |
| 792 | from code called below, which is very dangerous since Py_REFCNT(self) == 0 */ |
| 793 | if (self->weakreflist != NULL) |
| 794 | PyObject_ClearWeakRefs((PyObject *) self); |
| 795 | |
| 796 | PyObject_GC_UnTrack(self); |
| 797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | local_clear(self); |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 799 | Py_XDECREF(self->key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | Py_TYPE(self)->tp_free((PyObject*)self); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 803 | /* Returns a borrowed reference to the local dict, creating it if necessary */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 804 | static PyObject * |
| 805 | _ldict(localobject *self) |
| 806 | { |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 807 | PyObject *tdict, *ldict, *dummy; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | tdict = PyThreadState_GetDict(); |
| 810 | if (tdict == NULL) { |
| 811 | PyErr_SetString(PyExc_SystemError, |
| 812 | "Couldn't get thread-state dictionary"); |
| 813 | return NULL; |
| 814 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 815 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 816 | dummy = PyDict_GetItem(tdict, self->key); |
| 817 | if (dummy == NULL) { |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 818 | ldict = _local_create_dummy(self); |
| 819 | if (ldict == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 820 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init && |
| 823 | Py_TYPE(self)->tp_init((PyObject*)self, |
| 824 | self->args, self->kw) < 0) { |
| 825 | /* we need to get rid of ldict from thread so |
| 826 | we create a new one the next time we do an attr |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 827 | access */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | PyDict_DelItem(tdict, self->key); |
| 829 | return NULL; |
| 830 | } |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 831 | } |
| 832 | else { |
| 833 | assert(Py_TYPE(dummy) == &localdummytype); |
| 834 | ldict = ((localdummyobject *) dummy)->localdict; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | return ldict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 840 | static int |
| 841 | local_setattro(localobject *self, PyObject *name, PyObject *v) |
| 842 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | PyObject *ldict; |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 844 | int r; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | ldict = _ldict(self); |
| 847 | if (ldict == NULL) |
| 848 | return -1; |
| 849 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 850 | r = PyObject_RichCompareBool(name, str_dict, Py_EQ); |
| 851 | if (r == 1) { |
| 852 | PyErr_Format(PyExc_AttributeError, |
| 853 | "'%.50s' object attribute '%U' is read-only", |
| 854 | Py_TYPE(self)->tp_name, name); |
| 855 | return -1; |
| 856 | } |
| 857 | if (r == -1) |
| 858 | return -1; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 859 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 860 | return _PyObject_GenericSetAttrWithDict((PyObject *)self, name, v, ldict); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 863 | static PyObject *local_getattro(localobject *, PyObject *); |
| 864 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 865 | static PyTypeObject localtype = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | PyVarObject_HEAD_INIT(NULL, 0) |
| 867 | /* tp_name */ "_thread._local", |
| 868 | /* tp_basicsize */ sizeof(localobject), |
| 869 | /* tp_itemsize */ 0, |
| 870 | /* tp_dealloc */ (destructor)local_dealloc, |
| 871 | /* tp_print */ 0, |
| 872 | /* tp_getattr */ 0, |
| 873 | /* tp_setattr */ 0, |
| 874 | /* tp_reserved */ 0, |
| 875 | /* tp_repr */ 0, |
| 876 | /* tp_as_number */ 0, |
| 877 | /* tp_as_sequence */ 0, |
| 878 | /* tp_as_mapping */ 0, |
| 879 | /* tp_hash */ 0, |
| 880 | /* tp_call */ 0, |
| 881 | /* tp_str */ 0, |
| 882 | /* tp_getattro */ (getattrofunc)local_getattro, |
| 883 | /* tp_setattro */ (setattrofunc)local_setattro, |
| 884 | /* tp_as_buffer */ 0, |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 885 | /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 886 | | Py_TPFLAGS_HAVE_GC, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | /* tp_doc */ "Thread-local data", |
| 888 | /* tp_traverse */ (traverseproc)local_traverse, |
| 889 | /* tp_clear */ (inquiry)local_clear, |
| 890 | /* tp_richcompare */ 0, |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 891 | /* tp_weaklistoffset */ offsetof(localobject, weakreflist), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 892 | /* tp_iter */ 0, |
| 893 | /* tp_iternext */ 0, |
| 894 | /* tp_methods */ 0, |
| 895 | /* tp_members */ 0, |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 896 | /* tp_getset */ 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | /* tp_base */ 0, |
| 898 | /* tp_dict */ 0, /* internal use */ |
| 899 | /* tp_descr_get */ 0, |
| 900 | /* tp_descr_set */ 0, |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 901 | /* tp_dictoffset */ 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | /* tp_init */ 0, |
| 903 | /* tp_alloc */ 0, |
| 904 | /* tp_new */ local_new, |
| 905 | /* tp_free */ 0, /* Low-level free-mem routine */ |
| 906 | /* tp_is_gc */ 0, /* For PyObject_IS_GC */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 907 | }; |
| 908 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 909 | static PyObject * |
| 910 | local_getattro(localobject *self, PyObject *name) |
| 911 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | PyObject *ldict, *value; |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 913 | int r; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | ldict = _ldict(self); |
| 916 | if (ldict == NULL) |
| 917 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 918 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 919 | r = PyObject_RichCompareBool(name, str_dict, Py_EQ); |
| 920 | if (r == 1) { |
| 921 | Py_INCREF(ldict); |
| 922 | return ldict; |
| 923 | } |
| 924 | if (r == -1) |
| 925 | return NULL; |
| 926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | if (Py_TYPE(self) != &localtype) |
| 928 | /* use generic lookup for subtypes */ |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 929 | return _PyObject_GenericGetAttrWithDict((PyObject *)self, name, ldict); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 930 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | /* Optimization: just look in dict ourselves */ |
| 932 | value = PyDict_GetItem(ldict, name); |
| 933 | if (value == NULL) |
| 934 | /* Fall back on generic to get __class__ and __dict__ */ |
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 | Py_INCREF(value); |
| 938 | return value; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 939 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 941 | /* Called when a dummy is destroyed. */ |
| 942 | static PyObject * |
| 943 | _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref) |
| 944 | { |
| 945 | PyObject *obj; |
| 946 | localobject *self; |
| 947 | assert(PyWeakref_CheckRef(localweakref)); |
| 948 | obj = PyWeakref_GET_OBJECT(localweakref); |
| 949 | if (obj == Py_None) |
| 950 | Py_RETURN_NONE; |
| 951 | Py_INCREF(obj); |
| 952 | assert(PyObject_TypeCheck(obj, &localtype)); |
| 953 | /* If the thread-local object is still alive and not being cleared, |
| 954 | remove the corresponding local dict */ |
| 955 | self = (localobject *) obj; |
| 956 | if (self->dummies != NULL) { |
| 957 | PyObject *ldict; |
| 958 | ldict = PyDict_GetItem(self->dummies, dummyweakref); |
| 959 | if (ldict != NULL) { |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 960 | PyDict_DelItem(self->dummies, dummyweakref); |
| 961 | } |
| 962 | if (PyErr_Occurred()) |
| 963 | PyErr_WriteUnraisable(obj); |
| 964 | } |
| 965 | Py_DECREF(obj); |
| 966 | Py_RETURN_NONE; |
| 967 | } |
| 968 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 969 | /* Module functions */ |
| 970 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 971 | struct bootstate { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | PyInterpreterState *interp; |
| 973 | PyObject *func; |
| 974 | PyObject *args; |
| 975 | PyObject *keyw; |
| 976 | PyThreadState *tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 977 | }; |
| 978 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 979 | static void |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 980 | t_bootstrap(void *boot_raw) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 981 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | struct bootstate *boot = (struct bootstate *) boot_raw; |
| 983 | PyThreadState *tstate; |
| 984 | PyObject *res; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | tstate = boot->tstate; |
| 987 | tstate->thread_id = PyThread_get_thread_ident(); |
| 988 | _PyThreadState_Init(tstate); |
| 989 | PyEval_AcquireThread(tstate); |
| 990 | nb_threads++; |
| 991 | res = PyEval_CallObjectWithKeywords( |
| 992 | boot->func, boot->args, boot->keyw); |
| 993 | if (res == NULL) { |
| 994 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) |
| 995 | PyErr_Clear(); |
| 996 | else { |
| 997 | PyObject *file; |
| 998 | PySys_WriteStderr( |
| 999 | "Unhandled exception in thread started by "); |
| 1000 | file = PySys_GetObject("stderr"); |
| 1001 | if (file != NULL && file != Py_None) |
| 1002 | PyFile_WriteObject(boot->func, file, 0); |
| 1003 | else |
| 1004 | PyObject_Print(boot->func, stderr, 0); |
| 1005 | PySys_WriteStderr("\n"); |
| 1006 | PyErr_PrintEx(0); |
| 1007 | } |
| 1008 | } |
| 1009 | else |
| 1010 | Py_DECREF(res); |
| 1011 | Py_DECREF(boot->func); |
| 1012 | Py_DECREF(boot->args); |
| 1013 | Py_XDECREF(boot->keyw); |
| 1014 | PyMem_DEL(boot_raw); |
| 1015 | nb_threads--; |
| 1016 | PyThreadState_Clear(tstate); |
| 1017 | PyThreadState_DeleteCurrent(); |
| 1018 | PyThread_exit_thread(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1021 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 1022 | thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1023 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | PyObject *func, *args, *keyw = NULL; |
| 1025 | struct bootstate *boot; |
| 1026 | long ident; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3, |
| 1029 | &func, &args, &keyw)) |
| 1030 | return NULL; |
| 1031 | if (!PyCallable_Check(func)) { |
| 1032 | PyErr_SetString(PyExc_TypeError, |
| 1033 | "first arg must be callable"); |
| 1034 | return NULL; |
| 1035 | } |
| 1036 | if (!PyTuple_Check(args)) { |
| 1037 | PyErr_SetString(PyExc_TypeError, |
| 1038 | "2nd arg must be a tuple"); |
| 1039 | return NULL; |
| 1040 | } |
| 1041 | if (keyw != NULL && !PyDict_Check(keyw)) { |
| 1042 | PyErr_SetString(PyExc_TypeError, |
| 1043 | "optional 3rd arg must be a dictionary"); |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | boot = PyMem_NEW(struct bootstate, 1); |
| 1047 | if (boot == NULL) |
| 1048 | return PyErr_NoMemory(); |
| 1049 | boot->interp = PyThreadState_GET()->interp; |
| 1050 | boot->func = func; |
| 1051 | boot->args = args; |
| 1052 | boot->keyw = keyw; |
| 1053 | boot->tstate = _PyThreadState_Prealloc(boot->interp); |
| 1054 | if (boot->tstate == NULL) { |
| 1055 | PyMem_DEL(boot); |
| 1056 | return PyErr_NoMemory(); |
| 1057 | } |
| 1058 | Py_INCREF(func); |
| 1059 | Py_INCREF(args); |
| 1060 | Py_XINCREF(keyw); |
| 1061 | PyEval_InitThreads(); /* Start the interpreter's thread-awareness */ |
| 1062 | ident = PyThread_start_new_thread(t_bootstrap, (void*) boot); |
| 1063 | if (ident == -1) { |
| 1064 | PyErr_SetString(ThreadError, "can't start new thread"); |
| 1065 | Py_DECREF(func); |
| 1066 | Py_DECREF(args); |
| 1067 | Py_XDECREF(keyw); |
| 1068 | PyThreadState_Clear(boot->tstate); |
| 1069 | PyMem_DEL(boot); |
| 1070 | return NULL; |
| 1071 | } |
| 1072 | return PyLong_FromLong(ident); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1075 | PyDoc_STRVAR(start_new_doc, |
Andrew M. Kuchling | 38300c6 | 2001-10-05 12:24:15 +0000 | [diff] [blame] | 1076 | "start_new_thread(function, args[, kwargs])\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1077 | (start_new() is an obsolete synonym)\n\ |
| 1078 | \n\ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 1079 | Start a new thread and return its identifier. The thread will call the\n\ |
| 1080 | function with positional arguments from the tuple args and keyword arguments\n\ |
| 1081 | taken from the optional dictionary kwargs. The thread exits when the\n\ |
| 1082 | function returns; the return value is ignored. The thread will also exit\n\ |
| 1083 | 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] | 1084 | printed unless the exception is SystemExit.\n"); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1085 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1086 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 1087 | thread_PyThread_exit_thread(PyObject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1088 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | PyErr_SetNone(PyExc_SystemExit); |
| 1090 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1093 | PyDoc_STRVAR(exit_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1094 | "exit()\n\ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 1095 | (PyThread_exit_thread() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1096 | \n\ |
| 1097 | 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] | 1098 | thread to exit silently unless the exception is caught."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1099 | |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 1100 | static PyObject * |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 1101 | thread_PyThread_interrupt_main(PyObject * self) |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 1102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | PyErr_SetInterrupt(); |
| 1104 | Py_INCREF(Py_None); |
| 1105 | return Py_None; |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | PyDoc_STRVAR(interrupt_doc, |
| 1109 | "interrupt_main()\n\ |
| 1110 | \n\ |
| 1111 | Raise a KeyboardInterrupt in the main thread.\n\ |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 1112 | A subthread can use this function to interrupt the main thread." |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 1113 | ); |
| 1114 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1115 | static lockobject *newlockobject(void); |
| 1116 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1117 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 1118 | thread_PyThread_allocate_lock(PyObject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | return (PyObject *) newlockobject(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1123 | PyDoc_STRVAR(allocate_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1124 | "allocate_lock() -> lock object\n\ |
| 1125 | (allocate() is an obsolete synonym)\n\ |
| 1126 | \n\ |
Alexander Belopolsky | 977a684 | 2010-08-16 20:17:07 +0000 | [diff] [blame] | 1127 | 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] | 1128 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1129 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 1130 | thread_get_ident(PyObject *self) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1131 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | long ident; |
| 1133 | ident = PyThread_get_thread_ident(); |
| 1134 | if (ident == -1) { |
| 1135 | PyErr_SetString(ThreadError, "no current thread ident"); |
| 1136 | return NULL; |
| 1137 | } |
| 1138 | return PyLong_FromLong(ident); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1141 | PyDoc_STRVAR(get_ident_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1142 | "get_ident() -> integer\n\ |
| 1143 | \n\ |
| 1144 | Return a non-zero integer that uniquely identifies the current thread\n\ |
| 1145 | amongst other threads that exist simultaneously.\n\ |
| 1146 | This may be used to identify per-thread resources.\n\ |
| 1147 | Even though on some platforms threads identities may appear to be\n\ |
| 1148 | allocated consecutive numbers starting at 1, this behavior should not\n\ |
| 1149 | 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] | 1150 | 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] | 1151 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1152 | static PyObject * |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 1153 | thread__count(PyObject *self) |
| 1154 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | return PyLong_FromLong(nb_threads); |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | PyDoc_STRVAR(_count_doc, |
| 1159 | "_count() -> integer\n\ |
| 1160 | \n\ |
Antoine Pitrou | 9257f5e | 2009-10-30 22:23:02 +0000 | [diff] [blame] | 1161 | \ |
| 1162 | Return the number of currently running Python threads, excluding \n\ |
| 1163 | the main thread. The returned number comprises all threads created\n\ |
| 1164 | through `start_new_thread()` as well as `threading.Thread`, and not\n\ |
| 1165 | yet finished.\n\ |
| 1166 | \n\ |
| 1167 | This function is meant for internal and specialized purposes only.\n\ |
| 1168 | In most applications `threading.enumerate()` should be used instead."); |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 1169 | |
| 1170 | static PyObject * |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1171 | thread_stack_size(PyObject *self, PyObject *args) |
| 1172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | size_t old_size; |
| 1174 | Py_ssize_t new_size = 0; |
| 1175 | int rc; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size)) |
| 1178 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | if (new_size < 0) { |
| 1181 | PyErr_SetString(PyExc_ValueError, |
| 1182 | "size must be 0 or a positive value"); |
| 1183 | return NULL; |
| 1184 | } |
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 | old_size = PyThread_get_stacksize(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | rc = PyThread_set_stacksize((size_t) new_size); |
| 1189 | if (rc == -1) { |
| 1190 | PyErr_Format(PyExc_ValueError, |
| 1191 | "size not valid: %zd bytes", |
| 1192 | new_size); |
| 1193 | return NULL; |
| 1194 | } |
| 1195 | if (rc == -2) { |
| 1196 | PyErr_SetString(ThreadError, |
| 1197 | "setting stack size not supported"); |
| 1198 | return NULL; |
| 1199 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | return PyLong_FromSsize_t((Py_ssize_t) old_size); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | PyDoc_STRVAR(stack_size_doc, |
| 1205 | "stack_size([size]) -> size\n\ |
| 1206 | \n\ |
| 1207 | Return the thread stack size used when creating new threads. The\n\ |
| 1208 | optional size argument specifies the stack size (in bytes) to be used\n\ |
| 1209 | for subsequently created threads, and must be 0 (use platform or\n\ |
| 1210 | configured default) or a positive integer value of at least 32,768 (32k).\n\ |
| 1211 | If changing the thread stack size is unsupported, a ThreadError\n\ |
| 1212 | exception is raised. If the specified size is invalid, a ValueError\n\ |
| 1213 | exception is raised, and the stack size is unmodified. 32k bytes\n\ |
| 1214 | currently the minimum supported stack size value to guarantee\n\ |
| 1215 | sufficient stack space for the interpreter itself.\n\ |
| 1216 | \n\ |
| 1217 | Note that some platforms may have particular restrictions on values for\n\ |
| 1218 | the stack size, such as requiring a minimum stack size larger than 32kB or\n\ |
| 1219 | requiring allocation in multiples of the system memory page size\n\ |
| 1220 | - platform documentation should be referred to for more information\n\ |
| 1221 | (4kB pages are common; using multiples of 4096 for the stack size is\n\ |
| 1222 | the suggested approach in the absence of more specific information)."); |
| 1223 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1224 | static PyMethodDef thread_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, |
| 1226 | METH_VARARGS, |
| 1227 | start_new_doc}, |
| 1228 | {"start_new", (PyCFunction)thread_PyThread_start_new_thread, |
| 1229 | METH_VARARGS, |
| 1230 | start_new_doc}, |
| 1231 | {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, |
| 1232 | METH_NOARGS, allocate_doc}, |
| 1233 | {"allocate", (PyCFunction)thread_PyThread_allocate_lock, |
| 1234 | METH_NOARGS, allocate_doc}, |
| 1235 | {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, |
| 1236 | METH_NOARGS, exit_doc}, |
| 1237 | {"exit", (PyCFunction)thread_PyThread_exit_thread, |
| 1238 | METH_NOARGS, exit_doc}, |
| 1239 | {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main, |
| 1240 | METH_NOARGS, interrupt_doc}, |
| 1241 | {"get_ident", (PyCFunction)thread_get_ident, |
| 1242 | METH_NOARGS, get_ident_doc}, |
| 1243 | {"_count", (PyCFunction)thread__count, |
| 1244 | METH_NOARGS, _count_doc}, |
| 1245 | {"stack_size", (PyCFunction)thread_stack_size, |
| 1246 | METH_VARARGS, |
| 1247 | stack_size_doc}, |
| 1248 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1249 | }; |
| 1250 | |
| 1251 | |
| 1252 | /* Initialization function */ |
| 1253 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1254 | PyDoc_STRVAR(thread_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1255 | "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] | 1256 | The 'threading' module provides a more convenient interface."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1257 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1258 | PyDoc_STRVAR(lock_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1259 | "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] | 1260 | call the PyThread_allocate_lock() function. Methods are:\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1261 | \n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1262 | acquire() -- lock the lock, possibly blocking until it can be obtained\n\ |
| 1263 | release() -- unlock of the lock\n\ |
| 1264 | locked() -- test whether the lock is currently locked\n\ |
| 1265 | \n\ |
| 1266 | A lock is not owned by the thread that locked it; another thread may\n\ |
| 1267 | 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] | 1268 | will block until another thread unlocks it. Deadlocks may ensue."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1269 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1270 | static struct PyModuleDef threadmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | PyModuleDef_HEAD_INIT, |
| 1272 | "_thread", |
| 1273 | thread_doc, |
| 1274 | -1, |
| 1275 | thread_methods, |
| 1276 | NULL, |
| 1277 | NULL, |
| 1278 | NULL, |
| 1279 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1280 | }; |
| 1281 | |
| 1282 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1283 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1284 | PyInit__thread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | PyObject *m, *d, *timeout_max; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | /* Initialize types: */ |
Antoine Pitrou | 5af4f4b | 2010-08-09 22:38:19 +0000 | [diff] [blame] | 1289 | if (PyType_Ready(&localdummytype) < 0) |
| 1290 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | if (PyType_Ready(&localtype) < 0) |
| 1292 | return NULL; |
| 1293 | if (PyType_Ready(&Locktype) < 0) |
| 1294 | return NULL; |
| 1295 | if (PyType_Ready(&RLocktype) < 0) |
| 1296 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1298 | /* Create the module and add the functions */ |
| 1299 | m = PyModule_Create(&threadmodule); |
| 1300 | if (m == NULL) |
| 1301 | return NULL; |
Antoine Pitrou | 7c3e577 | 2010-04-14 15:44:10 +0000 | [diff] [blame] | 1302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1303 | timeout_max = PyFloat_FromDouble(PY_TIMEOUT_MAX / 1000000); |
| 1304 | if (!timeout_max) |
| 1305 | return NULL; |
| 1306 | if (PyModule_AddObject(m, "TIMEOUT_MAX", timeout_max) < 0) |
| 1307 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | /* Add a symbolic constant */ |
| 1310 | d = PyModule_GetDict(m); |
Antoine Pitrou | fcf81fd | 2011-02-28 22:03:34 +0000 | [diff] [blame] | 1311 | ThreadError = PyExc_RuntimeError; |
| 1312 | Py_INCREF(ThreadError); |
| 1313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | PyDict_SetItemString(d, "error", ThreadError); |
| 1315 | Locktype.tp_doc = lock_doc; |
| 1316 | Py_INCREF(&Locktype); |
| 1317 | PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 1318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | Py_INCREF(&RLocktype); |
| 1320 | if (PyModule_AddObject(m, "RLock", (PyObject *)&RLocktype) < 0) |
| 1321 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 1322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | Py_INCREF(&localtype); |
| 1324 | if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0) |
| 1325 | return NULL; |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 1326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | nb_threads = 0; |
| 1328 | |
Antoine Pitrou | 1a9a9d5 | 2010-08-28 18:17:03 +0000 | [diff] [blame] | 1329 | str_dict = PyUnicode_InternFromString("__dict__"); |
| 1330 | if (str_dict == NULL) |
| 1331 | return NULL; |
| 1332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | /* Initialize the C thread library */ |
| 1334 | PyThread_init_thread(); |
| 1335 | return m; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1336 | } |