blob: 56ac8ae40f1bbd6b514283d225d9467ba99e76b2 [file] [log] [blame]
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00001
2#include <stdlib.h>
3#include <stdio.h>
Guido van Rossumcf1474b1996-10-08 14:17:53 +00004#include <errno.h>
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00005#include </usr/include/thread.h>
Guido van Rossum6eea3261996-08-26 14:58:54 +00006#undef _POSIX_THREADS
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +00007
8
9/*
10 * Initialization.
11 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000012static void PyThread__init_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000013{
14}
15
16/*
17 * Thread support.
18 */
19struct func_arg {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000020 void (*func)(void *);
21 void *arg;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000022};
23
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000024static void *
25new_func(void *funcarg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000026{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000027 void (*func)(void *);
28 void *arg;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000029
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000030 func = ((struct func_arg *) funcarg)->func;
31 arg = ((struct func_arg *) funcarg)->arg;
32 free(funcarg);
33 (*func)(arg);
34 return 0;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000035}
36
37
Guido van Rossum3c288632001-10-16 21:13:49 +000038long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000039PyThread_start_new_thread(void (*func)(void *), void *arg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000040{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000041 thread_t tid;
42 struct func_arg *funcarg;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000043
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000044 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 Rossum2c8cb9f1994-05-09 15:12:46 +000057}
58
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000059long
60PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +000061{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000062 if (!initialized)
63 PyThread_init_thread();
64 return thr_self();
Guido van Rossume944da81994-05-23 12:43:41 +000065}
66
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000067static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000069{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000070 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);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000077}
78
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000079void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000080PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000081{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000082 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000083}
84
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000085void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000086PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000087{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000088 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000089}
90
91#ifndef NO_EXIT_PROG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000092static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000093do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000094{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000095 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);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000105}
106
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000107void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000108PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000109{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000110 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000111}
112
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000113void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000114PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000115{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000116 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000117}
118#endif /* NO_EXIT_PROG */
119
120/*
121 * Lock support.
122 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000123PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000124PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000125{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000126 mutex_t *lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000127
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000128 dprintf(("PyThread_allocate_lock called\n"));
129 if (!initialized)
130 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000131
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000132 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;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000140}
141
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000142void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000143PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000144{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000145 dprintf(("PyThread_free_lock(%p) called\n", lock));
146 mutex_destroy((mutex_t *) lock);
147 free((void *) lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000148}
149
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000150int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000151PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000152{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000153 int success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000154
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000155 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;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000166}
167
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000169PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000170{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000171 dprintf(("PyThread_release_lock(%p) called\n", lock));
172 if (mutex_unlock((mutex_t *) lock))
173 perror("mutex_unlock");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000174}