2000-12-29 01:29:56 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-12-29 01:29:56 +00:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2000-12-29 01:29:56 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-12-29 01:29:56 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <errno.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2000-12-29 01:29:56 +00:00
|
|
|
#include <stdio.h>
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <sys/time.h>
|
2000-12-29 01:29:56 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include <isc/mutex.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/once.h>
|
2015-05-23 14:21:51 +02:00
|
|
|
#include <isc/print.h>
|
2018-08-28 15:43:44 -07:00
|
|
|
#include <isc/strerr.h>
|
|
|
|
#include <isc/string.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2015-02-27 11:36:39 +11:00
|
|
|
#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
|
2020-02-13 14:44:37 -08:00
|
|
|
static bool attr_initialized = false;
|
2015-02-26 14:43:45 +05:30
|
|
|
static pthread_mutexattr_t attr;
|
2020-02-13 14:44:37 -08:00
|
|
|
static isc_once_t once_attr = ISC_ONCE_INIT;
|
2015-02-26 14:43:45 +05:30
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
initialize_attr(void) {
|
2015-02-26 14:43:45 +05:30
|
|
|
RUNTIME_CHECK(pthread_mutexattr_init(&attr) == 0);
|
2020-02-12 13:59:18 +01:00
|
|
|
RUNTIME_CHECK(pthread_mutexattr_settype(
|
|
|
|
&attr, PTHREAD_MUTEX_ADAPTIVE_NP) == 0);
|
2018-04-17 08:29:14 -07:00
|
|
|
attr_initialized = true;
|
2015-02-26 14:43:45 +05:30
|
|
|
}
|
|
|
|
#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
|
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) {
|
2005-07-12 01:00:20 +00:00
|
|
|
int err;
|
2015-02-26 14:43:45 +05:30
|
|
|
|
2014-03-10 12:14:35 -07:00
|
|
|
#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2015-02-26 14:43:45 +05:30
|
|
|
result = isc_once_do(&once_attr, initialize_attr);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
2005-07-12 01:00:20 +00:00
|
|
|
|
2014-03-10 12:14:35 -07:00
|
|
|
err = pthread_mutex_init(mp, &attr);
|
2020-02-12 13:59:18 +01:00
|
|
|
#else /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
|
2021-12-09 14:02:36 +01:00
|
|
|
err = pthread_mutex_init(mp, NULL);
|
2014-03-10 12:14:35 -07:00
|
|
|
#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
|
2005-07-12 01:00:20 +00:00
|
|
|
if (err != 0) {
|
2018-11-16 15:33:22 +01:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2018-08-21 15:27:42 +02:00
|
|
|
strerror_r(err, strbuf, sizeof(strbuf));
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_error_fatal(file, line, "pthread_mutex_init failed: %s",
|
|
|
|
strbuf);
|
2005-07-12 01:00:20 +00:00
|
|
|
}
|
|
|
|
}
|