thread_solaris.h 3.04 KB
Newer Older
1 2 3

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


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

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

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

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


38
long
39
PyThread_start_new_thread(void (*func)(void *), void *arg)
40
{
41
	thread_t tid;
42 43
	struct func_arg *funcarg;

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

59 60
long
PyThread_get_thread_ident(void)
61 62
{
	if (!initialized)
63
		PyThread_init_thread();
64 65 66
	return thr_self();
}

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

79 80
void 
PyThread_exit_thread(void)
81
{
82
	do_PyThread_exit_thread(0);
83 84
}

85 86
void 
PyThread__exit_thread(void)
87
{
88
	do_PyThread_exit_thread(1);
89 90 91
}

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

107 108
void 
PyThread_exit_prog(int status)
109
{
110
	do_PyThread_exit_prog(status, 0);
111 112
}

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

/*
 * Lock support.
 */
123 124
PyThread_type_lock 
PyThread_allocate_lock(void)
125 126 127
{
	mutex_t *lock;

128
	dprintf(("PyThread_allocate_lock called\n"));
129
	if (!initialized)
130
		PyThread_init_thread();
131 132 133 134 135 136 137

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

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

150 151
int 
PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
152 153 154
{
	int success;

155
	dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
156 157 158 159 160 161 162 163
	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 */
164
	dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
165 166 167
	return success;
}

168 169
void 
PyThread_release_lock(PyThread_type_lock lock)
170
{
171
	dprintf(("PyThread_release_lock(%p) called\n", lock));
172 173 174
	if (mutex_unlock((mutex_t *) lock))
		perror("mutex_unlock");
}