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