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.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
15 #include </usr/include/thread.h>
22 static void PyThread__init_thread(void)
35 new_func(void *funcarg
)
40 func
= ((struct func_arg
*) funcarg
)->func
;
41 arg
= ((struct func_arg
*) funcarg
)->arg
;
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"));
57 PyThread_init_thread();
58 funcarg
= (struct func_arg
*) malloc(sizeof(struct func_arg
));
61 if (thr_create(0, 0, new_func
, funcarg
,
62 THR_DETACHED
| THR_NEW_LWP
, 0)) {
64 free((void *) funcarg
);
67 return success
< 0 ? 0 : 1;
71 PyThread_get_thread_ident(void)
74 PyThread_init_thread();
79 do_PyThread_exit_thread(int no_cleanup
)
81 dprintf(("PyThread_exit_thread called\n"));
91 PyThread_exit_thread(void)
93 do_PyThread_exit_thread(0);
97 PyThread__exit_thread(void)
99 do_PyThread_exit_thread(1);
104 do_PyThread_exit_prog(int status
, int no_cleanup
)
106 dprintf(("PyThread_exit_prog(%d) called\n", status
));
119 PyThread_exit_prog(int status
)
121 do_PyThread_exit_prog(status
, 0);
125 PyThread__exit_prog(int status
)
127 do_PyThread_exit_prog(status
, 1);
129 #endif /* NO_EXIT_PROG */
135 PyThread_allocate_lock(void)
139 dprintf(("PyThread_allocate_lock called\n"));
141 PyThread_init_thread();
143 lock
= (mutex_t
*) malloc(sizeof(mutex_t
));
144 if (mutex_init(lock
, USYNC_THREAD
, 0)) {
145 perror("mutex_init");
149 dprintf(("PyThread_allocate_lock() -> %p\n", lock
));
150 return (PyThread_type_lock
) lock
;
154 PyThread_free_lock(PyThread_type_lock lock
)
156 dprintf(("PyThread_free_lock(%p) called\n", lock
));
157 mutex_destroy((mutex_t
*) lock
);
162 PyThread_acquire_lock(PyThread_type_lock lock
, int waitflag
)
166 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock
, waitflag
));
168 success
= mutex_lock((mutex_t
*) lock
);
170 success
= mutex_trylock((mutex_t
*) lock
);
172 perror(waitflag
? "mutex_lock" : "mutex_trylock");
174 success
= !success
; /* solaris does it the other way round */
175 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock
, waitflag
, success
));
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");
191 PyThread_allocate_sema(int value
)
194 dprintf(("PyThread_allocate_sema called\n"));
196 PyThread_init_thread();
198 sema
= (sema_t
*) malloc(sizeof(sema_t
));
199 if (sema_init(sema
, value
, USYNC_THREAD
, 0)) {
204 dprintf(("PyThread_allocate_sema() -> %p\n", sema
));
205 return (PyThread_type_sema
) sema
;
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");
218 PyThread_down_sema(PyThread_type_sema sema
, int waitflag
)
222 dprintf(("PyThread_down_sema(%p) called\n", sema
));
224 success
= sema_wait((sema_t
*) sema
);
226 success
= sema_trywait((sema_t
*) sema
);
235 dprintf(("PyThread_down_sema(%p) return %d\n", sema
, success
));
240 PyThread_up_sema(PyThread_type_sema sema
)
242 dprintf(("PyThread_up_sema(%p)\n", sema
));
243 if (sema_post((sema_t
*) sema
))