1 1.1 christos /* $NetBSD: thread.c,v 1.1 2024/02/18 20:57:56 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/strerr.h> 34 1.1 christos #include <isc/thread.h> 35 1.1 christos #include <isc/util.h> 36 1.1 christos 37 1.1 christos #include "trampoline_p.h" 38 1.1 christos 39 1.1 christos #ifndef THREAD_MINSTACKSIZE 40 1.1 christos #define THREAD_MINSTACKSIZE (1024U * 1024) 41 1.1 christos #endif /* ifndef THREAD_MINSTACKSIZE */ 42 1.1 christos 43 1.1 christos #define _FATAL(r, f) \ 44 1.1 christos { \ 45 1.1 christos char strbuf[ISC_STRERRORSIZE]; \ 46 1.1 christos strerror_r(r, strbuf, sizeof(strbuf)); \ 47 1.1 christos isc_error_fatal(__FILE__, __LINE__, f " failed: %s", strbuf); \ 48 1.1 christos } 49 1.1 christos 50 1.1 christos void 51 1.1 christos isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg, 52 1.1 christos isc_thread_t *thread) { 53 1.1 christos pthread_attr_t attr; 54 1.1 christos isc__trampoline_t *trampoline_arg; 55 1.1 christos 56 1.1 christos trampoline_arg = isc__trampoline_get(func, arg); 57 1.1 christos 58 1.1 christos #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \ 59 1.1 christos defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) 60 1.1 christos size_t stacksize; 61 1.1 christos #endif /* if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \ 62 1.1 christos * defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) */ 63 1.1 christos int ret; 64 1.1 christos 65 1.1 christos pthread_attr_init(&attr); 66 1.1 christos 67 1.1 christos #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \ 68 1.1 christos defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) 69 1.1 christos ret = pthread_attr_getstacksize(&attr, &stacksize); 70 1.1 christos if (ret != 0) { 71 1.1 christos _FATAL(ret, "pthread_attr_getstacksize()"); 72 1.1 christos } 73 1.1 christos 74 1.1 christos if (stacksize < THREAD_MINSTACKSIZE) { 75 1.1 christos ret = pthread_attr_setstacksize(&attr, THREAD_MINSTACKSIZE); 76 1.1 christos if (ret != 0) { 77 1.1 christos _FATAL(ret, "pthread_attr_setstacksize()"); 78 1.1 christos } 79 1.1 christos } 80 1.1 christos #endif /* if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \ 81 1.1 christos * defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) */ 82 1.1 christos 83 1.1 christos ret = pthread_create(thread, &attr, isc__trampoline_run, 84 1.1 christos trampoline_arg); 85 1.1 christos if (ret != 0) { 86 1.1 christos _FATAL(ret, "pthread_create()"); 87 1.1 christos } 88 1.1 christos 89 1.1 christos pthread_attr_destroy(&attr); 90 1.1 christos 91 1.1 christos return; 92 1.1 christos } 93 1.1 christos 94 1.1 christos void 95 1.1 christos isc_thread_join(isc_thread_t thread, isc_threadresult_t *result) { 96 1.1 christos int ret = pthread_join(thread, result); 97 1.1 christos if (ret != 0) { 98 1.1 christos _FATAL(ret, "pthread_join()"); 99 1.1 christos } 100 1.1 christos } 101 1.1 christos 102 1.1 christos void 103 1.1 christos isc_thread_setname(isc_thread_t thread, const char *name) { 104 1.1 christos #if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) 105 1.1 christos /* 106 1.1 christos * macOS has pthread_setname_np but only works on the 107 1.1 christos * current thread so it's not used here 108 1.1 christos */ 109 1.1 christos #if defined(__NetBSD__) 110 1.1 christos (void)pthread_setname_np(thread, name, NULL); 111 1.1 christos #else /* if defined(__NetBSD__) */ 112 1.1 christos (void)pthread_setname_np(thread, name); 113 1.1 christos #endif /* if defined(__NetBSD__) */ 114 1.1 christos #elif defined(HAVE_PTHREAD_SET_NAME_NP) 115 1.1 christos (void)pthread_set_name_np(thread, name); 116 1.1 christos #else /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */ 117 1.1 christos UNUSED(thread); 118 1.1 christos UNUSED(name); 119 1.1 christos #endif /* if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__APPLE__) */ 120 1.1 christos } 121 1.1 christos 122 1.1 christos void 123 1.1 christos isc_thread_yield(void) { 124 1.1 christos #if defined(HAVE_SCHED_YIELD) 125 1.1 christos sched_yield(); 126 1.1 christos #elif defined(HAVE_PTHREAD_YIELD) 127 1.1 christos pthread_yield(); 128 1.1 christos #elif defined(HAVE_PTHREAD_YIELD_NP) 129 1.1 christos pthread_yield_np(); 130 1.1 christos #endif /* if defined(HAVE_SCHED_YIELD) */ 131 1.1 christos } 132