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