annotate Modules/threadmodule.c @ 62449:53a152b400a7 legacy-trunk

must force gc here
author Benjamin Peterson <benjamin@python.org>
date Tue, 22 Jun 2010 20:26:20 +0000
parents fd25030223b0
children b65dce5902a2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Thread module */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 /* Interface to Sjoerd's portable C thread library */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
5 #include "Python.h"
55704
e21c4c527e66 Add weakref support to the thread.lock type.
Gregory P. Smith <greg@mad-scientist.com>
parents: 54321
diff changeset
6 #include "structmember.h" /* offsetof */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
8 #ifndef WITH_THREAD
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 4944
diff changeset
9 #error "Error! The rest of Python is not compiled with thread support."
25127
5165a0ee84c6 --with(out)-thread is deprecated according to configure --help
Neal Norwitz <nnorwitz@gmail.com>
parents: 24471
diff changeset
10 #error "Rerun configure, adding a --with-threads option."
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 4944
diff changeset
11 #error "Then run `make clean' followed by `make'."
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
12 #endif
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
13
9203
07dae6fdae79 Renamed thread.h to pythread.h.
Guido van Rossum <guido@python.org>
parents: 8823
diff changeset
14 #include "pythread.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
16 static PyObject *ThreadError;
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
17 static long nb_threads = 0;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 /* Lock objects */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 typedef struct {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
22 PyObject_HEAD
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
23 PyThread_type_lock lock_lock;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
24 PyObject *in_weakreflist;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 } lockobject;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 static void
13497
95454b73ea91 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
28 lock_dealloc(lockobject *self)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
30 if (self->in_weakreflist != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
31 PyObject_ClearWeakRefs((PyObject *) self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
32 if (self->lock_lock != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
33 /* Unlock the lock so it's safe to free it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
34 PyThread_acquire_lock(self->lock_lock, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
35 PyThread_release_lock(self->lock_lock);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
36
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
37 PyThread_free_lock(self->lock_lock);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
38 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
39 PyObject_Del(self);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
42 static PyObject *
13497
95454b73ea91 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
43 lock_PyThread_acquire_lock(lockobject *self, PyObject *args)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
45 int i = 1;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
47 if (!PyArg_ParseTuple(args, "|i:acquire", &i))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
48 return NULL;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
50 Py_BEGIN_ALLOW_THREADS
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
51 i = PyThread_acquire_lock(self->lock_lock, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
52 Py_END_ALLOW_THREADS
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
54 return PyBool_FromLong((long)i);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
57 PyDoc_STRVAR(acquire_doc,
22865
7014921820a5 Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents: 22709
diff changeset
58 "acquire([wait]) -> None or bool\n\
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36895
diff changeset
59 (acquire_lock() is an obsolete synonym)\n\
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
60 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
61 Lock the lock. Without argument, this blocks if the lock is already\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
62 locked (even by the same thread), waiting for another thread to release\n\
22865
7014921820a5 Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents: 22709
diff changeset
63 the lock, and return None once the lock is acquired.\n\
7014921820a5 Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents: 22709
diff changeset
64 With an argument, this will only block if the argument is true,\n\
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
65 and the return value reflects whether the lock is acquired.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
66 The blocking operation is not interruptible.");
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
67
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
68 static PyObject *
22561
043ad7adc896 Remove many uses of PyArg_NoArgs macro, change METH_OLDARGS to METH_NOARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21313
diff changeset
69 lock_PyThread_release_lock(lockobject *self)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
71 /* Sanity check: the lock must be locked */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
72 if (PyThread_acquire_lock(self->lock_lock, 0)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
73 PyThread_release_lock(self->lock_lock);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
74 PyErr_SetString(ThreadError, "release unlocked lock");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
75 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
76 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
78 PyThread_release_lock(self->lock_lock);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
79 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
80 return Py_None;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
83 PyDoc_STRVAR(release_doc,
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
84 "release()\n\
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36895
diff changeset
85 (release_lock() is an obsolete synonym)\n\
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
86 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
87 Release the lock, allowing another thread that is blocked waiting for\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
88 the lock to acquire the lock. The lock must be in the locked state,\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
89 but it needn't be locked by the same thread that unlocks it.");
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
90
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
91 static PyObject *
22561
043ad7adc896 Remove many uses of PyArg_NoArgs macro, change METH_OLDARGS to METH_NOARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21313
diff changeset
92 lock_locked_lock(lockobject *self)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
94 if (PyThread_acquire_lock(self->lock_lock, 0)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
95 PyThread_release_lock(self->lock_lock);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
96 return PyBool_FromLong(0L);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
97 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
98 return PyBool_FromLong(1L);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
101 PyDoc_STRVAR(locked_doc,
22865
7014921820a5 Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents: 22709
diff changeset
102 "locked() -> bool\n\
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
103 (locked_lock() is an obsolete synonym)\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
104 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
105 Return whether the lock is in the locked state.");
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
106
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
107 static PyMethodDef lock_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
108 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
109 METH_VARARGS, acquire_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
110 {"acquire", (PyCFunction)lock_PyThread_acquire_lock,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
111 METH_VARARGS, acquire_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
112 {"release_lock", (PyCFunction)lock_PyThread_release_lock,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
113 METH_NOARGS, release_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
114 {"release", (PyCFunction)lock_PyThread_release_lock,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
115 METH_NOARGS, release_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
116 {"locked_lock", (PyCFunction)lock_locked_lock,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
117 METH_NOARGS, locked_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
118 {"locked", (PyCFunction)lock_locked_lock,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
119 METH_NOARGS, locked_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
120 {"__enter__", (PyCFunction)lock_PyThread_acquire_lock,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
121 METH_VARARGS, acquire_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
122 {"__exit__", (PyCFunction)lock_PyThread_release_lock,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
123 METH_VARARGS, release_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
124 {NULL} /* sentinel */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 };
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
127 static PyTypeObject Locktype = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
128 PyVarObject_HEAD_INIT(&PyType_Type, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
129 "thread.lock", /*tp_name*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
130 sizeof(lockobject), /*tp_size*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
131 0, /*tp_itemsize*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
132 /* methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
133 (destructor)lock_dealloc, /*tp_dealloc*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
134 0, /*tp_print*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
135 0, /*tp_getattr*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
136 0, /*tp_setattr*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
137 0, /*tp_compare*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
138 0, /*tp_repr*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
139 0, /* tp_as_number */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
140 0, /* tp_as_sequence */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
141 0, /* tp_as_mapping */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
142 0, /* tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
143 0, /* tp_call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
144 0, /* tp_str */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
145 0, /* tp_getattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
146 0, /* tp_setattro */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
147 0, /* tp_as_buffer */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
148 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
149 0, /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
150 0, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
151 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
152 0, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
153 offsetof(lockobject, in_weakreflist), /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
154 0, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
155 0, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
156 lock_methods, /* tp_methods */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 };
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
37616
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
159 static lockobject *
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
160 newlockobject(void)
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
161 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
162 lockobject *self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
163 self = PyObject_New(lockobject, &Locktype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
164 if (self == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
165 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
166 self->lock_lock = PyThread_allocate_lock();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
167 self->in_weakreflist = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
168 if (self->lock_lock == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
169 Py_DECREF(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
170 PyErr_SetString(ThreadError, "can't allocate lock");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
171 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
172 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
173 return self;
37616
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
174 }
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
175
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
176 /* Thread-local objects */
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
177
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
178 #include "structmember.h"
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
179
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
180 typedef struct {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
181 PyObject_HEAD
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
182 PyObject *key;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
183 PyObject *args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
184 PyObject *kw;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
185 PyObject *dict;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
186 } localobject;
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
187
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
188 static PyObject *
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
189 local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
190 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
191 localobject *self;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
192 PyObject *tdict;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
193
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
194 if (type->tp_init == PyBaseObject_Type.tp_init
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
195 && ((args && PyObject_IsTrue(args))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
196 || (kw && PyObject_IsTrue(kw)))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
197 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
198 "Initialization arguments are not supported");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
199 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
200 }
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
201
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
202 self = (localobject *)type->tp_alloc(type, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
203 if (self == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
204 return NULL;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
205
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
206 Py_XINCREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
207 self->args = args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
208 Py_XINCREF(kw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
209 self->kw = kw;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
210 self->dict = NULL; /* making sure */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
211 self->key = PyString_FromFormat("thread.local.%p", self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
212 if (self->key == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
213 goto err;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
214
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
215 self->dict = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
216 if (self->dict == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
217 goto err;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
218
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
219 tdict = PyThreadState_GetDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
220 if (tdict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
221 PyErr_SetString(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
222 "Couldn't get thread-state dictionary");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
223 goto err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
224 }
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
225
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
226 if (PyDict_SetItem(tdict, self->key, self->dict) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
227 goto err;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
228
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
229 return (PyObject *)self;
35086
c6b7fadf0ada Consistently use hard tabs for indentation.
Michael W. Hudson <mwh@python.net>
parents: 35085
diff changeset
230
c6b7fadf0ada Consistently use hard tabs for indentation.
Michael W. Hudson <mwh@python.net>
parents: 35085
diff changeset
231 err:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
232 Py_DECREF(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
233 return NULL;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
234 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
235
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
236 static int
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
237 local_traverse(localobject *self, visitproc visit, void *arg)
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
238 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
239 Py_VISIT(self->args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
240 Py_VISIT(self->kw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
241 Py_VISIT(self->dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
242 return 0;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
243 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
244
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
245 static int
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
246 local_clear(localobject *self)
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
247 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
248 Py_CLEAR(self->args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
249 Py_CLEAR(self->kw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
250 Py_CLEAR(self->dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
251 return 0;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
252 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
253
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
254 static void
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
255 local_dealloc(localobject *self)
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
256 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
257 PyThreadState *tstate;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
258 if (self->key
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
259 && (tstate = PyThreadState_Get())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
260 && tstate->interp) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
261 for(tstate = PyInterpreterState_ThreadHead(tstate->interp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
262 tstate;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
263 tstate = PyThreadState_Next(tstate))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
264 if (tstate->dict &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
265 PyDict_GetItem(tstate->dict, self->key))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
266 PyDict_DelItem(tstate->dict, self->key);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
267 }
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
268
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
269 Py_XDECREF(self->key);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
270 local_clear(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
271 Py_TYPE(self)->tp_free((PyObject*)self);
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
272 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
273
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
274 static PyObject *
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
275 _ldict(localobject *self)
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
276 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
277 PyObject *tdict, *ldict;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
278
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
279 tdict = PyThreadState_GetDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
280 if (tdict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
281 PyErr_SetString(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
282 "Couldn't get thread-state dictionary");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
283 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
284 }
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
285
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
286 ldict = PyDict_GetItem(tdict, self->key);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
287 if (ldict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
288 ldict = PyDict_New(); /* we own ldict */
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
289
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
290 if (ldict == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
291 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
292 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
293 int i = PyDict_SetItem(tdict, self->key, ldict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
294 Py_DECREF(ldict); /* now ldict is borrowed */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
295 if (i < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
296 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
297 }
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
298
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
299 Py_CLEAR(self->dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
300 Py_INCREF(ldict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
301 self->dict = ldict; /* still borrowed */
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
302
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
303 if (Py_TYPE(self)->tp_init != PyBaseObject_Type.tp_init &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
304 Py_TYPE(self)->tp_init((PyObject*)self,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
305 self->args, self->kw) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
306 /* we need to get rid of ldict from thread so
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
307 we create a new one the next time we do an attr
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
308 acces */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
309 PyDict_DelItem(tdict, self->key);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
310 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
311 }
47859
8ccb35739a96 #Issue3088 in-progress: Race condition with instances of classes derived from threading.local:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47527
diff changeset
312
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
313 }
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
314
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
315 /* The call to tp_init above may have caused another thread to run.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
316 Install our ldict again. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
317 if (self->dict != ldict) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
318 Py_CLEAR(self->dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
319 Py_INCREF(ldict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
320 self->dict = ldict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
321 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
322
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
323 return ldict;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
324 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
325
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
326 static int
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
327 local_setattro(localobject *self, PyObject *name, PyObject *v)
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
328 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
329 PyObject *ldict;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
330
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
331 ldict = _ldict(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
332 if (ldict == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
333 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
334
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
335 return PyObject_GenericSetAttr((PyObject *)self, name, v);
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
336 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
337
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
338 static PyObject *
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
339 local_getdict(localobject *self, void *closure)
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
340 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
341 if (self->dict == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
342 PyErr_SetString(PyExc_AttributeError, "__dict__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
343 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
344 }
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
345
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
346 Py_INCREF(self->dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
347 return self->dict;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
348 }
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
349
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
350 static PyGetSetDef local_getset[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
351 {"__dict__", (getter)local_getdict, (setter)NULL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
352 "Local-data dictionary", NULL},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
353 {NULL} /* Sentinel */
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
354 };
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
355
37616
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
356 static PyObject *local_getattro(localobject *, PyObject *);
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
357
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
358 static PyTypeObject localtype = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
359 PyVarObject_HEAD_INIT(NULL, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
360 /* tp_name */ "thread._local",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
361 /* tp_basicsize */ sizeof(localobject),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
362 /* tp_itemsize */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
363 /* tp_dealloc */ (destructor)local_dealloc,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
364 /* tp_print */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
365 /* tp_getattr */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
366 /* tp_setattr */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
367 /* tp_compare */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
368 /* tp_repr */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
369 /* tp_as_number */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
370 /* tp_as_sequence */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
371 /* tp_as_mapping */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
372 /* tp_hash */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
373 /* tp_call */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
374 /* tp_str */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
375 /* tp_getattro */ (getattrofunc)local_getattro,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
376 /* tp_setattro */ (setattrofunc)local_setattro,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
377 /* tp_as_buffer */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
378 /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
379 /* tp_doc */ "Thread-local data",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
380 /* tp_traverse */ (traverseproc)local_traverse,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
381 /* tp_clear */ (inquiry)local_clear,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
382 /* tp_richcompare */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
383 /* tp_weaklistoffset */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
384 /* tp_iter */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
385 /* tp_iternext */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
386 /* tp_methods */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
387 /* tp_members */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
388 /* tp_getset */ local_getset,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
389 /* tp_base */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
390 /* tp_dict */ 0, /* internal use */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
391 /* tp_descr_get */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
392 /* tp_descr_set */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
393 /* tp_dictoffset */ offsetof(localobject, dict),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
394 /* tp_init */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
395 /* tp_alloc */ 0,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
396 /* tp_new */ local_new,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
397 /* tp_free */ 0, /* Low-level free-mem routine */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
398 /* tp_is_gc */ 0, /* For PyObject_IS_GC */
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
399 };
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
400
37616
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
401 static PyObject *
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
402 local_getattro(localobject *self, PyObject *name)
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
403 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
404 PyObject *ldict, *value;
37616
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
405
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
406 ldict = _ldict(self);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
407 if (ldict == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
408 return NULL;
37616
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
409
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
410 if (Py_TYPE(self) != &localtype)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
411 /* use generic lookup for subtypes */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
412 return PyObject_GenericGetAttr((PyObject *)self, name);
37616
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
413
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
414 /* Optimization: just look in dict ourselves */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
415 value = PyDict_GetItem(ldict, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
416 if (value == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
417 /* Fall back on generic to get __class__ and __dict__ */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
418 return PyObject_GenericGetAttr((PyObject *)self, name);
37616
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
419
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
420 Py_INCREF(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
421 return value;
37616
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
422 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 /* Module functions */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 4944
diff changeset
426 struct bootstate {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
427 PyInterpreterState *interp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
428 PyObject *func;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
429 PyObject *args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
430 PyObject *keyw;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
431 PyThreadState *tstate;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 4944
diff changeset
432 };
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 4944
diff changeset
434 static void
13497
95454b73ea91 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
435 t_bootstrap(void *boot_raw)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 4944
diff changeset
436 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
437 struct bootstate *boot = (struct bootstate *) boot_raw;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
438 PyThreadState *tstate;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
439 PyObject *res;
961
a31e07847063 New thread.c from Sjoerd, supports _exit_prog(). Use this in goaway()
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
440
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
441 tstate = boot->tstate;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
442 tstate->thread_id = PyThread_get_thread_ident();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
443 _PyThreadState_Init(tstate);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
444 PyEval_AcquireThread(tstate);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
445 nb_threads++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
446 res = PyEval_CallObjectWithKeywords(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
447 boot->func, boot->args, boot->keyw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
448 if (res == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
449 if (PyErr_ExceptionMatches(PyExc_SystemExit))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
450 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
451 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
452 PyObject *file;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
453 PySys_WriteStderr(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
454 "Unhandled exception in thread started by ");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
455 file = PySys_GetObject("stderr");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
456 if (file)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
457 PyFile_WriteObject(boot->func, file, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
458 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
459 PyObject_Print(boot->func, stderr, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
460 PySys_WriteStderr("\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
461 PyErr_PrintEx(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
462 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
463 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
464 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
465 Py_DECREF(res);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
466 Py_DECREF(boot->func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
467 Py_DECREF(boot->args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
468 Py_XDECREF(boot->keyw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
469 PyMem_DEL(boot_raw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
470 nb_threads--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
471 PyThreadState_Clear(tstate);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
472 PyThreadState_DeleteCurrent();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
473 PyThread_exit_thread();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
475
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
476 static PyObject *
13497
95454b73ea91 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
477 thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
479 PyObject *func, *args, *keyw = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
480 struct bootstate *boot;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
481 long ident;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
483 if (!PyArg_UnpackTuple(fargs, "start_new_thread", 2, 3,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
484 &func, &args, &keyw))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
485 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
486 if (!PyCallable_Check(func)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
487 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
488 "first arg must be callable");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
489 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
490 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
491 if (!PyTuple_Check(args)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
492 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
493 "2nd arg must be a tuple");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
494 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
495 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
496 if (keyw != NULL && !PyDict_Check(keyw)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
497 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
498 "optional 3rd arg must be a dictionary");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
499 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
500 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
501 boot = PyMem_NEW(struct bootstate, 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
502 if (boot == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
503 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
504 boot->interp = PyThreadState_GET()->interp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
505 boot->func = func;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
506 boot->args = args;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
507 boot->keyw = keyw;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
508 boot->tstate = _PyThreadState_Prealloc(boot->interp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
509 if (boot->tstate == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
510 PyMem_DEL(boot);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
511 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
512 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
513 Py_INCREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
514 Py_INCREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
515 Py_XINCREF(keyw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
516 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
517 ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
518 if (ident == -1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
519 PyErr_SetString(ThreadError, "can't start new thread");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
520 Py_DECREF(func);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
521 Py_DECREF(args);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
522 Py_XDECREF(keyw);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
523 PyThreadState_Clear(boot->tstate);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
524 PyMem_DEL(boot);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
525 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
526 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
527 return PyInt_FromLong(ident);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
529
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
530 PyDoc_STRVAR(start_new_doc,
20266
241ef8a923c7 Fix typo in docstring
Andrew M. Kuchling <amk@amk.ca>
parents: 16140
diff changeset
531 "start_new_thread(function, args[, kwargs])\n\
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
532 (start_new() is an obsolete synonym)\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
533 \n\
20468
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 20266
diff changeset
534 Start a new thread and return its identifier. The thread will call the\n\
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 20266
diff changeset
535 function with positional arguments from the tuple args and keyword arguments\n\
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 20266
diff changeset
536 taken from the optional dictionary kwargs. The thread exits when the\n\
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 20266
diff changeset
537 function returns; the return value is ignored. The thread will also exit\n\
ebfd87e87884 Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents: 20266
diff changeset
538 when the function raises an unhandled exception; a stack trace will be\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
539 printed unless the exception is SystemExit.\n");
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
540
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
541 static PyObject *
22561
043ad7adc896 Remove many uses of PyArg_NoArgs macro, change METH_OLDARGS to METH_NOARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21313
diff changeset
542 thread_PyThread_exit_thread(PyObject *self)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
544 PyErr_SetNone(PyExc_SystemExit);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
545 return NULL;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
547
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
548 PyDoc_STRVAR(exit_doc,
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
549 "exit()\n\
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
550 (PyThread_exit_thread() is an obsolete synonym)\n\
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
551 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
552 This is synonymous to ``raise SystemExit''. It will cause the current\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
553 thread to exit silently unless the exception is caught.");
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
554
29444
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
555 static PyObject *
29498
1402f265e48f Correct function name.
Kurt B. Kaiser <kbk@shore.net>
parents: 29444
diff changeset
556 thread_PyThread_interrupt_main(PyObject * self)
29444
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
557 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
558 PyErr_SetInterrupt();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
559 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
560 return Py_None;
29444
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
561 }
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
562
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
563 PyDoc_STRVAR(interrupt_doc,
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
564 "interrupt_main()\n\
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
565 \n\
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
566 Raise a KeyboardInterrupt in the main thread.\n\
29498
1402f265e48f Correct function name.
Kurt B. Kaiser <kbk@shore.net>
parents: 29444
diff changeset
567 A subthread can use this function to interrupt the main thread."
29444
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
568 );
8d160095eb66 Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents: 28889
diff changeset
569
37616
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
570 static lockobject *newlockobject(void);
1e9d7e66c1bc remove forward declarations, move constructor functions. makes code C++ safe.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37305
diff changeset
571
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
572 static PyObject *
22561
043ad7adc896 Remove many uses of PyArg_NoArgs macro, change METH_OLDARGS to METH_NOARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21313
diff changeset
573 thread_PyThread_allocate_lock(PyObject *self)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
575 return (PyObject *) newlockobject();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
577
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
578 PyDoc_STRVAR(allocate_doc,
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
579 "allocate_lock() -> lock object\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
580 (allocate() is an obsolete synonym)\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
581 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
582 Create a new lock object. See LockType.__doc__ for information about locks.");
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
583
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
584 static PyObject *
22561
043ad7adc896 Remove many uses of PyArg_NoArgs macro, change METH_OLDARGS to METH_NOARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21313
diff changeset
585 thread_get_ident(PyObject *self)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
586 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
587 long ident;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
588 ident = PyThread_get_thread_ident();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
589 if (ident == -1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
590 PyErr_SetString(ThreadError, "no current thread ident");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
591 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
592 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
593 return PyInt_FromLong(ident);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
594 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1418
diff changeset
595
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
596 PyDoc_STRVAR(get_ident_doc,
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
597 "get_ident() -> integer\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
598 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
599 Return a non-zero integer that uniquely identifies the current thread\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
600 amongst other threads that exist simultaneously.\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
601 This may be used to identify per-thread resources.\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
602 Even though on some platforms threads identities may appear to be\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
603 allocated consecutive numbers starting at 1, this behavior should not\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
604 be relied upon, and the number should be seen purely as a magic cookie.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
605 A thread's identity may be reused for another thread after it exits.");
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
606
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
607 static PyObject *
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
608 thread__count(PyObject *self)
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
609 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
610 return PyInt_FromLong(nb_threads);
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
611 }
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
612
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
613 PyDoc_STRVAR(_count_doc,
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
614 "_count() -> integer\n\
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
615 \n\
56991
d56b103490a5 Remove official documentation entry for thread._count() and make the
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
616 \
d56b103490a5 Remove official documentation entry for thread._count() and make the
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
617 Return the number of currently running Python threads, excluding \n\
d56b103490a5 Remove official documentation entry for thread._count() and make the
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
618 the main thread. The returned number comprises all threads created\n\
d56b103490a5 Remove official documentation entry for thread._count() and make the
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
619 through `start_new_thread()` as well as `threading.Thread`, and not\n\
d56b103490a5 Remove official documentation entry for thread._count() and make the
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
620 yet finished.\n\
d56b103490a5 Remove official documentation entry for thread._count() and make the
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
621 \n\
d56b103490a5 Remove official documentation entry for thread._count() and make the
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
622 This function is meant for internal and specialized purposes only.\n\
d56b103490a5 Remove official documentation entry for thread._count() and make the
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
623 In most applications `threading.enumerate()` should be used instead.");
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
624
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
625 static PyObject *
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
626 thread_stack_size(PyObject *self, PyObject *args)
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
627 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
628 size_t old_size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
629 Py_ssize_t new_size = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
630 int rc;
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
631
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
632 if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
633 return NULL;
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
634
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
635 if (new_size < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
636 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
637 "size must be 0 or a positive value");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
638 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
639 }
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
640
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
641 old_size = PyThread_get_stacksize();
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
642
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
643 rc = PyThread_set_stacksize((size_t) new_size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
644 if (rc == -1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
645 PyErr_Format(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
646 "size not valid: %zd bytes",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
647 new_size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
648 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
649 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
650 if (rc == -2) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
651 PyErr_SetString(ThreadError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
652 "setting stack size not supported");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
653 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
654 }
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
655
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
656 return PyInt_FromSsize_t((Py_ssize_t) old_size);
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
657 }
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
658
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
659 PyDoc_STRVAR(stack_size_doc,
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
660 "stack_size([size]) -> size\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
661 \n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
662 Return the thread stack size used when creating new threads. The\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
663 optional size argument specifies the stack size (in bytes) to be used\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
664 for subsequently created threads, and must be 0 (use platform or\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
665 configured default) or a positive integer value of at least 32,768 (32k).\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
666 If changing the thread stack size is unsupported, a ThreadError\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
667 exception is raised. If the specified size is invalid, a ValueError\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
668 exception is raised, and the stack size is unmodified. 32k bytes\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
669 currently the minimum supported stack size value to guarantee\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
670 sufficient stack space for the interpreter itself.\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
671 \n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
672 Note that some platforms may have particular restrictions on values for\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
673 the stack size, such as requiring a minimum stack size larger than 32kB or\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
674 requiring allocation in multiples of the system memory page size\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
675 - platform documentation should be referred to for more information\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
676 (4kB pages are common; using multiples of 4096 for the stack size is\n\
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
677 the suggested approach in the absence of more specific information).");
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
678
4751
7ab835f87f86 Renamed.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
679 static PyMethodDef thread_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
680 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
681 METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
682 start_new_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
683 {"start_new", (PyCFunction)thread_PyThread_start_new_thread,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
684 METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
685 start_new_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
686 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
687 METH_NOARGS, allocate_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
688 {"allocate", (PyCFunction)thread_PyThread_allocate_lock,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
689 METH_NOARGS, allocate_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
690 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
691 METH_NOARGS, exit_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
692 {"exit", (PyCFunction)thread_PyThread_exit_thread,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
693 METH_NOARGS, exit_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
694 {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
695 METH_NOARGS, interrupt_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
696 {"get_ident", (PyCFunction)thread_get_ident,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
697 METH_NOARGS, get_ident_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
698 {"_count", (PyCFunction)thread__count,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
699 METH_NOARGS, _count_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
700 {"stack_size", (PyCFunction)thread_stack_size,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
701 METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
702 stack_size_doc},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
703 {NULL, NULL} /* sentinel */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 };
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
705
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
706
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 /* Initialization function */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
709 PyDoc_STRVAR(thread_doc,
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
710 "This module provides primitive operations to write multi-threaded programs.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
711 The 'threading' module provides a more convenient interface.");
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
712
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
713 PyDoc_STRVAR(lock_doc,
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
714 "A lock object is a synchronization primitive. To create a lock,\n\
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
715 call the PyThread_allocate_lock() function. Methods are:\n\
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
716 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
717 acquire() -- lock the lock, possibly blocking until it can be obtained\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
718 release() -- unlock of the lock\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
719 locked() -- test whether the lock is currently locked\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
720 \n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
721 A lock is not owned by the thread that locked it; another thread may\n\
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
722 unlock it. A thread attempting to lock a lock that it has already locked\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22865
diff changeset
723 will block until another thread unlocks it. Deadlocks may ensue.");
8562
ff9adb57b8b5 Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents: 8433
diff changeset
724
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 24280
diff changeset
725 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13497
diff changeset
726 initthread(void)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
728 PyObject *m, *d;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
729
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
730 /* Initialize types: */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
731 if (PyType_Ready(&localtype) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
732 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
733
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
734 /* Create the module and add the functions */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
735 m = Py_InitModule3("thread", thread_methods, thread_doc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
736 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
737 return;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
738
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
739 /* Add a symbolic constant */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
740 d = PyModule_GetDict(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
741 ThreadError = PyErr_NewException("thread.error", NULL, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
742 PyDict_SetItemString(d, "error", ThreadError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
743 Locktype.tp_doc = lock_doc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
744 if (PyType_Ready(&Locktype) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
745 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
746 Py_INCREF(&Locktype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
747 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
748
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
749 Py_INCREF(&localtype);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
750 if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
751 return;
32673
97768d87394a Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents: 31797
diff changeset
752
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
753 nb_threads = 0;
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56274
diff changeset
754
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
755 /* Initialize the C thread library */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59315
diff changeset
756 PyThread_init_thread();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 }