thread_cthread.h revision a6e975801e37bef27deb3207f6fc8f0c59affb46
1
2#ifdef MACH_C_THREADS
3#include <mach/cthreads.h>
4#endif
5
6#ifdef HURD_C_THREADS
7#include <cthreads.h>
8#endif
9
10/*
11 * Initialization.
12 */
13static void
14PyThread__init_thread(void)
15{
16#ifndef HURD_C_THREADS
17	/* Roland McGrath said this should not be used since this is
18	done while linking to threads */
19	cthread_init();
20#else
21/* do nothing */
22	;
23#endif
24}
25
26/*
27 * Thread support.
28 */
29long
30PyThread_start_new_thread(void (*func)(void *), void *arg)
31{
32	int success = 0;	/* init not needed when SOLARIS_THREADS and */
33				/* C_THREADS implemented properly */
34
35	dprintf(("PyThread_start_new_thread called\n"));
36	if (!initialized)
37		PyThread_init_thread();
38	/* looks like solaris detaches the thread to never rejoin
39	 * so well do it here
40	 */
41	cthread_detach(cthread_fork((cthread_fn_t) func, arg));
42	return success < 0 ? -1 : 0;
43}
44
45long
46PyThread_get_thread_ident(void)
47{
48	if (!initialized)
49		PyThread_init_thread();
50	return (long) cthread_self();
51}
52
53static void
54do_PyThread_exit_thread(int no_cleanup)
55{
56	dprintf(("PyThread_exit_thread called\n"));
57	if (!initialized)
58		if (no_cleanup)
59			_exit(0);
60		else
61			exit(0);
62	cthread_exit(0);
63}
64
65void
66PyThread_exit_thread(void)
67{
68	do_PyThread_exit_thread(0);
69}
70
71void
72PyThread__exit_thread(void)
73{
74	do_PyThread_exit_thread(1);
75}
76
77#ifndef NO_EXIT_PROG
78static
79void do_PyThread_exit_prog(int status, int no_cleanup)
80{
81	dprintf(("PyThread_exit_prog(%d) called\n", status));
82	if (!initialized)
83		if (no_cleanup)
84			_exit(status);
85		else
86			exit(status);
87	if (no_cleanup)
88		_exit(status);
89	else
90		exit(status);
91}
92
93void
94PyThread_exit_prog(int status)
95{
96	do_PyThread_exit_prog(status, 0);
97}
98
99void
100PyThread__exit_prog(int status)
101{
102	do_PyThread_exit_prog(status, 1);
103}
104#endif /* NO_EXIT_PROG */
105
106/*
107 * Lock support.
108 */
109PyThread_type_lock
110PyThread_allocate_lock(void)
111{
112	mutex_t lock;
113
114	dprintf(("PyThread_allocate_lock called\n"));
115	if (!initialized)
116		PyThread_init_thread();
117
118	lock = mutex_alloc();
119	if (mutex_init(lock)) {
120		perror("mutex_init");
121		free((void *) lock);
122		lock = 0;
123	}
124	dprintf(("PyThread_allocate_lock() -> %p\n", lock));
125	return (PyThread_type_lock) lock;
126}
127
128void
129PyThread_free_lock(PyThread_type_lock lock)
130{
131	dprintf(("PyThread_free_lock(%p) called\n", lock));
132	mutex_free(lock);
133}
134
135int
136PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
137{
138	int success = FALSE;
139
140	dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
141	if (waitflag) { 	/* blocking */
142		mutex_lock((mutex_t)lock);
143		success = TRUE;
144	} else {		/* non blocking */
145		success = mutex_try_lock((mutex_t)lock);
146	}
147	dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
148	return success;
149}
150
151void
152PyThread_release_lock(PyThread_type_lock lock)
153{
154	dprintf(("PyThread_release_lock(%p) called\n", lock));
155	mutex_unlock((mutex_t )lock);
156}
157
158/*
159 * Semaphore support.
160 *
161 * This implementation is ripped directly from the pthreads implementation.
162 * Which is to say that it is 100% non-functional at this time.
163 *
164 * Assuming the page is still up, documentation can be found at:
165 *
166 * http://www.doc.ic.ac.uk/~mac/manuals/solaris-manual-pages/solaris/usr/man/man2/_lwp_sema_wait.2.html
167 *
168 * Looking at the man page, it seems that one could easily implement a
169 * semaphore using a condition.
170 *
171 */
172PyThread_type_sema
173PyThread_allocate_sema(int value)
174{
175	char *sema = 0;
176	dprintf(("PyThread_allocate_sema called\n"));
177	if (!initialized)
178		PyThread_init_thread();
179
180	dprintf(("PyThread_allocate_sema() -> %p\n", sema));
181	return (PyThread_type_sema) sema;
182}
183
184void
185PyThread_free_sema(PyThread_type_sema sema)
186{
187	dprintf(("PyThread_free_sema(%p) called\n", sema));
188}
189
190int
191PyThread_down_sema(PyThread_type_sema sema, int waitflag)
192{
193	dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
194	dprintf(("PyThread_down_sema(%p) return\n", sema));
195	return -1;
196}
197
198void
199PyThread_up_sema(PyThread_type_sema sema)
200{
201	dprintf(("PyThread_up_sema(%p)\n", sema));
202}
203