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" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7 | #ifndef WITH_THREAD |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 8 | #error "Error! The rest of Python is not compiled with thread support." |
Neal Norwitz | 884baa1 | 2002-09-05 21:31:04 +0000 | [diff] [blame] | 9 | #error "Rerun configure, adding a --with-threads option." |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 10 | #error "Then run `make clean' followed by `make'." |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11 | #endif |
| 12 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 13 | #include "pythread.h" |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 14 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 15 | static PyObject *ThreadError; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 16 | |
| 17 | |
| 18 | /* Lock objects */ |
| 19 | |
| 20 | typedef struct { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 21 | PyObject_HEAD |
| 22 | PyThread_type_lock lock_lock; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 23 | } lockobject; |
| 24 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 25 | static void |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 26 | lock_dealloc(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 27 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 28 | if (self->lock_lock != NULL) { |
| 29 | /* Unlock the lock so it's safe to free it */ |
| 30 | PyThread_acquire_lock(self->lock_lock, 0); |
| 31 | PyThread_release_lock(self->lock_lock); |
| 32 | |
| 33 | PyThread_free_lock(self->lock_lock); |
| 34 | } |
| 35 | PyObject_Del(self); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 38 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 39 | lock_PyThread_acquire_lock(lockobject *self, PyObject *args) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 40 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 41 | int i = 1; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 43 | if (!PyArg_ParseTuple(args, "|i:acquire", &i)) |
| 44 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 45 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 46 | Py_BEGIN_ALLOW_THREADS |
| 47 | i = PyThread_acquire_lock(self->lock_lock, i); |
| 48 | Py_END_ALLOW_THREADS |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 49 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 50 | return PyBool_FromLong((long)i); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 53 | PyDoc_STRVAR(acquire_doc, |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 54 | "acquire([wait]) -> None or bool\n\ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 55 | (acquire_lock() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 56 | \n\ |
| 57 | Lock the lock. Without argument, this blocks if the lock is already\n\ |
| 58 | 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] | 59 | the lock, and return None once the lock is acquired.\n\ |
| 60 | 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] | 61 | 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] | 62 | The blocking operation is not interruptible."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 63 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 64 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 65 | lock_PyThread_release_lock(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 66 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 67 | /* Sanity check: the lock must be locked */ |
| 68 | if (PyThread_acquire_lock(self->lock_lock, 0)) { |
| 69 | PyThread_release_lock(self->lock_lock); |
| 70 | PyErr_SetString(ThreadError, "release unlocked lock"); |
| 71 | return NULL; |
| 72 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 74 | PyThread_release_lock(self->lock_lock); |
| 75 | Py_INCREF(Py_None); |
| 76 | return Py_None; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 79 | PyDoc_STRVAR(release_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 80 | "release()\n\ |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 81 | (release_lock() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 82 | \n\ |
| 83 | Release the lock, allowing another thread that is blocked waiting for\n\ |
| 84 | 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] | 85 | 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] | 86 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 87 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 88 | lock_locked_lock(lockobject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 89 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 90 | if (PyThread_acquire_lock(self->lock_lock, 0)) { |
| 91 | PyThread_release_lock(self->lock_lock); |
| 92 | return PyBool_FromLong(0L); |
| 93 | } |
| 94 | return PyBool_FromLong(1L); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 97 | PyDoc_STRVAR(locked_doc, |
Guido van Rossum | 8fdc75b | 2002-04-07 06:32:21 +0000 | [diff] [blame] | 98 | "locked() -> bool\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 99 | (locked_lock() is an obsolete synonym)\n\ |
| 100 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 101 | Return whether the lock is in the locked state."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 102 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 103 | static PyMethodDef lock_methods[] = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 104 | {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, |
| 105 | METH_VARARGS, acquire_doc}, |
| 106 | {"acquire", (PyCFunction)lock_PyThread_acquire_lock, |
| 107 | METH_VARARGS, acquire_doc}, |
| 108 | {"release_lock", (PyCFunction)lock_PyThread_release_lock, |
| 109 | METH_NOARGS, release_doc}, |
| 110 | {"release", (PyCFunction)lock_PyThread_release_lock, |
| 111 | METH_NOARGS, release_doc}, |
| 112 | {"locked_lock", (PyCFunction)lock_locked_lock, |
| 113 | METH_NOARGS, locked_doc}, |
| 114 | {"locked", (PyCFunction)lock_locked_lock, |
| 115 | METH_NOARGS, locked_doc}, |
| 116 | {"__enter__", (PyCFunction)lock_PyThread_acquire_lock, |
| 117 | METH_VARARGS, acquire_doc}, |
| 118 | {"__exit__", (PyCFunction)lock_PyThread_release_lock, |
| 119 | METH_VARARGS, release_doc}, |
| 120 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 123 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 124 | lock_getattr(lockobject *self, char *name) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 125 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 126 | return Py_FindMethod(lock_methods, (PyObject *)self, name); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 129 | static PyTypeObject Locktype = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 130 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 131 | "thread.lock", /*tp_name*/ |
| 132 | sizeof(lockobject), /*tp_size*/ |
| 133 | 0, /*tp_itemsize*/ |
| 134 | /* methods */ |
| 135 | (destructor)lock_dealloc, /*tp_dealloc*/ |
| 136 | 0, /*tp_print*/ |
| 137 | (getattrfunc)lock_getattr, /*tp_getattr*/ |
| 138 | 0, /*tp_setattr*/ |
| 139 | 0, /*tp_compare*/ |
| 140 | 0, /*tp_repr*/ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
Anthony Baxter | 5576b54 | 2006-04-12 04:08:46 +0000 | [diff] [blame] | 143 | static lockobject * |
| 144 | newlockobject(void) |
| 145 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 146 | lockobject *self; |
| 147 | self = PyObject_New(lockobject, &Locktype); |
| 148 | if (self == NULL) |
| 149 | return NULL; |
| 150 | self->lock_lock = PyThread_allocate_lock(); |
| 151 | if (self->lock_lock == NULL) { |
| 152 | Py_DECREF(self); |
| 153 | PyErr_SetString(ThreadError, "can't allocate lock"); |
| 154 | return NULL; |
| 155 | } |
| 156 | return self; |
Anthony Baxter | 5576b54 | 2006-04-12 04:08:46 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 159 | /* Thread-local objects */ |
| 160 | |
| 161 | #include "structmember.h" |
| 162 | |
| 163 | typedef struct { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 164 | PyObject_HEAD |
| 165 | PyObject *key; |
| 166 | PyObject *args; |
| 167 | PyObject *kw; |
| 168 | PyObject *dict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 169 | } localobject; |
| 170 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 171 | static PyObject * |
| 172 | local_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 173 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 174 | localobject *self; |
| 175 | PyObject *tdict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 176 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 177 | if (type->tp_init == PyBaseObject_Type.tp_init |
| 178 | && ((args && PyObject_IsTrue(args)) |
| 179 | || (kw && PyObject_IsTrue(kw)))) { |
| 180 | PyErr_SetString(PyExc_TypeError, |
| 181 | "Initialization arguments are not supported"); |
| 182 | return NULL; |
| 183 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 184 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 185 | self = (localobject *)type->tp_alloc(type, 0); |
| 186 | if (self == NULL) |
| 187 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 189 | Py_XINCREF(args); |
| 190 | self->args = args; |
| 191 | Py_XINCREF(kw); |
| 192 | self->kw = kw; |
| 193 | self->dict = NULL; /* making sure */ |
| 194 | self->key = PyString_FromFormat("thread.local.%p", self); |
| 195 | if (self->key == NULL) |
| 196 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 197 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 198 | self->dict = PyDict_New(); |
| 199 | if (self->dict == NULL) |
| 200 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 201 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 202 | tdict = PyThreadState_GetDict(); |
| 203 | if (tdict == NULL) { |
| 204 | PyErr_SetString(PyExc_SystemError, |
| 205 | "Couldn't get thread-state dictionary"); |
| 206 | goto err; |
| 207 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 209 | if (PyDict_SetItem(tdict, self->key, self->dict) < 0) |
| 210 | goto err; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 211 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 212 | return (PyObject *)self; |
Michael W. Hudson | 2368b3c | 2005-06-15 12:48:40 +0000 | [diff] [blame] | 213 | |
| 214 | err: |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 215 | Py_DECREF(self); |
| 216 | return NULL; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static int |
| 220 | local_traverse(localobject *self, visitproc visit, void *arg) |
| 221 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 222 | Py_VISIT(self->args); |
| 223 | Py_VISIT(self->kw); |
| 224 | Py_VISIT(self->dict); |
| 225 | return 0; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static int |
| 229 | local_clear(localobject *self) |
| 230 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 231 | Py_CLEAR(self->args); |
| 232 | Py_CLEAR(self->kw); |
| 233 | Py_CLEAR(self->dict); |
| 234 | return 0; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void |
| 238 | local_dealloc(localobject *self) |
| 239 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 240 | PyThreadState *tstate; |
| 241 | if (self->key |
| 242 | && (tstate = PyThreadState_Get()) |
| 243 | && tstate->interp) { |
| 244 | for(tstate = PyInterpreterState_ThreadHead(tstate->interp); |
| 245 | tstate; |
| 246 | tstate = PyThreadState_Next(tstate)) |
| 247 | if (tstate->dict && |
| 248 | PyDict_GetItem(tstate->dict, self->key)) |
| 249 | PyDict_DelItem(tstate->dict, self->key); |
| 250 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 252 | Py_XDECREF(self->key); |
| 253 | local_clear(self); |
| 254 | Py_TYPE(self)->tp_free((PyObject*)self); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static PyObject * |
| 258 | _ldict(localobject *self) |
| 259 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 260 | PyObject *tdict, *ldict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 262 | tdict = PyThreadState_GetDict(); |
| 263 | if (tdict == NULL) { |
| 264 | PyErr_SetString(PyExc_SystemError, |
| 265 | "Couldn't get thread-state dictionary"); |
| 266 | return NULL; |
| 267 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 268 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 269 | ldict = PyDict_GetItem(tdict, self->key); |
| 270 | if (ldict == NULL) { |
| 271 | ldict = PyDict_New(); /* we own ldict */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 272 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 273 | if (ldict == NULL) |
| 274 | return NULL; |
| 275 | else { |
| 276 | int i = PyDict_SetItem(tdict, self->key, ldict); |
| 277 | Py_DECREF(ldict); /* now ldict is borrowed */ |
| 278 | if (i < 0) |
| 279 | return NULL; |
| 280 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 281 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 282 | Py_CLEAR(self->dict); |
| 283 | Py_INCREF(ldict); |
| 284 | self->dict = ldict; /* still borrowed */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 286 | if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init && |
| 287 | Py_TYPE(self)->tp_init((PyObject*)self, |
| 288 | self->args, self->kw) < 0) { |
| 289 | /* we need to get rid of ldict from thread so |
| 290 | we create a new one the next time we do an attr |
| 291 | acces */ |
| 292 | PyDict_DelItem(tdict, self->key); |
| 293 | return NULL; |
| 294 | } |
Amaury Forgeot d'Arc | 1f40c8a | 2008-06-30 22:42:40 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 296 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 298 | /* The call to tp_init above may have caused another thread to run. |
| 299 | Install our ldict again. */ |
| 300 | if (self->dict != ldict) { |
| 301 | Py_CLEAR(self->dict); |
| 302 | Py_INCREF(ldict); |
| 303 | self->dict = ldict; |
| 304 | } |
| 305 | |
| 306 | return ldict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 309 | static int |
| 310 | local_setattro(localobject *self, PyObject *name, PyObject *v) |
| 311 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 312 | PyObject *ldict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 313 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 314 | ldict = _ldict(self); |
| 315 | if (ldict == NULL) |
| 316 | return -1; |
| 317 | |
| 318 | return PyObject_GenericSetAttr((PyObject *)self, name, v); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static PyObject * |
| 322 | local_getdict(localobject *self, void *closure) |
| 323 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 324 | if (self->dict == NULL) { |
| 325 | PyErr_SetString(PyExc_AttributeError, "__dict__"); |
| 326 | return NULL; |
| 327 | } |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 329 | Py_INCREF(self->dict); |
| 330 | return self->dict; |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static PyGetSetDef local_getset[] = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 334 | {"__dict__", (getter)local_getdict, (setter)NULL, |
| 335 | "Local-data dictionary", NULL}, |
| 336 | {NULL} /* Sentinel */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
Anthony Baxter | 5576b54 | 2006-04-12 04:08:46 +0000 | [diff] [blame] | 339 | static PyObject *local_getattro(localobject *, PyObject *); |
| 340 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 341 | static PyTypeObject localtype = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 342 | PyVarObject_HEAD_INIT(NULL, 0) |
| 343 | /* tp_name */ "thread._local", |
| 344 | /* tp_basicsize */ sizeof(localobject), |
| 345 | /* tp_itemsize */ 0, |
| 346 | /* tp_dealloc */ (destructor)local_dealloc, |
| 347 | /* tp_print */ 0, |
| 348 | /* tp_getattr */ 0, |
| 349 | /* tp_setattr */ 0, |
| 350 | /* tp_compare */ 0, |
| 351 | /* tp_repr */ 0, |
| 352 | /* tp_as_number */ 0, |
| 353 | /* tp_as_sequence */ 0, |
| 354 | /* tp_as_mapping */ 0, |
| 355 | /* tp_hash */ 0, |
| 356 | /* tp_call */ 0, |
| 357 | /* tp_str */ 0, |
| 358 | /* tp_getattro */ (getattrofunc)local_getattro, |
| 359 | /* tp_setattro */ (setattrofunc)local_setattro, |
| 360 | /* tp_as_buffer */ 0, |
| 361 | /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 362 | /* tp_doc */ "Thread-local data", |
| 363 | /* tp_traverse */ (traverseproc)local_traverse, |
| 364 | /* tp_clear */ (inquiry)local_clear, |
| 365 | /* tp_richcompare */ 0, |
| 366 | /* tp_weaklistoffset */ 0, |
| 367 | /* tp_iter */ 0, |
| 368 | /* tp_iternext */ 0, |
| 369 | /* tp_methods */ 0, |
| 370 | /* tp_members */ 0, |
| 371 | /* tp_getset */ local_getset, |
| 372 | /* tp_base */ 0, |
| 373 | /* tp_dict */ 0, /* internal use */ |
| 374 | /* tp_descr_get */ 0, |
| 375 | /* tp_descr_set */ 0, |
| 376 | /* tp_dictoffset */ offsetof(localobject, dict), |
| 377 | /* tp_init */ 0, |
| 378 | /* tp_alloc */ 0, |
| 379 | /* tp_new */ local_new, |
| 380 | /* tp_free */ 0, /* Low-level free-mem routine */ |
| 381 | /* tp_is_gc */ 0, /* For PyObject_IS_GC */ |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
Anthony Baxter | 5576b54 | 2006-04-12 04:08:46 +0000 | [diff] [blame] | 384 | static PyObject * |
| 385 | local_getattro(localobject *self, PyObject *name) |
| 386 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 387 | PyObject *ldict, *value; |
Anthony Baxter | 5576b54 | 2006-04-12 04:08:46 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 389 | ldict = _ldict(self); |
| 390 | if (ldict == NULL) |
| 391 | return NULL; |
Anthony Baxter | 5576b54 | 2006-04-12 04:08:46 +0000 | [diff] [blame] | 392 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 393 | if (Py_TYPE(self) != &localtype) |
| 394 | /* use generic lookup for subtypes */ |
| 395 | return PyObject_GenericGetAttr((PyObject *)self, name); |
Anthony Baxter | 5576b54 | 2006-04-12 04:08:46 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 397 | /* Optimization: just look in dict ourselves */ |
| 398 | value = PyDict_GetItem(ldict, name); |
| 399 | if (value == NULL) |
| 400 | /* Fall back on generic to get __class__ and __dict__ */ |
| 401 | return PyObject_GenericGetAttr((PyObject *)self, name); |
Anthony Baxter | 5576b54 | 2006-04-12 04:08:46 +0000 | [diff] [blame] | 402 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 403 | Py_INCREF(value); |
| 404 | return value; |
Anthony Baxter | 5576b54 | 2006-04-12 04:08:46 +0000 | [diff] [blame] | 405 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 406 | |
| 407 | /* Module functions */ |
| 408 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 409 | struct bootstate { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 410 | PyInterpreterState *interp; |
| 411 | PyObject *func; |
| 412 | PyObject *args; |
| 413 | PyObject *keyw; |
| 414 | PyThreadState *tstate; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 415 | }; |
| 416 | |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 417 | static void |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 418 | t_bootstrap(void *boot_raw) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 419 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 420 | struct bootstate *boot = (struct bootstate *) boot_raw; |
| 421 | PyThreadState *tstate; |
| 422 | PyObject *res; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 423 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 424 | tstate = boot->tstate; |
| 425 | tstate->thread_id = PyThread_get_thread_ident(); |
| 426 | _PyThreadState_Init(tstate); |
| 427 | PyEval_AcquireThread(tstate); |
| 428 | res = PyEval_CallObjectWithKeywords( |
| 429 | boot->func, boot->args, boot->keyw); |
| 430 | if (res == NULL) { |
| 431 | if (PyErr_ExceptionMatches(PyExc_SystemExit)) |
| 432 | PyErr_Clear(); |
| 433 | else { |
| 434 | PyObject *file; |
| 435 | PySys_WriteStderr( |
| 436 | "Unhandled exception in thread started by "); |
| 437 | file = PySys_GetObject("stderr"); |
| 438 | if (file) |
| 439 | PyFile_WriteObject(boot->func, file, 0); |
| 440 | else |
| 441 | PyObject_Print(boot->func, stderr, 0); |
| 442 | PySys_WriteStderr("\n"); |
| 443 | PyErr_PrintEx(0); |
| 444 | } |
| 445 | } |
| 446 | else |
| 447 | Py_DECREF(res); |
| 448 | Py_DECREF(boot->func); |
| 449 | Py_DECREF(boot->args); |
| 450 | Py_XDECREF(boot->keyw); |
| 451 | PyMem_DEL(boot_raw); |
| 452 | PyThreadState_Clear(tstate); |
| 453 | PyThreadState_DeleteCurrent(); |
| 454 | PyThread_exit_thread(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 457 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 458 | thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 459 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 460 | PyObject *func, *args, *keyw = NULL; |
| 461 | struct bootstate *boot; |
| 462 | long ident; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 463 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 464 | if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3, |
| 465 | &func, &args, &keyw)) |
| 466 | return NULL; |
| 467 | if (!PyCallable_Check(func)) { |
| 468 | PyErr_SetString(PyExc_TypeError, |
| 469 | "first arg must be callable"); |
| 470 | return NULL; |
| 471 | } |
| 472 | if (!PyTuple_Check(args)) { |
| 473 | PyErr_SetString(PyExc_TypeError, |
| 474 | "2nd arg must be a tuple"); |
| 475 | return NULL; |
| 476 | } |
| 477 | if (keyw != NULL && !PyDict_Check(keyw)) { |
| 478 | PyErr_SetString(PyExc_TypeError, |
| 479 | "optional 3rd arg must be a dictionary"); |
| 480 | return NULL; |
| 481 | } |
| 482 | boot = PyMem_NEW(struct bootstate, 1); |
| 483 | if (boot == NULL) |
| 484 | return PyErr_NoMemory(); |
| 485 | boot->interp = PyThreadState_GET()->interp; |
| 486 | boot->func = func; |
| 487 | boot->args = args; |
| 488 | boot->keyw = keyw; |
| 489 | boot->tstate = _PyThreadState_Prealloc(boot->interp); |
| 490 | if (boot->tstate == NULL) { |
| 491 | PyMem_DEL(boot); |
| 492 | return PyErr_NoMemory(); |
| 493 | } |
| 494 | Py_INCREF(func); |
| 495 | Py_INCREF(args); |
| 496 | Py_XINCREF(keyw); |
| 497 | PyEval_InitThreads(); /* Start the interpreter's thread-awareness */ |
| 498 | ident = PyThread_start_new_thread(t_bootstrap, (void*) boot); |
| 499 | if (ident == -1) { |
| 500 | PyErr_SetString(ThreadError, "can't start new thread"); |
| 501 | Py_DECREF(func); |
| 502 | Py_DECREF(args); |
| 503 | Py_XDECREF(keyw); |
| 504 | PyThreadState_Clear(boot->tstate); |
| 505 | PyMem_DEL(boot); |
| 506 | return NULL; |
| 507 | } |
| 508 | return PyInt_FromLong(ident); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 511 | PyDoc_STRVAR(start_new_doc, |
Andrew M. Kuchling | 38300c6 | 2001-10-05 12:24:15 +0000 | [diff] [blame] | 512 | "start_new_thread(function, args[, kwargs])\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 513 | (start_new() is an obsolete synonym)\n\ |
| 514 | \n\ |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 515 | Start a new thread and return its identifier. The thread will call the\n\ |
| 516 | function with positional arguments from the tuple args and keyword arguments\n\ |
| 517 | taken from the optional dictionary kwargs. The thread exits when the\n\ |
| 518 | function returns; the return value is ignored. The thread will also exit\n\ |
| 519 | 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] | 520 | printed unless the exception is SystemExit.\n"); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 521 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 522 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 523 | thread_PyThread_exit_thread(PyObject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 524 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 525 | PyErr_SetNone(PyExc_SystemExit); |
| 526 | return NULL; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 529 | PyDoc_STRVAR(exit_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 530 | "exit()\n\ |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 531 | (PyThread_exit_thread() is an obsolete synonym)\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 532 | \n\ |
| 533 | 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] | 534 | thread to exit silently unless the exception is caught."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 535 | |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 536 | static PyObject * |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 537 | thread_PyThread_interrupt_main(PyObject * self) |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 538 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 539 | PyErr_SetInterrupt(); |
| 540 | Py_INCREF(Py_None); |
| 541 | return Py_None; |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | PyDoc_STRVAR(interrupt_doc, |
| 545 | "interrupt_main()\n\ |
| 546 | \n\ |
| 547 | Raise a KeyboardInterrupt in the main thread.\n\ |
Kurt B. Kaiser | a1ad5f6 | 2003-06-16 18:51:28 +0000 | [diff] [blame] | 548 | A subthread can use this function to interrupt the main thread." |
Kurt B. Kaiser | a11e846 | 2003-06-13 21:59:45 +0000 | [diff] [blame] | 549 | ); |
| 550 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 551 | #ifndef NO_EXIT_PROG |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 552 | static PyObject * |
Peter Schneider-Kamp | 3707efe | 2000-07-10 10:03:58 +0000 | [diff] [blame] | 553 | thread_PyThread_exit_prog(PyObject *self, PyObject *args) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 554 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 555 | int sts; |
| 556 | if (!PyArg_ParseTuple(args, "i:exit_prog", &sts)) |
| 557 | return NULL; |
| 558 | Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */ |
| 559 | for (;;) { } /* Should not be reached */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 560 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 561 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 562 | |
Anthony Baxter | 5576b54 | 2006-04-12 04:08:46 +0000 | [diff] [blame] | 563 | static lockobject *newlockobject(void); |
| 564 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 565 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 566 | thread_PyThread_allocate_lock(PyObject *self) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 567 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 568 | return (PyObject *) newlockobject(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 571 | PyDoc_STRVAR(allocate_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 572 | "allocate_lock() -> lock object\n\ |
| 573 | (allocate() is an obsolete synonym)\n\ |
| 574 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 575 | 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] | 576 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 577 | static PyObject * |
Neal Norwitz | 3a6f978 | 2002-03-25 20:46:46 +0000 | [diff] [blame] | 578 | thread_get_ident(PyObject *self) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 579 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 580 | long ident; |
| 581 | ident = PyThread_get_thread_ident(); |
| 582 | if (ident == -1) { |
| 583 | PyErr_SetString(ThreadError, "no current thread ident"); |
| 584 | return NULL; |
| 585 | } |
| 586 | return PyInt_FromLong(ident); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 589 | PyDoc_STRVAR(get_ident_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 590 | "get_ident() -> integer\n\ |
| 591 | \n\ |
| 592 | Return a non-zero integer that uniquely identifies the current thread\n\ |
| 593 | amongst other threads that exist simultaneously.\n\ |
| 594 | This may be used to identify per-thread resources.\n\ |
| 595 | Even though on some platforms threads identities may appear to be\n\ |
| 596 | allocated consecutive numbers starting at 1, this behavior should not\n\ |
| 597 | 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] | 598 | 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] | 599 | |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 600 | static PyObject * |
| 601 | thread_stack_size(PyObject *self, PyObject *args) |
| 602 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 603 | size_t old_size; |
| 604 | Py_ssize_t new_size = 0; |
| 605 | int rc; |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 607 | if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size)) |
| 608 | return NULL; |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 609 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 610 | if (new_size < 0) { |
| 611 | PyErr_SetString(PyExc_ValueError, |
| 612 | "size must be 0 or a positive value"); |
| 613 | return NULL; |
| 614 | } |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 615 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 616 | old_size = PyThread_get_stacksize(); |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 618 | rc = PyThread_set_stacksize((size_t) new_size); |
| 619 | if (rc == -1) { |
| 620 | PyErr_Format(PyExc_ValueError, |
| 621 | "size not valid: %zd bytes", |
| 622 | new_size); |
| 623 | return NULL; |
| 624 | } |
| 625 | if (rc == -2) { |
| 626 | PyErr_SetString(ThreadError, |
| 627 | "setting stack size not supported"); |
| 628 | return NULL; |
| 629 | } |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 631 | return PyInt_FromSsize_t((Py_ssize_t) old_size); |
Andrew MacIntyre | 9291332 | 2006-06-13 15:04:24 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | PyDoc_STRVAR(stack_size_doc, |
| 635 | "stack_size([size]) -> size\n\ |
| 636 | \n\ |
| 637 | Return the thread stack size used when creating new threads. The\n\ |
| 638 | optional size argument specifies the stack size (in bytes) to be used\n\ |
| 639 | for subsequently created threads, and must be 0 (use platform or\n\ |
| 640 | configured default) or a positive integer value of at least 32,768 (32k).\n\ |
| 641 | If changing the thread stack size is unsupported, a ThreadError\n\ |
| 642 | exception is raised. If the specified size is invalid, a ValueError\n\ |
| 643 | exception is raised, and the stack size is unmodified. 32k bytes\n\ |
| 644 | currently the minimum supported stack size value to guarantee\n\ |
| 645 | sufficient stack space for the interpreter itself.\n\ |
| 646 | \n\ |
| 647 | Note that some platforms may have particular restrictions on values for\n\ |
| 648 | the stack size, such as requiring a minimum stack size larger than 32kB or\n\ |
| 649 | requiring allocation in multiples of the system memory page size\n\ |
| 650 | - platform documentation should be referred to for more information\n\ |
| 651 | (4kB pages are common; using multiples of 4096 for the stack size is\n\ |
| 652 | the suggested approach in the absence of more specific information)."); |
| 653 | |
Barry Warsaw | d0c1042 | 1996-12-17 00:05:22 +0000 | [diff] [blame] | 654 | static PyMethodDef thread_methods[] = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 655 | {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, |
| 656 | METH_VARARGS, |
| 657 | start_new_doc}, |
| 658 | {"start_new", (PyCFunction)thread_PyThread_start_new_thread, |
| 659 | METH_VARARGS, |
| 660 | start_new_doc}, |
| 661 | {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, |
| 662 | METH_NOARGS, allocate_doc}, |
| 663 | {"allocate", (PyCFunction)thread_PyThread_allocate_lock, |
| 664 | METH_NOARGS, allocate_doc}, |
| 665 | {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, |
| 666 | METH_NOARGS, exit_doc}, |
| 667 | {"exit", (PyCFunction)thread_PyThread_exit_thread, |
| 668 | METH_NOARGS, exit_doc}, |
| 669 | {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main, |
| 670 | METH_NOARGS, interrupt_doc}, |
| 671 | {"get_ident", (PyCFunction)thread_get_ident, |
| 672 | METH_NOARGS, get_ident_doc}, |
| 673 | {"stack_size", (PyCFunction)thread_stack_size, |
| 674 | METH_VARARGS, |
| 675 | stack_size_doc}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 676 | #ifndef NO_EXIT_PROG |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 677 | {"exit_prog", (PyCFunction)thread_PyThread_exit_prog, |
| 678 | METH_VARARGS}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 679 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 680 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 681 | }; |
| 682 | |
| 683 | |
| 684 | /* Initialization function */ |
| 685 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 686 | PyDoc_STRVAR(thread_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 687 | "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] | 688 | The 'threading' module provides a more convenient interface."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 689 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 690 | PyDoc_STRVAR(lock_doc, |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 691 | "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] | 692 | call the PyThread_allocate_lock() function. Methods are:\n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 693 | \n\ |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 694 | acquire() -- lock the lock, possibly blocking until it can be obtained\n\ |
| 695 | release() -- unlock of the lock\n\ |
| 696 | locked() -- test whether the lock is currently locked\n\ |
| 697 | \n\ |
| 698 | A lock is not owned by the thread that locked it; another thread may\n\ |
| 699 | 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] | 700 | will block until another thread unlocks it. Deadlocks may ensue."); |
Guido van Rossum | 75e9fc3 | 1998-06-27 18:21:06 +0000 | [diff] [blame] | 701 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 702 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 703 | initthread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 704 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 705 | PyObject *m, *d; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 706 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 707 | /* Initialize types: */ |
| 708 | if (PyType_Ready(&localtype) < 0) |
| 709 | return; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 710 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 711 | /* Create the module and add the functions */ |
| 712 | m = Py_InitModule3("thread", thread_methods, thread_doc); |
| 713 | if (m == NULL) |
| 714 | return; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 715 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 716 | /* Add a symbolic constant */ |
| 717 | d = PyModule_GetDict(m); |
| 718 | ThreadError = PyErr_NewException("thread.error", NULL, NULL); |
| 719 | PyDict_SetItemString(d, "error", ThreadError); |
| 720 | Locktype.tp_doc = lock_doc; |
| 721 | Py_INCREF(&Locktype); |
| 722 | PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 723 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame^] | 724 | Py_INCREF(&localtype); |
| 725 | if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0) |
| 726 | return; |
| 727 | |
| 728 | /* Initialize the C thread library */ |
| 729 | PyThread_init_thread(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 730 | } |