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