Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 1 | |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
Guido van Rossum | cf1474b | 1996-10-08 14:17:53 +0000 | [diff] [blame] | 4 | #include <errno.h> |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 5 | #include </usr/include/thread.h> |
Guido van Rossum | 6eea326 | 1996-08-26 14:58:54 +0000 | [diff] [blame] | 6 | #undef _POSIX_THREADS |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 7 | |
| 8 | |
| 9 | /* |
| 10 | * Initialization. |
| 11 | */ |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 12 | static void PyThread__init_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 13 | { |
| 14 | } |
| 15 | |
| 16 | /* |
| 17 | * Thread support. |
| 18 | */ |
| 19 | struct func_arg { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | void (*func)(void *); |
| 21 | void *arg; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 22 | }; |
| 23 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 24 | static void * |
| 25 | new_func(void *funcarg) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 26 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | void (*func)(void *); |
| 28 | void *arg; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 29 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | func = ((struct func_arg *) funcarg)->func; |
| 31 | arg = ((struct func_arg *) funcarg)->arg; |
| 32 | free(funcarg); |
| 33 | (*func)(arg); |
| 34 | return 0; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | |
Guido van Rossum | 3c28863 | 2001-10-16 21:13:49 +0000 | [diff] [blame] | 38 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 39 | PyThread_start_new_thread(void (*func)(void *), void *arg) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 40 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | thread_t tid; |
| 42 | struct func_arg *funcarg; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 43 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | dprintf(("PyThread_start_new_thread called\n")); |
| 45 | if (!initialized) |
| 46 | PyThread_init_thread(); |
| 47 | funcarg = (struct func_arg *) malloc(sizeof(struct func_arg)); |
| 48 | funcarg->func = func; |
| 49 | funcarg->arg = arg; |
| 50 | if (thr_create(0, 0, new_func, funcarg, |
| 51 | THR_DETACHED | THR_NEW_LWP, &tid)) { |
| 52 | perror("thr_create"); |
| 53 | free((void *) funcarg); |
| 54 | return -1; |
| 55 | } |
| 56 | return tid; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 59 | long |
| 60 | PyThread_get_thread_ident(void) |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 61 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | if (!initialized) |
| 63 | PyThread_init_thread(); |
| 64 | return thr_self(); |
Guido van Rossum | e944da8 | 1994-05-23 12:43:41 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 68 | PyThread_exit_thread(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 69 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | dprintf(("PyThread_exit_thread called\n")); |
| 71 | if (!initialized) |
| 72 | exit(0); |
| 73 | thr_exit(0); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 76 | /* |
| 77 | * Lock support. |
| 78 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | PyThread_type_lock |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 80 | PyThread_allocate_lock(void) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 81 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | mutex_t *lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | dprintf(("PyThread_allocate_lock called\n")); |
| 85 | if (!initialized) |
| 86 | PyThread_init_thread(); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 87 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | lock = (mutex_t *) malloc(sizeof(mutex_t)); |
| 89 | if (mutex_init(lock, USYNC_THREAD, 0)) { |
| 90 | perror("mutex_init"); |
| 91 | free((void *) lock); |
| 92 | lock = 0; |
| 93 | } |
| 94 | dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
| 95 | return (PyThread_type_lock) lock; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 99 | PyThread_free_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 100 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | dprintf(("PyThread_free_lock(%p) called\n", lock)); |
| 102 | mutex_destroy((mutex_t *) lock); |
| 103 | free((void *) lock); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 107 | PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | int success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
| 112 | if (waitflag) |
| 113 | success = mutex_lock((mutex_t *) lock); |
| 114 | else |
| 115 | success = mutex_trylock((mutex_t *) lock); |
| 116 | if (success < 0) |
| 117 | perror(waitflag ? "mutex_lock" : "mutex_trylock"); |
| 118 | else |
| 119 | success = !success; /* solaris does it the other way round */ |
| 120 | dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
| 121 | return success; |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 124 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 125 | PyThread_release_lock(PyThread_type_lock lock) |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | dprintf(("PyThread_release_lock(%p) called\n", lock)); |
| 128 | if (mutex_unlock((mutex_t *) lock)) |
| 129 | perror("mutex_unlock"); |
Guido van Rossum | 2c8cb9f | 1994-05-09 15:12:46 +0000 | [diff] [blame] | 130 | } |