File indexing completed on 2025-05-11 08:24:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include <threads.h>
0030 #include <pthread.h>
0031 #include <sched.h>
0032 #include <stdint.h>
0033 #include <stdlib.h>
0034
0035 struct thrd_param {
0036 thrd_start_t func;
0037 void *arg;
0038 };
0039
0040 static void *
0041 thrd_entry(void *arg)
0042 {
0043 struct thrd_param tp;
0044
0045 tp = *(struct thrd_param *)arg;
0046 free(arg);
0047 return ((void *)(intptr_t)tp.func(tp.arg));
0048 }
0049
0050 int
0051 thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
0052 {
0053 struct thrd_param *tp;
0054
0055
0056
0057
0058
0059 tp = malloc(sizeof(*tp));
0060 if (tp == NULL)
0061 return (thrd_nomem);
0062 tp->func = func;
0063 tp->arg = arg;
0064 if (pthread_create(thr, NULL, thrd_entry, tp) != 0) {
0065 free(tp);
0066 return (thrd_error);
0067 }
0068 return (thrd_success);
0069 }
0070
0071 thrd_t
0072 thrd_current(void)
0073 {
0074
0075 return (pthread_self());
0076 }
0077
0078 int
0079 thrd_detach(thrd_t thr)
0080 {
0081
0082 if (pthread_detach(thr) != 0)
0083 return (thrd_error);
0084 return (thrd_success);
0085 }
0086
0087 int
0088 thrd_equal(thrd_t thr0, thrd_t thr1)
0089 {
0090
0091 return (pthread_equal(thr0, thr1));
0092 }
0093
0094 _Noreturn void
0095 thrd_exit(int res)
0096 {
0097
0098 pthread_exit((void *)(intptr_t)res);
0099 }
0100
0101 int
0102 thrd_join(thrd_t thr, int *res)
0103 {
0104 void *value_ptr;
0105
0106 if (pthread_join(thr, &value_ptr) != 0)
0107 return (thrd_error);
0108 if (res != NULL)
0109 *res = (intptr_t)value_ptr;
0110 return (thrd_success);
0111 }
0112
0113 int
0114 thrd_sleep(const struct timespec *duration, struct timespec *remaining)
0115 {
0116
0117 return (nanosleep(duration, remaining));
0118 }
0119
0120 void
0121 thrd_yield(void)
0122 {
0123
0124 sched_yield();
0125 }