Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Python / thread_lwp.h
blob5ad4df48568ae1e7f1d1ca7610062abd09fa4749
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 <stdlib.h>
33 #include <lwp/lwp.h>
34 #include <lwp/stackdep.h>
36 #define STACKSIZE 1000 /* stacksize for a thread */
37 #define NSTACKS 2 /* # stacks to be put in cache initialy */
39 struct lock {
40 int lock_locked;
41 cv_t lock_condvar;
42 mon_t lock_monitor;
47 * Initialization.
49 static void PyThread__init_thread _P0()
51 lwp_setstkcache(STACKSIZE, NSTACKS);
55 * Thread support.
59 int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
61 thread_t tid;
62 int success;
63 dprintf(("PyThread_start_new_thread called\n"));
64 if (!initialized)
65 PyThread_init_thread();
66 success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg);
67 return success < 0 ? 0 : 1;
70 long PyThread_get_thread_ident _P0()
72 thread_t tid;
73 if (!initialized)
74 PyThread_init_thread();
75 if (lwp_self(&tid) < 0)
76 return -1;
77 return tid.thread_id;
80 static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup)
82 dprintf(("PyThread_exit_thread called\n"));
83 if (!initialized)
84 if (no_cleanup)
85 _exit(0);
86 else
87 exit(0);
88 lwp_destroy(SELF);
91 void PyThread_exit_thread _P0()
93 do_PyThread_exit_thread(0);
96 void PyThread__exit_thread _P0()
98 do_PyThread_exit_thread(1);
101 #ifndef NO_EXIT_PROG
102 static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
104 dprintf(("PyThread_exit_prog(%d) called\n", status));
105 if (!initialized)
106 if (no_cleanup)
107 _exit(status);
108 else
109 exit(status);
110 pod_exit(status);
113 void PyThread_exit_prog _P1(status, int status)
115 do_PyThread_exit_prog(status, 0);
118 void PyThread__exit_prog _P1(status, int status)
120 do_PyThread_exit_prog(status, 1);
122 #endif /* NO_EXIT_PROG */
125 * Lock support.
127 PyThread_type_lock PyThread_allocate_lock _P0()
129 struct lock *lock;
130 extern char *malloc();
132 dprintf(("PyThread_allocate_lock called\n"));
133 if (!initialized)
134 PyThread_init_thread();
136 lock = (struct lock *) malloc(sizeof(struct lock));
137 lock->lock_locked = 0;
138 (void) mon_create(&lock->lock_monitor);
139 (void) cv_create(&lock->lock_condvar, lock->lock_monitor);
140 dprintf(("PyThread_allocate_lock() -> %lx\n", (long)lock));
141 return (PyThread_type_lock) lock;
144 void PyThread_free_lock _P1(lock, PyThread_type_lock lock)
146 dprintf(("PyThread_free_lock(%lx) called\n", (long)lock));
147 mon_destroy(((struct lock *) lock)->lock_monitor);
148 free((char *) lock);
151 int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag)
153 int success;
155 dprintf(("PyThread_acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
156 success = 0;
158 (void) mon_enter(((struct lock *) lock)->lock_monitor);
159 if (waitflag)
160 while (((struct lock *) lock)->lock_locked)
161 cv_wait(((struct lock *) lock)->lock_condvar);
162 if (!((struct lock *) lock)->lock_locked) {
163 success = 1;
164 ((struct lock *) lock)->lock_locked = 1;
166 cv_broadcast(((struct lock *) lock)->lock_condvar);
167 mon_exit(((struct lock *) lock)->lock_monitor);
168 dprintf(("PyThread_acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
169 return success;
172 void PyThread_release_lock _P1(lock, PyThread_type_lock lock)
174 dprintf(("PyThread_release_lock(%lx) called\n", (long)lock));
175 (void) mon_enter(((struct lock *) lock)->lock_monitor);
176 ((struct lock *) lock)->lock_locked = 0;
177 cv_broadcast(((struct lock *) lock)->lock_condvar);
178 mon_exit(((struct lock *) lock)->lock_monitor);
182 * Semaphore support.
184 PyThread_type_sema PyThread_allocate_sema _P1(value, int value)
186 PyThread_type_sema sema = 0;
187 dprintf(("PyThread_allocate_sema called\n"));
188 if (!initialized)
189 PyThread_init_thread();
191 dprintf(("PyThread_allocate_sema() -> %lx\n", (long) sema));
192 return (PyThread_type_sema) sema;
195 void PyThread_free_sema _P1(sema, PyThread_type_sema sema)
197 dprintf(("PyThread_free_sema(%lx) called\n", (long) sema));
200 int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag)
202 dprintf(("PyThread_down_sema(%lx, %d) called\n", (long) sema, waitflag));
203 dprintf(("PyThread_down_sema(%lx) return\n", (long) sema));
204 return -1;
207 void PyThread_up_sema _P1(sema, PyThread_type_sema sema)
209 dprintf(("PyThread_up_sema(%lx)\n", (long) sema));