1 /* $NetBSD: thread.h,v 1.1 2024/02/18 20:57:56 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 #ifndef ISC_THREAD_H 17 #define ISC_THREAD_H 1 18 19 /*! \file */ 20 21 #include <pthread.h> 22 23 #if defined(HAVE_PTHREAD_NP_H) 24 #include <pthread_np.h> 25 #endif /* if defined(HAVE_PTHREAD_NP_H) */ 26 27 #include <isc/lang.h> 28 #include <isc/result.h> 29 30 #if defined(HAVE_THREAD_LOCAL) 31 #include <threads.h> 32 extern thread_local size_t isc_tid_v; 33 #elif defined(HAVE___THREAD) 34 extern __thread size_t isc_tid_v; 35 #endif /* if defined(HAVE_THREAD_LOCAL) */ 36 37 ISC_LANG_BEGINDECLS 38 39 typedef pthread_t isc_thread_t; 40 typedef void *isc_threadresult_t; 41 typedef void *isc_threadarg_t; 42 typedef isc_threadresult_t (*isc_threadfunc_t)(isc_threadarg_t); 43 44 void 45 isc_thread_create(isc_threadfunc_t, isc_threadarg_t, isc_thread_t *); 46 47 void 48 isc_thread_join(isc_thread_t thread, isc_threadresult_t *result); 49 50 void 51 isc_thread_yield(void); 52 53 void 54 isc_thread_setname(isc_thread_t thread, const char *name); 55 56 #define isc_thread_self (uintptr_t) pthread_self 57 58 /*** 59 *** Thread-Local Storage 60 ***/ 61 62 #if defined(HAVE_TLS) 63 #if defined(HAVE_THREAD_LOCAL) 64 #define ISC_THREAD_LOCAL static thread_local 65 #elif defined(HAVE___THREAD) 66 #define ISC_THREAD_LOCAL static __thread 67 #else /* if defined(HAVE_THREAD_LOCAL) */ 68 #error "Unknown method for defining a TLS variable!" 69 #endif /* if defined(HAVE_THREAD_LOCAL) */ 70 #else /* if defined(HAVE_TLS) */ 71 #error "Thread-local storage support is required!" 72 #endif /* if defined(HAVE_TLS) */ 73 74 ISC_LANG_ENDDECLS 75 76 #endif /* ISC_THREAD_H */ 77