Skip to content

Commit

Permalink
compat UPDATE rwlock timed functions
Browse files Browse the repository at this point in the history
Refs #441
  • Loading branch information
michalvasko committed Oct 17, 2023
1 parent 1c2d265 commit 62ba134
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeModules/UseCompat.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ macro(USE_COMPAT)

check_symbol_exists(pthread_mutex_timedlock "pthread.h" HAVE_PTHREAD_MUTEX_TIMEDLOCK)
check_symbol_exists(pthread_mutex_clocklock "pthread.h" HAVE_PTHREAD_MUTEX_CLOCKLOCK)
check_symbol_exists(pthread_rwlock_timedrdlock "pthread.h" HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK)
check_symbol_exists(pthread_rwlock_clockrdlock "pthread.h" HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK)
check_symbol_exists(pthread_rwlock_timedwrlock "pthread.h" HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK)
check_symbol_exists(pthread_rwlock_clockwrlock "pthread.h" HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK)
check_symbol_exists(pthread_cond_clockwait "pthread.h" HAVE_PTHREAD_COND_CLOCKWAIT)
if(HAVE_PTHREAD_MUTEX_CLOCKLOCK)
Expand Down
80 changes: 80 additions & 0 deletions compat/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,46 @@ pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct

#endif

#ifndef HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK
int
pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)
{
int64_t nsec_diff;
struct timespec cur, dur;
int rc;

/* try to acquire the lock and, if we fail, sleep for 5ms. */
while ((rc = pthread_rwlock_tryrdlock(rwlock)) == EBUSY) {
/* get time */
clock_gettime(COMPAT_CLOCK_ID, &cur);

/* get time diff */
nsec_diff = 0;
nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);

if (nsec_diff <= 0) {
/* timeout */
rc = ETIMEDOUT;
break;
} else if (nsec_diff < 5000000) {
/* sleep until timeout */
dur.tv_sec = 0;
dur.tv_nsec = nsec_diff;
} else {
/* sleep 5 ms */
dur.tv_sec = 0;
dur.tv_nsec = 5000000;
}

nanosleep(&dur, NULL);
}

return rc;
}

#endif

#ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
int
pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime)
Expand All @@ -96,6 +136,46 @@ pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const st

#endif

#ifndef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
int
pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)
{
int64_t nsec_diff;
struct timespec cur, dur;
int rc;

/* try to acquire the lock and, if we fail, sleep for 5ms. */
while ((rc = pthread_rwlock_trywrlock(rwlock)) == EBUSY) {
/* get time */
clock_gettime(COMPAT_CLOCK_ID, &cur);

/* get time diff */
nsec_diff = 0;
nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);

if (nsec_diff <= 0) {
/* timeout */
rc = ETIMEDOUT;
break;
} else if (nsec_diff < 5000000) {
/* sleep until timeout */
dur.tv_sec = 0;
dur.tv_nsec = nsec_diff;
} else {
/* sleep 5 ms */
dur.tv_sec = 0;
dur.tv_nsec = 5000000;
}

nanosleep(&dur, NULL);
}

return rc;
}

#endif

#ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
int
pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime)
Expand Down
2 changes: 2 additions & 0 deletions compat/compat.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
#define COMPAT_CLOCK_ID @COMPAT_CLOCK_ID@
#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
#cmakedefine HAVE_PTHREAD_MUTEX_CLOCKLOCK
#cmakedefine HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK
#cmakedefine HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
#cmakedefine HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
#cmakedefine HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
#cmakedefine HAVE_PTHREAD_COND_CLOCKWAIT

Expand Down

0 comments on commit 62ba134

Please sign in to comment.