1 /* $NetBSD: condition.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 #pragma once 17 18 /*! \file */ 19 20 #include <errno.h> 21 22 #include <isc/error.h> 23 #include <isc/lang.h> 24 #include <isc/mutex.h> 25 #include <isc/result.h> 26 #include <isc/strerr.h> 27 #include <isc/string.h> 28 #include <isc/types.h> 29 30 typedef pthread_cond_t isc_condition_t; 31 32 #define isc_condition_init(cond) \ 33 if (pthread_cond_init(cond, NULL) != 0) { \ 34 char isc_condition_strbuf[ISC_STRERRORSIZE]; \ 35 strerror_r(errno, isc_condition_strbuf, \ 36 sizeof(isc_condition_strbuf)); \ 37 isc_error_fatal(__FILE__, __LINE__, \ 38 "pthread_cond_init failed: %s", \ 39 isc_condition_strbuf); \ 40 } 41 42 #if ISC_MUTEX_PROFILE 43 #define isc_condition_wait(cp, mp) \ 44 ((pthread_cond_wait((cp), &((mp)->mutex)) == 0) ? ISC_R_SUCCESS \ 45 : ISC_R_UNEXPECTED) 46 #else /* if ISC_MUTEX_PROFILE */ 47 #define isc_condition_wait(cp, mp) \ 48 ((pthread_cond_wait((cp), (mp)) == 0) ? ISC_R_SUCCESS \ 49 : ISC_R_UNEXPECTED) 50 #endif /* if ISC_MUTEX_PROFILE */ 51 52 #define isc_condition_signal(cp) \ 53 ((pthread_cond_signal((cp)) == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED) 54 55 #define isc_condition_broadcast(cp) \ 56 ((pthread_cond_broadcast((cp)) == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED) 57 58 #define isc_condition_destroy(cp) \ 59 ((pthread_cond_destroy((cp)) == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED) 60 61 ISC_LANG_BEGINDECLS 62 63 isc_result_t 64 isc_condition_waituntil(isc_condition_t *, isc_mutex_t *, isc_time_t *); 65 66 ISC_LANG_ENDDECLS 67