1 /* $NetBSD: mutex.c,v 1.1.1.1 2024/02/21 21:54:49 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 /*! \file */ 17 18 #include <errno.h> 19 #include <stdbool.h> 20 #include <stdio.h> 21 #include <sys/time.h> 22 #include <time.h> 23 24 #include <isc/mutex.h> 25 #include <isc/once.h> 26 #include <isc/print.h> 27 #include <isc/string.h> 28 #include <isc/util.h> 29 30 #ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP 31 static bool attr_initialized = false; 32 static pthread_mutexattr_t attr; 33 static isc_once_t once_attr = ISC_ONCE_INIT; 34 35 static void 36 initialize_attr(void) { 37 RUNTIME_CHECK(pthread_mutexattr_init(&attr) == 0); 38 RUNTIME_CHECK(pthread_mutexattr_settype( 39 &attr, PTHREAD_MUTEX_ADAPTIVE_NP) == 0); 40 attr_initialized = true; 41 } 42 #endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */ 43 44 int 45 isc__mutex_init(isc_mutex_t *mp) { 46 #ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP 47 isc_result_t result = ISC_R_SUCCESS; 48 result = isc_once_do(&once_attr, initialize_attr); 49 RUNTIME_CHECK(result == ISC_R_SUCCESS); 50 51 return (pthread_mutex_init(mp, &attr)); 52 #else /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */ 53 return (pthread_mutex_init(mp, NULL)); 54 #endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */ 55 } 56