|
1 |
|
2 #include <stdlib.h> |
|
3 #include <stdio.h> |
|
4 #include <errno.h> |
|
5 #include </usr/include/thread.h> |
|
6 #undef _POSIX_THREADS |
|
7 |
|
8 |
|
9 /* |
|
10 * Initialization. |
|
11 */ |
|
12 static void PyThread__init_thread(void) |
|
13 { |
|
14 } |
|
15 |
|
16 /* |
|
17 * Thread support. |
|
18 */ |
|
19 struct func_arg { |
|
20 void (*func)(void *); |
|
21 void *arg; |
|
22 }; |
|
23 |
|
24 static void * |
|
25 new_func(void *funcarg) |
|
26 { |
|
27 void (*func)(void *); |
|
28 void *arg; |
|
29 |
|
30 func = ((struct func_arg *) funcarg)->func; |
|
31 arg = ((struct func_arg *) funcarg)->arg; |
|
32 free(funcarg); |
|
33 (*func)(arg); |
|
34 return 0; |
|
35 } |
|
36 |
|
37 |
|
38 long |
|
39 PyThread_start_new_thread(void (*func)(void *), void *arg) |
|
40 { |
|
41 thread_t tid; |
|
42 struct func_arg *funcarg; |
|
43 |
|
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; |
|
57 } |
|
58 |
|
59 long |
|
60 PyThread_get_thread_ident(void) |
|
61 { |
|
62 if (!initialized) |
|
63 PyThread_init_thread(); |
|
64 return thr_self(); |
|
65 } |
|
66 |
|
67 static void |
|
68 do_PyThread_exit_thread(int no_cleanup) |
|
69 { |
|
70 dprintf(("PyThread_exit_thread called\n")); |
|
71 if (!initialized) |
|
72 if (no_cleanup) |
|
73 _exit(0); |
|
74 else |
|
75 exit(0); |
|
76 thr_exit(0); |
|
77 } |
|
78 |
|
79 void |
|
80 PyThread_exit_thread(void) |
|
81 { |
|
82 do_PyThread_exit_thread(0); |
|
83 } |
|
84 |
|
85 void |
|
86 PyThread__exit_thread(void) |
|
87 { |
|
88 do_PyThread_exit_thread(1); |
|
89 } |
|
90 |
|
91 #ifndef NO_EXIT_PROG |
|
92 static void |
|
93 do_PyThread_exit_prog(int status, int no_cleanup) |
|
94 { |
|
95 dprintf(("PyThread_exit_prog(%d) called\n", status)); |
|
96 if (!initialized) |
|
97 if (no_cleanup) |
|
98 _exit(status); |
|
99 else |
|
100 exit(status); |
|
101 if (no_cleanup) |
|
102 _exit(status); |
|
103 else |
|
104 exit(status); |
|
105 } |
|
106 |
|
107 void |
|
108 PyThread_exit_prog(int status) |
|
109 { |
|
110 do_PyThread_exit_prog(status, 0); |
|
111 } |
|
112 |
|
113 void |
|
114 PyThread__exit_prog(int status) |
|
115 { |
|
116 do_PyThread_exit_prog(status, 1); |
|
117 } |
|
118 #endif /* NO_EXIT_PROG */ |
|
119 |
|
120 /* |
|
121 * Lock support. |
|
122 */ |
|
123 PyThread_type_lock |
|
124 PyThread_allocate_lock(void) |
|
125 { |
|
126 mutex_t *lock; |
|
127 |
|
128 dprintf(("PyThread_allocate_lock called\n")); |
|
129 if (!initialized) |
|
130 PyThread_init_thread(); |
|
131 |
|
132 lock = (mutex_t *) malloc(sizeof(mutex_t)); |
|
133 if (mutex_init(lock, USYNC_THREAD, 0)) { |
|
134 perror("mutex_init"); |
|
135 free((void *) lock); |
|
136 lock = 0; |
|
137 } |
|
138 dprintf(("PyThread_allocate_lock() -> %p\n", lock)); |
|
139 return (PyThread_type_lock) lock; |
|
140 } |
|
141 |
|
142 void |
|
143 PyThread_free_lock(PyThread_type_lock lock) |
|
144 { |
|
145 dprintf(("PyThread_free_lock(%p) called\n", lock)); |
|
146 mutex_destroy((mutex_t *) lock); |
|
147 free((void *) lock); |
|
148 } |
|
149 |
|
150 int |
|
151 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag) |
|
152 { |
|
153 int success; |
|
154 |
|
155 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag)); |
|
156 if (waitflag) |
|
157 success = mutex_lock((mutex_t *) lock); |
|
158 else |
|
159 success = mutex_trylock((mutex_t *) lock); |
|
160 if (success < 0) |
|
161 perror(waitflag ? "mutex_lock" : "mutex_trylock"); |
|
162 else |
|
163 success = !success; /* solaris does it the other way round */ |
|
164 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success)); |
|
165 return success; |
|
166 } |
|
167 |
|
168 void |
|
169 PyThread_release_lock(PyThread_type_lock lock) |
|
170 { |
|
171 dprintf(("PyThread_release_lock(%p) called\n", lock)); |
|
172 if (mutex_unlock((mutex_t *) lock)) |
|
173 perror("mutex_unlock"); |
|
174 } |