Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 1 | |
| 2 | #include <stdlib.h> |
| 3 | #include <lwp/lwp.h> |
| 4 | #include <lwp/stackdep.h> |
| 5 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 6 | #define STACKSIZE 1000 /* stacksize for a thread */ |
| 7 | #define NSTACKS 2 /* # stacks to be put in cache initially */ |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 8 | |
| 9 | struct lock { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 10 | int lock_locked; |
| 11 | cv_t lock_condvar; |
| 12 | mon_t lock_monitor; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 13 | }; |
| 14 | |
| 15 | |
| 16 | /* |
| 17 | * Initialization. |
| 18 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 19 | static void PyThread__init_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 20 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 21 | lwp_setstkcache(STACKSIZE, NSTACKS); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | /* |
| 25 | * Thread support. |
| 26 | */ |
| 27 | |
| 28 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 29 | long PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 30 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 31 | thread_t tid; |
| 32 | int success; |
| 33 | dprintf(("PyThread_start_new_thread called\n")); |
| 34 | if (!initialized) |
| 35 | PyThread_init_thread(); |
| 36 | success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg); |
| 37 | return success < 0 ? -1 : 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 40 | long PyThread_get_thread_ident(void) |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 41 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 42 | thread_t tid; |
| 43 | if (!initialized) |
| 44 | PyThread_init_thread(); |
| 45 | if (lwp_self(&tid) < 0) |
| 46 | return -1; |
| 47 | return tid.thread_id; |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Amaury Forgeot d'Arc | adfc80b | 2010-02-23 23:19:39 +0000 | [diff] [blame] | 50 | void PyThread_exit_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 51 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 52 | dprintf(("PyThread_exit_thread called\n")); |
| 53 | if (!initialized) |
| 54 | exit(0); |
| 55 | lwp_destroy(SELF); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 58 | /* |
| 59 | * Lock support. |
| 60 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 61 | PyThread_type_lock PyThread_allocate_lock(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 62 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 63 | struct lock *lock; |
| 64 | extern char *malloc(size_t); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 65 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 66 | dprintf(("PyThread_allocate_lock called\n")); |
| 67 | if (!initialized) |
| 68 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 70 | lock = (struct lock *) malloc(sizeof(struct lock)); |
| 71 | lock->lock_locked = 0; |
| 72 | (void) mon_create(&lock->lock_monitor); |
| 73 | (void) cv_create(&lock->lock_condvar, lock->lock_monitor); |
| 74 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
| 75 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 78 | void PyThread_free_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 79 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 80 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
| 81 | mon_destroy(((struct lock *) lock)->lock_monitor); |
| 82 | free((char *) lock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 85 | int PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 86 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 87 | int success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 88 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 89 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
| 90 | success = 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 91 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 92 | (void) mon_enter(((struct lock *) lock)->lock_monitor); |
| 93 | if (waitflag) |
| 94 | while (((struct lock *) lock)->lock_locked) |
| 95 | cv_wait(((struct lock *) lock)->lock_condvar); |
| 96 | if (!((struct lock *) lock)->lock_locked) { |
| 97 | success = 1; |
| 98 | ((struct lock *) lock)->lock_locked = 1; |
| 99 | } |
| 100 | cv_broadcast(((struct lock *) lock)->lock_condvar); |
| 101 | mon_exit(((struct lock *) lock)->lock_monitor); |
| 102 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
| 103 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 106 | void PyThread_release_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 107 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 108 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
| 109 | (void) mon_enter(((struct lock *) lock)->lock_monitor); |
| 110 | ((struct lock *) lock)->lock_locked = 0; |
| 111 | cv_broadcast(((struct lock *) lock)->lock_condvar); |
| 112 | mon_exit(((struct lock *) lock)->lock_monitor); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 113 | } |