Bump version to 0.9.1.
[python/dscho.git] / Python / thread_solaris.h
blob24ceef00323cca25632c060151dcee008acec190
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include </usr/include/thread.h>
16 #undef _POSIX_THREADS
20 * Initialization.
22 static void PyThread__init_thread(void)
27 * Thread support.
29 struct func_arg {
30 void (*func)(void *);
31 void *arg;
34 static void *
35 new_func(void *funcarg)
37 void (*func)(void *);
38 void *arg;
40 func = ((struct func_arg *) funcarg)->func;
41 arg = ((struct func_arg *) funcarg)->arg;
42 free(funcarg);
43 (*func)(arg);
44 return 0;
48 int
49 PyThread_start_new_thread(void (*func)(void *), void *arg)
51 struct func_arg *funcarg;
52 int success = 0; /* init not needed when SOLARIS_THREADS and */
53 /* C_THREADS implemented properly */
55 dprintf(("PyThread_start_new_thread called\n"));
56 if (!initialized)
57 PyThread_init_thread();
58 funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
59 funcarg->func = func;
60 funcarg->arg = arg;
61 if (thr_create(0, 0, new_func, funcarg,
62 THR_DETACHED | THR_NEW_LWP, 0)) {
63 perror("thr_create");
64 free((void *) funcarg);
65 success = -1;
67 return success < 0 ? 0 : 1;
70 long
71 PyThread_get_thread_ident(void)
73 if (!initialized)
74 PyThread_init_thread();
75 return thr_self();
78 static void
79 do_PyThread_exit_thread(int no_cleanup)
81 dprintf(("PyThread_exit_thread called\n"));
82 if (!initialized)
83 if (no_cleanup)
84 _exit(0);
85 else
86 exit(0);
87 thr_exit(0);
90 void
91 PyThread_exit_thread(void)
93 do_PyThread_exit_thread(0);
96 void
97 PyThread__exit_thread(void)
99 do_PyThread_exit_thread(1);
102 #ifndef NO_EXIT_PROG
103 static void
104 do_PyThread_exit_prog(int status, int no_cleanup)
106 dprintf(("PyThread_exit_prog(%d) called\n", status));
107 if (!initialized)
108 if (no_cleanup)
109 _exit(status);
110 else
111 exit(status);
112 if (no_cleanup)
113 _exit(status);
114 else
115 exit(status);
118 void
119 PyThread_exit_prog(int status)
121 do_PyThread_exit_prog(status, 0);
124 void
125 PyThread__exit_prog(int status)
127 do_PyThread_exit_prog(status, 1);
129 #endif /* NO_EXIT_PROG */
132 * Lock support.
134 PyThread_type_lock
135 PyThread_allocate_lock(void)
137 mutex_t *lock;
139 dprintf(("PyThread_allocate_lock called\n"));
140 if (!initialized)
141 PyThread_init_thread();
143 lock = (mutex_t *) malloc(sizeof(mutex_t));
144 if (mutex_init(lock, USYNC_THREAD, 0)) {
145 perror("mutex_init");
146 free((void *) lock);
147 lock = 0;
149 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
150 return (PyThread_type_lock) lock;
153 void
154 PyThread_free_lock(PyThread_type_lock lock)
156 dprintf(("PyThread_free_lock(%p) called\n", lock));
157 mutex_destroy((mutex_t *) lock);
158 free((void *) lock);
161 int
162 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
164 int success;
166 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
167 if (waitflag)
168 success = mutex_lock((mutex_t *) lock);
169 else
170 success = mutex_trylock((mutex_t *) lock);
171 if (success < 0)
172 perror(waitflag ? "mutex_lock" : "mutex_trylock");
173 else
174 success = !success; /* solaris does it the other way round */
175 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
176 return success;
179 void
180 PyThread_release_lock(PyThread_type_lock lock)
182 dprintf(("PyThread_release_lock(%p) called\n", lock));
183 if (mutex_unlock((mutex_t *) lock))
184 perror("mutex_unlock");
188 * Semaphore support.
190 PyThread_type_sema
191 PyThread_allocate_sema(int value)
193 sema_t *sema;
194 dprintf(("PyThread_allocate_sema called\n"));
195 if (!initialized)
196 PyThread_init_thread();
198 sema = (sema_t *) malloc(sizeof(sema_t));
199 if (sema_init(sema, value, USYNC_THREAD, 0)) {
200 perror("sema_init");
201 free((void *) sema);
202 sema = 0;
204 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
205 return (PyThread_type_sema) sema;
208 void
209 PyThread_free_sema(PyThread_type_sema sema)
211 dprintf(("PyThread_free_sema(%p) called\n", sema));
212 if (sema_destroy((sema_t *) sema))
213 perror("sema_destroy");
214 free((void *) sema);
217 int
218 PyThread_down_sema(PyThread_type_sema sema, int waitflag)
220 int success;
222 dprintf(("PyThread_down_sema(%p) called\n", sema));
223 if (waitflag)
224 success = sema_wait((sema_t *) sema);
225 else
226 success = sema_trywait((sema_t *) sema);
227 if (success < 0) {
228 if (errno == EBUSY)
229 success = 0;
230 else
231 perror("sema_wait");
233 else
234 success = !success;
235 dprintf(("PyThread_down_sema(%p) return %d\n", sema, success));
236 return success;
239 void
240 PyThread_up_sema(PyThread_type_sema sema)
242 dprintf(("PyThread_up_sema(%p)\n", sema));
243 if (sema_post((sema_t *) sema))
244 perror("sema_post");