blob: 1ce1cfcba92cc2ebf3fc4ac0836243b3134a1b8a [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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000027 void (*func)(void *);
28 void *arg;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000041 thread_t tid;
42 struct func_arg *funcarg;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000062 if (!initialized)
63 PyThread_init_thread();
64 return thr_self();
Guido van Rossume944da81994-05-23 12:43:41 +000065}
66
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068PyThread_exit_thread(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 dprintf(("PyThread_exit_thread called\n"));
71 if (!initialized)
72 exit(0);
73 thr_exit(0);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000074}
75
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000076/*
77 * Lock support.
78 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079PyThread_type_lock
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000080PyThread_allocate_lock(void)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 mutex_t *lock;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 dprintf(("PyThread_allocate_lock called\n"));
85 if (!initialized)
86 PyThread_init_thread();
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +000087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 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 Rossum2c8cb9f1994-05-09 15:12:46 +000096}
97
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000099PyThread_free_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 dprintf(("PyThread_free_lock(%p) called\n", lock));
102 mutex_destroy((mutex_t *) lock);
103 free((void *) lock);
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000104}
105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000107PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 int success;
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 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 Rossum2c8cb9f1994-05-09 15:12:46 +0000122}
123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125PyThread_release_lock(PyThread_type_lock lock)
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 dprintf(("PyThread_release_lock(%p) called\n", lock));
128 if (mutex_unlock((mutex_t *) lock))
129 perror("mutex_unlock");
Guido van Rossum2c8cb9f1994-05-09 15:12:46 +0000130}