Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:21

0001 /*-
0002  * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
0003  * All rights reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
0015  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0016  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0017  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
0018  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0019  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0020  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0021  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0022  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0023  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0024  * SUCH DAMAGE.
0025  *
0026  * $FreeBSD r279318 2015-02-26T09:42:03Z$
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      * Work around return type inconsistency.  Wrap execution using
0057      * a function conforming to pthread_create()'s start_routine.
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 }