Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <signal.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/wait.h> |
| 7 | #include <sys/prctl.h> |
| 8 | #include <ulocks.h> |
| 9 | #include <errno.h> |
| 10 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 11 | #define HDR_SIZE 2680 /* sizeof(ushdr_t) */ |
| 12 | #define MAXPROC 100 /* max # of threads that can be started */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 13 | |
| 14 | static usptr_t *shared_arena; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 15 | static ulock_t count_lock; /* protection for some variables */ |
| 16 | static ulock_t wait_lock; /* lock used to wait for other threads */ |
| 17 | static int waiting_for_threads; /* protected by count_lock */ |
| 18 | static int nthreads; /* protected by count_lock */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 19 | static int exit_status; |
| 20 | #ifndef NO_EXIT_PROG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 21 | static int do_exit; /* indicates that the program is to exit */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 22 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 23 | static int exiting; /* we're already exiting (for maybe_exit) */ |
| 24 | static pid_t my_pid; /* PID of main thread */ |
Guido van Rossum | 6f5f8dd | 1994-05-30 13:39:05 +0000 | [diff] [blame] | 25 | static struct pidlist { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 26 | pid_t parent; |
| 27 | pid_t child; |
| 28 | } pidlist[MAXPROC]; /* PIDs of other threads; protected by count_lock */ |
| 29 | static int maxpidindex; /* # of PIDs in pidlist */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 30 | |
| 31 | #ifndef NO_EXIT_PROG |
| 32 | /* |
| 33 | * This routine is called as a signal handler when another thread |
| 34 | * exits. When that happens, we must see whether we have to exit as |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 35 | * well (because of an PyThread_exit_prog()) or whether we should continue on. |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 36 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 37 | static void exit_sig(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 38 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 39 | d2printf(("exit_sig called\n")); |
| 40 | if (exiting && getpid() == my_pid) { |
| 41 | d2printf(("already exiting\n")); |
| 42 | return; |
| 43 | } |
| 44 | if (do_exit) { |
| 45 | d2printf(("exiting in exit_sig\n")); |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 46 | #ifdef Py_DEBUG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 47 | if ((thread_debug & 8) == 0) |
| 48 | thread_debug &= ~1; /* don't produce debug messages */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 49 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 50 | PyThread_exit_thread(); |
| 51 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* |
| 55 | * This routine is called when a process calls exit(). If that wasn't |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 56 | * done from the library, we do as if an PyThread_exit_prog() was intended. |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 57 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 58 | static void maybe_exit(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 59 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 60 | dprintf(("maybe_exit called\n")); |
| 61 | if (exiting) { |
| 62 | dprintf(("already exiting\n")); |
| 63 | return; |
| 64 | } |
| 65 | PyThread_exit_prog(0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 66 | } |
| 67 | #endif /* NO_EXIT_PROG */ |
| 68 | |
| 69 | /* |
| 70 | * Initialization. |
| 71 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 72 | static void PyThread__init_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 73 | { |
| 74 | #ifndef NO_EXIT_PROG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 75 | struct sigaction s; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 76 | #endif /* NO_EXIT_PROG */ |
| 77 | #ifdef USE_DL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 78 | long addr, size; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 79 | #endif /* USE_DL */ |
| 80 | |
| 81 | |
| 82 | #ifdef USE_DL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 83 | if ((size = usconfig(CONF_INITSIZE, 64*1024)) < 0) |
| 84 | perror("usconfig - CONF_INITSIZE (check)"); |
| 85 | if (usconfig(CONF_INITSIZE, size) < 0) |
| 86 | perror("usconfig - CONF_INITSIZE (reset)"); |
| 87 | addr = (long) dl_getrange(size + HDR_SIZE); |
| 88 | dprintf(("trying to use addr %p-%p for shared arena\n", addr, addr+size)); |
| 89 | errno = 0; |
| 90 | if ((addr = usconfig(CONF_ATTACHADDR, addr)) < 0 && errno != 0) |
| 91 | perror("usconfig - CONF_ATTACHADDR (set)"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 92 | #endif /* USE_DL */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 93 | if (usconfig(CONF_INITUSERS, 16) < 0) |
| 94 | perror("usconfig - CONF_INITUSERS"); |
| 95 | my_pid = getpid(); /* so that we know which is the main thread */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 96 | #ifndef NO_EXIT_PROG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 97 | atexit(maybe_exit); |
| 98 | s.sa_handler = exit_sig; |
| 99 | sigemptyset(&s.sa_mask); |
| 100 | /*sigaddset(&s.sa_mask, SIGUSR1);*/ |
| 101 | s.sa_flags = 0; |
| 102 | sigaction(SIGUSR1, &s, 0); |
| 103 | if (prctl(PR_SETEXITSIG, SIGUSR1) < 0) |
| 104 | perror("prctl - PR_SETEXITSIG"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 105 | #endif /* NO_EXIT_PROG */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 106 | if (usconfig(CONF_ARENATYPE, US_SHAREDONLY) < 0) |
| 107 | perror("usconfig - CONF_ARENATYPE"); |
| 108 | usconfig(CONF_LOCKTYPE, US_DEBUG); /* XXX */ |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 109 | #ifdef Py_DEBUG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 110 | if (thread_debug & 4) |
| 111 | usconfig(CONF_LOCKTYPE, US_DEBUGPLUS); |
| 112 | else if (thread_debug & 2) |
| 113 | usconfig(CONF_LOCKTYPE, US_DEBUG); |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 114 | #endif /* Py_DEBUG */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 115 | if ((shared_arena = usinit(tmpnam(0))) == 0) |
| 116 | perror("usinit"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 117 | #ifdef USE_DL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 118 | if (usconfig(CONF_ATTACHADDR, addr) < 0) /* reset address */ |
| 119 | perror("usconfig - CONF_ATTACHADDR (reset)"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 120 | #endif /* USE_DL */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 121 | if ((count_lock = usnewlock(shared_arena)) == NULL) |
| 122 | perror("usnewlock (count_lock)"); |
| 123 | (void) usinitlock(count_lock); |
| 124 | if ((wait_lock = usnewlock(shared_arena)) == NULL) |
| 125 | perror("usnewlock (wait_lock)"); |
| 126 | dprintf(("arena start: %p, arena size: %ld\n", shared_arena, (long) usconfig(CONF_GETSIZE, shared_arena))); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Thread support. |
| 131 | */ |
| 132 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 133 | static void clean_threads(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 134 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 135 | int i, j; |
| 136 | pid_t mypid, pid; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 137 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 138 | /* clean up any exited threads */ |
| 139 | mypid = getpid(); |
| 140 | i = 0; |
| 141 | while (i < maxpidindex) { |
| 142 | if (pidlist[i].parent == mypid && (pid = pidlist[i].child) > 0) { |
| 143 | pid = waitpid(pid, 0, WNOHANG); |
| 144 | if (pid > 0) { |
| 145 | /* a thread has exited */ |
| 146 | pidlist[i] = pidlist[--maxpidindex]; |
| 147 | /* remove references to children of dead proc */ |
| 148 | for (j = 0; j < maxpidindex; j++) |
| 149 | if (pidlist[j].parent == pid) |
| 150 | pidlist[j].child = -1; |
| 151 | continue; /* don't increment i */ |
| 152 | } |
| 153 | } |
| 154 | i++; |
| 155 | } |
| 156 | /* clean up the list */ |
| 157 | i = 0; |
| 158 | while (i < maxpidindex) { |
| 159 | if (pidlist[i].child == -1) { |
| 160 | pidlist[i] = pidlist[--maxpidindex]; |
| 161 | continue; /* don't increment i */ |
| 162 | } |
| 163 | i++; |
| 164 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 167 | long PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 168 | { |
| 169 | #ifdef USE_DL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 170 | long addr, size; |
| 171 | static int local_initialized = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 172 | #endif /* USE_DL */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 173 | int success = 0; /* init not needed when SOLARIS_THREADS and */ |
| 174 | /* C_THREADS implemented properly */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 176 | dprintf(("PyThread_start_new_thread called\n")); |
| 177 | if (!initialized) |
| 178 | PyThread_init_thread(); |
| 179 | switch (ussetlock(count_lock)) { |
| 180 | case 0: return 0; |
| 181 | case -1: perror("ussetlock (count_lock)"); |
| 182 | } |
| 183 | if (maxpidindex >= MAXPROC) |
| 184 | success = -1; |
| 185 | else { |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 186 | #ifdef USE_DL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 187 | if (!local_initialized) { |
| 188 | if ((size = usconfig(CONF_INITSIZE, 64*1024)) < 0) |
| 189 | perror("usconfig - CONF_INITSIZE (check)"); |
| 190 | if (usconfig(CONF_INITSIZE, size) < 0) |
| 191 | perror("usconfig - CONF_INITSIZE (reset)"); |
| 192 | addr = (long) dl_getrange(size + HDR_SIZE); |
| 193 | dprintf(("trying to use addr %p-%p for sproc\n", |
| 194 | addr, addr+size)); |
| 195 | errno = 0; |
| 196 | if ((addr = usconfig(CONF_ATTACHADDR, addr)) < 0 && |
| 197 | errno != 0) |
| 198 | perror("usconfig - CONF_ATTACHADDR (set)"); |
| 199 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 200 | #endif /* USE_DL */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 201 | clean_threads(); |
| 202 | if ((success = sproc(func, PR_SALL, arg)) < 0) |
| 203 | perror("sproc"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 204 | #ifdef USE_DL |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 205 | if (!local_initialized) { |
| 206 | if (usconfig(CONF_ATTACHADDR, addr) < 0) |
| 207 | /* reset address */ |
| 208 | perror("usconfig - CONF_ATTACHADDR (reset)"); |
| 209 | local_initialized = 1; |
| 210 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 211 | #endif /* USE_DL */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 212 | if (success >= 0) { |
| 213 | nthreads++; |
| 214 | pidlist[maxpidindex].parent = getpid(); |
| 215 | pidlist[maxpidindex++].child = success; |
| 216 | dprintf(("pidlist[%d] = %d\n", |
| 217 | maxpidindex-1, success)); |
| 218 | } |
| 219 | } |
| 220 | if (usunsetlock(count_lock) < 0) |
| 221 | perror("usunsetlock (count_lock)"); |
| 222 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 225 | long PyThread_get_thread_ident(void) |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 226 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 227 | return getpid(); |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 230 | static void do_PyThread_exit_thread(int no_cleanup) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 231 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 232 | dprintf(("PyThread_exit_thread called\n")); |
| 233 | if (!initialized) |
| 234 | if (no_cleanup) |
| 235 | _exit(0); |
| 236 | else |
| 237 | exit(0); |
| 238 | if (ussetlock(count_lock) < 0) |
| 239 | perror("ussetlock (count_lock)"); |
| 240 | nthreads--; |
| 241 | if (getpid() == my_pid) { |
| 242 | /* main thread; wait for other threads to exit */ |
| 243 | exiting = 1; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 244 | #ifndef NO_EXIT_PROG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 245 | if (do_exit) { |
| 246 | int i; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 248 | /* notify other threads */ |
| 249 | clean_threads(); |
| 250 | if (nthreads >= 0) { |
| 251 | dprintf(("kill other threads\n")); |
| 252 | for (i = 0; i < maxpidindex; i++) |
| 253 | if (pidlist[i].child > 0) |
| 254 | (void) kill(pidlist[i].child, |
| 255 | SIGKILL); |
| 256 | _exit(exit_status); |
| 257 | } |
| 258 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 259 | #endif /* NO_EXIT_PROG */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 260 | waiting_for_threads = 1; |
| 261 | if (ussetlock(wait_lock) < 0) |
| 262 | perror("ussetlock (wait_lock)"); |
| 263 | for (;;) { |
| 264 | if (nthreads < 0) { |
| 265 | dprintf(("really exit (%d)\n", exit_status)); |
| 266 | if (no_cleanup) |
| 267 | _exit(exit_status); |
| 268 | else |
| 269 | exit(exit_status); |
| 270 | } |
| 271 | if (usunsetlock(count_lock) < 0) |
| 272 | perror("usunsetlock (count_lock)"); |
| 273 | dprintf(("waiting for other threads (%d)\n", nthreads)); |
| 274 | if (ussetlock(wait_lock) < 0) |
| 275 | perror("ussetlock (wait_lock)"); |
| 276 | if (ussetlock(count_lock) < 0) |
| 277 | perror("ussetlock (count_lock)"); |
| 278 | } |
| 279 | } |
| 280 | /* not the main thread */ |
| 281 | if (waiting_for_threads) { |
| 282 | dprintf(("main thread is waiting\n")); |
| 283 | if (usunsetlock(wait_lock) < 0) |
| 284 | perror("usunsetlock (wait_lock)"); |
| 285 | } |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 286 | #ifndef NO_EXIT_PROG |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 287 | else if (do_exit) |
| 288 | (void) kill(my_pid, SIGUSR1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 289 | #endif /* NO_EXIT_PROG */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 290 | if (usunsetlock(count_lock) < 0) |
| 291 | perror("usunsetlock (count_lock)"); |
| 292 | _exit(0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 295 | void PyThread_exit_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 296 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 297 | do_PyThread_exit_thread(0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 300 | void PyThread__exit_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 301 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 302 | do_PyThread_exit_thread(1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | #ifndef NO_EXIT_PROG |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 306 | static void do_PyThread_exit_prog(int status, int no_cleanup) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 307 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 308 | dprintf(("PyThread_exit_prog(%d) called\n", status)); |
| 309 | if (!initialized) |
| 310 | if (no_cleanup) |
| 311 | _exit(status); |
| 312 | else |
| 313 | exit(status); |
| 314 | do_exit = 1; |
| 315 | exit_status = status; |
| 316 | do_PyThread_exit_thread(no_cleanup); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 319 | void PyThread_exit_prog(int status) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 320 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 321 | do_PyThread_exit_prog(status, 0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 324 | void PyThread__exit_prog(int status) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 325 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 326 | do_PyThread_exit_prog(status, 1); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 327 | } |
| 328 | #endif /* NO_EXIT_PROG */ |
| 329 | |
| 330 | /* |
| 331 | * Lock support. |
| 332 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 333 | PyThread_type_lock PyThread_allocate_lock(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 334 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 335 | ulock_t lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 337 | dprintf(("PyThread_allocate_lock called\n")); |
| 338 | if (!initialized) |
| 339 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 341 | if ((lock = usnewlock(shared_arena)) == NULL) |
| 342 | perror("usnewlock"); |
| 343 | (void) usinitlock(lock); |
| 344 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
| 345 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 348 | void PyThread_free_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 349 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 350 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
| 351 | usfreelock((ulock_t) lock, shared_arena); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 354 | int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 355 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 356 | int success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 357 | |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 358 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
| 359 | errno = 0; /* clear it just in case */ |
| 360 | if (waitflag) |
| 361 | success = ussetlock((ulock_t) lock); |
| 362 | else |
| 363 | success = uscsetlock((ulock_t) lock, 1); /* Try it once */ |
| 364 | if (success < 0) |
| 365 | perror(waitflag ? "ussetlock" : "uscsetlock"); |
| 366 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
| 367 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 370 | void PyThread_release_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 371 | { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame^] | 372 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
| 373 | if (usunsetlock((ulock_t) lock) < 0) |
| 374 | perror("usunsetlock"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 375 | } |