Mercurial > cpython-lru_cache-weakref
annotate Modules/threadmodule.c @ 68239:ceec209b26d4 2.4
Close 2.4 branch.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sat, 05 Mar 2011 14:56:41 +0100 |
| parents | 1696d85ba043 |
| children |
| 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 | 5 #include "Python.h" |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
7 #ifndef WITH_THREAD |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
8 #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
|
9 #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
|
10 #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
|
11 #endif |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
12 |
|
9203
07dae6fdae79
Renamed thread.h to pythread.h.
Guido van Rossum <guido@python.org>
parents:
8823
diff
changeset
|
13 #include "pythread.h" |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
| 4751 | 15 static PyObject *ThreadError; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 /* Lock objects */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 typedef struct { |
| 4751 | 21 PyObject_HEAD |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
22 PyThread_type_lock lock_lock; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 } lockobject; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
23832
diff
changeset
|
25 static PyTypeObject Locktype; |
|
923
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 lockobject * |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13497
diff
changeset
|
28 newlockobject(void) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 { |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 lockobject *self; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
31 self = PyObject_New(lockobject, &Locktype); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 if (self == NULL) |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 return NULL; |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
34 self->lock_lock = PyThread_allocate_lock(); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 if (self->lock_lock == NULL) { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
36 PyObject_Del(self); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 self = NULL; |
| 4751 | 38 PyErr_SetString(ThreadError, "can't allocate lock"); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 return self; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 static void |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
44 lock_dealloc(lockobject *self) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 { |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 /* Unlock the lock so it's safe to free it */ |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
47 PyThread_acquire_lock(self->lock_lock, 0); |
|
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
48 PyThread_release_lock(self->lock_lock); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
50 PyThread_free_lock(self->lock_lock); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
51 PyObject_Del(self); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 |
| 4751 | 54 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
55 lock_PyThread_acquire_lock(lockobject *self, PyObject *args) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 { |
| 22709 | 57 int i = 1; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
| 22709 | 59 if (!PyArg_ParseTuple(args, "|i:acquire", &i)) |
| 60 return NULL; | |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 |
| 4751 | 62 Py_BEGIN_ALLOW_THREADS |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
63 i = PyThread_acquire_lock(self->lock_lock, i); |
| 4751 | 64 Py_END_ALLOW_THREADS |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 if (args == NULL) { |
| 4751 | 67 Py_INCREF(Py_None); |
| 68 return Py_None; | |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 else |
|
22865
7014921820a5
Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents:
22709
diff
changeset
|
71 return PyBool_FromLong((long)i); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
74 PyDoc_STRVAR(acquire_doc, |
|
22865
7014921820a5
Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents:
22709
diff
changeset
|
75 "acquire([wait]) -> None or bool\n\ |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
76 (PyThread_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
|
77 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 |
| 4751 | 85 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
|
86 lock_PyThread_release_lock(lockobject *self) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 { |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 /* Sanity check: the lock must be locked */ |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
89 if (PyThread_acquire_lock(self->lock_lock, 0)) { |
|
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
90 PyThread_release_lock(self->lock_lock); |
| 4751 | 91 PyErr_SetString(ThreadError, "release unlocked lock"); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 return NULL; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
95 PyThread_release_lock(self->lock_lock); |
| 4751 | 96 Py_INCREF(Py_None); |
| 97 return Py_None; | |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
100 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
|
101 "release()\n\ |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
102 (PyThread_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
|
103 \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 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
|
105 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
|
106 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
|
107 |
| 4751 | 108 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
|
109 lock_locked_lock(lockobject *self) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 { |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
111 if (PyThread_acquire_lock(self->lock_lock, 0)) { |
|
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
112 PyThread_release_lock(self->lock_lock); |
|
22865
7014921820a5
Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents:
22709
diff
changeset
|
113 return PyBool_FromLong(0L); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 } |
|
22865
7014921820a5
Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents:
22709
diff
changeset
|
115 return PyBool_FromLong(1L); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
118 PyDoc_STRVAR(locked_doc, |
|
22865
7014921820a5
Lock methods acquire() and locked() now return bools.
Guido van Rossum <guido@python.org>
parents:
22709
diff
changeset
|
119 "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
|
120 (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
|
121 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
122 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
|
123 |
| 4751 | 124 static PyMethodDef lock_methods[] = { |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
125 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, |
| 22709 | 126 METH_VARARGS, acquire_doc}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
127 {"acquire", (PyCFunction)lock_PyThread_acquire_lock, |
| 22709 | 128 METH_VARARGS, acquire_doc}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
129 {"release_lock", (PyCFunction)lock_PyThread_release_lock, |
|
22561
043ad7adc896
Remove many uses of PyArg_NoArgs macro, change METH_OLDARGS to METH_NOARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
21313
diff
changeset
|
130 METH_NOARGS, release_doc}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
131 {"release", (PyCFunction)lock_PyThread_release_lock, |
|
22562
303b6ad13a32
Missed change METH_OLDARGS to METH_NOARGS for two aliased functions
Neal Norwitz <nnorwitz@gmail.com>
parents:
22561
diff
changeset
|
132 METH_NOARGS, release_doc}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
133 {"locked_lock", (PyCFunction)lock_locked_lock, |
|
22561
043ad7adc896
Remove many uses of PyArg_NoArgs macro, change METH_OLDARGS to METH_NOARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
21313
diff
changeset
|
134 METH_NOARGS, locked_doc}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
135 {"locked", (PyCFunction)lock_locked_lock, |
|
22562
303b6ad13a32
Missed change METH_OLDARGS to METH_NOARGS for two aliased functions
Neal Norwitz <nnorwitz@gmail.com>
parents:
22561
diff
changeset
|
136 METH_NOARGS, locked_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
|
137 {NULL, NULL} /* sentinel */ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 }; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
| 4751 | 140 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
141 lock_getattr(lockobject *self, char *name) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 { |
| 4751 | 143 return Py_FindMethod(lock_methods, (PyObject *)self, name); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 |
| 4751 | 146 static PyTypeObject Locktype = { |
| 147 PyObject_HEAD_INIT(&PyType_Type) | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
148 0, /*ob_size*/ |
|
21313
91dd223829c6
Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents:
20468
diff
changeset
|
149 "thread.lock", /*tp_name*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
150 sizeof(lockobject), /*tp_size*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
151 0, /*tp_itemsize*/ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 /* methods */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
153 (destructor)lock_dealloc, /*tp_dealloc*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
154 0, /*tp_print*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
155 (getattrfunc)lock_getattr, /*tp_getattr*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
156 0, /*tp_setattr*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
157 0, /*tp_compare*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
158 0, /*tp_repr*/ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 }; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
161 /* Thread-local objects */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
162 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
163 #include "structmember.h" |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
164 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
165 typedef struct { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
166 PyObject_HEAD |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
167 PyObject *key; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
168 PyObject *args; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
169 PyObject *kw; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
170 PyObject *dict; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
171 } localobject; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
172 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
173 static PyTypeObject localtype; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
174 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
175 static PyObject * |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
176 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
|
177 { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
178 localobject *self; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
179 PyObject *tdict; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
180 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
181 if (type->tp_init == PyBaseObject_Type.tp_init |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
182 && ((args && PyObject_IsTrue(args)) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
183 || |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
184 (kw && PyObject_IsTrue(kw)) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
185 ) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
186 ) { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
187 PyErr_SetString(PyExc_TypeError, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
188 "Initialization arguments are not supported"); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
189 return NULL; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
190 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
191 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
192 self = (localobject *)type->tp_alloc(type, 0); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
193 if (self == NULL) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
194 return NULL; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
195 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
196 Py_XINCREF(args); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
197 self->args = args; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
198 Py_XINCREF(kw); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
199 self->kw = kw; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
200 self->dict = NULL; /* making sure */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
201 self->key = PyString_FromFormat("thread.local.%p", self); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
202 if (self->key == NULL) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
203 goto err; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
204 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
205 self->dict = PyDict_New(); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
206 if (self->dict == NULL) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
207 goto err; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
208 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
209 tdict = PyThreadState_GetDict(); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
210 if (tdict == NULL) { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
211 PyErr_SetString(PyExc_SystemError, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
212 "Couldn't get thread-state dictionary"); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
213 goto err; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
214 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
215 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
216 if (PyDict_SetItem(tdict, self->key, self->dict) < 0) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
217 goto err; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
218 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
219 return (PyObject *)self; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
220 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
221 err: |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
222 Py_DECREF(self); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
223 return NULL; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
224 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
225 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
226 static int |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
227 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
|
228 { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
229 Py_VISIT(self->args); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
230 Py_VISIT(self->kw); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
231 Py_VISIT(self->dict); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
232 return 0; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
233 } |
|
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 static int |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
236 local_clear(localobject *self) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
237 { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
238 Py_CLEAR(self->key); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
239 Py_CLEAR(self->args); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
240 Py_CLEAR(self->kw); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
241 Py_CLEAR(self->dict); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
242 return 0; |
|
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 void |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
246 local_dealloc(localobject *self) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
247 { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
248 PyThreadState *tstate; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
249 if (self->key |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
250 && (tstate = PyThreadState_Get()) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
251 && tstate->interp) { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
252 for(tstate = PyInterpreterState_ThreadHead(tstate->interp); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
253 tstate; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
254 tstate = PyThreadState_Next(tstate) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
255 ) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
256 if (tstate->dict && |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
257 PyDict_GetItem(tstate->dict, self->key)) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
258 PyDict_DelItem(tstate->dict, self->key); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
259 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
260 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
261 local_clear(self); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
262 self->ob_type->tp_free((PyObject*)self); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
263 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
264 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
265 static PyObject * |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
266 _ldict(localobject *self) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
267 { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
268 PyObject *tdict, *ldict; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
269 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
270 tdict = PyThreadState_GetDict(); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
271 if (tdict == NULL) { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
272 PyErr_SetString(PyExc_SystemError, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
273 "Couldn't get thread-state dictionary"); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
274 return NULL; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
275 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
276 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
277 ldict = PyDict_GetItem(tdict, self->key); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
278 if (ldict == NULL) { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
279 ldict = PyDict_New(); /* we own ldict */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
280 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
281 if (ldict == NULL) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
282 return NULL; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
283 else { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
284 int i = PyDict_SetItem(tdict, self->key, ldict); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
285 Py_DECREF(ldict); /* now ldict is borowed */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
286 if (i < 0) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
287 return NULL; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
288 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
289 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
290 Py_CLEAR(self->dict); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
291 Py_INCREF(ldict); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
292 self->dict = ldict; /* still borrowed */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
293 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
294 if (self->ob_type->tp_init != PyBaseObject_Type.tp_init && |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
295 self->ob_type->tp_init((PyObject*)self, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
296 self->args, self->kw) < 0 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
297 ) { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
298 /* we need to get rid of ldict from thread so |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
299 we create a new one the next time we do an attr |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
300 acces */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
301 PyDict_DelItem(tdict, self->key); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
302 return NULL; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
303 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
304 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
305 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
306 else if (self->dict != ldict) { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
307 Py_CLEAR(self->dict); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
308 Py_INCREF(ldict); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
309 self->dict = ldict; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
310 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
311 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
312 return ldict; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
313 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
314 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
315 static PyObject * |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
316 local_getattro(localobject *self, PyObject *name) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
317 { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
318 PyObject *ldict, *value; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
319 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
320 ldict = _ldict(self); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
321 if (ldict == NULL) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
322 return NULL; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
323 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
324 if (self->ob_type != &localtype) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
325 /* use generic lookup for subtypes */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
326 return PyObject_GenericGetAttr((PyObject *)self, name); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
327 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
328 /* Optimization: just look in dict ourselves */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
329 value = PyDict_GetItem(ldict, name); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
330 if (value == NULL) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
331 /* Fall back on generic to get __class__ and __dict__ */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
332 return PyObject_GenericGetAttr((PyObject *)self, name); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
333 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
334 Py_INCREF(value); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
335 return value; |
|
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 int |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
339 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
|
340 { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
341 PyObject *ldict; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
342 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
343 ldict = _ldict(self); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
344 if (ldict == NULL) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
345 return -1; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
346 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
347 return PyObject_GenericSetAttr((PyObject *)self, name, v); |
|
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 PyObject * |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
351 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
|
352 { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
353 if (self->dict == NULL) { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
354 PyErr_SetString(PyExc_AttributeError, "__dict__"); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
355 return NULL; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
356 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
357 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
358 Py_INCREF(self->dict); |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
359 return self->dict; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
360 } |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
361 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
362 static PyGetSetDef local_getset[] = { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
363 {"__dict__", |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
364 (getter)local_getdict, (setter)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
365 "Local-data dictionary", |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
366 NULL}, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
367 {NULL} /* Sentinel */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
368 }; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
369 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
370 static PyTypeObject localtype = { |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
371 PyObject_HEAD_INIT(NULL) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
372 /* ob_size */ 0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
373 /* tp_name */ "thread._local", |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
374 /* tp_basicsize */ sizeof(localobject), |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
375 /* tp_itemsize */ 0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
376 /* tp_dealloc */ (destructor)local_dealloc, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
377 /* tp_print */ (printfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
378 /* tp_getattr */ (getattrfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
379 /* tp_setattr */ (setattrfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
380 /* tp_compare */ (cmpfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
381 /* tp_repr */ (reprfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
382 /* tp_as_number */ 0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
383 /* tp_as_sequence */ 0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
384 /* tp_as_mapping */ 0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
385 /* tp_hash */ (hashfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
386 /* tp_call */ (ternaryfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
387 /* tp_str */ (reprfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
388 /* tp_getattro */ (getattrofunc)local_getattro, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
389 /* tp_setattro */ (setattrofunc)local_setattro, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
390 /* tp_as_buffer */ 0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
391 /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
392 /* tp_doc */ "Thread-local data", |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
393 /* tp_traverse */ (traverseproc)local_traverse, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
394 /* tp_clear */ (inquiry)local_clear, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
395 /* tp_richcompare */ (richcmpfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
396 /* tp_weaklistoffset */ (long)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
397 /* tp_iter */ (getiterfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
398 /* tp_iternext */ (iternextfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
399 /* tp_methods */ 0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
400 /* tp_members */ 0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
401 /* tp_getset */ local_getset, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
402 /* tp_base */ 0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
403 /* tp_dict */ 0, /* internal use */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
404 /* tp_descr_get */ (descrgetfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
405 /* tp_descr_set */ (descrsetfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
406 /* tp_dictoffset */ offsetof(localobject, dict), |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
407 /* tp_init */ (initproc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
408 /* tp_alloc */ (allocfunc)0, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
409 /* tp_new */ (newfunc)local_new, |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
410 /* tp_free */ 0, /* Low-level free-mem routine */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
411 /* tp_is_gc */ (inquiry)0, /* For PyObject_IS_GC */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
412 }; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
413 |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 /* Module functions */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
417 struct bootstate { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
418 PyInterpreterState *interp; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
419 PyObject *func; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
420 PyObject *args; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
421 PyObject *keyw; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
422 }; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
424 static void |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
425 t_bootstrap(void *boot_raw) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
426 { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
427 struct bootstate *boot = (struct bootstate *) boot_raw; |
| 35631 | 428 PyThreadState *tstate; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
429 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
|
430 |
| 35631 | 431 tstate = PyThreadState_New(boot->interp); |
| 432 | |
| 433 PyEval_AcquireThread(tstate); | |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
434 res = PyEval_CallObjectWithKeywords( |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
435 boot->func, boot->args, boot->keyw); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 if (res == NULL) { |
|
8433
7cdff8aa8318
t_bootstrap(): Use PyErr_ExceptionMatches(...) instead of PyErr_Occurred(...).
Fred Drake <fdrake@acm.org>
parents:
7211
diff
changeset
|
437 if (PyErr_ExceptionMatches(PyExc_SystemExit)) |
| 4751 | 438 PyErr_Clear(); |
|
2568
2c61786327cf
implement exit_thread through SystemExit exception
Guido van Rossum <guido@python.org>
parents:
2123
diff
changeset
|
439 else { |
|
28889
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
440 PyObject *file; |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
441 PySys_WriteStderr( |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
442 "Unhandled exception in thread started by "); |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
443 file = PySys_GetObject("stderr"); |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
444 if (file) |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
445 PyFile_WriteObject(boot->func, file, 0); |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
446 else |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
447 PyObject_Print(boot->func, stderr, 0); |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
448 PySys_WriteStderr("\n"); |
|
7211
c764217ae6eb
Don't store the exception info from an unhandled exception in a thread
Guido van Rossum <guido@python.org>
parents:
6367
diff
changeset
|
449 PyErr_PrintEx(0); |
|
2568
2c61786327cf
implement exit_thread through SystemExit exception
Guido van Rossum <guido@python.org>
parents:
2123
diff
changeset
|
450 } |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
451 } |
|
2055
cf31eab6ab5a
Another bulky set of minor changes.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
452 else |
| 4751 | 453 Py_DECREF(res); |
|
28889
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
454 Py_DECREF(boot->func); |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
455 Py_DECREF(boot->args); |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
456 Py_XDECREF(boot->keyw); |
|
96b6cc5d408b
When an unhandled exception happens, report the repr() of the function
Guido van Rossum <guido@python.org>
parents:
25127
diff
changeset
|
457 PyMem_DEL(boot_raw); |
| 35631 | 458 PyThreadState_Clear(tstate); |
| 459 PyThreadState_DeleteCurrent(); | |
|
8823
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8567
diff
changeset
|
460 PyThread_exit_thread(); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
461 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
462 |
| 4751 | 463 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
464 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
|
465 { |
|
14363
5da406ebef55
The second argument to start_new_thread() is not a tuple, as pointed
Guido van Rossum <guido@python.org>
parents:
13820
diff
changeset
|
466 PyObject *func, *args, *keyw = NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
467 struct bootstate *boot; |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
20266
diff
changeset
|
468 long ident; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
469 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
9679
diff
changeset
|
470 if (!PyArg_ParseTuple(fargs, "OO|O:start_new_thread", &func, &args, &keyw)) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
471 return NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
472 if (!PyCallable_Check(func)) { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
473 PyErr_SetString(PyExc_TypeError, |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
474 "first arg must be callable"); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
475 return NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
476 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
477 if (!PyTuple_Check(args)) { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
478 PyErr_SetString(PyExc_TypeError, |
|
14363
5da406ebef55
The second argument to start_new_thread() is not a tuple, as pointed
Guido van Rossum <guido@python.org>
parents:
13820
diff
changeset
|
479 "2nd arg must be a tuple"); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
480 return NULL; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
481 } |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
482 if (keyw != NULL && !PyDict_Check(keyw)) { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
483 PyErr_SetString(PyExc_TypeError, |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
484 "optional 3rd arg must be a dictionary"); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
485 return NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
486 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
487 boot = PyMem_NEW(struct bootstate, 1); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
488 if (boot == NULL) |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
489 return PyErr_NoMemory(); |
|
31797
846b3ee7e106
Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents:
29498
diff
changeset
|
490 boot->interp = PyThreadState_GET()->interp; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
491 boot->func = func; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
492 boot->args = args; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
493 boot->keyw = keyw; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
494 Py_INCREF(func); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
495 Py_INCREF(args); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
496 Py_XINCREF(keyw); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
497 PyEval_InitThreads(); /* Start the interpreter's thread-awareness */ |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
20266
diff
changeset
|
498 ident = PyThread_start_new_thread(t_bootstrap, (void*) boot); |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
20266
diff
changeset
|
499 if (ident == -1) { |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
500 PyErr_SetString(ThreadError, "can't start new thread\n"); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
501 Py_DECREF(func); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
502 Py_DECREF(args); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
503 Py_XDECREF(keyw); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
504 PyMem_DEL(boot); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
505 return NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
506 } |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
20266
diff
changeset
|
507 return PyInt_FromLong(ident); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
508 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
509 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
510 PyDoc_STRVAR(start_new_doc, |
| 20266 | 511 "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
|
512 (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
|
513 \n\ |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
20266
diff
changeset
|
514 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
|
515 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
|
516 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
|
517 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
|
518 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
|
519 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
|
520 |
| 4751 | 521 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
|
522 thread_PyThread_exit_thread(PyObject *self) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
523 { |
| 4751 | 524 PyErr_SetNone(PyExc_SystemExit); |
|
2568
2c61786327cf
implement exit_thread through SystemExit exception
Guido van Rossum <guido@python.org>
parents:
2123
diff
changeset
|
525 return NULL; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
528 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
|
529 "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
|
530 (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
|
531 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
532 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
|
533 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
|
534 |
|
29444
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
535 static PyObject * |
| 29498 | 536 thread_PyThread_interrupt_main(PyObject * self) |
|
29444
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
537 { |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
538 PyErr_SetInterrupt(); |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
539 Py_INCREF(Py_None); |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
540 return Py_None; |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
541 } |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
542 |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
543 PyDoc_STRVAR(interrupt_doc, |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
544 "interrupt_main()\n\ |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
545 \n\ |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
546 Raise a KeyboardInterrupt in the main thread.\n\ |
| 29498 | 547 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
|
548 ); |
|
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
549 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
550 #ifndef NO_EXIT_PROG |
| 4751 | 551 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
552 thread_PyThread_exit_prog(PyObject *self, PyObject *args) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
553 { |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
554 int sts; |
| 22709 | 555 if (!PyArg_ParseTuple(args, "i:exit_prog", &sts)) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
556 return NULL; |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
557 Py_Exit(sts); /* Calls PyThread_exit_prog(sts) or _PyThread_exit_prog(sts) */ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
558 for (;;) { } /* Should not be reached */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
559 } |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
560 #endif |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
561 |
| 4751 | 562 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
|
563 thread_PyThread_allocate_lock(PyObject *self) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
564 { |
| 4751 | 565 return (PyObject *) newlockobject(); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
566 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
567 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
568 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
|
569 "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
|
570 (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
|
571 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
572 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
|
573 |
| 4751 | 574 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
|
575 thread_get_ident(PyObject *self) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
576 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
577 long ident; |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
578 ident = PyThread_get_thread_ident(); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
579 if (ident == -1) { |
| 4751 | 580 PyErr_SetString(ThreadError, "no current thread ident"); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
581 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
582 } |
| 4751 | 583 return PyInt_FromLong(ident); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
584 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
585 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
586 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
|
587 "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
|
588 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
589 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
|
590 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
|
591 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
|
592 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
|
593 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
|
594 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
|
595 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
|
596 |
| 4751 | 597 static PyMethodDef thread_methods[] = { |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
598 {"start_new_thread", (PyCFunction)thread_PyThread_start_new_thread, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
599 METH_VARARGS, |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
600 start_new_doc}, |
|
13817
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
601 {"start_new", (PyCFunction)thread_PyThread_start_new_thread, |
|
3c0f75a2198f
Use METH_VARARGS instead of numeric constant 1 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13651
diff
changeset
|
602 METH_VARARGS, |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
603 start_new_doc}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
604 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, |
|
22561
043ad7adc896
Remove many uses of PyArg_NoArgs macro, change METH_OLDARGS to METH_NOARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
21313
diff
changeset
|
605 METH_NOARGS, allocate_doc}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
606 {"allocate", (PyCFunction)thread_PyThread_allocate_lock, |
|
22584
bcd1fdf13acf
Missed change METH_OLDARGS to METH_NOARGS for two aliased functions
Neal Norwitz <nnorwitz@gmail.com>
parents:
22562
diff
changeset
|
607 METH_NOARGS, allocate_doc}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
608 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, |
|
22561
043ad7adc896
Remove many uses of PyArg_NoArgs macro, change METH_OLDARGS to METH_NOARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
21313
diff
changeset
|
609 METH_NOARGS, exit_doc}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
610 {"exit", (PyCFunction)thread_PyThread_exit_thread, |
|
22584
bcd1fdf13acf
Missed change METH_OLDARGS to METH_NOARGS for two aliased functions
Neal Norwitz <nnorwitz@gmail.com>
parents:
22562
diff
changeset
|
611 METH_NOARGS, exit_doc}, |
| 29498 | 612 {"interrupt_main", (PyCFunction)thread_PyThread_interrupt_main, |
|
29444
8d160095eb66
Add interrupt_main() to thread module.
Kurt B. Kaiser <kbk@shore.net>
parents:
28889
diff
changeset
|
613 METH_NOARGS, interrupt_doc}, |
|
13820
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
614 {"get_ident", (PyCFunction)thread_get_ident, |
|
22561
043ad7adc896
Remove many uses of PyArg_NoArgs macro, change METH_OLDARGS to METH_NOARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
21313
diff
changeset
|
615 METH_NOARGS, get_ident_doc}, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
616 #ifndef NO_EXIT_PROG |
| 22709 | 617 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog, |
| 618 METH_VARARGS}, | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
619 #endif |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
620 {NULL, NULL} /* sentinel */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
621 }; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
622 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
623 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
624 /* Initialization function */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
625 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
626 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
|
627 "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
|
628 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
|
629 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22865
diff
changeset
|
630 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
|
631 "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
|
632 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
|
633 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
634 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
|
635 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
|
636 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
|
637 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
638 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
|
639 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
|
640 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
|
641 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
24280
diff
changeset
|
642 PyMODINIT_FUNC |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13497
diff
changeset
|
643 initthread(void) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
644 { |
| 4751 | 645 PyObject *m, *d; |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
646 |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
647 /* Initialize types: */ |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
648 if (PyType_Ready(&localtype) < 0) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
649 return; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
650 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
651 /* Create the module and add the functions */ |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
652 m = Py_InitModule3("thread", thread_methods, thread_doc); |
|
39893
6fb46e87d5f9
[Backport of rev. 42093 by neal.norwitz]
Andrew M. Kuchling <amk@amk.ca>
parents:
35631
diff
changeset
|
653 if (m == NULL) |
|
6fb46e87d5f9
[Backport of rev. 42093 by neal.norwitz]
Andrew M. Kuchling <amk@amk.ca>
parents:
35631
diff
changeset
|
654 return; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
655 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
656 /* Add a symbolic constant */ |
| 4751 | 657 d = PyModule_GetDict(m); |
|
6367
2c29b8982643
Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents:
5927
diff
changeset
|
658 ThreadError = PyErr_NewException("thread.error", NULL, NULL); |
| 4751 | 659 PyDict_SetItemString(d, "error", ThreadError); |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
660 Locktype.tp_doc = lock_doc; |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
661 Py_INCREF(&Locktype); |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
662 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
663 |
|
39898
1696d85ba043
[Backport rev. 39012 by mwh]
Andrew M. Kuchling <amk@amk.ca>
parents:
39893
diff
changeset
|
664 Py_INCREF(&localtype); |
|
32673
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
665 if (PyModule_AddObject(m, "_local", (PyObject *)&localtype) < 0) |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
666 return; |
|
97768d87394a
Implemented thread-local data as proposed on python-dev:
Jim Fulton <jim@zope.com>
parents:
31797
diff
changeset
|
667 |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
668 /* Initialize the C thread library */ |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
669 PyThread_init_thread(); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
670 } |
