This is the mail archive of the cygwin-cvs@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[newlib-cygwin] cygwin: Implement pthread_mutex_timedlock


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=37738448a024f1a2702ff5971dd7bda981d45923

commit 37738448a024f1a2702ff5971dd7bda981d45923
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Thu Aug 3 19:13:21 2017 +0200

    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 <corinna@vinschen.de>

Diff:
---
 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(-)

diff --git a/winsup/cygwin/common.din b/winsup/cygwin/common.din
index 73e6768..9c8da37 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 ce548b1..c258dc1 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 47ee6bd..9e8eb6f 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 c604853..c06c077 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
@@ -3285,6 +3285,34 @@ pthread_mutex_lock (pthread_mutex_t *mutex)
 }
 
 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)
 {
   if (pthread_mutex::is_initializer (mutex))
diff --git a/winsup/cygwin/thread.h b/winsup/cygwin/thread.h
index 9bb8618..88586ac 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 ();


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]