Mercurial > cpython-lru_cache-weakref
annotate Modules/threadmodule.c @ 20737:00a581bdedeb legacy-trunk
When overriding __str__ or __repr__, set the tp_print slot to NULL.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 29 Oct 2001 14:33:44 +0000 |
| parents | ebfd87e87884 |
| children | 91dd223829c6 |
| 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." |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
9 #error "Rerun configure, adding a --with-thread option." |
|
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 |
| 4751 | 25 staticforward 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 { |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 int i; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 if (args != NULL) { |
| 4751 | 60 if (!PyArg_Parse(args, "i", &i)) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 return NULL; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 else |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 i = 1; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 |
| 4751 | 66 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
|
67 i = PyThread_acquire_lock(self->lock_lock, i); |
| 4751 | 68 Py_END_ALLOW_THREADS |
|
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 if (args == NULL) { |
| 4751 | 71 Py_INCREF(Py_None); |
| 72 return Py_None; | |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 else |
| 4751 | 75 return PyInt_FromLong((long)i); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
78 static char acquire_doc[] = |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
79 "acquire([wait]) -> None or Boolean\n\ |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
80 (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
|
81 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
82 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
|
83 locked (even by the same thread), waiting for another thread to release\n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
84 the lock, and return None when the lock is acquired.\n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
85 With a Boolean argument, this will only block if the argument is true,\n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
86 and the return value reflects whether the lock is acquired.\n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
87 The blocking operation is not interruptible."; |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
88 |
| 4751 | 89 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
90 lock_PyThread_release_lock(lockobject *self, PyObject *args) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 { |
| 4751 | 92 if (!PyArg_NoArgs(args)) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 return NULL; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 /* 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
|
96 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
|
97 PyThread_release_lock(self->lock_lock); |
| 4751 | 98 PyErr_SetString(ThreadError, "release unlocked lock"); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 return NULL; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 |
|
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(self->lock_lock); |
| 4751 | 103 Py_INCREF(Py_None); |
| 104 return Py_None; | |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 |
|
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 static char release_doc[] = |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
108 "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
|
109 (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
|
110 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
111 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
|
112 the lock to acquire the lock. The lock must be in the locked state,\n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
113 but it needn't be locked by the same thread that unlocks it."; |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
114 |
| 4751 | 115 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
116 lock_locked_lock(lockobject *self, PyObject *args) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 { |
| 4751 | 118 if (!PyArg_NoArgs(args)) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 return NULL; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
121 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
|
122 PyThread_release_lock(self->lock_lock); |
| 4751 | 123 return PyInt_FromLong(0L); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 } |
| 4751 | 125 return PyInt_FromLong(1L); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
128 static char locked_doc[] = |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
129 "locked() -> Boolean\n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
130 (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
|
131 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
132 Return whether the lock is in the locked state."; |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
133 |
| 4751 | 134 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
|
135 {"acquire_lock", (PyCFunction)lock_PyThread_acquire_lock, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
136 METH_OLDARGS, acquire_doc}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
137 {"acquire", (PyCFunction)lock_PyThread_acquire_lock, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
138 METH_OLDARGS, acquire_doc}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
139 {"release_lock", (PyCFunction)lock_PyThread_release_lock, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
140 METH_OLDARGS, release_doc}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
141 {"release", (PyCFunction)lock_PyThread_release_lock, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
142 METH_OLDARGS, release_doc}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
143 {"locked_lock", (PyCFunction)lock_locked_lock, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
144 METH_OLDARGS, locked_doc}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
145 {"locked", (PyCFunction)lock_locked_lock, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
146 METH_OLDARGS, 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
|
147 {NULL, NULL} /* sentinel */ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 }; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 |
| 4751 | 150 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
151 lock_getattr(lockobject *self, char *name) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 { |
| 4751 | 153 return Py_FindMethod(lock_methods, (PyObject *)self, name); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 |
| 4751 | 156 static PyTypeObject Locktype = { |
| 157 PyObject_HEAD_INIT(&PyType_Type) | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
158 0, /*ob_size*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
159 "lock", /*tp_name*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
160 sizeof(lockobject), /*tp_size*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
161 0, /*tp_itemsize*/ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 /* methods */ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
163 (destructor)lock_dealloc, /*tp_dealloc*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
164 0, /*tp_print*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
165 (getattrfunc)lock_getattr, /*tp_getattr*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
166 0, /*tp_setattr*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
167 0, /*tp_compare*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
168 0, /*tp_repr*/ |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 }; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 /* Module functions */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
174 struct bootstate { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
175 PyInterpreterState *interp; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
176 PyObject *func; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
177 PyObject *args; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
178 PyObject *keyw; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
179 }; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
181 static void |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
182 t_bootstrap(void *boot_raw) |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
183 { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
184 struct bootstate *boot = (struct bootstate *) boot_raw; |
|
5847
ff9c0110079b
Use the new functions PyEval_AcquireThread() and
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
185 PyThreadState *tstate; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
186 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
|
187 |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
188 tstate = PyThreadState_New(boot->interp); |
|
5847
ff9c0110079b
Use the new functions PyEval_AcquireThread() and
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
189 PyEval_AcquireThread(tstate); |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
190 res = PyEval_CallObjectWithKeywords( |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
191 boot->func, boot->args, boot->keyw); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
192 Py_DECREF(boot->func); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
193 Py_DECREF(boot->args); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
194 Py_XDECREF(boot->keyw); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
195 PyMem_DEL(boot_raw); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 if (res == NULL) { |
|
8433
7cdff8aa8318
t_bootstrap(): Use PyErr_ExceptionMatches(...) instead of PyErr_Occurred(...).
Fred Drake <fdrake@acm.org>
parents:
7211
diff
changeset
|
197 if (PyErr_ExceptionMatches(PyExc_SystemExit)) |
| 4751 | 198 PyErr_Clear(); |
|
2568
2c61786327cf
implement exit_thread through SystemExit exception
Guido van Rossum <guido@python.org>
parents:
2123
diff
changeset
|
199 else { |
|
15288
1f4a9ade30ec
t_bootstram(): Use PySys_WriteStderr() instead of fprintf(stderr,...).
Fred Drake <fdrake@acm.org>
parents:
14375
diff
changeset
|
200 PySys_WriteStderr("Unhandled exception in thread:\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
|
201 PyErr_PrintEx(0); |
|
2568
2c61786327cf
implement exit_thread through SystemExit exception
Guido van Rossum <guido@python.org>
parents:
2123
diff
changeset
|
202 } |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 } |
|
2055
cf31eab6ab5a
Another bulky set of minor changes.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
204 else |
| 4751 | 205 Py_DECREF(res); |
|
5927
63bc9289ab28
Oops, one more checkin. Use the new tstate/interp interface.
Guido van Rossum <guido@python.org>
parents:
5847
diff
changeset
|
206 PyThreadState_Clear(tstate); |
|
16140
38dcec06617b
Use PyThreadState_DeleteCurrent() instead of PyThreadState_Delete()
Guido van Rossum <guido@python.org>
parents:
15288
diff
changeset
|
207 PyThreadState_DeleteCurrent(); |
|
8823
f2a4a2b60860
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8567
diff
changeset
|
208 PyThread_exit_thread(); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 |
| 4751 | 211 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
212 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
|
213 { |
|
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
|
214 PyObject *func, *args, *keyw = NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
215 struct bootstate *boot; |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
20266
diff
changeset
|
216 long ident; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
9679
diff
changeset
|
218 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
|
219 return NULL; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
220 if (!PyCallable_Check(func)) { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
221 PyErr_SetString(PyExc_TypeError, |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
222 "first arg must be callable"); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
223 return NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
224 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
225 if (!PyTuple_Check(args)) { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
226 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
|
227 "2nd arg must be a tuple"); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 return NULL; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 } |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
230 if (keyw != NULL && !PyDict_Check(keyw)) { |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
231 PyErr_SetString(PyExc_TypeError, |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
232 "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
|
233 return NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
234 } |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
235 boot = PyMem_NEW(struct bootstate, 1); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
236 if (boot == NULL) |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
237 return PyErr_NoMemory(); |
|
5927
63bc9289ab28
Oops, one more checkin. Use the new tstate/interp interface.
Guido van Rossum <guido@python.org>
parents:
5847
diff
changeset
|
238 boot->interp = PyThreadState_Get()->interp; |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
239 boot->func = func; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
240 boot->args = args; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
241 boot->keyw = keyw; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
242 Py_INCREF(func); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
243 Py_INCREF(args); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
244 Py_XINCREF(keyw); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
245 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
|
246 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
|
247 if (ident == -1) { |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
248 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
|
249 Py_DECREF(func); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
250 Py_DECREF(args); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
251 Py_XDECREF(keyw); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
252 PyMem_DEL(boot); |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
253 return NULL; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
4944
diff
changeset
|
254 } |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
20266
diff
changeset
|
255 return PyInt_FromLong(ident); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
258 static char start_new_doc[] = |
| 20266 | 259 "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
|
260 (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
|
261 \n\ |
|
20468
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
20266
diff
changeset
|
262 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
|
263 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
|
264 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
|
265 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
|
266 when the function raises an unhandled exception; a stack trace will be\n\ |
|
ebfd87e87884
Partial patch from SF #452266, by Jason Petrone.
Guido van Rossum <guido@python.org>
parents:
20266
diff
changeset
|
267 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
|
268 |
| 4751 | 269 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
270 thread_PyThread_exit_thread(PyObject *self, PyObject *args) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 { |
| 4751 | 272 if (!PyArg_NoArgs(args)) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 return NULL; |
| 4751 | 274 PyErr_SetNone(PyExc_SystemExit); |
|
2568
2c61786327cf
implement exit_thread through SystemExit exception
Guido van Rossum <guido@python.org>
parents:
2123
diff
changeset
|
275 return NULL; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
278 static char exit_doc[] = |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
279 "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
|
280 (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
|
281 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
282 This is synonymous to ``raise SystemExit''. It will cause the current\n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
283 thread to exit silently unless the exception is caught."; |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
284 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
285 #ifndef NO_EXIT_PROG |
| 4751 | 286 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
287 thread_PyThread_exit_prog(PyObject *self, PyObject *args) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 { |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 int sts; |
| 4751 | 290 if (!PyArg_Parse(args, "i", &sts)) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 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
|
292 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
|
293 for (;;) { } /* Should not be reached */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 } |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
295 #endif |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 |
| 4751 | 297 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
298 thread_PyThread_allocate_lock(PyObject *self, PyObject *args) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 { |
| 4751 | 300 if (!PyArg_NoArgs(args)) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 return NULL; |
| 4751 | 302 return (PyObject *) newlockobject(); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 } |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
305 static char allocate_doc[] = |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
306 "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
|
307 (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
|
308 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
309 Create a new lock object. See LockType.__doc__ for information about locks."; |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
310 |
| 4751 | 311 static PyObject * |
|
13497
95454b73ea91
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13256
diff
changeset
|
312 thread_get_ident(PyObject *self, PyObject *args) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
313 { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
314 long ident; |
| 4751 | 315 if (!PyArg_NoArgs(args)) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
316 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
|
317 ident = PyThread_get_thread_ident(); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
318 if (ident == -1) { |
| 4751 | 319 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
|
320 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
321 } |
| 4751 | 322 return PyInt_FromLong(ident); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
323 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
324 |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
325 static char get_ident_doc[] = |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
326 "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
|
327 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 be relied upon, and the number should be seen purely as a magic cookie.\n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
334 A thread's identity may be reused for another thread after it exits."; |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
335 |
| 4751 | 336 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
|
337 {"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
|
338 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
|
339 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
|
340 {"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
|
341 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
|
342 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
|
343 {"allocate_lock", (PyCFunction)thread_PyThread_allocate_lock, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
344 METH_OLDARGS, allocate_doc}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
345 {"allocate", (PyCFunction)thread_PyThread_allocate_lock, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
346 METH_OLDARGS, allocate_doc}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
347 {"exit_thread", (PyCFunction)thread_PyThread_exit_thread, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
348 METH_OLDARGS, exit_doc}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
349 {"exit", (PyCFunction)thread_PyThread_exit_thread, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
350 METH_OLDARGS, exit_doc}, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
351 {"get_ident", (PyCFunction)thread_get_ident, |
|
8bd82cf361aa
Use METH_OLDARGS instead of numeric constant 0 in method def. tables
Andrew M. Kuchling <amk@amk.ca>
parents:
13817
diff
changeset
|
352 METH_OLDARGS, get_ident_doc}, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
353 #ifndef NO_EXIT_PROG |
|
9679
877d23bd5bdc
Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
354 {"exit_prog", (PyCFunction)thread_PyThread_exit_prog}, |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1418
diff
changeset
|
355 #endif |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 {NULL, NULL} /* sentinel */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 }; |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 /* Initialization function */ |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 |
|
8562
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
362 static char thread_doc[] = |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
363 "This module provides primitive operations to write multi-threaded programs.\n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
364 The 'threading' module provides a more convenient interface."; |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
365 |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
366 static char 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
|
367 "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
|
368 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
|
369 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
370 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
|
371 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
|
372 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
|
373 \n\ |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
374 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
|
375 unlock it. A thread attempting to lock a lock that it has already 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
|
376 will block until another thread unlocks it. Deadlocks may ensue."; |
|
ff9adb57b8b5
Added doc strings. Also export LockType from the modoule, and give it
Guido van Rossum <guido@python.org>
parents:
8433
diff
changeset
|
377 |
|
9588
38023a425e48
Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents:
9288
diff
changeset
|
378 DL_EXPORT(void) |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13497
diff
changeset
|
379 initthread(void) |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 { |
| 4751 | 381 PyObject *m, *d; |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 /* 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
|
384 m = Py_InitModule3("thread", thread_methods, thread_doc); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 /* Add a symbolic constant */ |
| 4751 | 387 d = PyModule_GetDict(m); |
|
6367
2c29b8982643
Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents:
5927
diff
changeset
|
388 ThreadError = PyErr_NewException("thread.error", NULL, NULL); |
| 4751 | 389 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
|
390 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
|
391 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
|
392 PyDict_SetItemString(d, "LockType", (PyObject *)&Locktype); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 |
|
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 /* 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
|
395 PyThread_init_thread(); |
|
923
72aceed365d4
* Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
396 } |
