Issue #7295: Do not use a hardcoded file name in test_tarfile.
[python.git] / Python / thread_solaris.h
blobff3e6f3591c778963f6db42814261ab4551e977c
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <errno.h>
5 #include </usr/include/thread.h>
6 #undef _POSIX_THREADS
9 /*
10 * Initialization.
12 static void PyThread__init_thread(void)
17 * Thread support.
19 struct func_arg {
20 void (*func)(void *);
21 void *arg;
24 static void *
25 new_func(void *funcarg)
27 void (*func)(void *);
28 void *arg;
30 func = ((struct func_arg *) funcarg)->func;
31 arg = ((struct func_arg *) funcarg)->arg;
32 free(funcarg);
33 (*func)(arg);
34 return 0;
38 long
39 PyThread_start_new_thread(void (*func)(void *), void *arg)
41 thread_t tid;
42 struct func_arg *funcarg;
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;
56 return tid;
59 long
60 PyThread_get_thread_ident(void)
62 if (!initialized)
63 PyThread_init_thread();
64 return thr_self();
67 static void
68 do_PyThread_exit_thread(int no_cleanup)
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);
79 void
80 PyThread_exit_thread(void)
82 do_PyThread_exit_thread(0);
85 void
86 PyThread__exit_thread(void)
88 do_PyThread_exit_thread(1);
91 #ifndef NO_EXIT_PROG
92 static void
93 do_PyThread_exit_prog(int status, int no_cleanup)
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);
107 void
108 PyThread_exit_prog(int status)
110 do_PyThread_exit_prog(status, 0);
113 void
114 PyThread__exit_prog(int status)
116 do_PyThread_exit_prog(status, 1);
118 #endif /* NO_EXIT_PROG */
121 * Lock support.
123 PyThread_type_lock
124 PyThread_allocate_lock(void)
126 mutex_t *lock;
128 dprintf(("PyThread_allocate_lock called\n"));
129 if (!initialized)
130 PyThread_init_thread();
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;
138 dprintf(("PyThread_allocate_lock() -> %p\n", lock));
139 return (PyThread_type_lock) lock;
142 void
143 PyThread_free_lock(PyThread_type_lock lock)
145 dprintf(("PyThread_free_lock(%p) called\n", lock));
146 mutex_destroy((mutex_t *) lock);
147 free((void *) lock);
150 int
151 PyThread_acquire_lock(PyThread_type_lock lock, int waitflag)
153 int success;
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;
168 void
169 PyThread_release_lock(PyThread_type_lock lock)
171 dprintf(("PyThread_release_lock(%p) called\n", lock));
172 if (mutex_unlock((mutex_t *) lock))
173 perror("mutex_unlock");