Home | History | Annotate | Line # | Download | only in libpthread
thrd.c revision 1.2
      1 /*	$NetBSD: thrd.c,v 1.2 2019/04/24 18:47:54 kamil Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Kamil Rytarowski.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __RCSID("$NetBSD: thrd.c,v 1.2 2019/04/24 18:47:54 kamil Exp $");
     34 
     35 #include <assert.h>
     36 #include <errno.h>
     37 #include <pthread.h>
     38 #include <sched.h>
     39 #include <time.h>
     40 #include <threads.h>
     41 
     42 int
     43 thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
     44 {
     45 
     46 	_DIAGASSERT(thr != NULL);
     47 	_DIAGASSERT(func != NULL);
     48 
     49 	switch(pthread_create(thr, NULL, (void *(*)(void *))func, arg)) {
     50 	case 0:
     51 		return thrd_success;
     52 	case EAGAIN:
     53 		return thrd_nomem;
     54 	default:
     55 		return thrd_error;
     56 	}
     57 }
     58 
     59 thrd_t
     60 thrd_current(void)
     61 {
     62 
     63 	return pthread_self();
     64 }
     65 
     66 int
     67 thrd_detach(thrd_t thr)
     68 {
     69 
     70 	_DIAGASSERT(thr != NULL);
     71 
     72 	if (pthread_detach(thr) == 0)
     73 		return thrd_success;
     74 
     75 	return thrd_error;
     76 }
     77 
     78 int
     79 thrd_equal(thrd_t t1, thrd_t t2)
     80 {
     81 
     82 	_DIAGASSERT(t1 != NULL);
     83 	_DIAGASSERT(t2 != NULL);
     84 
     85 	return pthread_equal(t1, t2);
     86 }
     87 
     88 _Noreturn void
     89 thrd_exit(int res)
     90 {
     91 
     92 	pthread_exit((void *)(intptr_t)res);
     93 }
     94 
     95 int
     96 thrd_join(thrd_t thrd, int *res)
     97 {
     98 	void *ptr;
     99 
    100 	_DIAGASSERT(thrd != NULL);
    101 
    102 	if (pthread_join(thrd, &ptr) == 0) {
    103 		if (res)
    104 			*res = (int)(intptr_t)ptr;
    105 
    106 		return thrd_success;
    107 	}
    108 
    109 	return thrd_error;
    110 }
    111 
    112 int
    113 thrd_sleep(const struct timespec *duration, struct timespec *remaining)
    114 {
    115 
    116 	_DIAGASSERT(duration != NULL);
    117 
    118 	/* Use clock_nanosleep(3) to skip handling errno */
    119 
    120 	switch (clock_nanosleep(CLOCK_MONOTONIC, TIMER_RELTIME, duration,
    121 	    remaining)) {
    122 	case 0:
    123 		return 0;
    124 	case EINTR:
    125 		return -1;
    126 	default:
    127 		/* Negative value different than -1 */
    128 		return -2;
    129 	}
    130 }
    131 
    132 void
    133 thrd_yield(void)
    134 {
    135 
    136 	sched_yield();
    137 }
    138