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