Hi,

While working on enabling upnp/natpmp support into net/mldonkey,
I've found we miss pthread_mutex_timedlock().

For now I've the diff below, which is ripped/adapted from xine-lib,
and it seems to do the trick...

Would it be useful to add it to libpthread?

Reference is at:
http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_timedlock.html

cheers!
David

--- cut here ---

$OpenBSD$

Work around missing pthread_mutex_timedlock()

--- src/utils/net/upnp_stubs.c.orig     Mon Nov  1 18:19:23 2010
+++ src/utils/net/upnp_stubs.c  Mon Dec 12 14:22:58 2011
@@ -168,7 +168,34 @@ static pthread_t g_pthread;
 static pthread_mutex_t g_mutex;
 static pthread_mutex_t g_delay_mutex;
 
+#ifdef __OpenBSD__
+int
+pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec 
*abs_timeout)
+{
+       int pthread_rc;
+       struct timespec remaining, slept, ts;
 
+       remaining = *abs_timeout;
+       while ((pthread_rc = pthread_mutex_trylock(mutex)) == EBUSY) {
+               ts.tv_sec  = 0;
+               ts.tv_nsec = (remaining.tv_sec > 0 ? 10000000 : 
MIN(remaining.tv_nsec, 10000000));
+               nanosleep(&ts, &slept);
+               ts.tv_nsec -= slept.tv_nsec;
+               if (ts.tv_nsec <= remaining.tv_nsec) {
+                       remaining.tv_nsec -= ts.tv_nsec;
+               } else {
+                       remaining.tv_sec--;
+                       remaining.tv_nsec = (1000000 - (ts.tv_nsec - 
remaining.tv_nsec));
+               }
+               if (remaining.tv_sec < 0 || (!remaining.tv_sec && 
remaining.tv_nsec <= 0)) {
+                       return ETIMEDOUT;
+               }
+       }
+
+       return pthread_rc;
+}
+#endif /* __OpenBSD__ */
+
 /** @brief init memset 0 g_maps g_unmaps */
 static void init_maps()
 {

Reply via email to