apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Darroch <chr...@pearsoncmg.com>
Subject [PATCH] trial implementation of proc_pthread trylock
Date Thu, 22 Aug 2002 19:59:24 GMT
Hi --

    OK, pursuant to my previous email, here's a stab at an implementation
of proc_mutex_proc_pthread_tryacquire().  Mostly I studied
apr_thread_mutex_trylock() and proc_mutex_proc_pthread_acquire()
to see what they were doing, plus read the man page a bit
on what pthread_mutex_consistent_np() did.

    With luck, the logic should be:

- Check for non-zero return value.
- If return value is EOWNERDEAD and we're trying to handle consistency
   ourselves, call pthread_mutex_consistent_np() and set return
   value to zero.
- If return value is EBUSY, make it APR_EBUSY.
- If return value is zero (APR_SUCCESS), set our lock flag.
- Return.

    The proc_mutex_proc_pthread_acquire() function seems to
suck up the EOWNERDEAD return value, setting the curr_locked flag
to 1 and returning APR_SUCCESS, so that's what I made the tryacquire
case do also.

    However, it seems to me that this behaviour, for both functions,
might not be too helpful for callers, since any data they
are protecting with our lock might be in an inconsistent state,
but they'd have no way of determining that from our return value.
Maybe we should invent an APR_EOWNERDEAD value and return that?

Chris.

--- proc_mutex.c.orig	2002-08-22 14:04:12.259144000 -0400
+++ proc_mutex.c	2002-08-22 15:51:46.438241000 -0400
@@ -138,6 +138,11 @@
      return APR_SUCCESS;
  }

+static apr_status_t proc_mutex_posix_tryacquire(apr_proc_mutex_t *mutex)
+{
+    return APR_ENOTIMPL;
+}
+
  static apr_status_t proc_mutex_posix_release(apr_proc_mutex_t *mutex)
  {
      int rc;
@@ -176,7 +181,7 @@
  #endif
      proc_mutex_posix_create,
      proc_mutex_posix_acquire,
-    NULL, /* no tryacquire */
+    proc_mutex_posix_tryacquire,
      proc_mutex_posix_release,
      proc_mutex_posix_destroy,
      proc_mutex_posix_child_init,
@@ -253,6 +258,11 @@
      return APR_SUCCESS;
  }

+static apr_status_t proc_mutex_sysv_tryacquire(apr_proc_mutex_t *mutex)
+{
+    return APR_ENOTIMPL;
+}
+
  static apr_status_t proc_mutex_sysv_release(apr_proc_mutex_t *mutex)
  {
      int rc;
@@ -292,7 +302,7 @@
  #endif
      proc_mutex_sysv_create,
      proc_mutex_sysv_acquire,
-    NULL, /* no tryacquire */
+    proc_mutex_sysv_tryacquire,
      proc_mutex_sysv_release,
      proc_mutex_sysv_destroy,
      proc_mutex_sysv_child_init,
@@ -427,7 +437,30 @@
      return APR_SUCCESS;
  }

-/* TODO: Add proc_mutex_proc_pthread_tryacquire(apr_proc_mutex_t *mutex) */
+static apr_status_t proc_mutex_proc_pthread_tryacquire(apr_proc_mutex_t 
*mutex)
+{
+    apr_status_t rv;
+
+    if ((rv = pthread_mutex_trylock(mutex->pthread_interproc))) {
+#ifdef PTHREAD_SETS_ERRNO
+        rv = errno;
+#endif
+#ifdef HAVE_PTHREAD_MUTEXATTR_SETROBUST_NP
+        /* Okay, our owner died.  Let's try to make it consistent again. */
+        if (rv == EOWNERDEAD) {
+            pthread_mutex_consistent_np(mutex->pthread_interproc);
+            rv = APR_SUCCESS;
+        }
+#endif
+        if (rv == EBUSY)
+            rv = APR_EBUSY;
+    }
+
+    if (rv == APR_SUCCESS)
+        mutex->curr_locked = 1;
+
+    return rv;
+}

  static apr_status_t proc_mutex_proc_pthread_release(apr_proc_mutex_t 
*mutex)
  {
@@ -467,7 +500,7 @@
      APR_PROCESS_LOCK_MECH_IS_GLOBAL,
      proc_mutex_proc_pthread_create,
      proc_mutex_proc_pthread_acquire,
-    NULL, /* no tryacquire */
+    proc_mutex_proc_pthread_tryacquire,
      proc_mutex_proc_pthread_release,
      proc_mutex_proc_pthread_destroy,
      proc_mutex_proc_pthread_child_init,
@@ -563,6 +596,11 @@
      return APR_SUCCESS;
  }

+static apr_status_t proc_mutex_fcntl_tryacquire(apr_proc_mutex_t *mutex)
+{
+    return APR_ENOTIMPL;
+}
+
  static apr_status_t proc_mutex_fcntl_release(apr_proc_mutex_t *mutex)
  {
      int rc;
@@ -603,7 +641,7 @@
  #endif
      proc_mutex_fcntl_create,
      proc_mutex_fcntl_acquire,
-    NULL, /* no tryacquire */
+    proc_mutex_fcntl_tryacquire,
      proc_mutex_fcntl_release,
      proc_mutex_fcntl_destroy,
      proc_mutex_fcntl_child_init,
@@ -678,6 +716,11 @@
      return APR_SUCCESS;
  }

+static apr_status_t proc_mutex_flock_tryacquire(apr_proc_mutex_t *mutex)
+{
+    return APR_ENOTIMPL;
+}
+
  static apr_status_t proc_mutex_flock_release(apr_proc_mutex_t *mutex)
  {
      int rc;
@@ -733,7 +776,7 @@
  #endif
      proc_mutex_flock_create,
      proc_mutex_flock_acquire,
-    NULL, /* no tryacquire */
+    proc_mutex_flock_tryacquire,
      proc_mutex_flock_release,
      proc_mutex_flock_destroy,
      proc_mutex_flock_child_init,


Mime
View raw message