| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
|
| 23 |
# define _GLTHREAD_THREAD_INLINE _GL_EXTERN_INLINE |
| 24 |
#include "glthread/thread.h" |
| 25 |
|
| 26 |
#include <stdlib.h> |
| 27 |
#include "glthread/lock.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#if USE_ISOC_THREADS |
| 32 |
|
| 33 |
struct thrd_with_exitvalue |
| 34 |
{ |
| 35 |
thrd_t volatile tid; |
| 36 |
void * volatile exitvalue; |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static tss_t thrd_with_exitvalue_key; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
static void |
| 46 |
do_init_thrd_with_exitvalue_key (void) |
| 47 |
{ |
| 48 |
if (tss_create (&thrd_with_exitvalue_key, NULL) != thrd_success) |
| 49 |
abort (); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
static void |
| 54 |
init_thrd_with_exitvalue_key (void) |
| 55 |
{ |
| 56 |
static once_flag once = ONCE_FLAG_INIT; |
| 57 |
call_once (&once, do_init_thrd_with_exitvalue_key); |
| 58 |
} |
| 59 |
|
| 60 |
typedef union |
| 61 |
{ |
| 62 |
struct thrd_with_exitvalue t; |
| 63 |
struct |
| 64 |
{ |
| 65 |
thrd_t tid; |
| 66 |
void *(*mainfunc) (void *); |
| 67 |
void *arg; |
| 68 |
} a; |
| 69 |
} |
| 70 |
main_arg_t; |
| 71 |
|
| 72 |
static int |
| 73 |
thrd_main_func (void *pmarg) |
| 74 |
{ |
| 75 |
|
| 76 |
main_arg_t *main_arg = (main_arg_t *) pmarg; |
| 77 |
void *(*mainfunc) (void *) = main_arg->a.mainfunc; |
| 78 |
void *arg = main_arg->a.arg; |
| 79 |
|
| 80 |
if (tss_set (thrd_with_exitvalue_key, &main_arg->t) != thrd_success) |
| 81 |
abort (); |
| 82 |
|
| 83 |
|
| 84 |
{ |
| 85 |
void *exitvalue = mainfunc (arg); |
| 86 |
|
| 87 |
main_arg->t.exitvalue = exitvalue; |
| 88 |
return 0; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
int |
| 93 |
glthread_create (gl_thread_t *threadp, void *(*mainfunc) (void *), void *arg) |
| 94 |
{ |
| 95 |
init_thrd_with_exitvalue_key (); |
| 96 |
{ |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
main_arg_t *main_arg = (main_arg_t *) malloc (sizeof (main_arg_t)); |
| 102 |
if (main_arg == NULL) |
| 103 |
return ENOMEM; |
| 104 |
main_arg->a.mainfunc = mainfunc; |
| 105 |
main_arg->a.arg = arg; |
| 106 |
switch (thrd_create ((thrd_t *) &main_arg->t.tid, thrd_main_func, main_arg)) |
| 107 |
{ |
| 108 |
case thrd_success: |
| 109 |
break; |
| 110 |
case thrd_nomem: |
| 111 |
free (main_arg); |
| 112 |
return ENOMEM; |
| 113 |
default: |
| 114 |
free (main_arg); |
| 115 |
return EAGAIN; |
| 116 |
} |
| 117 |
*threadp = &main_arg->t; |
| 118 |
return 0; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
gl_thread_t |
| 123 |
gl_thread_self (void) |
| 124 |
{ |
| 125 |
init_thrd_with_exitvalue_key (); |
| 126 |
{ |
| 127 |
gl_thread_t thread = |
| 128 |
(struct thrd_with_exitvalue *) tss_get (thrd_with_exitvalue_key); |
| 129 |
if (thread == NULL) |
| 130 |
{ |
| 131 |
|
| 132 |
|
| 133 |
for (;;) |
| 134 |
{ |
| 135 |
thread = |
| 136 |
(struct thrd_with_exitvalue *) |
| 137 |
malloc (sizeof (struct thrd_with_exitvalue)); |
| 138 |
if (thread != NULL) |
| 139 |
break; |
| 140 |
|
| 141 |
|
| 142 |
{ |
| 143 |
struct timespec ts; |
| 144 |
ts.tv_sec = 1; |
| 145 |
ts.tv_nsec = 0; |
| 146 |
thrd_sleep (&ts, NULL); |
| 147 |
} |
| 148 |
} |
| 149 |
thread->tid = thrd_current (); |
| 150 |
thread->exitvalue = NULL; |
| 151 |
if (tss_set (thrd_with_exitvalue_key, thread) != thrd_success) |
| 152 |
abort (); |
| 153 |
} |
| 154 |
return thread; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
int |
| 159 |
glthread_join (gl_thread_t thread, void **return_value_ptr) |
| 160 |
{ |
| 161 |
|
| 162 |
|
| 163 |
int dummy; |
| 164 |
|
| 165 |
if (thread == gl_thread_self ()) |
| 166 |
return EINVAL; |
| 167 |
if (thrd_join (thread->tid, &dummy) != thrd_success) |
| 168 |
return EINVAL; |
| 169 |
if (return_value_ptr != NULL) |
| 170 |
*return_value_ptr = thread->exitvalue; |
| 171 |
free (thread); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
_Noreturn void |
| 176 |
gl_thread_exit (void *return_value) |
| 177 |
{ |
| 178 |
gl_thread_t thread = gl_thread_self (); |
| 179 |
thread->exitvalue = return_value; |
| 180 |
thrd_exit (0); |
| 181 |
} |
| 182 |
|
| 183 |
#endif |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
#if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS |
| 188 |
|
| 189 |
#include <pthread.h> |
| 190 |
|
| 191 |
#if defined PTW32_VERSION || defined __MVS__ |
| 192 |
|
| 193 |
const gl_thread_t gl_null_thread ; |
| 194 |
|
| 195 |
#endif |
| 196 |
|
| 197 |
#endif |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
#if USE_WINDOWS_THREADS |
| 202 |
|
| 203 |
#endif |
| 204 |
|
| 205 |
|