150472Speter/*- 21823Sphk * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org> 345763Sobrien * All rights reserved. 445763Sobrien * 518390Speter * Redistribution and use in source and binary forms, with or without 695294Sobrien * modification, are permitted provided that the following conditions 795294Sobrien * are met: 895294Sobrien * 1. Redistributions of source code must retain the above copyright 9207995Sobrien * notice, this list of conditions and the following disclaimer. 1096340Sobrien * 2. Redistributions in binary form must reproduce the above copyright 11169718Skan * notice, this list of conditions and the following disclaimer in the 122375Sbde * documentation and/or other materials provided with the distribution. 1396340Sobrien * 1445943Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1518390Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1696340Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17167471Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1845299Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19207995Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2073306Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2155220Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2296340Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23189834Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2468572Sobrien * SUCH DAMAGE. 2568572Sobrien * 2668572Sobrien * $FreeBSD$ 2754987Sobrien */ 2854987Sobrien 2954987Sobrien#include <sys/cdefs.h> 3018390Speter__FBSDID("$FreeBSD$"); 3155220Sobrien 3245943Sobrien#include <pthread.h> 3345943Sobrien#include <stdint.h> 3445943Sobrien#include <stdlib.h> 35208737Sjmallett 36208737Sjmallett#include "threads.h" 37208737Sjmallett 38204771Simpstruct thrd_param { 39204771Simp thrd_start_t func; 40204771Simp void *arg; 41208737Sjmallett}; 42208737Sjmallett 43208737Sjmallettstatic void * 44208737Sjmallettthrd_entry(void *arg) 45208737Sjmallett{ 46208737Sjmallett struct thrd_param tp; 47208737Sjmallett 48208737Sjmallett tp = *(struct thrd_param *)arg; 49208737Sjmallett free(arg); 50208737Sjmallett return ((void *)(intptr_t)tp.func(tp.arg)); 51208737Sjmallett} 52208737Sjmallett 53208737Sjmallettint 54208737Sjmallettthrd_create(thrd_t *thr, thrd_start_t func, void *arg) 55208737Sjmallett{ 56208737Sjmallett struct thrd_param *tp; 57208737Sjmallett 58208737Sjmallett /* 59208737Sjmallett * Work around return type inconsistency. Wrap execution using 6081749Sobrien * a function conforming to pthread_create()'s start_routine. 6181749Sobrien */ 6281749Sobrien tp = malloc(sizeof(*tp)); 6381749Sobrien if (tp == NULL) 6418390Speter return (thrd_nomem); 6518390Speter tp->func = func; 6645301Sobrien tp->arg = arg; 6718390Speter if (pthread_create(thr, NULL, thrd_entry, tp) != 0) { 6854987Sobrien free(tp); 6954987Sobrien return (thrd_error); 7052170Sobrien } 7118390Speter return (thrd_success); 72169718Skan} 73169718Skan 74169718Skanthrd_t 75169718Skanthrd_current(void) 7616663Sjkh{ 77169718Skan 782375Sbde return (pthread_self()); 79169718Skan} 802375Sbde 812375Sbdeint 82169718Skanthrd_detach(thrd_t thr) 83169718Skan{ 84169718Skan 85169718Skan if (pthread_detach(thr) != 0) 86169718Skan return (thrd_error); 87169718Skan return (thrd_success); 88169718Skan} 89169718Skan 90169718Skanint 91169718Skanthrd_equal(thrd_t thr0, thrd_t thr1) 92169718Skan{ 93169718Skan 94169718Skan return (pthread_equal(thr0, thr1)); 95169718Skan} 96169718Skan 97169718Skan_Noreturn void 98169718Skanthrd_exit(int res) 99169718Skan{ 10095294Sobrien 101 pthread_exit((void *)(intptr_t)res); 102} 103 104int 105thrd_join(thrd_t thr, int *res) 106{ 107 void *value_ptr; 108 109 if (pthread_join(thr, &value_ptr) != 0) 110 return (thrd_error); 111 if (res != NULL) 112 *res = (intptr_t)value_ptr; 113 return (thrd_success); 114} 115 116int 117thrd_sleep(const struct timespec *duration, struct timespec *remaining) 118{ 119 120 return (nanosleep(duration, remaining)); 121} 122 123void 124thrd_yield(void) 125{ 126 127 pthread_yield(); 128} 129