1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
36 #include </usr/include/thread.h>
43 static void PyThread__init_thread
_P0()
51 void (*func
) _P((void *));
55 static void *new_func
_P1(funcarg
, void *funcarg
)
57 void (*func
) _P((void *));
60 func
= ((struct func_arg
*) funcarg
)->func
;
61 arg
= ((struct func_arg
*) funcarg
)->arg
;
68 int PyThread_start_new_thread
_P2(func
, void (*func
) _P((void *)), arg
, void *arg
)
70 struct func_arg
*funcarg
;
71 int success
= 0; /* init not needed when SOLARIS_THREADS and */
72 /* C_THREADS implemented properly */
74 dprintf(("PyThread_start_new_thread called\n"));
76 PyThread_init_thread();
77 funcarg
= (struct func_arg
*) malloc(sizeof(struct func_arg
));
80 if (thr_create(0, 0, new_func
, funcarg
,
81 THR_DETACHED
| THR_NEW_LWP
, 0)) {
83 free((void *) funcarg
);
86 return success
< 0 ? 0 : 1;
89 long PyThread_get_thread_ident
_P0()
92 PyThread_init_thread();
96 static void do_PyThread_exit_thread
_P1(no_cleanup
, int no_cleanup
)
98 dprintf(("PyThread_exit_thread called\n"));
107 void PyThread_exit_thread
_P0()
109 do_PyThread_exit_thread(0);
112 void PyThread__exit_thread
_P0()
114 do_PyThread_exit_thread(1);
118 static void do_PyThread_exit_prog
_P2(status
, int status
, no_cleanup
, int no_cleanup
)
120 dprintf(("PyThread_exit_prog(%d) called\n", status
));
132 void PyThread_exit_prog
_P1(status
, int status
)
134 do_PyThread_exit_prog(status
, 0);
137 void PyThread__exit_prog
_P1(status
, int status
)
139 do_PyThread_exit_prog(status
, 1);
141 #endif /* NO_EXIT_PROG */
146 PyThread_type_lock PyThread_allocate_lock
_P0()
150 dprintf(("PyThread_allocate_lock called\n"));
152 PyThread_init_thread();
154 lock
= (mutex_t
*) malloc(sizeof(mutex_t
));
155 if (mutex_init(lock
, USYNC_THREAD
, 0)) {
156 perror("mutex_init");
160 dprintf(("PyThread_allocate_lock() -> %lx\n", (long)lock
));
161 return (PyThread_type_lock
) lock
;
164 void PyThread_free_lock
_P1(lock
, PyThread_type_lock lock
)
166 dprintf(("PyThread_free_lock(%lx) called\n", (long)lock
));
167 mutex_destroy((mutex_t
*) lock
);
171 int PyThread_acquire_lock
_P2(lock
, PyThread_type_lock lock
, waitflag
, int waitflag
)
175 dprintf(("PyThread_acquire_lock(%lx, %d) called\n", (long)lock
, waitflag
));
177 success
= mutex_lock((mutex_t
*) lock
);
179 success
= mutex_trylock((mutex_t
*) lock
);
181 perror(waitflag
? "mutex_lock" : "mutex_trylock");
183 success
= !success
; /* solaris does it the other way round */
184 dprintf(("PyThread_acquire_lock(%lx, %d) -> %d\n", (long)lock
, waitflag
, success
));
188 void PyThread_release_lock
_P1(lock
, PyThread_type_lock lock
)
190 dprintf(("PyThread_release_lock(%lx) called\n", (long)lock
));
191 if (mutex_unlock((mutex_t
*) lock
))
192 perror("mutex_unlock");
198 PyThread_type_sema PyThread_allocate_sema
_P1(value
, int value
)
201 dprintf(("PyThread_allocate_sema called\n"));
203 PyThread_init_thread();
205 sema
= (sema_t
*) malloc(sizeof(sema_t
));
206 if (sema_init(sema
, value
, USYNC_THREAD
, 0)) {
211 dprintf(("PyThread_allocate_sema() -> %lx\n", (long) sema
));
212 return (PyThread_type_sema
) sema
;
215 void PyThread_free_sema
_P1(sema
, PyThread_type_sema sema
)
217 dprintf(("PyThread_free_sema(%lx) called\n", (long) sema
));
218 if (sema_destroy((sema_t
*) sema
))
219 perror("sema_destroy");
223 int PyThread_down_sema
_P2(sema
, PyThread_type_sema sema
, waitflag
, int waitflag
)
227 dprintf(("PyThread_down_sema(%lx) called\n", (long) sema
));
229 success
= sema_wait((sema_t
*) sema
);
231 success
= sema_trywait((sema_t
*) sema
);
240 dprintf(("PyThread_down_sema(%lx) return %d\n", (long) sema
, success
));
244 void PyThread_up_sema
_P1(sema
, PyThread_type_sema sema
)
246 dprintf(("PyThread_up_sema(%lx)\n", (long) sema
));
247 if (sema_post((sema_t
*) sema
))