blob: d3512d4eca08587183f1a297ecfe10ddbe62f833 [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 {
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000020 void (*func)(void *);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000021 void *arg;
22};
23
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000024static void *
25new_func(void *funcarg)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000026{
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000027 void (*func)(void *);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000028 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
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{
Guido van Rossum3c288632001-10-16 21:13:49 +000041 thread_t tid;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000042 struct func_arg *funcarg;
43 int success = 0; /* init not needed when SOLARIS_THREADS and */
44 /* C_THREADS implemented properly */
45
Guido van Rossum65d5b571998-12-21 19:32:43 +000046 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000047 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000048 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000049 funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
50 funcarg->func = func;
51 funcarg->arg = arg;
Guido van Rossum495894e1999-04-13 14:32:12 +000052 if (thr_create(0, 0, new_func, funcarg,
Guido van Rossum3c288632001-10-16 21:13:49 +000053 THR_DETACHED | THR_NEW_LWP, &tid)) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000054 perror("thr_create");
55 free((void *) funcarg);
56 success = -1;
57 }
Guido van Rossum3c288632001-10-16 21:13:49 +000058 return tid;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000059}
60
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000061long
62PyThread_get_thread_ident(void)
Guido van Rossume944da81994-05-23 12:43:41 +000063{
64 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000065 PyThread_init_thread();
Guido van Rossume944da81994-05-23 12:43:41 +000066 return thr_self();
67}
68
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000069static void
70do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000071{
Guido van Rossum65d5b571998-12-21 19:32:43 +000072 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000073 if (!initialized)
74 if (no_cleanup)
75 _exit(0);
76 else
77 exit(0);
78 thr_exit(0);
79}
80
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000081void
82PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000083{
Guido van Rossum65d5b571998-12-21 19:32:43 +000084 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000085}
86
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000087void
88PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000089{
Guido van Rossum65d5b571998-12-21 19:32:43 +000090 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000091}
92
93#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000094static void
95do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000096{
Guido van Rossum65d5b571998-12-21 19:32:43 +000097 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000098 if (!initialized)
99 if (no_cleanup)
100 _exit(status);
101 else
102 exit(status);
103 if (no_cleanup)
104 _exit(status);
105 else
106 exit(status);
107}
108
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000109void
110PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000111{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000112 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000113}
114
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000115void
116PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000117{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000118 do_PyThread_exit_prog(status, 1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000119}
120#endif /* NO_EXIT_PROG */
121
122/*
123 * Lock support.
124 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125PyThread_type_lock
126PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000127{
128 mutex_t *lock;
129
Guido van Rossum65d5b571998-12-21 19:32:43 +0000130 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000131 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000132 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000133
134 lock = (mutex_t *) malloc(sizeof(mutex_t));
135 if (mutex_init(lock, USYNC_THREAD, 0)) {
136 perror("mutex_init");
137 free((void *) lock);
138 lock = 0;
139 }
Fred Drakea44d3532000-06-30 15:01:00 +0000140 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000141 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000142}
143
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000144void
145PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000146{
Fred Drakea44d3532000-06-30 15:01:00 +0000147 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000148 mutex_destroy((mutex_t *) lock);
149 free((void *) lock);
150}
151
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000152int
153PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000154{
155 int success;
156
Fred Drakea44d3532000-06-30 15:01:00 +0000157 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000158 if (waitflag)
159 success = mutex_lock((mutex_t *) lock);
160 else
161 success = mutex_trylock((mutex_t *) lock);
162 if (success < 0)
163 perror(waitflag ? "mutex_lock" : "mutex_trylock");
164 else
165 success = !success; /* solaris does it the other way round */
Fred Drakea44d3532000-06-30 15:01:00 +0000166 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000167 return success;
168}
169
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000170void
171PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000172{
Fred Drakea44d3532000-06-30 15:01:00 +0000173 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000174 if (mutex_unlock((mutex_t *) lock))
175 perror("mutex_unlock");
176}