1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * SPDX-License-Identifier: MPL-2.0
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 #pragma once
15 
16 /*! \file */
17 
18 #include <errno.h>
19 
20 #include <isc/error.h>
21 #include <isc/lang.h>
22 #include <isc/mutex.h>
23 #include <isc/result.h>
24 #include <isc/strerr.h>
25 #include <isc/string.h>
26 #include <isc/types.h>
27 
28 typedef pthread_cond_t isc_condition_t;
29 
30 #define isc_condition_init(cond)                                \
31 	if (pthread_cond_init(cond, NULL) != 0) {               \
32 		char isc_condition_strbuf[ISC_STRERRORSIZE];    \
33 		strerror_r(errno, isc_condition_strbuf,         \
34 			   sizeof(isc_condition_strbuf));       \
35 		isc_error_fatal(__FILE__, __LINE__,             \
36 				"pthread_cond_init failed: %s", \
37 				isc_condition_strbuf);          \
38 	}
39 
40 #if ISC_MUTEX_PROFILE
41 #define isc_condition_wait(cp, mp)                                      \
42 	((pthread_cond_wait((cp), &((mp)->mutex)) == 0) ? ISC_R_SUCCESS \
43 							: ISC_R_UNEXPECTED)
44 #else /* if ISC_MUTEX_PROFILE */
45 #define isc_condition_wait(cp, mp)                            \
46 	((pthread_cond_wait((cp), (mp)) == 0) ? ISC_R_SUCCESS \
47 					      : ISC_R_UNEXPECTED)
48 #endif /* if ISC_MUTEX_PROFILE */
49 
50 #define isc_condition_signal(cp) \
51 	((pthread_cond_signal((cp)) == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED)
52 
53 #define isc_condition_broadcast(cp) \
54 	((pthread_cond_broadcast((cp)) == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED)
55 
56 #define isc_condition_destroy(cp) \
57 	((pthread_cond_destroy((cp)) == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED)
58 
59 ISC_LANG_BEGINDECLS
60 
61 isc_result_t
62 isc_condition_waituntil(isc_condition_t *, isc_mutex_t *, isc_time_t *);
63 
64 ISC_LANG_ENDDECLS
65