Home | History | Annotate | Line # | Download | only in isc
thread.c revision 1.2
      1  1.1  christos /*	$NetBSD: thread.c,v 1.2 2024/02/21 22:52:29 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  *
      6  1.1  christos  * SPDX-License-Identifier: MPL-2.0
      7  1.1  christos  *
      8  1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      9  1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  1.1  christos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  1.1  christos  *
     12  1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     13  1.1  christos  * information regarding copyright ownership.
     14  1.1  christos  */
     15  1.1  christos 
     16  1.1  christos /*! \file */
     17  1.1  christos 
     18  1.1  christos #if defined(HAVE_SCHED_H)
     19  1.1  christos #include <sched.h>
     20  1.1  christos #endif /* if defined(HAVE_SCHED_H) */
     21  1.1  christos 
     22  1.1  christos #if defined(HAVE_CPUSET_H)
     23  1.1  christos #include <sys/cpuset.h>
     24  1.1  christos #include <sys/param.h>
     25  1.1  christos #endif /* if defined(HAVE_CPUSET_H) */
     26  1.1  christos 
     27  1.1  christos #if defined(HAVE_SYS_PROCSET_H)
     28  1.1  christos #include <sys/processor.h>
     29  1.1  christos #include <sys/procset.h>
     30  1.1  christos #include <sys/types.h>
     31  1.1  christos #endif /* if defined(HAVE_SYS_PROCSET_H) */
     32  1.1  christos 
     33  1.1  christos #include <isc/thread.h>
     34  1.1  christos #include <isc/util.h>
     35  1.1  christos 
     36  1.1  christos #include "trampoline_p.h"
     37  1.1  christos 
     38  1.1  christos #ifndef THREAD_MINSTACKSIZE
     39  1.1  christos #define THREAD_MINSTACKSIZE (1024U * 1024)
     40  1.1  christos #endif /* ifndef THREAD_MINSTACKSIZE */
     41  1.1  christos 
     42  1.1  christos void
     43  1.1  christos isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg,
     44  1.1  christos 		  isc_thread_t *thread) {
     45  1.1  christos 	pthread_attr_t attr;
     46  1.1  christos 	isc__trampoline_t *trampoline_arg;
     47  1.1  christos 
     48  1.1  christos 	trampoline_arg = isc__trampoline_get(func, arg);
     49  1.1  christos 
     50  1.1  christos #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
     51  1.1  christos 	defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
     52  1.1  christos 	size_t stacksize;
     53  1.1  christos #endif /* if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
     54  1.1  christos 	* defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) */
     55  1.1  christos 	int ret;
     56  1.1  christos 
     57  1.1  christos 	pthread_attr_init(&attr);
     58  1.1  christos 
     59  1.1  christos #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
     60  1.1  christos 	defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
     61  1.1  christos 	ret = pthread_attr_getstacksize(&attr, &stacksize);
     62  1.1  christos 	if (ret != 0) {
     63  1.1  christos 		FATAL_SYSERROR(ret, "pthread_attr_getstacksize()");
     64  1.1  christos 	}
     65  1.1  christos 
     66  1.1  christos 	if (stacksize < THREAD_MINSTACKSIZE) {
     67  1.1  christos 		ret = pthread_attr_setstacksize(&attr, THREAD_MINSTACKSIZE);
     68  1.1  christos 		if (ret != 0) {
     69  1.1  christos 			FATAL_SYSERROR(ret, "pthread_attr_setstacksize()");
     70  1.1  christos 		}
     71  1.1  christos 	}
     72  1.1  christos #endif /* if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
     73  1.1  christos 	* defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) */
     74  1.1  christos 
     75  1.1  christos 	ret = pthread_create(thread, &attr, isc__trampoline_run,
     76  1.1  christos 			     trampoline_arg);
     77  1.1  christos 	if (ret != 0) {
     78  1.1  christos 		FATAL_SYSERROR(ret, "pthread_create()");
     79  1.1  christos 	}
     80  1.1  christos 
     81  1.1  christos 	pthread_attr_destroy(&attr);
     82  1.1  christos 
     83  1.1  christos 	return;
     84  1.1  christos }
     85  1.1  christos 
     86  1.1  christos void
     87  1.1  christos isc_thread_join(isc_thread_t thread, isc_threadresult_t *result) {
     88  1.1  christos 	int ret = pthread_join(thread, result);
     89  1.1  christos 	if (ret != 0) {
     90  1.1  christos 		FATAL_SYSERROR(ret, "pthread_join()");
     91  1.1  christos 	}
     92  1.1  christos }
     93  1.1  christos 
     94  1.1  christos void
     95  1.1  christos isc_thread_setname(isc_thread_t thread, const char *name) {
     96  1.1  christos #if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__)
     97  1.1  christos 	/*
     98  1.1  christos 	 * macOS has pthread_setname_np but only works on the
     99  1.1  christos 	 * current thread so it's not used here
    100  1.1  christos 	 */
    101  1.1  christos #if defined(__NetBSD__)
    102  1.1  christos 	(void)pthread_setname_np(thread, name, NULL);
    103  1.1  christos #else  /* if defined(__NetBSD__) */
    104  1.1  christos 	(void)pthread_setname_np(thread, name);
    105  1.1  christos #endif /* if defined(__NetBSD__) */
    106  1.1  christos #elif defined(HAVE_PTHREAD_SET_NAME_NP)
    107  1.1  christos 	(void)pthread_set_name_np(thread, name);
    108  1.1  christos #else  /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */
    109  1.1  christos 	UNUSED(thread);
    110  1.1  christos 	UNUSED(name);
    111  1.1  christos #endif /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */
    112  1.1  christos }
    113  1.1  christos 
    114  1.1  christos void
    115  1.1  christos isc_thread_yield(void) {
    116  1.1  christos #if defined(HAVE_SCHED_YIELD)
    117  1.1  christos 	sched_yield();
    118  1.1  christos #elif defined(HAVE_PTHREAD_YIELD)
    119  1.1  christos 	pthread_yield();
    120  1.1  christos #elif defined(HAVE_PTHREAD_YIELD_NP)
    121  1.1  christos 	pthread_yield_np();
    122  1.1  christos #endif /* if defined(HAVE_SCHED_YIELD) */
    123  1.1  christos }
    124