Bump version to 0.9.1.
[python/dscho.git] / Python / thread_lwp.h
blob8858e911bd221daf9df40ae29fbb149e0cb5a501
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 <lwp/lwp.h>
13 #include <lwp/stackdep.h>
15 #define STACKSIZE 1000 /* stacksize for a thread */
16 #define NSTACKS 2 /* # stacks to be put in cache initially */
18 struct lock {
19 int lock_locked;
20 cv_t lock_condvar;
21 mon_t lock_monitor;
26 * Initialization.
28 static void PyThread__init_thread(void)
30 lwp_setstkcache(STACKSIZE, NSTACKS);
34 * Thread support.
38 int PyThread_start_new_thread(void (*func)(void *), void *arg)
40 thread_t tid;
41 int success;
42 dprintf(("PyThread_start_new_thread called\n"));
43 if (!initialized)
44 PyThread_init_thread();
45 success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg);
46 return success < 0 ? 0 : 1;
49 long PyThread_get_thread_ident(void)
51 thread_t tid;
52 if (!initialized)
53 PyThread_init_thread();
54 if (lwp_self(&tid) < 0)
55 return -1;
56 return tid.thread_id;
59 static void do_PyThread_exit_thread(int no_cleanup)
61 dprintf(("PyThread_exit_thread called\n"));
62 if (!initialized)
63 if (no_cleanup)
64 _exit(0);
65 else
66 exit(0);
67 lwp_destroy(SELF);
70 void PyThread_exit_thread(void)
72 do_PyThread_exit_thread(0);
75 void PyThread__exit_thread(void)
77 do_PyThread_exit_thread(1);
80 #ifndef NO_EXIT_PROG
81 static void do_PyThread_exit_prog(int status, int no_cleanup)
83 dprintf(("PyThread_exit_prog(%d) called\n", status));
84 if (!initialized)
85 if (no_cleanup)
86 _exit(status);
87 else
88 exit(status);
89 pod_exit(status);
92 void PyThread_exit_prog(int status)
94 do_PyThread_exit_prog(status, 0);
97 void PyThread__exit_prog(int status)
99 do_PyThread_exit_prog(status, 1);
101 #endif /* NO_EXIT_PROG */
104 * Lock support.
106 PyThread_type_lock PyThread_allocate_lock(void)
108 struct lock *lock;
109 extern char *malloc(size_t);
111 dprintf(("PyThread_allocate_lock called\n"));
112 if (!initialized)
113 PyThread_init_thread();
115 lock = (struct lock *) malloc(sizeof(struct lock));
116 lock->lock_locked = 0;
117 (void) mon_create(&lock->lock_monitor);
118 (void) cv_create(&lock->lock_condvar, lock->lock_monitor);
119 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
120 return (PyThread_type_lock) lock;
123 void PyThread_free_lock(PyThread_type_lock lock)
125 dprintf(("PyThread_free_lock(%p) called\n", lock));
126 mon_destroy(((struct lock *) lock)->lock_monitor);
127 free((char *) lock);
130 int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
132 int success;
134 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
135 success = 0;
137 (void) mon_enter(((struct lock *) lock)->lock_monitor);
138 if (waitflag)
139 while (((struct lock *) lock)->lock_locked)
140 cv_wait(((struct lock *) lock)->lock_condvar);
141 if (!((struct lock *) lock)->lock_locked) {
142 success = 1;
143 ((struct lock *) lock)->lock_locked = 1;
145 cv_broadcast(((struct lock *) lock)->lock_condvar);
146 mon_exit(((struct lock *) lock)->lock_monitor);
147 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
148 return success;
151 void PyThread_release_lock(PyThread_type_lock lock)
153 dprintf(("PyThread_release_lock(%p) called\n", lock));
154 (void) mon_enter(((struct lock *) lock)->lock_monitor);
155 ((struct lock *) lock)->lock_locked = 0;
156 cv_broadcast(((struct lock *) lock)->lock_condvar);
157 mon_exit(((struct lock *) lock)->lock_monitor);
161 * Semaphore support.
163 PyThread_type_sema PyThread_allocate_sema(int value)
165 PyThread_type_sema sema = 0;
166 dprintf(("PyThread_allocate_sema called\n"));
167 if (!initialized)
168 PyThread_init_thread();
170 dprintf(("PyThread_allocate_sema() -> %p\n", sema));
171 return (PyThread_type_sema) sema;
174 void PyThread_free_sema(PyThread_type_sema sema)
176 dprintf(("PyThread_free_sema(%p) called\n", sema));
179 int PyThread_down_sema(PyThread_type_sema sema, int waitflag)
181 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag));
182 dprintf(("PyThread_down_sema(%p) return\n", sema));
183 return -1;
186 void PyThread_up_sema(PyThread_type_sema sema)
188 dprintf(("PyThread_up_sema(%p)\n", sema));