Clarify portability and main program.
[python/dscho.git] / Python / thread_cthread.h
blob3f48d592ddb1a201e5f313e1d639e5defcee46fc
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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
15 permission.
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 ******************************************************************/
32 #include <mach/cthreads.h>
36 * Initialization.
38 static void _init_thread _P0()
40 cthread_init();
44 * Thread support.
46 int start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
48 int success = 0; /* init not needed when SOLARIS_THREADS and */
49 /* C_THREADS implemented properly */
51 dprintf(("start_new_thread called\n"));
52 if (!initialized)
53 init_thread();
54 /* looks like solaris detaches the thread to never rejoin
55 * so well do it here
57 cthread_detach(cthread_fork((cthread_fn_t) func, arg));
58 return success < 0 ? 0 : 1;
61 long get_thread_ident _P0()
63 if (!initialized)
64 init_thread();
65 return (long) cthread_self();
68 static void do_exit_thread _P1(no_cleanup, int no_cleanup)
70 dprintf(("exit_thread called\n"));
71 if (!initialized)
72 if (no_cleanup)
73 _exit(0);
74 else
75 exit(0);
76 cthread_exit(0);
79 void exit_thread _P0()
81 do_exit_thread(0);
84 void _exit_thread _P0()
86 do_exit_thread(1);
89 #ifndef NO_EXIT_PROG
90 static void do_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
92 dprintf(("exit_prog(%d) called\n", status));
93 if (!initialized)
94 if (no_cleanup)
95 _exit(status);
96 else
97 exit(status);
98 if (no_cleanup)
99 _exit(status);
100 else
101 exit(status);
104 void exit_prog _P1(status, int status)
106 do_exit_prog(status, 0);
109 void _exit_prog _P1(status, int status)
111 do_exit_prog(status, 1);
113 #endif /* NO_EXIT_PROG */
116 * Lock support.
118 type_lock allocate_lock _P0()
120 mutex_t lock;
122 dprintf(("allocate_lock called\n"));
123 if (!initialized)
124 init_thread();
126 lock = mutex_alloc();
127 if (mutex_init(lock)) {
128 perror("mutex_init");
129 free((void *) lock);
130 lock = 0;
132 dprintf(("allocate_lock() -> %lx\n", (long)lock));
133 return (type_lock) lock;
136 void free_lock _P1(lock, type_lock lock)
138 dprintf(("free_lock(%lx) called\n", (long)lock));
139 mutex_free(lock);
142 int acquire_lock _P2(lock, type_lock lock, waitflag, int waitflag)
144 int success = FALSE;
146 dprintf(("acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
147 if (waitflag) { /* blocking */
148 mutex_lock(lock);
149 success = TRUE;
150 } else { /* non blocking */
151 success = mutex_try_lock(lock);
153 dprintf(("acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
154 return success;
157 void release_lock _P1(lock, type_lock lock)
159 dprintf(("release_lock(%lx) called\n", (long)lock));
160 mutex_unlock((mutex_t )lock);
164 * Semaphore support.
166 * This implementation is ripped directly from the pthreads implementation.
167 * Which is to say that it is 100% non-functional at this time.
169 * Assuming the page is still up, documentation can be found at:
171 * http://www.doc.ic.ac.uk/~mac/manuals/solaris-manual-pages/solaris/usr/man/man2/_lwp_sema_wait.2.html
173 * Looking at the man page, it seems that one could easily implement a
174 * semaphore using a condition.
177 type_sema allocate_sema _P1(value, int value)
179 char *sema = 0;
180 dprintf(("allocate_sema called\n"));
181 if (!initialized)
182 init_thread();
184 dprintf(("allocate_sema() -> %lx\n", (long) sema));
185 return (type_sema) sema;
188 void free_sema _P1(sema, type_sema sema)
190 dprintf(("free_sema(%lx) called\n", (long) sema));
193 int down_sema _P2(sema, type_sema sema, waitflag, int waitflag)
195 dprintf(("down_sema(%lx, %d) called\n", (long) sema, waitflag));
196 dprintf(("down_sema(%lx) return\n", (long) sema));
197 return -1;
200 void up_sema _P1(sema, type_sema sema)
202 dprintf(("up_sema(%lx)\n", (long) sema));