thread_lwp.h 3.83 KB
Newer Older
1 2 3 4 5 6

#include <stdlib.h>
#include <lwp/lwp.h>
#include <lwp/stackdep.h>

#define STACKSIZE	1000	/* stacksize for a thread */
7
#define NSTACKS		2	/* # stacks to be put in cache initially */
8 9 10 11 12 13 14 15 16 17 18

struct lock {
	int lock_locked;
	cv_t lock_condvar;
	mon_t lock_monitor;
};


/*
 * Initialization.
 */
19
static void PyThread__init_thread(void)
20 21 22 23 24 25 26 27 28
{
	lwp_setstkcache(STACKSIZE, NSTACKS);
}

/*
 * Thread support.
 */


29
int PyThread_start_new_thread(void (*func)(void *), void *arg)
30 31
{
	thread_t tid;
32
	int success;
33
	dprintf(("PyThread_start_new_thread called\n"));
34
	if (!initialized)
35
		PyThread_init_thread();
36 37 38 39
	success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg);
	return success < 0 ? 0 : 1;
}

40
long PyThread_get_thread_ident(void)
41 42 43
{
	thread_t tid;
	if (!initialized)
44
		PyThread_init_thread();
45 46 47 48 49
	if (lwp_self(&tid) < 0)
		return -1;
	return tid.thread_id;
}

50
static void do_PyThread_exit_thread(int no_cleanup)
51
{
52
	dprintf(("PyThread_exit_thread called\n"));
53 54 55 56 57 58 59 60
	if (!initialized)
		if (no_cleanup)
			_exit(0);
		else
			exit(0);
	lwp_destroy(SELF);
}

61
void PyThread_exit_thread(void)
62
{
63
	do_PyThread_exit_thread(0);
64 65
}

66
void PyThread__exit_thread(void)
67
{
68
	do_PyThread_exit_thread(1);
69 70 71
}

#ifndef NO_EXIT_PROG
72
static void do_PyThread_exit_prog(int status, int no_cleanup)
73
{
74
	dprintf(("PyThread_exit_prog(%d) called\n", status));
75 76 77 78 79 80 81 82
	if (!initialized)
		if (no_cleanup)
			_exit(status);
		else
			exit(status);
	pod_exit(status);
}

83
void PyThread_exit_prog(int status)
84
{
85
	do_PyThread_exit_prog(status, 0);
86 87
}

88
void PyThread__exit_prog(int status)
89
{
90
	do_PyThread_exit_prog(status, 1);
91 92 93 94 95 96
}
#endif /* NO_EXIT_PROG */

/*
 * Lock support.
 */
97
PyThread_type_lock PyThread_allocate_lock(void)
98 99
{
	struct lock *lock;
100
	extern char *malloc(size_t);
101

102
	dprintf(("PyThread_allocate_lock called\n"));
103
	if (!initialized)
104
		PyThread_init_thread();
105 106 107 108 109

	lock = (struct lock *) malloc(sizeof(struct lock));
	lock->lock_locked = 0;
	(void) mon_create(&lock->lock_monitor);
	(void) cv_create(&lock->lock_condvar, lock->lock_monitor);
110
	dprintf(("PyThread_allocate_lock() -> %p\n", lock));
111
	return (PyThread_type_lock) lock;
112 113
}

114
void PyThread_free_lock(PyThread_type_lock lock)
115
{
116
	dprintf(("PyThread_free_lock(%p) called\n", lock));
117 118 119 120
	mon_destroy(((struct lock *) lock)->lock_monitor);
	free((char *) lock);
}

121
int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
122 123 124
{
	int success;

125
	dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
126 127 128 129 130 131 132 133 134 135 136 137
	success = 0;

	(void) mon_enter(((struct lock *) lock)->lock_monitor);
	if (waitflag)
		while (((struct lock *) lock)->lock_locked)
			cv_wait(((struct lock *) lock)->lock_condvar);
	if (!((struct lock *) lock)->lock_locked) {
		success = 1;
		((struct lock *) lock)->lock_locked = 1;
	}
	cv_broadcast(((struct lock *) lock)->lock_condvar);
	mon_exit(((struct lock *) lock)->lock_monitor);
138
	dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
139 140 141
	return success;
}

142
void PyThread_release_lock(PyThread_type_lock lock)
143
{
144
	dprintf(("PyThread_release_lock(%p) called\n", lock));
145 146 147 148 149 150 151 152 153
	(void) mon_enter(((struct lock *) lock)->lock_monitor);
	((struct lock *) lock)->lock_locked = 0;
	cv_broadcast(((struct lock *) lock)->lock_condvar);
	mon_exit(((struct lock *) lock)->lock_monitor);
}

/*
 * Semaphore support.
 */
154
PyThread_type_sema PyThread_allocate_sema(int value)
155
{
156 157
	PyThread_type_sema sema = 0;
	dprintf(("PyThread_allocate_sema called\n"));
158
	if (!initialized)
159
		PyThread_init_thread();
160

161
	dprintf(("PyThread_allocate_sema() -> %p\n",  sema));
162
	return (PyThread_type_sema) sema;
163 164
}

165
void PyThread_free_sema(PyThread_type_sema sema)
166
{
167
	dprintf(("PyThread_free_sema(%p) called\n",  sema));
168 169
}

170
int PyThread_down_sema(PyThread_type_sema sema, int waitflag)
171
{
172 173
	dprintf(("PyThread_down_sema(%p, %d) called\n",  sema, waitflag));
	dprintf(("PyThread_down_sema(%p) return\n",  sema));
174
	return -1;
175 176
}

177
void PyThread_up_sema(PyThread_type_sema sema)
178
{
179
	dprintf(("PyThread_up_sema(%p)\n",  sema));
180
}