Skip to content

Commit fbd70af

Browse files
committed
Do not try to use pthread_cond_timedwait_relative_np on newer Android
API was deprecated, do not use (still available on Apple). Rename USE_CLOCK_IN_COND to USE_CLOCK_GETTIME_IN_COND to make more clear. Put check for pthread_cond_timedwait_relative_np in a single place defining a new USE_COND_TIMEDWAIT_RELATIVE if we can use it. Declare tv variable near gettimeofday to reduce conditional compilation. Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
1 parent b2b9614 commit fbd70af

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dnl ------------------------------------------------------------
1111
# ------------------------------------------------------------
1212
# Initialization
1313
# ------------------------------------------------------------
14-
AC_INIT(FreeTDS, 1.4.9)
14+
AC_INIT(FreeTDS, 1.4.10)
1515
AC_CONFIG_SRCDIR(src/dblib/dblib.c)
1616
AC_PREREQ(2.53)
1717

src/utils/tds_cond.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,23 @@ int (*tds_raw_cond_timedwait) (tds_condition * cond, tds_raw_mutex * mtx, int ti
168168
#include <freetds/thread.h>
169169
#include <freetds/time.h>
170170

171+
/* check if we can use pthread_cond_timedwait_relative_np */
172+
#undef USE_COND_TIMEDWAIT_RELATIVE
173+
#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP) && \
174+
(!defined(__ANDROID__) || ((__ANDROID_API__ < 21) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE)))
175+
#define USE_COND_TIMEDWAIT_RELATIVE 1
176+
#endif
177+
171178
/* check if we can use clock_gettime */
172-
#undef USE_CLOCK_IN_COND
173-
#if !defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP) && \
179+
#undef USE_CLOCK_GETTIME_IN_COND
180+
#if !defined(USE_COND_TIMEDWAIT_RELATIVE) && \
174181
defined(HAVE_CLOCK_GETTIME) && (defined(CLOCK_REALTIME) || defined(CLOCK_MONOTONIC))
175-
#define USE_CLOCK_IN_COND 1
182+
#define USE_CLOCK_GETTIME_IN_COND 1
176183
#endif
177184

178185
/* check if we can use CLOCK_MONOTONIC for conditions */
179186
#undef USE_MONOTONIC_CLOCK_IN_COND
180-
#if defined(USE_CLOCK_IN_COND) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(CLOCK_MONOTONIC)
187+
#if defined(USE_CLOCK_GETTIME_IN_COND) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(CLOCK_MONOTONIC)
181188
#define USE_MONOTONIC_CLOCK_IN_COND 1
182189
#endif
183190

@@ -201,29 +208,27 @@ int tds_raw_cond_init(tds_condition *cond)
201208
int tds_raw_cond_timedwait(tds_condition *cond, tds_raw_mutex *mtx, int timeout_sec)
202209
{
203210
struct timespec ts;
204-
#if !defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP) && !defined(USE_CLOCK_IN_COND)
205-
struct timeval tv;
206-
#endif
207211

208212
if (timeout_sec <= 0)
209213
return tds_raw_cond_wait(cond, mtx);
210214

211-
#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP)
215+
#if defined(USE_COND_TIMEDWAIT_RELATIVE)
212216
ts.tv_sec = timeout_sec;
213217
ts.tv_nsec = 0;
214218
return pthread_cond_timedwait_relative_np(cond, mtx, &ts);
215219
#else
216220

217-
# ifdef USE_CLOCK_IN_COND
218-
# if defined(USE_MONOTONIC_CLOCK_IN_COND)
221+
# if defined(USE_MONOTONIC_CLOCK_IN_COND)
219222
clock_gettime(CLOCK_MONOTONIC, &ts);
220-
# else
223+
# elif defined(USE_CLOCK_GETTIME_IN_COND)
221224
clock_gettime(CLOCK_REALTIME, &ts);
222-
# endif
223225
# elif defined(HAVE_GETTIMEOFDAY)
224-
gettimeofday(&tv, NULL);
225-
ts.tv_sec = tv.tv_sec;
226-
ts.tv_nsec = tv.tv_usec * 1000u;
226+
do {
227+
struct timeval tv;
228+
gettimeofday(&tv, NULL);
229+
ts.tv_sec = tv.tv_sec;
230+
ts.tv_nsec = tv.tv_usec * 1000u;
231+
} while(0);
227232
# else
228233
# error No way to get a proper time!
229234
# endif

0 commit comments

Comments
 (0)