blob: c2d67fc14952e949cd97f5889a057091783cd712 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001/***********************************************************
2Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Thread module */
26/* Interface to Sjoerd's portable C thread library */
27
28#include "allobjects.h"
29#include "modsupport.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000030#include "ceval.h"
31
32#include "thread.h"
33
Guido van Rossumf9f2e821992-08-17 08:59:08 +000034int threads_started = 0;
35
Guido van Rossum1984f1e1992-08-04 12:41:02 +000036object *ThreadError;
37
38
39/* Lock objects */
40
41typedef struct {
42 OB_HEAD
43 type_lock lock_lock;
44} lockobject;
45
46extern typeobject Locktype; /* Really static, forward */
47
48#define is_lockobject(v) ((v)->ob_type == &Locktype)
49
50static lockobject *
51newlockobject()
52{
53 lockobject *self;
54 self = NEWOBJ(lockobject, &Locktype);
55 if (self == NULL)
56 return NULL;
57 self->lock_lock = allocate_lock();
58 if (self->lock_lock == NULL) {
59 DEL(self);
60 self = NULL;
61 err_setstr(ThreadError, "can't allocate lock");
62 }
63 return self;
64}
65
66static void
67lock_dealloc(self)
68 lockobject *self;
69{
70 /* Unlock the lock so it's safe to free it */
71 acquire_lock(self->lock_lock, 0);
72 release_lock(self->lock_lock);
73
74 free_lock(self->lock_lock);
75 DEL(self);
76}
77
78static object *
79lock_acquire_lock(self, args)
80 lockobject *self;
81 object *args;
82{
Guido van Rossum1984f1e1992-08-04 12:41:02 +000083 int i;
84
85 if (args != NULL) {
86 if (!getargs(args, "i", &i))
87 return NULL;
88 }
89 else
90 i = 1;
91
Guido van Rossumff4949e1992-08-05 19:58:53 +000092 BGN_SAVE
Guido van Rossum1984f1e1992-08-04 12:41:02 +000093 i = acquire_lock(self->lock_lock, i);
Guido van Rossumff4949e1992-08-05 19:58:53 +000094 END_SAVE
Guido van Rossum1984f1e1992-08-04 12:41:02 +000095
96 if (args == NULL) {
97 INCREF(None);
98 return None;
99 }
100 else
101 return newintobject((long)i);
102}
103
104static object *
105lock_release_lock(self, args)
106 lockobject *self;
107 object *args;
108{
109 if (!getnoarg(args))
110 return NULL;
111
112 /* Sanity check: the lock must be locked */
113 if (acquire_lock(self->lock_lock, 0)) {
114 release_lock(self->lock_lock);
115 err_setstr(ThreadError, "release unlocked lock");
116 return NULL;
117 }
118
119 release_lock(self->lock_lock);
120 INCREF(None);
121 return None;
122}
123
124static object *
125lock_locked_lock(self, args)
126 lockobject *self;
127 object *args;
128{
129 if (!getnoarg(args))
130 return NULL;
131
132 if (acquire_lock(self->lock_lock, 0)) {
133 release_lock(self->lock_lock);
134 return newintobject(0L);
135 }
136 return newintobject(1L);
137}
138
139static struct methodlist lock_methods[] = {
140 {"acquire_lock", lock_acquire_lock},
141 {"acquire", lock_acquire_lock},
142 {"release_lock", lock_release_lock},
143 {"release", lock_release_lock},
144 {"locked_lock", lock_locked_lock},
145 {"locked", lock_locked_lock},
146 {NULL, NULL} /* sentinel */
147};
148
149static object *
150lock_getattr(self, name)
151 lockobject *self;
152 char *name;
153{
154 return findmethod(lock_methods, (object *)self, name);
155}
156
157static typeobject Locktype = {
158 OB_HEAD_INIT(&Typetype)
159 0, /*ob_size*/
160 "lock", /*tp_name*/
161 sizeof(lockobject), /*tp_size*/
162 0, /*tp_itemsize*/
163 /* methods */
164 lock_dealloc, /*tp_dealloc*/
165 0, /*tp_print*/
166 lock_getattr, /*tp_getattr*/
167 0, /*tp_setattr*/
168 0, /*tp_compare*/
169 0, /*tp_repr*/
170};
171
172
173/* Module functions */
174
175static void
176t_bootstrap(args_raw)
177 void *args_raw;
178{
179 object *args = (object *) args_raw;
180 object *func, *arg, *res;
181
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000182 threads_started++;
183
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000184 restore_thread((void *)NULL);
185 func = gettupleitem(args, 0);
186 arg = gettupleitem(args, 1);
187 res = call_object(func, arg);
188 DECREF(arg); /* Matches the INCREF(arg) in thread_start_new_thread */
189 if (res == NULL) {
190 fprintf(stderr, "Unhandled exception in thread:\n");
191 print_error(); /* From pythonmain.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000192 }
193 (void) save_thread();
194 exit_thread();
195}
196
197static object *
198thread_start_new_thread(self, args)
199 object *self; /* Not used */
200 object *args;
201{
202 object *func, *arg;
203
204 if (!getargs(args, "(OO)", &func, &arg))
205 return NULL;
206 INCREF(args);
207 if (!start_new_thread(t_bootstrap, (void*) args)) {
208 DECREF(args);
209 err_setstr(ThreadError, "can't start new thread\n");
210 return NULL;
211 }
212 /* Otherwise the DECREF(args) is done by t_bootstrap */
213 INCREF(None);
214 return None;
215}
216
217static object *
218thread_exit_thread(self, args)
219 object *self; /* Not used */
220 object *args;
221{
222 if (!getnoarg(args))
223 return NULL;
224 (void) save_thread();
225 exit_thread();
226 for (;;) { } /* Should not be reached */
227}
228
229static object *
230thread_exit_prog(self, args)
231 object *self; /* Not used */
232 object *args;
233{
234 int sts;
235 if (!getargs(args, "i", &sts))
236 return NULL;
Guido van Rossumf9f2e821992-08-17 08:59:08 +0000237 goaway(sts); /* Calls exit_prog(sts) or _exit_prog(sts) */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000238 for (;;) { } /* Should not be reached */
239}
240
241static object *
242thread_allocate_lock(self, args)
243 object *self; /* Not used */
244 object *args;
245{
246 if (!getnoarg(args))
247 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000248 return (object *) newlockobject();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000249}
250
251static struct methodlist thread_methods[] = {
252 {"start_new_thread", thread_start_new_thread},
253 {"start_new", thread_start_new_thread},
254 {"allocate_lock", thread_allocate_lock},
255 {"allocate", thread_allocate_lock},
256 {"exit_thread", thread_exit_thread},
257 {"exit", thread_exit_thread},
258 {"exit_prog", thread_exit_prog},
259 {NULL, NULL} /* sentinel */
260};
261
262
263/* Initialization function */
264
265void
266initthread()
267{
268 object *m, *d, *x;
269
270 /* Create the module and add the functions */
271 m = initmodule("thread", thread_methods);
272
273 /* Add a symbolic constant */
274 d = getmoduledict(m);
275 ThreadError = newstringobject("thread.error");
276 INCREF(ThreadError);
277 dictinsert(d, "error", ThreadError);
278
279 /* Check for errors */
280 if (err_occurred())
281 fatal("can't initialize module thread");
282
283 /* Initialize the C thread library */
284 init_thread();
285
286 /* Initialize the interpreter's stack save/restore mechanism */
287 init_save_thread();
288}