Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1 | |
| 2 | /* Thread module */ |
| 3 | /* Interface to Sjoerd's portable C thread library */ |
| 4 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Benjamin Peterson | bec4d57 | 2009-10-10 01:16:07 +0000 | [diff] [blame] | 6 | #include "structmember.h" /* offsetof */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8 | #ifndef WITH_THREAD |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 9 | #error "Error! The rest of Python is not compiled with thread support." |
Neal Norwitz | 884baa1 | 2002-09-05 21:31:04 +0000 | [diff] [blame] | 10 | #error "Rerun configure, adding a --with-threads option." |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 11 | #error "Then run `make clean' followed by `make'." |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 14 | #include "pythread.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 15 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 16 | static PyObject *ThreadError; |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 17 | static long nb_threads = 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 18 | |
| 19 | /* Lock objects */ |
| 20 | |
| 21 | typedef struct { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 22 | PyObject_HEAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 23 | PyThread_type_lock lock_lock; |
Benjamin Peterson | bec4d57 | 2009-10-10 01:16:07 +0000 | [diff] [blame] | 24 | PyObject *in_weakreflist; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 25 | } lockobject; |
| 26 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 27 | static void |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 28 | lock_dealloc(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 29 | { |
Benjamin Peterson | bec4d57 | 2009-10-10 01:16:07 +0000 | [diff] [blame] | 30 | if (self->in_weakreflist != NULL) |
| 31 | PyObject_ClearWeakRefs((PyObject *) self); |
Victor Stinner | 8f3216a | 2010-03-03 00:50:12 +0000 | [diff] [blame] | 32 | if (self->lock_lock != NULL) { |
| 33 | /* Unlock the lock so it's safe to free it */ |
| 34 | PyThread_acquire_lock(self->lock_lock, 0); |
| 35 | PyThread_release_lock(self->lock_lock); |
| 36 | |
| 37 | PyThread_free_lock(self->lock_lock); |
| 38 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 39 | PyObject_Del(self); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 42 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 43 | lock_PyThread_acquire_lock(lockobject *self, PyObject *args) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 44 | { |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 45 | int i = 1; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 46 | |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 47 | if (!PyArg_ParseTuple(args, "|i:acquire", &i)) |
| 48 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 49 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 50 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 51 | i = PyThread_acquire_lock(self->lock_lock, i); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 52 | Py_END_ALLOW_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 53 | |
Andrew M. Kuchling | a43ece9 | 2005-06-02 17:07:11 +0000 | [diff] [blame] | 54 | return PyBool_FromLong((long)i); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 57 | PyDoc_STRVAR(acquire_doc, |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 58 | "acquire([wait]) -> None or bool\n\ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 59 | (acquire_lock() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 60 | \n\ |
| 61 | Lock the lock. Without argument, this blocks if the lock is already\n\ |
| 62 | 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] | 63 | the lock, and return None once the lock is acquired.\n\ |
| 64 | 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] | 65 | 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] | 66 | The blocking operation is not interruptible."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 67 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 68 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 69 | lock_PyThread_release_lock(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 70 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 71 | /* Sanity check: the lock must be locked */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 72 | if (PyThread_acquire_lock(self->lock_lock, 0)) { |
| 73 | PyThread_release_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 74 | PyErr_SetString(ThreadError, "release unlocked lock"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
| 77 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 78 | PyThread_release_lock(self->lock_lock); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 79 | Py_INCREF(Py_None); |
| 80 | return Py_None; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 83 | PyDoc_STRVAR(release_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 84 | "release()\n\ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 85 | (release_lock() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 86 | \n\ |
| 87 | Release the lock, allowing another thread that is blocked waiting for\n\ |
| 88 | 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] | 89 | 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] | 90 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 91 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 92 | lock_locked_lock(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 93 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 94 | if (PyThread_acquire_lock(self->lock_lock, 0)) { |
| 95 | PyThread_release_lock(self->lock_lock); |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 96 | return PyBool_FromLong(0L); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 97 | } |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 98 | return PyBool_FromLong(1L); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 101 | PyDoc_STRVAR(locked_doc, |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 102 | "locked() -> bool\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 103 | (locked_lock() is an obsolete synonym)\n\ |
| 104 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 105 | Return whether the lock is in the locked state."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 106 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 107 | static PyMethodDef lock_methods[] = { |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 108 | {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 109 | METH_VARARGS, acquire_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 110 | {"acquire", (PyCFunction)lock_PyThread_acquire_lock, |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 111 | METH_VARARGS, acquire_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 112 | {"release_lock", (PyCFunction)lock_PyThread_release_lock, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 113 | METH_NOARGS, release_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 114 | {"release", (PyCFunction)lock_PyThread_release_lock, |
Neal Norwitz | 2358425 | 2002-03-25 21:05:50 +0000 | [diff] [blame] | 115 | METH_NOARGS, release_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 116 | {"locked_lock", (PyCFunction)lock_locked_lock, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 117 | METH_NOARGS, locked_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 118 | {"locked", (PyCFunction)lock_locked_lock, |
Neal Norwitz | 2358425 | 2002-03-25 21:05:50 +0000 | [diff] [blame] | 119 | METH_NOARGS, locked_doc}, |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 120 | {"__enter__", (PyCFunction)lock_PyThread_acquire_lock, |
| 121 | METH_VARARGS, acquire_doc}, |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 122 | {"__exit__", (PyCFunction)lock_PyThread_release_lock, |
| 123 | METH_VARARGS, release_doc}, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 124 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 127 | static PyTypeObject Locktype = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 128 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 129 | "_thread.lock", /*tp_name*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 130 | sizeof(lockobject), /*tp_size*/ |
| 131 | 0, /*tp_itemsize*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 132 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 133 | (destructor)lock_dealloc, /*tp_dealloc*/ |
| 134 | 0, /*tp_print*/ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 135 | 0, /*tp_getattr*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 136 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 137 | 0, /*tp_reserved*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 138 | 0, /*tp_repr*/ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 139 | 0, /*tp_as_number*/ |
| 140 | 0, /*tp_as_sequence*/ |
| 141 | 0, /*tp_as_mapping*/ |
| 142 | 0, /*tp_hash*/ |
| 143 | 0, /*tp_call*/ |
| 144 | 0, /*tp_str*/ |
| 145 | 0, /*tp_getattro*/ |
| 146 | 0, /*tp_setattro*/ |
| 147 | 0, /*tp_as_buffer*/ |
Benjamin Peterson | fe51a1f | 2009-10-09 20:36:25 +0000 | [diff] [blame] | 148 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 149 | 0, /*tp_doc*/ |
| 150 | 0, /*tp_traverse*/ |
| 151 | 0, /*tp_clear*/ |
| 152 | 0, /*tp_richcompare*/ |
Benjamin Peterson | bec4d57 | 2009-10-10 01:16:07 +0000 | [diff] [blame] | 153 | offsetof(lockobject, in_weakreflist), /*tp_weaklistoffset*/ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 154 | 0, /*tp_iter*/ |
| 155 | 0, /*tp_iternext*/ |
| 156 | lock_methods, /*tp_methods*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 159 | /* Recursive lock objects */ |
| 160 | |
| 161 | typedef struct { |
| 162 | PyObject_HEAD |
| 163 | PyThread_type_lock rlock_lock; |
| 164 | long rlock_owner; |
| 165 | unsigned long rlock_count; |
| 166 | PyObject *in_weakreflist; |
| 167 | } rlockobject; |
| 168 | |
| 169 | static void |
| 170 | rlock_dealloc(rlockobject *self) |
| 171 | { |
| 172 | assert(self->rlock_lock); |
| 173 | if (self->in_weakreflist != NULL) |
| 174 | PyObject_ClearWeakRefs((PyObject *) self); |
| 175 | /* Unlock the lock so it's safe to free it */ |
| 176 | if (self->rlock_count > 0) |
| 177 | PyThread_release_lock(self->rlock_lock); |
| 178 | |
| 179 | PyThread_free_lock(self->rlock_lock); |
| 180 | Py_TYPE(self)->tp_free(self); |
| 181 | } |
| 182 | |
| 183 | static PyObject * |
| 184 | rlock_acquire(rlockobject *self, PyObject *args, PyObject *kwds) |
| 185 | { |
| 186 | char *kwlist[] = {"blocking", NULL}; |
| 187 | int blocking = 1; |
| 188 | long tid; |
| 189 | int r = 1; |
| 190 | |
| 191 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:acquire", kwlist, |
| 192 | &blocking)) |
| 193 | return NULL; |
| 194 | |
| 195 | tid = PyThread_get_thread_ident(); |
| 196 | if (self->rlock_count > 0 && tid == self->rlock_owner) { |
| 197 | unsigned long count = self->rlock_count + 1; |
| 198 | if (count <= self->rlock_count) { |
| 199 | PyErr_SetString(PyExc_OverflowError, |
| 200 | "Internal lock count overflowed"); |
| 201 | return NULL; |
| 202 | } |
| 203 | self->rlock_count = count; |
| 204 | Py_RETURN_TRUE; |
| 205 | } |
| 206 | |
| 207 | if (self->rlock_count > 0 || |
| 208 | !PyThread_acquire_lock(self->rlock_lock, 0)) { |
| 209 | if (!blocking) { |
| 210 | Py_RETURN_FALSE; |
| 211 | } |
| 212 | Py_BEGIN_ALLOW_THREADS |
| 213 | r = PyThread_acquire_lock(self->rlock_lock, blocking); |
| 214 | Py_END_ALLOW_THREADS |
| 215 | } |
| 216 | if (r) { |
| 217 | assert(self->rlock_count == 0); |
| 218 | self->rlock_owner = tid; |
| 219 | self->rlock_count = 1; |
| 220 | } |
| 221 | |
| 222 | return PyBool_FromLong(r); |
| 223 | } |
| 224 | |
| 225 | PyDoc_STRVAR(rlock_acquire_doc, |
| 226 | "acquire(blocking=True) -> bool\n\ |
| 227 | \n\ |
| 228 | Lock the lock. `blocking` indicates whether we should wait\n\ |
| 229 | for the lock to be available or not. If `blocking` is False\n\ |
| 230 | and another thread holds the lock, the method will return False\n\ |
| 231 | immediately. If `blocking` is True and another thread holds\n\ |
| 232 | the lock, the method will wait for the lock to be released,\n\ |
| 233 | take it and then return True.\n\ |
| 234 | (note: the blocking operation is not interruptible.)\n\ |
| 235 | \n\ |
| 236 | In all other cases, the method will return True immediately.\n\ |
| 237 | Precisely, if the current thread already holds the lock, its\n\ |
| 238 | internal counter is simply incremented. If nobody holds the lock,\n\ |
| 239 | the lock is taken and its internal counter initialized to 1."); |
| 240 | |
| 241 | static PyObject * |
| 242 | rlock_release(rlockobject *self) |
| 243 | { |
| 244 | long tid = PyThread_get_thread_ident(); |
| 245 | |
| 246 | if (self->rlock_count == 0 || self->rlock_owner != tid) { |
| 247 | PyErr_SetString(PyExc_RuntimeError, |
| 248 | "cannot release un-acquired lock"); |
| 249 | return NULL; |
| 250 | } |
| 251 | if (--self->rlock_count == 0) { |
| 252 | self->rlock_owner = 0; |
| 253 | PyThread_release_lock(self->rlock_lock); |
| 254 | } |
| 255 | Py_RETURN_NONE; |
| 256 | } |
| 257 | |
| 258 | PyDoc_STRVAR(rlock_release_doc, |
| 259 | "release()\n\ |
| 260 | \n\ |
| 261 | Release the lock, allowing another thread that is blocked waiting for\n\ |
| 262 | the lock to acquire the lock. The lock must be in the locked state,\n\ |
| 263 | and must be locked by the same thread that unlocks it; otherwise a\n\ |
| 264 | `RuntimeError` is raised.\n\ |
| 265 | \n\ |
| 266 | Do note that if the lock was acquire()d several times in a row by the\n\ |
| 267 | current thread, release() needs to be called as many times for the lock\n\ |
| 268 | to be available for other threads."); |
| 269 | |
| 270 | static PyObject * |
| 271 | rlock_acquire_restore(rlockobject *self, PyObject *arg) |
| 272 | { |
| 273 | long owner; |
| 274 | unsigned long count; |
| 275 | int r = 1; |
| 276 | |
| 277 | if (!PyArg_ParseTuple(arg, "kl:_acquire_restore", &count, &owner)) |
| 278 | return NULL; |
| 279 | |
| 280 | if (!PyThread_acquire_lock(self->rlock_lock, 0)) { |
| 281 | Py_BEGIN_ALLOW_THREADS |
| 282 | r = PyThread_acquire_lock(self->rlock_lock, 1); |
| 283 | Py_END_ALLOW_THREADS |
| 284 | } |
| 285 | if (!r) { |
| 286 | PyErr_SetString(ThreadError, "couldn't acquire lock"); |
| 287 | return NULL; |
| 288 | } |
| 289 | assert(self->rlock_count == 0); |
| 290 | self->rlock_owner = owner; |
| 291 | self->rlock_count = count; |
| 292 | Py_RETURN_NONE; |
| 293 | } |
| 294 | |
| 295 | PyDoc_STRVAR(rlock_acquire_restore_doc, |
| 296 | "_acquire_restore(state) -> None\n\ |
| 297 | \n\ |
| 298 | For internal use by `threading.Condition`."); |
| 299 | |
| 300 | static PyObject * |
| 301 | rlock_release_save(rlockobject *self) |
| 302 | { |
| 303 | long owner; |
| 304 | unsigned long count; |
| 305 | |
| 306 | owner = self->rlock_owner; |
| 307 | count = self->rlock_count; |
| 308 | self->rlock_count = 0; |
| 309 | self->rlock_owner = 0; |
| 310 | PyThread_release_lock(self->rlock_lock); |
| 311 | return Py_BuildValue("kl", count, owner); |
| 312 | } |
| 313 | |
| 314 | PyDoc_STRVAR(rlock_release_save_doc, |
| 315 | "_release_save() -> tuple\n\ |
| 316 | \n\ |
| 317 | For internal use by `threading.Condition`."); |
| 318 | |
| 319 | |
| 320 | static PyObject * |
| 321 | rlock_is_owned(rlockobject *self) |
| 322 | { |
| 323 | long tid = PyThread_get_thread_ident(); |
| 324 | |
| 325 | if (self->rlock_count > 0 && self->rlock_owner == tid) { |
| 326 | Py_RETURN_TRUE; |
| 327 | } |
| 328 | Py_RETURN_FALSE; |
| 329 | } |
| 330 | |
| 331 | PyDoc_STRVAR(rlock_is_owned_doc, |
| 332 | "_is_owned() -> bool\n\ |
| 333 | \n\ |
| 334 | For internal use by `threading.Condition`."); |
| 335 | |
| 336 | static PyObject * |
| 337 | rlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 338 | { |
| 339 | rlockobject *self; |
| 340 | |
| 341 | self = (rlockobject *) type->tp_alloc(type, 0); |
| 342 | if (self != NULL) { |
| 343 | self->rlock_lock = PyThread_allocate_lock(); |
| 344 | if (self->rlock_lock == NULL) { |
| 345 | type->tp_free(self); |
| 346 | PyErr_SetString(ThreadError, "can't allocate lock"); |
| 347 | return NULL; |
| 348 | } |
| 349 | self->in_weakreflist = NULL; |
| 350 | self->rlock_owner = 0; |
| 351 | self->rlock_count = 0; |
| 352 | } |
| 353 | |
| 354 | return (PyObject *) self; |
| 355 | } |
| 356 | |
| 357 | static PyObject * |
| 358 | rlock_repr(rlockobject *self) |
| 359 | { |
| 360 | return PyUnicode_FromFormat("<%s owner=%ld count=%lu>", |
| 361 | Py_TYPE(self)->tp_name, self->rlock_owner, self->rlock_count); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | static PyMethodDef rlock_methods[] = { |
| 366 | {"acquire", (PyCFunction)rlock_acquire, |
| 367 | METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc}, |
| 368 | {"release", (PyCFunction)rlock_release, |
| 369 | METH_NOARGS, rlock_release_doc}, |
| 370 | {"_is_owned", (PyCFunction)rlock_is_owned, |
| 371 | METH_NOARGS, rlock_is_owned_doc}, |
| 372 | {"_acquire_restore", (PyCFunction)rlock_acquire_restore, |
| 373 | METH_O, rlock_acquire_restore_doc}, |
| 374 | {"_release_save", (PyCFunction)rlock_release_save, |
| 375 | METH_NOARGS, rlock_release_save_doc}, |
| 376 | {"__enter__", (PyCFunction)rlock_acquire, |
| 377 | METH_VARARGS | METH_KEYWORDS, rlock_acquire_doc}, |
| 378 | {"__exit__", (PyCFunction)rlock_release, |
| 379 | METH_VARARGS, rlock_release_doc}, |
| 380 | {NULL, NULL} /* sentinel */ |
| 381 | }; |
| 382 | |
| 383 | |
| 384 | static PyTypeObject RLocktype = { |
| 385 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 386 | "_thread.RLock", /*tp_name*/ |
| 387 | sizeof(rlockobject), /*tp_size*/ |
| 388 | 0, /*tp_itemsize*/ |
| 389 | /* methods */ |
| 390 | (destructor)rlock_dealloc, /*tp_dealloc*/ |
| 391 | 0, /*tp_print*/ |
| 392 | 0, /*tp_getattr*/ |
| 393 | 0, /*tp_setattr*/ |
| 394 | 0, /*tp_reserved*/ |
| 395 | (reprfunc)rlock_repr, /*tp_repr*/ |
| 396 | 0, /*tp_as_number*/ |
| 397 | 0, /*tp_as_sequence*/ |
| 398 | 0, /*tp_as_mapping*/ |
| 399 | 0, /*tp_hash*/ |
| 400 | 0, /*tp_call*/ |
| 401 | 0, /*tp_str*/ |
| 402 | 0, /*tp_getattro*/ |
| 403 | 0, /*tp_setattro*/ |
| 404 | 0, /*tp_as_buffer*/ |
| 405 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 406 | 0, /*tp_doc*/ |
| 407 | 0, /*tp_traverse*/ |
| 408 | 0, /*tp_clear*/ |
| 409 | 0, /*tp_richcompare*/ |
| 410 | offsetof(rlockobject, in_weakreflist), /*tp_weaklistoffset*/ |
| 411 | 0, /*tp_iter*/ |
| 412 | 0, /*tp_iternext*/ |
| 413 | rlock_methods, /*tp_methods*/ |
| 414 | 0, /* tp_members */ |
| 415 | 0, /* tp_getset */ |
| 416 | 0, /* tp_base */ |
| 417 | 0, /* tp_dict */ |
| 418 | 0, /* tp_descr_get */ |
| 419 | 0, /* tp_descr_set */ |
| 420 | 0, /* tp_dictoffset */ |
| 421 | 0, /* tp_init */ |
| 422 | PyType_GenericAlloc, /* tp_alloc */ |
| 423 | rlock_new /* tp_new */ |
| 424 | }; |
| 425 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 426 | static lockobject * |
| 427 | newlockobject(void) |
| 428 | { |
| 429 | lockobject *self; |
| 430 | self = PyObject_New(lockobject, &Locktype); |
| 431 | if (self == NULL) |
| 432 | return NULL; |
| 433 | self->lock_lock = PyThread_allocate_lock(); |
Benjamin Peterson | bec4d57 | 2009-10-10 01:16:07 +0000 | [diff] [blame] | 434 | self->in_weakreflist = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 435 | if (self->lock_lock == NULL) { |
Victor Stinner | 8f3216a | 2010-03-03 00:50:12 +0000 | [diff] [blame] | 436 | Py_DECREF(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 437 | PyErr_SetString(ThreadError, "can't allocate lock"); |
Victor Stinner | 8f3216a | 2010-03-03 00:50:12 +0000 | [diff] [blame] | 438 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 439 | } |
| 440 | return self; |
| 441 | } |
| 442 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 443 | /* Thread-local objects */ |
| 444 | |
| 445 | #include "structmember.h" |
| 446 | |
| 447 | typedef struct { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 448 | PyObject_HEAD |
| 449 | PyObject *key; |
| 450 | PyObject *args; |
| 451 | PyObject *kw; |
| 452 | PyObject *dict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 453 | } localobject; |
| 454 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 455 | static PyObject * |
| 456 | local_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 457 | { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 458 | localobject *self; |
| 459 | PyObject *tdict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 460 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 461 | if (type->tp_init == PyBaseObject_Type.tp_init |
| 462 | && ((args && PyObject_IsTrue(args)) |
| 463 | || (kw && PyObject_IsTrue(kw)))) { |
| 464 | PyErr_SetString(PyExc_TypeError, |
| 465 | "Initialization arguments are not supported"); |
| 466 | return NULL; |
| 467 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 468 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 469 | self = (localobject *)type->tp_alloc(type, 0); |
| 470 | if (self == NULL) |
| 471 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 472 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 473 | Py_XINCREF(args); |
| 474 | self->args = args; |
| 475 | Py_XINCREF(kw); |
| 476 | self->kw = kw; |
| 477 | self->dict = NULL; /* making sure */ |
Walter Dörwald | 4ee631e | 2007-06-20 14:55:01 +0000 | [diff] [blame] | 478 | self->key = PyUnicode_FromFormat("thread.local.%p", self); |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 479 | if (self->key == NULL) |
| 480 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 481 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 482 | self->dict = PyDict_New(); |
| 483 | if (self->dict == NULL) |
| 484 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 485 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 486 | tdict = PyThreadState_GetDict(); |
| 487 | if (tdict == NULL) { |
| 488 | PyErr_SetString(PyExc_SystemError, |
| 489 | "Couldn't get thread-state dictionary"); |
| 490 | goto err; |
| 491 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 492 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 493 | if (PyDict_SetItem(tdict, self->key, self->dict) < 0) |
| 494 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 495 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 496 | return (PyObject *)self; |
| 497 | |
| 498 | err: |
| 499 | Py_DECREF(self); |
| 500 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static int |
| 504 | local_traverse(localobject *self, visitproc visit, void *arg) |
| 505 | { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 506 | Py_VISIT(self->args); |
| 507 | Py_VISIT(self->kw); |
| 508 | Py_VISIT(self->dict); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int |
| 513 | local_clear(localobject *self) |
| 514 | { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 515 | Py_CLEAR(self->args); |
| 516 | Py_CLEAR(self->kw); |
| 517 | Py_CLEAR(self->dict); |
| 518 | return 0; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static void |
| 522 | local_dealloc(localobject *self) |
| 523 | { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 524 | PyThreadState *tstate; |
| 525 | if (self->key |
| 526 | && (tstate = PyThreadState_Get()) |
| 527 | && tstate->interp) { |
| 528 | for(tstate = PyInterpreterState_ThreadHead(tstate->interp); |
| 529 | tstate; |
| 530 | tstate = PyThreadState_Next(tstate)) |
| 531 | if (tstate->dict && |
| 532 | PyDict_GetItem(tstate->dict, self->key)) |
| 533 | PyDict_DelItem(tstate->dict, self->key); |
| 534 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 535 | |
Philip Jenvey | 26713ca | 2009-09-29 04:57:18 +0000 | [diff] [blame] | 536 | Py_XDECREF(self->key); |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 537 | local_clear(self); |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 538 | Py_TYPE(self)->tp_free((PyObject*)self); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | static PyObject * |
| 542 | _ldict(localobject *self) |
| 543 | { |
| 544 | PyObject *tdict, *ldict; |
| 545 | |
| 546 | tdict = PyThreadState_GetDict(); |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 547 | if (tdict == NULL) { |
| 548 | PyErr_SetString(PyExc_SystemError, |
| 549 | "Couldn't get thread-state dictionary"); |
| 550 | return NULL; |
| 551 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 552 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 553 | ldict = PyDict_GetItem(tdict, self->key); |
| 554 | if (ldict == NULL) { |
| 555 | ldict = PyDict_New(); /* we own ldict */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 556 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 557 | if (ldict == NULL) |
| 558 | return NULL; |
| 559 | else { |
| 560 | int i = PyDict_SetItem(tdict, self->key, ldict); |
Georg Brandl | f3c4ad1 | 2006-03-08 12:24:33 +0000 | [diff] [blame] | 561 | Py_DECREF(ldict); /* now ldict is borrowed */ |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 562 | if (i < 0) |
| 563 | return NULL; |
| 564 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 565 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 566 | Py_CLEAR(self->dict); |
| 567 | Py_INCREF(ldict); |
| 568 | self->dict = ldict; /* still borrowed */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 569 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 570 | if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init && |
| 571 | Py_TYPE(self)->tp_init((PyObject*)self, |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 572 | self->args, self->kw) < 0) { |
| 573 | /* we need to get rid of ldict from thread so |
| 574 | we create a new one the next time we do an attr |
| 575 | acces */ |
| 576 | PyDict_DelItem(tdict, self->key); |
| 577 | return NULL; |
| 578 | } |
| 579 | |
| 580 | } |
Benjamin Peterson | 8a250ae | 2008-06-30 23:30:24 +0000 | [diff] [blame] | 581 | |
| 582 | /* The call to tp_init above may have caused another thread to run. |
| 583 | Install our ldict again. */ |
| 584 | if (self->dict != ldict) { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 585 | Py_CLEAR(self->dict); |
| 586 | Py_INCREF(ldict); |
| 587 | self->dict = ldict; |
| 588 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 589 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 590 | return ldict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 593 | static int |
| 594 | local_setattro(localobject *self, PyObject *name, PyObject *v) |
| 595 | { |
| 596 | PyObject *ldict; |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 597 | |
| 598 | ldict = _ldict(self); |
| 599 | if (ldict == NULL) |
| 600 | return -1; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 601 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 602 | return PyObject_GenericSetAttr((PyObject *)self, name, v); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static PyObject * |
| 606 | local_getdict(localobject *self, void *closure) |
| 607 | { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 608 | if (self->dict == NULL) { |
| 609 | PyErr_SetString(PyExc_AttributeError, "__dict__"); |
| 610 | return NULL; |
| 611 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 612 | |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 613 | Py_INCREF(self->dict); |
| 614 | return self->dict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static PyGetSetDef local_getset[] = { |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 618 | {"__dict__", (getter)local_getdict, (setter)NULL, |
| 619 | "Local-data dictionary", NULL}, |
| 620 | {NULL} /* Sentinel */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 621 | }; |
| 622 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 623 | static PyObject *local_getattro(localobject *, PyObject *); |
| 624 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 625 | static PyTypeObject localtype = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 626 | PyVarObject_HEAD_INIT(NULL, 0) |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 627 | /* tp_name */ "_thread._local", |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 628 | /* tp_basicsize */ sizeof(localobject), |
| 629 | /* tp_itemsize */ 0, |
| 630 | /* tp_dealloc */ (destructor)local_dealloc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 631 | /* tp_print */ 0, |
| 632 | /* tp_getattr */ 0, |
| 633 | /* tp_setattr */ 0, |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 634 | /* tp_reserved */ 0, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 635 | /* tp_repr */ 0, |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 636 | /* tp_as_number */ 0, |
| 637 | /* tp_as_sequence */ 0, |
| 638 | /* tp_as_mapping */ 0, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 639 | /* tp_hash */ 0, |
| 640 | /* tp_call */ 0, |
| 641 | /* tp_str */ 0, |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 642 | /* tp_getattro */ (getattrofunc)local_getattro, |
| 643 | /* tp_setattro */ (setattrofunc)local_setattro, |
| 644 | /* tp_as_buffer */ 0, |
| 645 | /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 646 | /* tp_doc */ "Thread-local data", |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 647 | /* tp_traverse */ (traverseproc)local_traverse, |
| 648 | /* tp_clear */ (inquiry)local_clear, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 649 | /* tp_richcompare */ 0, |
| 650 | /* tp_weaklistoffset */ 0, |
| 651 | /* tp_iter */ 0, |
| 652 | /* tp_iternext */ 0, |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 653 | /* tp_methods */ 0, |
| 654 | /* tp_members */ 0, |
| 655 | /* tp_getset */ local_getset, |
| 656 | /* tp_base */ 0, |
| 657 | /* tp_dict */ 0, /* internal use */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 658 | /* tp_descr_get */ 0, |
| 659 | /* tp_descr_set */ 0, |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 660 | /* tp_dictoffset */ offsetof(localobject, dict), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 661 | /* tp_init */ 0, |
| 662 | /* tp_alloc */ 0, |
| 663 | /* tp_new */ local_new, |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 664 | /* tp_free */ 0, /* Low-level free-mem routine */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 665 | /* tp_is_gc */ 0, /* For PyObject_IS_GC */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 666 | }; |
| 667 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 668 | static PyObject * |
| 669 | local_getattro(localobject *self, PyObject *name) |
| 670 | { |
| 671 | PyObject *ldict, *value; |
| 672 | |
| 673 | ldict = _ldict(self); |
| 674 | if (ldict == NULL) |
| 675 | return NULL; |
| 676 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 677 | if (Py_TYPE(self) != &localtype) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 678 | /* use generic lookup for subtypes */ |
| 679 | return PyObject_GenericGetAttr((PyObject *)self, name); |
| 680 | |
| 681 | /* Optimization: just look in dict ourselves */ |
| 682 | value = PyDict_GetItem(ldict, name); |
| 683 | if (value == NULL) |
| 684 | /* Fall back on generic to get __class__ and __dict__ */ |
| 685 | return PyObject_GenericGetAttr((PyObject *)self, name); |
| 686 | |
| 687 | Py_INCREF(value); |
| 688 | return value; |
| 689 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 690 | |
| 691 | /* Module functions */ |
| 692 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 693 | struct bootstate { |
| 694 | PyInterpreterState *interp; |
| 695 | PyObject *func; |
| 696 | PyObject *args; |
| 697 | PyObject *keyw; |
| 698 | }; |
| 699 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 700 | static void |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 701 | t_bootstrap(void *boot_raw) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 702 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 703 | struct bootstate *boot = (struct bootstate *) boot_raw; |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 704 | PyThreadState *tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 705 | PyObject *res; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 706 | |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 707 | tstate = PyThreadState_New(boot->interp); |
| 708 | |
| 709 | PyEval_AcquireThread(tstate); |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 710 | nb_threads++; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 711 | res = PyEval_CallObjectWithKeywords( |
| 712 | boot->func, boot->args, boot->keyw); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 713 | if (res == NULL) { |
Fred Drake | bebc97f | 1998-05-28 04:35:12 +0000 | [diff] [blame] | 714 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 715 | PyErr_Clear(); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 716 | else { |
Guido van Rossum | 24ccca1 | 2003-04-29 19:44:05 +0000 | [diff] [blame] | 717 | PyObject *file; |
| 718 | PySys_WriteStderr( |
| 719 | "Unhandled exception in thread started by "); |
| 720 | file = PySys_GetObject("stderr"); |
Christian Heimes | 2be0373 | 2007-11-15 02:26:46 +0000 | [diff] [blame] | 721 | if (file != NULL && file != Py_None) |
Guido van Rossum | 24ccca1 | 2003-04-29 19:44:05 +0000 | [diff] [blame] | 722 | PyFile_WriteObject(boot->func, file, 0); |
| 723 | else |
| 724 | PyObject_Print(boot->func, stderr, 0); |
| 725 | PySys_WriteStderr("\n"); |
Guido van Rossum | 40769dd | 1998-02-06 22:32:08 +0000 | [diff] [blame] | 726 | PyErr_PrintEx(0); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 727 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 728 | } |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 729 | else |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 730 | Py_DECREF(res); |
Guido van Rossum | 24ccca1 | 2003-04-29 19:44:05 +0000 | [diff] [blame] | 731 | Py_DECREF(boot->func); |
| 732 | Py_DECREF(boot->args); |
| 733 | Py_XDECREF(boot->keyw); |
| 734 | PyMem_DEL(boot_raw); |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 735 | nb_threads--; |
Michael W. Hudson | 188d436 | 2005-06-20 16:52:57 +0000 | [diff] [blame] | 736 | PyThreadState_Clear(tstate); |
| 737 | PyThreadState_DeleteCurrent(); |
Guido van Rossum | bcc2074 | 1998-08-04 22:53:56 +0000 | [diff] [blame] | 738 | PyThread_exit_thread(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 741 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 742 | thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 743 | { |
Guido van Rossum | 38d45b7 | 2000-09-01 20:47:58 +0000 | [diff] [blame] | 744 | PyObject *func, *args, *keyw = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 745 | struct bootstate *boot; |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 746 | long ident; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 747 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 748 | if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3, |
| 749 | &func, &args, &keyw)) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 750 | return NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 751 | if (!PyCallable_Check(func)) { |
| 752 | PyErr_SetString(PyExc_TypeError, |
| 753 | "first arg must be callable"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 754 | return NULL; |
| 755 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 756 | if (!PyTuple_Check(args)) { |
| 757 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 38d45b7 | 2000-09-01 20:47:58 +0000 | [diff] [blame] | 758 | "2nd arg must be a tuple"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 759 | return NULL; |
| 760 | } |
| 761 | if (keyw != NULL && !PyDict_Check(keyw)) { |
| 762 | PyErr_SetString(PyExc_TypeError, |
| 763 | "optional 3rd arg must be a dictionary"); |
| 764 | return NULL; |
| 765 | } |
| 766 | boot = PyMem_NEW(struct bootstate, 1); |
| 767 | if (boot == NULL) |
| 768 | return PyErr_NoMemory(); |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 769 | boot->interp = PyThreadState_GET()->interp; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 770 | boot->func = func; |
| 771 | boot->args = args; |
| 772 | boot->keyw = keyw; |
| 773 | Py_INCREF(func); |
| 774 | Py_INCREF(args); |
| 775 | Py_XINCREF(keyw); |
| 776 | PyEval_InitThreads(); /* Start the interpreter's thread-awareness */ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 777 | ident = PyThread_start_new_thread(t_bootstrap, (void*) boot); |
| 778 | if (ident == -1) { |
Guido van Rossum | 54c273c | 2005-02-20 03:02:16 +0000 | [diff] [blame] | 779 | PyErr_SetString(ThreadError, "can't start new thread"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 780 | Py_DECREF(func); |
| 781 | Py_DECREF(args); |
| 782 | Py_XDECREF(keyw); |
| 783 | PyMem_DEL(boot); |
| 784 | return NULL; |
| 785 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 786 | return PyLong_FromLong(ident); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 789 | PyDoc_STRVAR(start_new_doc, |
Andrew M. Kuchling | 38300c6 | 2001-10-05 12:24:15 +0000 | [diff] [blame] | 790 | "start_new_thread(function, args[, kwargs])\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 791 | (start_new() is an obsolete synonym)\n\ |
| 792 | \n\ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 793 | Start a new thread and return its identifier. The thread will call the\n\ |
| 794 | function with positional arguments from the tuple args and keyword arguments\n\ |
| 795 | taken from the optional dictionary kwargs. The thread exits when the\n\ |
| 796 | function returns; the return value is ignored. The thread will also exit\n\ |
| 797 | 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] | 798 | printed unless the exception is SystemExit.\n"); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 799 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 800 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 801 | thread_PyThread_exit_thread(PyObject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 802 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 803 | PyErr_SetNone(PyExc_SystemExit); |
Guido van Rossum | 385e7c6 | 1995-03-17 10:42:27 +0000 | [diff] [blame] | 804 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 807 | PyDoc_STRVAR(exit_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 808 | "exit()\n\ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 809 | (PyThread_exit_thread() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 810 | \n\ |
| 811 | 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] | 812 | thread to exit silently unless the exception is caught."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 813 | |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 814 | static PyObject * |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 815 | thread_PyThread_interrupt_main(PyObject * self) |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 816 | { |
| 817 | PyErr_SetInterrupt(); |
| 818 | Py_INCREF(Py_None); |
| 819 | return Py_None; |
| 820 | } |
| 821 | |
| 822 | PyDoc_STRVAR(interrupt_doc, |
| 823 | "interrupt_main()\n\ |
| 824 | \n\ |
| 825 | Raise a KeyboardInterrupt in the main thread.\n\ |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 826 | A subthread can use this function to interrupt the main thread." |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 827 | ); |
| 828 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 829 | static lockobject *newlockobject(void); |
| 830 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 831 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 832 | thread_PyThread_allocate_lock(PyObject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 833 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 834 | return (PyObject *) newlockobject(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 837 | PyDoc_STRVAR(allocate_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 838 | "allocate_lock() -> lock object\n\ |
| 839 | (allocate() is an obsolete synonym)\n\ |
| 840 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 841 | Create a new lock object. See LockType.__doc__ for information about locks."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 842 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 843 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 844 | thread_get_ident(PyObject *self) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 845 | { |
| 846 | long ident; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 847 | ident = PyThread_get_thread_ident(); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 848 | if (ident == -1) { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 849 | PyErr_SetString(ThreadError, "no current thread ident"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 850 | return NULL; |
| 851 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 852 | return PyLong_FromLong(ident); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 855 | PyDoc_STRVAR(get_ident_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 856 | "get_ident() -> integer\n\ |
| 857 | \n\ |
| 858 | Return a non-zero integer that uniquely identifies the current thread\n\ |
| 859 | amongst other threads that exist simultaneously.\n\ |
| 860 | This may be used to identify per-thread resources.\n\ |
| 861 | Even though on some platforms threads identities may appear to be\n\ |
| 862 | allocated consecutive numbers starting at 1, this behavior should not\n\ |
| 863 | 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] | 864 | 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] | 865 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 866 | static PyObject * |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 867 | thread__count(PyObject *self) |
| 868 | { |
| 869 | return PyLong_FromLong(nb_threads); |
| 870 | } |
| 871 | |
| 872 | PyDoc_STRVAR(_count_doc, |
| 873 | "_count() -> integer\n\ |
| 874 | \n\ |
Antoine Pitrou | 9257f5e | 2009-10-30 22:23:02 +0000 | [diff] [blame] | 875 | \ |
| 876 | Return the number of currently running Python threads, excluding \n\ |
| 877 | the main thread. The returned number comprises all threads created\n\ |
| 878 | through `start_new_thread()` as well as `threading.Thread`, and not\n\ |
| 879 | yet finished.\n\ |
| 880 | \n\ |
| 881 | This function is meant for internal and specialized purposes only.\n\ |
| 882 | In most applications `threading.enumerate()` should be used instead."); |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 883 | |
| 884 | static PyObject * |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 885 | thread_stack_size(PyObject *self, PyObject *args) |
| 886 | { |
| 887 | size_t old_size; |
| 888 | Py_ssize_t new_size = 0; |
| 889 | int rc; |
| 890 | |
| 891 | if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size)) |
| 892 | return NULL; |
| 893 | |
| 894 | if (new_size < 0) { |
| 895 | PyErr_SetString(PyExc_ValueError, |
| 896 | "size must be 0 or a positive value"); |
| 897 | return NULL; |
| 898 | } |
| 899 | |
| 900 | old_size = PyThread_get_stacksize(); |
| 901 | |
| 902 | rc = PyThread_set_stacksize((size_t) new_size); |
| 903 | if (rc == -1) { |
| 904 | PyErr_Format(PyExc_ValueError, |
| 905 | "size not valid: %zd bytes", |
| 906 | new_size); |
| 907 | return NULL; |
| 908 | } |
| 909 | if (rc == -2) { |
| 910 | PyErr_SetString(ThreadError, |
| 911 | "setting stack size not supported"); |
| 912 | return NULL; |
| 913 | } |
| 914 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 915 | return PyLong_FromSsize_t((Py_ssize_t) old_size); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | PyDoc_STRVAR(stack_size_doc, |
| 919 | "stack_size([size]) -> size\n\ |
| 920 | \n\ |
| 921 | Return the thread stack size used when creating new threads. The\n\ |
| 922 | optional size argument specifies the stack size (in bytes) to be used\n\ |
| 923 | for subsequently created threads, and must be 0 (use platform or\n\ |
| 924 | configured default) or a positive integer value of at least 32,768 (32k).\n\ |
| 925 | If changing the thread stack size is unsupported, a ThreadError\n\ |
| 926 | exception is raised. If the specified size is invalid, a ValueError\n\ |
| 927 | exception is raised, and the stack size is unmodified. 32k bytes\n\ |
| 928 | currently the minimum supported stack size value to guarantee\n\ |
| 929 | sufficient stack space for the interpreter itself.\n\ |
| 930 | \n\ |
| 931 | Note that some platforms may have particular restrictions on values for\n\ |
| 932 | the stack size, such as requiring a minimum stack size larger than 32kB or\n\ |
| 933 | requiring allocation in multiples of the system memory page size\n\ |
| 934 | - platform documentation should be referred to for more information\n\ |
| 935 | (4kB pages are common; using multiples of 4096 for the stack size is\n\ |
| 936 | the suggested approach in the absence of more specific information)."); |
| 937 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 938 | static PyMethodDef thread_methods[] = { |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 939 | {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, |
| 940 | METH_VARARGS, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 941 | start_new_doc}, |
Andrew M. Kuchling | e365fb8 | 2000-08-03 02:06:16 +0000 | [diff] [blame] | 942 | {"start_new", (PyCFunction)thread_PyThread_start_new_thread, |
| 943 | METH_VARARGS, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 944 | start_new_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 945 | {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 946 | METH_NOARGS, allocate_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 947 | {"allocate", (PyCFunction)thread_PyThread_allocate_lock, |
Neal Norwitz | 4632117 | 2002-03-26 14:52:00 +0000 | [diff] [blame] | 948 | METH_NOARGS, allocate_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 949 | {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 950 | METH_NOARGS, exit_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 951 | {"exit", (PyCFunction)thread_PyThread_exit_thread, |
Neal Norwitz | 4632117 | 2002-03-26 14:52:00 +0000 | [diff] [blame] | 952 | METH_NOARGS, exit_doc}, |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 953 | {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main, |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 954 | METH_NOARGS, interrupt_doc}, |
Andrew M. Kuchling | a1abb72 | 2000-08-03 02:34:44 +0000 | [diff] [blame] | 955 | {"get_ident", (PyCFunction)thread_get_ident, |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 956 | METH_NOARGS, get_ident_doc}, |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 957 | {"_count", (PyCFunction)thread__count, |
| 958 | METH_NOARGS, _count_doc}, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 959 | {"stack_size", (PyCFunction)thread_stack_size, |
| 960 | METH_VARARGS, |
| 961 | stack_size_doc}, |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 962 | {NULL, NULL} /* sentinel */ |
| 963 | }; |
| 964 | |
| 965 | |
| 966 | /* Initialization function */ |
| 967 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 968 | PyDoc_STRVAR(thread_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 969 | "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] | 970 | The 'threading' module provides a more convenient interface."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 971 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 972 | PyDoc_STRVAR(lock_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 973 | "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] | 974 | call the PyThread_allocate_lock() function. Methods are:\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 975 | \n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 976 | acquire() -- lock the lock, possibly blocking until it can be obtained\n\ |
| 977 | release() -- unlock of the lock\n\ |
| 978 | locked() -- test whether the lock is currently locked\n\ |
| 979 | \n\ |
| 980 | A lock is not owned by the thread that locked it; another thread may\n\ |
| 981 | 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] | 982 | will block until another thread unlocks it. Deadlocks may ensue."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 983 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 984 | static struct PyModuleDef threadmodule = { |
| 985 | PyModuleDef_HEAD_INIT, |
| 986 | "_thread", |
| 987 | thread_doc, |
| 988 | -1, |
| 989 | thread_methods, |
| 990 | NULL, |
| 991 | NULL, |
| 992 | NULL, |
| 993 | NULL |
| 994 | }; |
| 995 | |
| 996 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 997 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 998 | PyInit__thread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 999 | { |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1000 | PyObject *m, *d; |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 1001 | |
| 1002 | /* Initialize types: */ |
| 1003 | if (PyType_Ready(&localtype) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1004 | return NULL; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1005 | if (PyType_Ready(&Locktype) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1006 | return NULL; |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 1007 | if (PyType_Ready(&RLocktype) < 0) |
| 1008 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1009 | |
| 1010 | /* Create the module and add the functions */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1011 | m = PyModule_Create(&threadmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1012 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1013 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1014 | |
| 1015 | /* Add a symbolic constant */ |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1016 | d = PyModule_GetDict(m); |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 1017 | ThreadError = PyErr_NewException("_thread.error", NULL, NULL); |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 1018 | PyDict_SetItemString(d, "error", ThreadError); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 1019 | Locktype.tp_doc = lock_doc; |
| 1020 | Py_INCREF(&Locktype); |
| 1021 | PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1022 | |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 1023 | Py_INCREF(&RLocktype); |
| 1024 | if (PyModule_AddObject(m, "RLock", (PyObject *)&RLocktype) < 0) |
| 1025 | return NULL; |
| 1026 | |
Michael W. Hudson | 64e0814 | 2005-06-15 12:25:20 +0000 | [diff] [blame] | 1027 | Py_INCREF(&localtype); |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 1028 | if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1029 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 1031 | nb_threads = 0; |
| 1032 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1033 | /* Initialize the C thread library */ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 1034 | PyThread_init_thread(); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1035 | return m; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1036 | } |