19 #include <ndb_global.h>
21 #include <NdbCondition.h>
26 #ifdef HAVE_CLOCK_GETTIME
27 static int clock_id = CLOCK_REALTIME;
31 NdbCondition_initialize(
int need_monotonic)
33 #if defined HAVE_CLOCK_GETTIME && defined HAVE_PTHREAD_CONDATTR_SETCLOCK && \
34 defined CLOCK_MONOTONIC
36 int res, condattr_init = 0;
38 pthread_condattr_t attr;
45 if ((res = pthread_condattr_init(&attr)) != 0)
50 if ((res = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)) != 0)
53 if ((res = pthread_cond_init(&tmp, &attr)) != 0)
56 pthread_condattr_destroy(&attr);
57 pthread_cond_destroy(&tmp);
59 clock_id = CLOCK_MONOTONIC;
66 pthread_condattr_destroy(&attr);
69 clock_id = CLOCK_REALTIME;
71 "Failed to use CLOCK_MONOTONIC for pthread_condition res: %u\n",
87 #if defined HAVE_CLOCK_GETTIME && defined HAVE_PTHREAD_CONDATTR_SETCLOCK && \
88 defined CLOCK_MONOTONIC
89 if (clock_id == CLOCK_MONOTONIC)
91 pthread_condattr_t attr;
92 pthread_condattr_init(&attr);
93 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
94 result = pthread_cond_init(&ndb_cond->cond, &attr);
95 pthread_condattr_destroy(&attr);
99 result = pthread_cond_init(&ndb_cond->cond, NULL);
102 result = pthread_cond_init(&ndb_cond->cond, NULL);
110 NdbCondition_Create(
void)
119 (void)NdbCondition_Init(tmpCond);
129 if (p_cond == NULL || p_mutex == NULL)
132 #ifdef NDB_MUTEX_STAT
133 result = pthread_cond_wait(&p_cond->cond, &p_mutex->mutex);
135 result = pthread_cond_wait(&p_cond->cond, p_mutex);
148 NdbCondition_ComputeAbsTime(&abstime, msecs);
149 return NdbCondition_WaitTimeoutAbs(p_cond, p_mutex, &abstime);
153 NdbCondition_ComputeAbsTime(
struct timespec * abstime,
unsigned msecs)
157 #ifdef HAVE_CLOCK_GETTIME
158 clock_gettime(clock_id, abstime);
162 gettimeofday(&tick_time, 0);
163 abstime->tv_sec = tick_time.tv_sec;
164 abstime->tv_nsec = tick_time.tv_usec * 1000;
170 msecs = msecs % 1000;
173 abstime->tv_sec += secs;
174 abstime->tv_nsec += msecs * 1000000;
175 if (abstime->tv_nsec >= 1000000000) {
176 abstime->tv_sec += 1;
177 abstime->tv_nsec -= 1000000000;
180 set_timespec_nsec(*abstime,msecs*1000000ULL);
185 NdbCondition_WaitTimeoutAbs(
struct NdbCondition* p_cond,
194 if (p_cond == NULL || p_mutex == NULL)
197 #ifdef NDB_MUTEX_STAT
198 return pthread_cond_timedwait(&p_cond->cond, &p_mutex->mutex, abstime);
200 return pthread_cond_timedwait(&p_cond->cond, p_mutex, abstime);
211 result = pthread_cond_signal(&p_cond->cond);
224 result = pthread_cond_broadcast(&p_cond->cond);
237 result = pthread_cond_destroy(&p_cond->cond);