summaryrefslogtreecommitdiffstats
path: root/Modules/threadmodule.c
blob: 461953ea2acf7a6b19821244e8a5b7dda7f61575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.

STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

/* Thread module */
/* Interface to Sjoerd's portable C thread library */

#include "Python.h"

#ifndef WITH_THREAD
Error!  The rest of Python is not compiled with thread support.
Rerun configure, adding a --with-thread option.
#endif

#include "thread.h"

extern int _PyThread_Started;

static PyObject *ThreadError;


/* Lock objects */

typedef struct {
	PyObject_HEAD
	type_lock lock_lock;
} lockobject;

staticforward PyTypeObject Locktype;

#define is_lockobject(v)		((v)->ob_type == &Locktype)

type_lock
getlocklock(lock)
	PyObject *lock;
{
	if (lock == NULL || !is_lockobject(lock))
		return NULL;
	else
		return ((lockobject *) lock)->lock_lock;
}

static lockobject *
newlockobject()
{
	lockobject *self;
	self = PyObject_NEW(lockobject, &Locktype);
	if (self == NULL)
		return NULL;
	self->lock_lock = allocate_lock();
	if (self->lock_lock == NULL) {
		PyMem_DEL(self);
		self = NULL;
		PyErr_SetString(ThreadError, "can't allocate lock");
	}
	return self;
}

static void
lock_dealloc(self)
	lockobject *self;
{
	/* Unlock the lock so it's safe to free it */
	acquire_lock(self->lock_lock, 0);
	release_lock(self->lock_lock);
	
	free_lock(self->lock_lock);
	PyMem_DEL(self);
}

static PyObject *
lock_acquire_lock(self, args)
	lockobject *self;
	PyObject *args;
{
	int i;

	if (args != NULL) {
		if (!PyArg_Parse(args, "i", &i))
			return NULL;
	}
	else
		i = 1;

	Py_BEGIN_ALLOW_THREADS
	i = acquire_lock(self->lock_lock, i);
	Py_END_ALLOW_THREADS

	if (args == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}
	else
		return PyInt_FromLong((long)i);
}

static PyObject *
lock_release_lock(self, args)
	lockobject *self;
	PyObject *args;
{
	if (!PyArg_NoArgs(args))
		return NULL;

	/* Sanity check: the lock must be locked */
	if (acquire_lock(self->lock_lock, 0)) {
		release_lock(self->lock_lock);
		PyErr_SetString(ThreadError, "release unlocked lock");
		return NULL;
	}

	release_lock(self->lock_lock);
	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
lock_locked_lock(self, args)
	lockobject *self;
	PyObject *args;
{
	if (!PyArg_NoArgs(args))
		return NULL;

	if (acquire_lock(self->lock_lock, 0)) {
		release_lock(self->lock_lock);
		return PyInt_FromLong(0L);
	}
	return PyInt_FromLong(1L);
}

static PyMethodDef lock_methods[] = {
	{"acquire_lock",	(PyCFunction)lock_acquire_lock},
	{"acquire",		(PyCFunction)lock_acquire_lock},
	{"release_lock",	(PyCFunction)lock_release_lock},
	{"release",		(PyCFunction)lock_release_lock},
	{"locked_lock",		(PyCFunction)lock_locked_lock},
	{"locked",		(PyCFunction)lock_locked_lock},
	{NULL,			NULL}		/* sentinel */
};

static PyObject *
lock_getattr(self, name)
	lockobject *self;
	char *name;
{
	return Py_FindMethod(lock_methods, (PyObject *)self, name);
}

static PyTypeObject Locktype = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,				/*ob_size*/
	"lock",				/*tp_name*/
	sizeof(lockobject),		/*tp_size*/
	0,				/*tp_itemsize*/
	/* methods */
	(destructor)lock_dealloc,	/*tp_dealloc*/
	0,				/*tp_print*/
	(getattrfunc)lock_getattr,	/*tp_getattr*/
	0,				/*tp_setattr*/
	0,				/*tp_compare*/
	0,				/*tp_repr*/
};


