Files for 2.1b1 distribution.
[python/dscho.git] / Python / thread_solaris.h
blob66bdfa25a3445438032ee7a71d1444db68672319
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include </usr/include/thread.h>
7 #undef _POSIX_THREADS
11 * Initialization.
13 static void PyThread__init_thread(void)
18 * Thread support.
20 struct func_arg {
21 void (*func)(void *);
22 void *arg;
25 static void *
26 new_func(void *funcarg)
28 void (*func)(void *);
29 void *arg;
31 func = ((struct func_arg *) funcarg)->func;
32 arg = ((struct func_arg *) funcarg)->arg;
33 free(funcarg);
34 (*func)(arg);
35 return 0;
39 int
40 PyThread_start_new_thread(void (*func)(void *), void *arg)
42 struct func_arg *funcarg;
43 int success = 0; /* init not needed when SOLARIS_THREADS and */
44 /* C_THREADS implemented properly */
46 dprintf(("PyThread_start_new_thread called\n"));
47 if (!initialized)
48 PyThread_init_thread();
49 funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
50 funcarg->func = func;
51 funcarg->arg = arg;
52 if (thr_create(0, 0, new_func, funcarg,
53 THR_DETACHED | THR_NEW_LWP, 0)) {
54 perror("thr_create");
55 free((void *) funcarg);
56 success = -1;
58 return success < 0 ? 0 : 1;
61 long
62 PyThread_get_thread_ident(void)
64 if (!initialized)
65 PyThread_init_thread();
66 return thr_self();
69 static void
70 do_PyThread_exit_thread(int no_cleanup)
72 dprintf(("PyThread_exit_thread called\n"));
73 if (!initialized)
74 if (no_cleanup)
75 _exit(0);
76 else
77 exit(0);
78 thr_exit(0);
81 void
82 PyThread_exit_thread(void)
84 do_PyThread_exit_thread(0);
87 void
88 PyThread__exit_thread(void)
90 do_PyThread_exit_thread(1);
93 #ifndef NO_EXIT_PROG
94 static void
95 do_PyThread_exit_prog(int status, int no_cleanup)
97 dprintf(("PyThread_exit_prog(%d) called\n", status));
98 if (!initialized)
99 if (no_cleanup)
100 _exit(status);
101 else
102 exit(status);
103 if (no_cleanup)
104 _exit(status);
105 else
106 exit(status);
109 void
110 PyThread_exit_prog(int status)
112 do_PyThread_exit_prog(status, 0);
115 void
116 PyThread__exit_prog(int status)
118 do_PyThread_exit_prog(status, 1);
120 #endif /* NO_EXIT_PROG */
123 * Lock support.
125 PyThread_type_lock
126 PyThread_allocate_lock(void)
128 mutex_t *lock;
130 dprintf(("PyThread_allocate_lock called\n"));
131 if (!initialized)
132 PyThread_init_thread();
134 lock = (mutex_t *) malloc(sizeof(mutex_t));
135 if (mutex_init(lock, USYNC_THREAD, 0)) {
136 perror("mutex_init");
137 free((void *) lock);
138 lock = 0;
140 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
141 return (PyThread_type_lock) lock;
144 void
145 PyThread_free_lock(PyThread_type_lock lock)
147 dprintf(("PyThread_free_lock(%p) called\n", lock));
148 mutex_destroy((mutex_t *) lock);
149 free((void *) lock);
152 int
153 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
155 int success;
157 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
158 if (waitflag)
159 success = mutex_lock((mutex_t *) lock);
160 else
161 success = mutex_trylock((mutex_t *) lock);
162 if (success < 0)
163 perror(waitflag ? "mutex_lock" : "mutex_trylock");
164 else
165 success = !success; /* solaris does it the other way round */
166 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
167 return success;
170 void
171 PyThread_release_lock(PyThread_type_lock lock)
173 dprintf(("PyThread_release_lock(%p) called\n", lock));
174 if (mutex_unlock((mutex_t *) lock))
175 perror("mutex_unlock");
179 * Semaphore support.
181 PyThread_type_sema
182 PyThread_allocate_sema(int value)
184 sema_t *sema;
185 dprintf(("PyThread_allocate_sema called\n"));
186 if (!initialized)
187 PyThread_init_thread();
189 sema = (sema_t *) malloc(sizeof(sema_t));
190 if (sema_init(sema, value, USYNC_THREAD, 0)) {
191 perror("sema_init");
192 free((void *) sema);
193 sema = 0;
195 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
196 return (PyThread_type_sema) sema;
199 void
200 PyThread_free_sema(PyThread_type_sema sema)
202 dprintf(("PyThread_free_sema(%p) called\n", sema));
203 if (sema_destroy((sema_t *) sema))
204 perror("sema_destroy");
205 free((void *) sema);
208 int
209 PyThread_down_sema(PyThread_type_sema sema, int waitflag)
211 int success;
213 dprintf(("PyThread_down_sema(%p) called\n", sema));
214 if (waitflag)
215 success = sema_wait((sema_t *) sema);
216 else
217 success = sema_trywait((sema_t *) sema);
218 if (success < 0) {
219 if (errno == EBUSY)
220 success = 0;
221 else
222 perror("sema_wait");
224 else
225 success = !success;
226 dprintf(("PyThread_down_sema(%p) return %d\n", sema, success));
227 return success;
230 void
231 PyThread_up_sema(PyThread_type_sema sema)
233 dprintf(("PyThread_up_sema(%p)\n", sema));
234 if (sema_post((sema_t *) sema))
235 perror("sema_post");