thread_solaris.h 4.37 KB
Newer Older
1 2 3 4

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
5
#include <errno.h>
6
#include </usr/include/thread.h>
7
#undef _POSIX_THREADS
8 9 10 11 12


/*
 * Initialization.
 */
13
static void PyThread__init_thread(void)
14 15 16 17 18 19 20
{
}

/*
 * Thread support.
 */
struct func_arg {
21
	void (*func)(void *);
22 23 24
	void *arg;
};

25 26
static void *
new_func(void *funcarg)
27
{
28
	void (*func)(void *);
29 30 31 32 33 34 35 36 37 38
	void *arg;

	func = ((struct func_arg *) funcarg)->func;
	arg = ((struct func_arg *) funcarg)->arg;
	free(funcarg);
	(*func)(arg);
	return 0;
}


39
long
40
PyThread_start_new_thread(void (*func)(void *), void *arg)
41
{
42
	thread_t tid;
43 44 45 46
	struct func_arg *funcarg;
	int success = 0;	/* init not needed when SOLARIS_THREADS and */
				/* C_THREADS implemented properly */

47
	dprintf(("PyThread_start_new_thread called\n"));
48
	if (!initialized)
49
		PyThread_init_thread();
50 51 52
	funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
	funcarg->func = func;
	funcarg->arg = arg;
53
	if (thr_create(0, 0, new_func, funcarg,
54
		       THR_DETACHED | THR_NEW_LWP, &tid)) {
55 56 57 58
		perror("thr_create");
		free((void *) funcarg);
		success = -1;
	}
59
	return tid;
60 61
}

62 63
long
PyThread_get_thread_ident(void)
64 65
{
	if (!initialized)
66
		PyThread_init_thread();
67 68 69
	return thr_self();
}

70 71
static void 
do_PyThread_exit_thread(int no_cleanup)
72
{
73
	dprintf(("PyThread_exit_thread called\n"));
74 75 76 77 78 79 80 81
	if (!initialized)
		if (no_cleanup)
			_exit(0);
		else
			exit(0);
	thr_exit(0);
}

82 83
void 
PyThread_exit_thread(void)
84
{
85
	do_PyThread_exit_thread(0);
86 87
}

88 89
void 
PyThread__exit_thread(void)
90
{
91
	do_PyThread_exit_thread(1);
92 93 94
}

#ifndef NO_EXIT_PROG
95 96
static void 
do_PyThread_exit_prog(int status, int no_cleanup)
97
{
98
	dprintf(("PyThread_exit_prog(%d) called\n", status));
99 100 101 102 103 104 105 106 107 108 109
	if (!initialized)
		if (no_cleanup)
			_exit(status);
		else
			exit(status);
	if (no_cleanup)
		_exit(status);
	else
		exit(status);
}

110 111
void 
PyThread_exit_prog(int status)
112
{
113
	do_PyThread_exit_prog(status, 0);
114 115
}

116 117
void 
PyThread__exit_prog(int status)
118
{
119
	do_PyThread_exit_prog(status, 1);
120 121 122 123 124 125
}
#endif /* NO_EXIT_PROG */

/*
 * Lock support.
 */
126 127
PyThread_type_lock 
PyThread_allocate_lock(void)
128 129 130
{
	mutex_t *lock;

131
	dprintf(("PyThread_allocate_lock called\n"));
132
	if (!initialized)
133
		PyThread_init_thread();
134 135 136 137 138 139 140

	lock = (mutex_t *) malloc(sizeof(mutex_t));
	if (mutex_init(lock, USYNC_THREAD, 0)) {
		perror("mutex_init");
		free((void *) lock);
		lock = 0;
	}
141
	dprintf(("PyThread_allocate_lock() -> %p\n", lock));
142
	return (PyThread_type_lock) lock;
143 144
}

145 146
void 
PyThread_free_lock(PyThread_type_lock lock)
147
{
148
	dprintf(("PyThread_free_lock(%p) called\n", lock));
149 150 151 152
	mutex_destroy((mutex_t *) lock);
	free((void *) lock);
}

153 154
int 
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
155 156 157
{
	int success;

158
	dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
159 160 161 162 163 164 165 166
	if (waitflag)
		success = mutex_lock((mutex_t *) lock);
	else
		success = mutex_trylock((mutex_t *) lock);
	if (success < 0)
		perror(waitflag ? "mutex_lock" : "mutex_trylock");
	else
		success = !success; /* solaris does it the other way round */
167
	dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
168 169 170
	return success;
}

171 172
void 
PyThread_release_lock(PyThread_type_lock lock)
173
{
174
	dprintf(("PyThread_release_lock(%p) called\n", lock));
175 176 177 178 179 180 181
	if (mutex_unlock((mutex_t *) lock))
		perror("mutex_unlock");
}

/*
 * Semaphore support.
 */
182 183
PyThread_type_sema 
PyThread_allocate_sema(int value)
184 185
{
	sema_t *sema;
186
	dprintf(("PyThread_allocate_sema called\n"));
187
	if (!initialized)
188
		PyThread_init_thread();
189 190 191 192 193 194 195

	sema = (sema_t *) malloc(sizeof(sema_t));
	if (sema_init(sema, value, USYNC_THREAD, 0)) {
		perror("sema_init");
		free((void *) sema);
		sema = 0;
	}
196
	dprintf(("PyThread_allocate_sema() -> %p\n",  sema));
197
	return (PyThread_type_sema) sema;
198 199
}

200 201
void 
PyThread_free_sema(PyThread_type_sema sema)
202
{
203
	dprintf(("PyThread_free_sema(%p) called\n",  sema));
204 205 206 207 208
	if (sema_destroy((sema_t *) sema))
		perror("sema_destroy");
	free((void *) sema);
}

209 210
int 
PyThread_down_sema(PyThread_type_sema sema, int waitflag)
211
{
212 213
	int success;

214
	dprintf(("PyThread_down_sema(%p) called\n",  sema));
215 216 217 218 219 220 221 222 223 224 225 226
	if (waitflag)
		success = sema_wait((sema_t *) sema);
	else
		success = sema_trywait((sema_t *) sema);
	if (success < 0) {
		if (errno == EBUSY)
			success = 0;
		else
			perror("sema_wait");
	}
	else
		success = !success;
227
	dprintf(("PyThread_down_sema(%p) return %d\n",  sema, success));
228
	return success;
229 230
}

231 232
void 
PyThread_up_sema(PyThread_type_sema sema)
233
{
234
	dprintf(("PyThread_up_sema(%p)\n",  sema));
235 236 237
	if (sema_post((sema_t *) sema))
		perror("sema_post");
}