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