From 37738448a024f1a2702ff5971dd7bda981d45923 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Thu, 3 Aug 2017 19:13:21 +0200 Subject: cygwin: Implement pthread_mutex_timedlock - pthread_mutex::lock now takes a PLARGE_INTEGER timeout pointer and uses that in the call to cygwait. Signed-off-by: Corinna Vinschen --- winsup/cygwin/common.din | 1 + winsup/cygwin/include/cygwin/version.h | 3 ++- winsup/cygwin/include/pthread.h | 1 + winsup/cygwin/thread.cc | 32 ++++++++++++++++++++++++++++++-- winsup/cygwin/thread.h | 2 +- 5 files changed, 35 insertions(+), 4 deletions(-) (limited to 'winsup') diff --git a/winsup/cygwin/common.din b/winsup/cygwin/common.din index 73e676841..9c8da379d 100644 --- a/winsup/cygwin/common.din +++ b/winsup/cygwin/common.din @@ -1068,6 +1068,7 @@ pthread_mutex_getprioceiling SIGFE pthread_mutex_init SIGFE pthread_mutex_lock SIGFE pthread_mutex_setprioceiling SIGFE +pthread_mutex_timedlock SIGFE pthread_mutex_trylock SIGFE pthread_mutex_unlock SIGFE pthread_mutexattr_destroy SIGFE diff --git a/winsup/cygwin/include/cygwin/version.h b/winsup/cygwin/include/cygwin/version.h index ce548b13a..c258dc195 100644 --- a/winsup/cygwin/include/cygwin/version.h +++ b/winsup/cygwin/include/cygwin/version.h @@ -479,12 +479,13 @@ details. */ 312: Export strverscmp, versionsort. 313: Export fls, flsl, flsll. 314: Export explicit_bzero. + 315: Export pthread_mutex_timedlock. Note that we forgot to bump the api for ualarm, strtoll, strtoull, sigaltstack, sethostname. */ #define CYGWIN_VERSION_API_MAJOR 0 -#define CYGWIN_VERSION_API_MINOR 314 +#define CYGWIN_VERSION_API_MINOR 315 /* There is also a compatibity version number associated with the shared memory regions. It is incremented when incompatible changes are made to the shared diff --git a/winsup/cygwin/include/pthread.h b/winsup/cygwin/include/pthread.h index 47ee6bd65..9e8eb6f2b 100644 --- a/winsup/cygwin/include/pthread.h +++ b/winsup/cygwin/include/pthread.h @@ -163,6 +163,7 @@ int pthread_mutex_getprioceiling (const pthread_mutex_t *, int *); int pthread_mutex_init (pthread_mutex_t *, const pthread_mutexattr_t *); int pthread_mutex_lock (pthread_mutex_t *); int pthread_mutex_setprioceiling (pthread_mutex_t *, int, int *); +int pthread_mutex_timedlock (pthread_mutex_t *, const struct timespec *); int pthread_mutex_trylock (pthread_mutex_t *); int pthread_mutex_unlock (pthread_mutex_t *); int pthread_mutexattr_destroy (pthread_mutexattr_t *); diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc index c6048534b..c06c077c8 100644 --- a/winsup/cygwin/thread.cc +++ b/winsup/cygwin/thread.cc @@ -1765,7 +1765,7 @@ pthread_mutex::~pthread_mutex () } int -pthread_mutex::lock () +pthread_mutex::lock (PLARGE_INTEGER timeout) { pthread_t self = ::pthread_self (); int result = 0; @@ -1775,7 +1775,7 @@ pthread_mutex::lock () else if (type == PTHREAD_MUTEX_NORMAL /* potentially causes deadlock */ || !pthread::equal (owner, self)) { - cygwait (win32_obj_id, cw_infinite, cw_sig | cw_sig_restart); + cygwait (win32_obj_id, timeout, cw_sig | cw_sig_restart); set_owner (self); } else @@ -3284,6 +3284,34 @@ pthread_mutex_lock (pthread_mutex_t *mutex) return (*mutex)->lock (); } +extern "C" int +pthread_mutex_timedlock (pthread_mutex_t *mutex, const struct timespec *abstime) +{ + LARGE_INTEGER timeout; + + if (pthread_mutex::is_initializer (mutex)) + pthread_mutex::init (mutex, NULL, *mutex); + if (!pthread_mutex::is_good_object (mutex)) + return EINVAL; + + /* According to SUSv3, abstime need not be checked for validity, + if the mutex can be locked immediately. */ + if (!(*mutex)->trylock ()) + return 0; + + __try + { + int err = pthread_convert_abstime (CLOCK_REALTIME, abstime, &timeout); + if (err) + return err; + + return (*mutex)->lock (&timeout); + } + __except (NO_ERROR) {} + __endtry + return EINVAL; +} + extern "C" int pthread_mutex_trylock (pthread_mutex_t *mutex) { diff --git a/winsup/cygwin/thread.h b/winsup/cygwin/thread.h index 9bb861824..88586ac4e 100644 --- a/winsup/cygwin/thread.h +++ b/winsup/cygwin/thread.h @@ -267,7 +267,7 @@ public: static bool is_initializer_or_object (pthread_mutex_t const *); static bool is_initializer_or_bad_object (pthread_mutex_t const *); - int lock (); + int lock (PLARGE_INTEGER timeout = NULL); int trylock (); int unlock (); int destroy (); -- cgit v1.2.3