Skip to content

Commit

Permalink
Do not try to use pthread_cond_timedwait_relative_np on newer Android
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
freddy77 committed Dec 4, 2023
1 parent b2b9614 commit fbd70af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dnl ------------------------------------------------------------
# ------------------------------------------------------------
# Initialization
# ------------------------------------------------------------
AC_INIT(FreeTDS, 1.4.9)
AC_INIT(FreeTDS, 1.4.10)
AC_CONFIG_SRCDIR(src/dblib/dblib.c)
AC_PREREQ(2.53)

Expand Down
35 changes: 20 additions & 15 deletions src/utils/tds_cond.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,23 @@ int (*tds_raw_cond_timedwait) (tds_condition * cond, tds_raw_mutex * mtx, int ti
#include <freetds/thread.h>
#include <freetds/time.h>

/* check if we can use pthread_cond_timedwait_relative_np */
#undef USE_COND_TIMEDWAIT_RELATIVE
#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP) && \
(!defined(__ANDROID__) || ((__ANDROID_API__ < 21) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE)))
#define USE_COND_TIMEDWAIT_RELATIVE 1
#endif

/* check if we can use clock_gettime */
#undef USE_CLOCK_IN_COND
#if !defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP) && \
#undef USE_CLOCK_GETTIME_IN_COND
#if !defined(USE_COND_TIMEDWAIT_RELATIVE) && \
defined(HAVE_CLOCK_GETTIME) && (defined(CLOCK_REALTIME) || defined(CLOCK_MONOTONIC))
#define USE_CLOCK_IN_COND 1
#define USE_CLOCK_GETTIME_IN_COND 1
#endif

/* check if we can use CLOCK_MONOTONIC for conditions */
#undef USE_MONOTONIC_CLOCK_IN_COND
#if defined(USE_CLOCK_IN_COND) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(CLOCK_MONOTONIC)
#if defined(USE_CLOCK_GETTIME_IN_COND) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(CLOCK_MONOTONIC)
#define USE_MONOTONIC_CLOCK_IN_COND 1
#endif

Expand All @@ -201,29 +208,27 @@ int tds_raw_cond_init(tds_condition *cond)
int tds_raw_cond_timedwait(tds_condition *cond, tds_raw_mutex *mtx, int timeout_sec)
{
struct timespec ts;
#if !defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP) && !defined(USE_CLOCK_IN_COND)
struct timeval tv;
#endif

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

#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP)
#if defined(USE_COND_TIMEDWAIT_RELATIVE)
ts.tv_sec = timeout_sec;
ts.tv_nsec = 0;
return pthread_cond_timedwait_relative_np(cond, mtx, &ts);
#else

# ifdef USE_CLOCK_IN_COND
# if defined(USE_MONOTONIC_CLOCK_IN_COND)
# if defined(USE_MONOTONIC_CLOCK_IN_COND)
clock_gettime(CLOCK_MONOTONIC, &ts);
# else
# elif defined(USE_CLOCK_GETTIME_IN_COND)
clock_gettime(CLOCK_REALTIME, &ts);
# endif
# elif defined(HAVE_GETTIMEOFDAY)
gettimeofday(&tv, NULL);
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000u;
do {
struct timeval tv;
gettimeofday(&tv, NULL);
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000u;
} while(0);
# else
# error No way to get a proper time!
# endif
Expand Down

0 comments on commit fbd70af

Please sign in to comment.