1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*
* Copyright (C) 2013-2020 Argonne National Laboratory, Department of Energy,
* UChicago Argonne, LLC and The HDF Group.
* All rights reserved.
*
* The full copyright notice, including terms governing use, modification,
* and redistribution, is contained in the COPYING file that can be
* found at the root of the source code distribution tree.
*/
#ifndef MERCURY_THREAD_CONDITION_H
#define MERCURY_THREAD_CONDITION_H
#include "mercury_thread_mutex.h"
#ifdef _WIN32
typedef CONDITION_VARIABLE hg_thread_cond_t;
#else
#if defined(HG_UTIL_HAS_PTHREAD_CONDATTR_SETCLOCK) && defined(HG_UTIL_HAS_CLOCK_MONOTONIC_COARSE)
#include <time.h>
#elif defined(HG_UTIL_HAS_SYSTIME_H)
#include <sys/time.h>
#endif
#include <stdlib.h>
typedef pthread_cond_t hg_thread_cond_t;
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* Initialize the condition.
*
* \param cond [IN/OUT] pointer to condition object
*
* \return Non-negative on success or negative on failure
*/
HG_UTIL_PUBLIC int hg_thread_cond_init(hg_thread_cond_t *cond);
/**
* Destroy the condition.
*
* \param cond [IN/OUT] pointer to condition object
*
* \return Non-negative on success or negative on failure
*/
HG_UTIL_PUBLIC int hg_thread_cond_destroy(hg_thread_cond_t *cond);
/**
* Wake one thread waiting for the condition to change.
*
* \param cond [IN/OUT] pointer to condition object
*
* \return Non-negative on success or negative on failure
*/
static HG_UTIL_INLINE int hg_thread_cond_signal(hg_thread_cond_t *cond);
/**
* Wake all the threads waiting for the condition to change.
*
* \param cond [IN/OUT] pointer to condition object
*
* \return Non-negative on success or negative on failure
*/
static HG_UTIL_INLINE int hg_thread_cond_broadcast(hg_thread_cond_t *cond);
/**
* Wait for the condition to change.
*
* \param cond [IN/OUT] pointer to condition object
* \param mutex [IN/OUT] pointer to mutex object
*
* \return Non-negative on success or negative on failure
*/
static HG_UTIL_INLINE int hg_thread_cond_wait(hg_thread_cond_t *cond, hg_thread_mutex_t *mutex);
/**
* Wait timeout ms for the condition to change.
*
* \param cond [IN/OUT] pointer to condition object
* \param mutex [IN/OUT] pointer to mutex object
* \param timeout [IN] timeout (in milliseconds)
*
* \return Non-negative on success or negative on failure
*/
static HG_UTIL_INLINE int hg_thread_cond_timedwait(hg_thread_cond_t *cond, hg_thread_mutex_t *mutex,
unsigned int timeout);
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_thread_cond_signal(hg_thread_cond_t *cond)
{
#ifdef _WIN32
WakeConditionVariable(cond);
#else
if (pthread_cond_signal(cond))
return HG_UTIL_FAIL;
#endif
return HG_UTIL_SUCCESS;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_thread_cond_broadcast(hg_thread_cond_t *cond)
{
#ifdef _WIN32
WakeAllConditionVariable(cond);
#else
if (pthread_cond_broadcast(cond))
return HG_UTIL_FAIL;
#endif
return HG_UTIL_SUCCESS;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_thread_cond_wait(hg_thread_cond_t *cond, hg_thread_mutex_t *mutex)
{
#ifdef _WIN32
if (!SleepConditionVariableCS(cond, mutex, INFINITE))
return HG_UTIL_FAIL;
#else
if (pthread_cond_wait(cond, mutex))
return HG_UTIL_FAIL;
#endif
return HG_UTIL_SUCCESS;
}
/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_thread_cond_timedwait(hg_thread_cond_t *cond, hg_thread_mutex_t *mutex, unsigned int timeout)
{
#ifdef _WIN32
if (!SleepConditionVariableCS(cond, mutex, timeout))
return HG_UTIL_FAIL;
#else
#if defined(HG_UTIL_HAS_PTHREAD_CONDATTR_SETCLOCK) && defined(HG_UTIL_HAS_CLOCK_MONOTONIC_COARSE)
struct timespec now;
#else
struct timeval now;
#endif
struct timespec abs_timeout;
ldiv_t ld;
/* Need to convert timeout (ms) to absolute time */
#if defined(HG_UTIL_HAS_PTHREAD_CONDATTR_SETCLOCK) && defined(HG_UTIL_HAS_CLOCK_MONOTONIC_COARSE)
clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
/* Get sec / nsec */
ld = ldiv(now.tv_nsec + timeout * 1000000L, 1000000000L);
abs_timeout.tv_nsec = ld.rem;
#elif defined(HG_UTIL_HAS_SYSTIME_H)
gettimeofday(&now, NULL);
/* Get sec / usec */
ld = ldiv(now.tv_usec + timeout * 1000L, 1000000L);
abs_timeout.tv_nsec = ld.rem * 1000L;
#endif
abs_timeout.tv_sec = now.tv_sec + ld.quot;
if (pthread_cond_timedwait(cond, mutex, &abs_timeout))
return HG_UTIL_FAIL;
#endif
return HG_UTIL_SUCCESS;
}
#ifdef __cplusplus
}
#endif
#endif /* MERCURY_THREAD_CONDITION_H */
|