thread_lwp.h revision 7e47402264cf87b9bbb61fc9ff610af08add7c7b
1/*********************************************************** 2Copyright (c) 2000, BeOpen.com. 3Copyright (c) 1995-2000, Corporation for National Research Initiatives. 4Copyright (c) 1990-1995, Stichting Mathematisch Centrum. 5All rights reserved. 6 7See the file "Misc/COPYRIGHT" for information on usage and 8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. 9******************************************************************/ 10 11#include <stdlib.h> 12#include <lwp/lwp.h> 13#include <lwp/stackdep.h> 14 15#define STACKSIZE 1000 /* stacksize for a thread */ 16#define NSTACKS 2 /* # stacks to be put in cache initially */ 17 18struct lock { 19 int lock_locked; 20 cv_t lock_condvar; 21 mon_t lock_monitor; 22}; 23 24 25/* 26 * Initialization. 27 */ 28static void PyThread__init_thread _P0() 29{ 30 lwp_setstkcache(STACKSIZE, NSTACKS); 31} 32 33/* 34 * Thread support. 35 */ 36 37 38int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg) 39{ 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; 47} 48 49long PyThread_get_thread_ident _P0() 50{ 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; 57} 58 59static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup) 60{ 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); 68} 69 70void PyThread_exit_thread _P0() 71{ 72 do_PyThread_exit_thread(0); 73} 74 75void PyThread__exit_thread _P0() 76{ 77 do_PyThread_exit_thread(1); 78} 79 80#ifndef NO_EXIT_PROG 81static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup) 82{ 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); 90} 91 92void PyThread_exit_prog _P1(status, int status) 93{ 94 do_PyThread_exit_prog(status, 0); 95} 96 97void PyThread__exit_prog _P1(status, int status) 98{ 99 do_PyThread_exit_prog(status, 1); 100} 101#endif /* NO_EXIT_PROG */ 102 103/* 104 * Lock support. 105 */ 106PyThread_type_lock PyThread_allocate_lock _P0() 107{ 108 struct lock *lock; 109 extern char *malloc(); 110 111 dprintf(("PyThread_allocate_lock called\n")); 112 if (!initialized) 113 PyThread_init_thread(); 114 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; 121} 122 123void PyThread_free_lock _P1(lock, PyThread_type_lock lock) 124{ 125 dprintf(("PyThread_free_lock(%p) called\n", lock)); 126 mon_destroy(((struct lock *) lock)->lock_monitor); 127 free((char *) lock); 128} 129 130int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag) 131{ 132 int success; 133 134 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); 135 success = 0; 136 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; 144 } 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; 149} 150 151void PyThread_release_lock _P1(lock, PyThread_type_lock lock) 152{ 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); 158} 159 160/* 161 * Semaphore support. 162 */ 163PyThread_type_sema PyThread_allocate_sema _P1(value, int value) 164{ 165 PyThread_type_sema sema = 0; 166 dprintf(("PyThread_allocate_sema called\n")); 167 if (!initialized) 168 PyThread_init_thread(); 169 170 dprintf(("PyThread_allocate_sema() -> %p\n", sema)); 171 return (PyThread_type_sema) sema; 172} 173 174void PyThread_free_sema _P1(sema, PyThread_type_sema sema) 175{ 176 dprintf(("PyThread_free_sema(%p) called\n", sema)); 177} 178 179int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag) 180{ 181 dprintf(("PyThread_down_sema(%p, %d) called\n", sema, waitflag)); 182 dprintf(("PyThread_down_sema(%p) return\n", sema)); 183 return -1; 184} 185 186void PyThread_up_sema _P1(sema, PyThread_type_sema sema) 187{ 188 dprintf(("PyThread_up_sema(%p)\n", sema)); 189} 190