2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-09-05 09:05:40 +00:00
Files
bind/lib/isc/mutex.c
Michał Kępień 3608abc8fa Add an ERRNO_CHECK() preprocessor macro
In a number of situations in pthreads-related code, a common sequence of
steps is taken: if the value returned by a library function is not 0,
pass errno to strerror_r(), log the string returned by the latter, and
immediately abort execution.  Add an ERRNO_CHECK() preprocessor macro
which takes those exact steps and use it wherever (conveniently)
possible.

Notes:

 1. The "log the return value of strerror_r() and abort" pattern is used
    in a number of other places that this commit does not touch; only
    "!= 0" checks followed by isc_error_fatal() calls with
    non-customized error messages are replaced here.

 2. This change temporarily breaks file name & line number reporting for
    isc__mutex_init() errors, to prevent breaking the build.  This issue
    will be rectified in a subsequent change.
-

58 lines
1.5 KiB
C

/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* 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
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
/*! \file */
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <isc/mutex.h>
#include <isc/once.h>
#include <isc/print.h>
#include <isc/strerr.h>
#include <isc/string.h>
#include <isc/util.h>
#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
static bool attr_initialized = false;
static pthread_mutexattr_t attr;
static isc_once_t once_attr = ISC_ONCE_INIT;
static void
initialize_attr(void) {
RUNTIME_CHECK(pthread_mutexattr_init(&attr) == 0);
RUNTIME_CHECK(pthread_mutexattr_settype(
&attr, PTHREAD_MUTEX_ADAPTIVE_NP) == 0);
attr_initialized = true;
}
#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
void
isc__mutex_init(isc_mutex_t *mp) {
int err;
#ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
isc_result_t result = ISC_R_SUCCESS;
result = isc_once_do(&once_attr, initialize_attr);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
err = pthread_mutex_init(mp, &attr);
#else /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
err = pthread_mutex_init(mp, NULL);
#endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
ERRNO_CHECK(pthread_mutex_init, err);
}