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