PageRenderTime 867ms CodeModel.GetById 707ms app.highlight 10ms RepoModel.GetById 148ms app.codeStats 0ms

/Python/thread_solaris.h

http://unladen-swallow.googlecode.com/
C++ Header | 174 lines | 142 code | 23 blank | 9 comment | 13 complexity | 20b26997027afc745d08d0554fc18f6a MD5 | raw file
  1
  2#include <stdlib.h>
  3#include <stdio.h>
  4#include <errno.h>
  5#include </usr/include/thread.h>
  6#undef _POSIX_THREADS
  7
  8
  9/*
 10 * Initialization.
 11 */
 12static void PyThread__init_thread(void)
 13{
 14}
 15
 16/*
 17 * Thread support.
 18 */
 19struct func_arg {
 20	void (*func)(void *);
 21	void *arg;
 22};
 23
 24static void *
 25new_func(void *funcarg)
 26{
 27	void (*func)(void *);
 28	void *arg;
 29
 30	func = ((struct func_arg *) funcarg)->func;
 31	arg = ((struct func_arg *) funcarg)->arg;
 32	free(funcarg);
 33	(*func)(arg);
 34	return 0;
 35}
 36
 37
 38long
 39PyThread_start_new_thread(void (*func)(void *), void *arg)
 40{
 41	thread_t tid;
 42	struct func_arg *funcarg;
 43
 44	dprintf(("PyThread_start_new_thread called\n"));
 45	if (!initialized)
 46		PyThread_init_thread();
 47	funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
 48	funcarg->func = func;
 49	funcarg->arg = arg;
 50	if (thr_create(0, 0, new_func, funcarg,
 51		       THR_DETACHED | THR_NEW_LWP, &tid)) {
 52		perror("thr_create");
 53		free((void *) funcarg);
 54		return -1;
 55	}
 56	return tid;
 57}
 58
 59long
 60PyThread_get_thread_ident(void)
 61{
 62	if (!initialized)
 63		PyThread_init_thread();
 64	return thr_self();
 65}
 66
 67static void 
 68do_PyThread_exit_thread(int no_cleanup)
 69{
 70	dprintf(("PyThread_exit_thread called\n"));
 71	if (!initialized)
 72		if (no_cleanup)
 73			_exit(0);
 74		else
 75			exit(0);
 76	thr_exit(0);
 77}
 78
 79void 
 80PyThread_exit_thread(void)
 81{
 82	do_PyThread_exit_thread(0);
 83}
 84
 85void 
 86PyThread__exit_thread(void)
 87{
 88	do_PyThread_exit_thread(1);
 89}
 90
 91#ifndef NO_EXIT_PROG
 92static void 
 93do_PyThread_exit_prog(int status, int no_cleanup)
 94{
 95	dprintf(("PyThread_exit_prog(%d) called\n", status));
 96	if (!initialized)
 97		if (no_cleanup)
 98			_exit(status);
 99		else
100			exit(status);
101	if (no_cleanup)
102		_exit(status);
103	else
104		exit(status);
105}
106
107void 
108PyThread_exit_prog(int status)
109{
110	do_PyThread_exit_prog(status, 0);
111}
112
113void 
114PyThread__exit_prog(int status)
115{
116	do_PyThread_exit_prog(status, 1);
117}
118#endif /* NO_EXIT_PROG */
119
120/*
121 * Lock support.
122 */
123PyThread_type_lock 
124PyThread_allocate_lock(void)
125{
126	mutex_t *lock;
127
128	dprintf(("PyThread_allocate_lock called\n"));
129	if (!initialized)
130		PyThread_init_thread();
131
132	lock = (mutex_t *) malloc(sizeof(mutex_t));
133	if (mutex_init(lock, USYNC_THREAD, 0)) {
134		perror("mutex_init");
135		free((void *) lock);
136		lock = 0;
137	}
138	dprintf(("PyThread_allocate_lock() -> %p\n", lock));
139	return (PyThread_type_lock) lock;
140}
141
142void 
143PyThread_free_lock(PyThread_type_lock lock)
144{
145	dprintf(("PyThread_free_lock(%p) called\n", lock));
146	mutex_destroy((mutex_t *) lock);
147	free((void *) lock);
148}
149
150int 
151PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
152{
153	int success;
154
155	dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
156	if (waitflag)
157		success = mutex_lock((mutex_t *) lock);
158	else
159		success = mutex_trylock((mutex_t *) lock);
160	if (success < 0)
161		perror(waitflag ? "mutex_lock" : "mutex_trylock");
162	else
163		success = !success; /* solaris does it the other way round */
164	dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
165	return success;
166}
167
168void 
169PyThread_release_lock(PyThread_type_lock lock)
170{
171	dprintf(("PyThread_release_lock(%p) called\n", lock));
172	if (mutex_unlock((mutex_t *) lock))
173		perror("mutex_unlock");
174}