[PATCH] pthread_mutex_timedlock

Marijn Ros <[email protected]>
Newsgroups gmane.linux.ngpt.devel
Message-ID <[email protected]>
Mono wants to have a function pthread_mutex_timedlock (or else it
emulates it with pthread_mutex_trylock and sleep), that, according to
the linuxthreads pthread.h is defined in XOpen2K. The standard
probably contains lots of other interesting functions
(rwlock_timedwait for example), but this happened to be the one I
needed.

I took the liberty to make a patch to include this function in
NGPT. To keep in style with the patches the NGPT-team distributes for
the linux-kernel, the patch also contains totally unrelated hacks to
pth_p.h.in to stop the preprocessor-warnings about double
declarations :)

Bye,
        Marijn

PS: libgc is moved to at least the weekend. I hope that won't be to
late to make changes to NGPT if necessary, before 2.0.


===File ~/NGPT/ngpt-1.9.90-pthread_mutex_timedlock.patch====
diff -aur ngpt-1.9.90/pth_p.h.in ngpt-1.9.90-JMR/pth_p.h.in
--- ngpt-1.9.90/pth_p.h.in	Fri Jun 21 18:55:18 2002
+++ ngpt-1.9.90-JMR/pth_p.h.in	Tue Jun 25 22:26:43 2002
@@ -171,8 +171,20 @@
 #ifdef NATIVE_SELF
 #define current_tid()		    \
     (pth_shutdown_inprogress == TRUE) ? gettid() : NATIVE_SELF->tid
+
+/* keep the pre-processor from whining */
+#ifdef pth_get_native_descr
+#undef pth_get_native_descr
+#endif
+
 #define pth_get_native_descr()      \
     ((pth_descr_t)(pth_shutdown_inprogress ? NULL : NATIVE_SELF))
+
+/* keep the pre-processor from whining */
+#ifdef pth_get_current
+#undef pth_get_current
+#endif
+
 #define pth_get_current()           \
     ((pth_t)(pth_shutdown_inprogress ?  NULL : NATIVE_SELF->current))
 #else
diff -aur ngpt-1.9.90/pthread.c ngpt-1.9.90-JMR/pthread.c
--- ngpt-1.9.90/pthread.c	Fri Jun 21 19:18:32 2002
+++ ngpt-1.9.90-JMR/pthread.c	Tue Jun 25 22:19:04 2002
@@ -1379,6 +1379,45 @@
 }
 
 /*
+**  XOPEN2K
+*/
+
+int pthread_mutex_timedlock(pthread_mutex_t *mutex,
+                            const struct timespec *abstime)
+{
+    pth_event_t ev;
+    static pth_key_t ev_key = PTH_KEY_INIT;
+    int rc;
+
+    if (mutex == NULL || abstime == NULL)
+        return EINVAL;
+#ifdef __amigaos__
+    if (abstime->ts_sec < 0 || abstime->ts_nsec < 0 || abstime->ts_nsec >= 1000000000)
+#else
+    if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
+#endif
+        return EINVAL;
+    if (*mutex == PTHREAD_MUTEX_INITIALIZER || *mutex == LT_MUTEX_INITIALIZER)
+        if (pthread_mutex_init(mutex, NULL) != OK)
+            return EINVAL;
+    ev = pth_event(PTH_EVENT_TIME|PTH_MODE_STATIC, &ev_key,
+#ifdef __amigaos__
+                   pth_time(abstime->ts_sec, (abstime->ts_nsec)/1000)
+#else
+                   pth_time(abstime->tv_sec, (abstime->tv_nsec)/1000)
+#endif
+    );
+    if (((pth_mutex_t *)*mutex)->mx_state & PTH_MUTEX_INTERNAL_LOCKED) {
+	_pth_acquire_lock(&(((pth_mutex_t *)*mutex)->mx_lock), 0);
+	return OK;
+    }
+    rc = pth_mutex_acquire((pth_mutex_t *)(*mutex), FALSE, ev);
+    if (pth_event_occurred(ev))
+        return ETIMEDOUT;
+    return rc;
+}
+
+/*
 **  THREAD-SAFE REPLACEMENT FUNCTIONS
 */
 
diff -aur ngpt-1.9.90/pthread.h.in ngpt-1.9.90-JMR/pthread.h.in
--- ngpt-1.9.90/pthread.h.in	Fri Jun 21 19:15:32 2002
+++ ngpt-1.9.90-JMR/pthread.h.in	Tue Jun 25 22:09:48 2002
@@ -557,6 +557,11 @@
 extern int       pthread_abort __P((pthread_t));
 
 /*
+ * Extentions created by XOpen2K
+ */
+extern int       pthread_mutex_timedlock __P((pthread_mutex_t *, const struct timespec *));
+
+/*
  * Optionally fake poll(2) data structure and options
  */
 #if !(@PTH_FAKE_POLL@)
============================================================
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.