/* Module functions */

static void
t_bootstrap(args_raw)
	void *args_raw;
{
	PyObject *args = (PyObject *) args_raw;
	PyObject *func, *arg, *res;

	_PyThread_Started++;

	PyEval_RestoreThread((void *)NULL);
	func = PyTuple_GetItem(args, 0);
	arg = PyTuple_GetItem(args, 1);
	res = PyEval_CallObject(func, arg);
	Py_DECREF(args); /* Matches the INCREF(args) in thread_start_new_thread */
	if (res == NULL) {
		if (PyErr_Occurred() == PyExc_SystemExit)
			PyErr_Clear();
		else {
			fprintf(stderr, "Unhandled exception in thread:\n");
			PyErr_Print(); /* From pythonmain.c */
		}
	}
	else
		Py_DECREF(res);
	(void) PyEval_SaveThread(); /* Should always be NULL */
	exit_thread();
}

static PyObject *
thread_start_new_thread(self, args)
	PyObject *self; /* Not used */
	PyObject *args;
{
	PyObject *func, *arg;

	if (!PyArg_Parse(args, "(OO)", &func, &arg))
		return NULL;
	Py_INCREF(args);
	/* Initialize the interpreter's stack save/restore mechanism */
	PyEval_InitThreads();
	if (!start_new_thread(t_bootstrap, (void*) args)) {
		Py_DECREF(args);
		PyErr_SetString(ThreadError, "can't start new thread\n");
		return NULL;
	}
	/* Otherwise the DECREF(args) is done by t_bootstrap */
	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
thread_exit_thread(self, args)
	PyObject *self; /* Not used */
	PyObject *args;
{
	if (!PyArg_NoArgs(args))
		return NULL;
	PyErr_SetNone(PyExc_SystemExit);
	return NULL;
}

#ifndef NO_EXIT_PROG
static PyObject *
thread_exit_prog(self, args)
	PyObject *self; /* Not used */
	PyObject *args;
{
	int sts;
	if (!PyArg_Parse(args, "i", &sts))
		return NULL;
	Py_Exit(sts); /* Calls exit_prog(sts) or _exit_prog(sts) */
	for (;;) { } /* Should not be reached */
}
#endif

static PyObject *
thread_allocate_lock(self, args)
	PyObject *self; /* Not used */
	PyObject *args;
{
	if (!PyArg_NoArgs(args))
		return NULL;
	return (PyObject *) newlockobject();
}

static PyObject *
thread_get_ident(self, args)
	PyObject *self; /* Not used */
	PyObject *args;
{
	long ident;
	if (!PyArg_NoArgs(args))
		return NULL;
	ident = get_thread_ident();
	if (ident == -1) {
		PyErr_SetString(ThreadError, "no current thread ident");
		return NULL;
	}
	return PyInt_FromLong(ident);
}

static PyMethodDef thread_methods[] = {
	{"start_new_thread",	(PyCFunction)thread_start_new_thread},
	{"start_new",		(PyCFunction)thread_start_new_thread},
	{"allocate_lock",	(PyCFunction)thread_allocate_lock},
	{"allocate",		(PyCFunction)thread_allocate_lock},
	{"exit_thread",		(PyCFunction)thread_exit_thread},
	{"exit",		(PyCFunction)thread_exit_thread},
	{"get_ident",		(PyCFunction)thread_get_ident},
#ifndef NO_EXIT_PROG
	{"exit_prog",		(PyCFunction)thread_exit_prog},
#endif
	{NULL,			NULL}		/* sentinel */
};


/* Initialization function */

void
initthread()
{
	PyObject *m, *d;

	/* Create the module and add the functions */
	m = Py_InitModule("thread", thread_methods);

	/* Add a symbolic constant */
	d = PyModule_GetDict(m);
	ThreadError = PyString_FromString("thread.error");
	PyDict_SetItemString(d, "error", ThreadError);

	/* Check for errors */
	if (PyErr_Occurred())
		Py_FatalError("can't initialize module thread");

	/* Initialize the C thread library */
	init_thread();
}