blob: ff3e6f3591c778963f6db42814261ab4551e977c [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;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000043
Guido van Rossum65d5b571998-12-21 19:32:43 +000044 dprintf(("PyThread_start_new_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000045 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000046 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000047 funcarg = (struct func_arg *) malloc(sizeof(struct func_arg));
48 funcarg->func = func;
49 funcarg->arg = arg;
Guido van Rossum495894e1999-04-13 14:32:12 +000050 if (thr_create(0, 0, new_func, funcarg,
Guido van Rossum3c288632001-10-16 21:13:49 +000051 THR_DETACHED | THR_NEW_LWP, &tid)) {
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000052 perror("thr_create");
53 free((void *) funcarg);
Martin v. Löwis910ae622003-04-19 07:44:52 +000054 return -1;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000055 }
Guido van Rossum3c288632001-10-16 21:13:49 +000056 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{
62 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +000063 PyThread_init_thread();
Guido van Rossume944da81994-05-23 12:43:41 +000064 return thr_self();
65}
66
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000067static void
68do_PyThread_exit_thread(int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000069{
Guido van Rossum65d5b571998-12-21 19:32:43 +000070 dprintf(("PyThread_exit_thread called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000071 if (!initialized)
72 if (no_cleanup)
73 _exit(0);
74 else
75 exit(0);
76 thr_exit(0);
77}
78
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000079void
80PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000081{
Guido van Rossum65d5b571998-12-21 19:32:43 +000082 do_PyThread_exit_thread(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000083}
84
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000085void
86PyThread__exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000087{
Guido van Rossum65d5b571998-12-21 19:32:43 +000088 do_PyThread_exit_thread(1);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000089}
90
91#ifndef NO_EXIT_PROG
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000092static void
93do_PyThread_exit_prog(int status, int no_cleanup)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000094{
Guido van Rossum65d5b571998-12-21 19:32:43 +000095 dprintf(("PyThread_exit_prog(%d) called\n", status));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000096 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
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000107void
108PyThread_exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000109{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000110 do_PyThread_exit_prog(status, 0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000111}
112
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000113void
114PyThread__exit_prog(int status)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000115{
Guido van Rossum65d5b571998-12-21 19:32:43 +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 */
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000123PyThread_type_lock
124PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000125{
126 mutex_t *lock;
127
Guido van Rossum65d5b571998-12-21 19:32:43 +0000128 dprintf(("PyThread_allocate_lock called\n"));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000129 if (!initialized)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000130 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000131
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 }
Fred Drakea44d3532000-06-30 15:01:00 +0000138 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
Guido van Rossum65d5b571998-12-21 19:32:43 +0000139 return (PyThread_type_lock) lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000140}
141
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000142void
143PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000144{
Fred Drakea44d3532000-06-30 15:01:00 +0000145 dprintf(("PyThread_free_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000146 mutex_destroy((mutex_t *) lock);
147 free((void *) lock);
148}
149
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000150int
151PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000152{
153 int success;
154
Fred Drakea44d3532000-06-30 15:01:00 +0000155 dprintf(("PyThread_acquire_lock(%p, %d) called\n", lock, waitflag));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000156 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 */
Fred Drakea44d3532000-06-30 15:01:00 +0000164 dprintf(("PyThread_acquire_lock(%p, %d) -> %d\n", lock, waitflag, success));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000165 return success;
166}
167
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000168void
169PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000170{
Fred Drakea44d3532000-06-30 15:01:00 +0000171 dprintf(("PyThread_release_lock(%p) called\n", lock));
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000172 if (mutex_unlock((mutex_t *) lock))
173 perror("mutex_unlock");
174}