apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Trawick <traw...@attglobal.net>
Subject [PATCH] prepare for lock mechanism selection on Unix
Date Mon, 25 Jun 2001 15:05:49 GMT
Note that one or two bugs were fixed in the PROC_PTHREAD_SERIALIZE
support, but I wouldn't bet on it working...  maybe we've never
tried this with APR, or at least not in a long time?

1) bugs:

   a) APR_PROCESS_LOCK_IS_GLOBAL and the idea of selecting the
      mechanism at run-time doesn't seem safe...  need something like
      APR_FCNTL_LOCK_IS_GLOBAL to make it work correctly

   b) create_lock() makes a copy of the filename parm even if not
      needed for the desired mechanism

   c) apr_os_lock_get/apr_os_lock_put/apr_os_lock_t

      I haven't attempted to straighten out the definition of
      apr_os_lock_t.  We'll need to use separate fields for
      the "int crossproc" mechanisms vs. the "pthread_mutex_t
      *crossproc" mechanism.

   I dunno what else... I haven't tested it yet :)

2) misfeatures:

   APR_HAVE_xxx_SERIALIZE is not in apr.h

   maybe the pthread rw lock stuff needs to use the method ptrs
   too...  we could add a acquire_read and acquire_write field...
   less code specific to rw lock would then be needed

3) minimal further work needed to allow lock mechanism selection:

   add appropriate parm to apr_lock_create(), replace my logic in
   locks.c:create_lock() with logic to look at parameter and
   APR_HAVE_* defines rather than APR_USE_* defines

Any reason I shouldn't proceed with testing/committing?  This is
enough function to allow someone else to carry it further, and
meanwhile we can see what build tweaks are needed on various Unix
flavors, as this will start to compile more code into APR.

Index: include/arch/unix/locks.h
===================================================================
RCS file: /home/cvs/apr/include/arch/unix/locks.h,v
retrieving revision 1.30
diff -u -r1.30 locks.h
--- include/arch/unix/locks.h	2001/06/06 18:11:28	1.30
+++ include/arch/unix/locks.h	2001/06/25 14:47:20
@@ -100,7 +100,43 @@
 #endif
 /* End System Headers */
 
-#if !APR_HAVE_UNION_SEMUN && APR_USE_SYSVSEM_SERIALIZE
+struct apr_unix_lock_methods_t {
+    apr_status_t (*create)(apr_lock_t *);
+    apr_status_t (*acquire)(apr_lock_t *);
+    apr_status_t (*release)(apr_lock_t *);
+    apr_status_t (*destroy)(apr_lock_t *);
+    apr_status_t (*child_init)(apr_lock_t **, apr_pool_t *, const char *);
+};
+
+#if defined(HAVE_SEMCTL) && defined(HAVE_SEMGET)
+#define APR_HAVE_SYSVSEM_SERIALIZE      1
+extern const struct apr_unix_lock_methods_t apr_unix_sysv_methods;
+#else
+#define APR_HAVE_SYSVSEM_SERIALIZE      0
+#endif
+
+#if defined(HAVE_FCNTL_H) && defined(HAVE_F_SETLK)
+#define APR_HAVE_FCNTL_SERIALIZE        1
+extern const struct apr_unix_lock_methods_t apr_unix_fcntl_methods;
+#else
+#define APR_HAVE_FCNTL_SERIALIZE        0
+#endif
+
+#if defined(HAVE_SYS_FILE_H) && defined(HAVE_LOCK_EX)
+#define APR_HAVE_FLOCK_SERIALIZE        1
+extern const struct apr_unix_lock_methods_t apr_unix_flock_methods;
+#else
+#define APR_HAVE_FLOCK_SERIALIZE        0
+#endif
+
+#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_PROCESS_SHARED) && defined(HAVE_PTHREAD_MUTEXATTR_SETPSHARED)
+#define APR_HAVE_PROC_PTHREAD_SERIALIZE 1
+extern const struct apr_unix_lock_methods_t apr_unix_proc_pthread_methods;
+#else
+#define APR_HAVE_PROC_PTHREAD_SERIALIZE 0
+#endif
+
+#if !APR_HAVE_UNION_SEMUN && defined(APR_HAVE_SYSVSEM_SERIALIZE)
 /* it makes no sense, but this isn't defined on solaris */
 union semun {
     long val;
@@ -111,22 +147,18 @@
 
 struct apr_lock_t {
     apr_pool_t *pool;
+    const struct apr_unix_lock_methods_t *meth;
+    const struct apr_unix_lock_methods_t *inter_meth, *intra_meth; /* for APR_LOCK_ALL */
     apr_locktype_e type;
     apr_lockscope_e scope;
     int curr_locked;
     char *fname;
-
-#if APR_USE_SYSVSEM_SERIALIZE
-    int interproc;
-#elif APR_USE_FCNTL_SERIALIZE
+#if APR_HAVE_SYSVSEM_SERIALIZE || APR_HAVE_FCNTL_SERIALIZE || APR_HAVE_FLOCK_SERIALIZE
     int interproc;
-#elif APR_USE_PROC_PTHREAD_SERIALIZE
-    pthread_mutex_t *interproc;
-#elif APR_USE_FLOCK_SERIALIZE
-    int interproc;
-#else
-    /* No Interprocess serialization.  Too bad. */
-#endif 
+#endif
+#if APR_HAVE_PROC_PTHREAD_SERIALIZE
+    pthread_mutex_t *pthread_interproc;
+#endif
 #if APR_HAS_THREADS
     /* APR doesn't have threads, no sense in having an thread lock mechanism.
      */
@@ -147,20 +179,10 @@
 };
 
 #if APR_HAS_THREADS
-apr_status_t apr_unix_create_intra_lock(struct apr_lock_t *new);
-apr_status_t apr_unix_lock_intra(struct apr_lock_t *lock);
-apr_status_t apr_unix_unlock_intra(struct apr_lock_t *lock);
-apr_status_t apr_unix_destroy_intra_lock(struct apr_lock_t *lock);
+extern const struct apr_unix_lock_methods_t apr_unix_intra_methods;
 #endif
 
 void apr_unix_setup_lock(void);
-apr_status_t apr_unix_create_inter_lock(struct apr_lock_t *new);
-apr_status_t apr_unix_lock_inter(struct apr_lock_t *lock);
-apr_status_t apr_unix_unlock_inter(struct apr_lock_t *lock);
-apr_status_t apr_unix_destroy_inter_lock(struct apr_lock_t *lock);
-
-apr_status_t apr_unix_child_init_lock(struct apr_lock_t **lock, 
-                                      apr_pool_t *cont, const char *fname);
 
 #endif  /* LOCKS_H */
 
Index: locks/unix/crossproc.c
===================================================================
RCS file: /home/cvs/apr/locks/unix/crossproc.c,v
retrieving revision 1.45
diff -u -r1.45 crossproc.c
--- locks/unix/crossproc.c	2001/06/06 18:11:55	1.45
+++ locks/unix/crossproc.c	2001/06/25 14:47:28
@@ -57,12 +57,12 @@
 #include "locks.h"
 #include "fileio.h" /* for apr_mkstemp() */
 
-#if APR_USE_SYSVSEM_SERIALIZE  
+#if APR_HAVE_SYSVSEM_SERIALIZE
 
 static struct sembuf op_on;
 static struct sembuf op_off;
 
-void apr_unix_setup_lock(void)
+static void sysv_setup(void)
 {
     op_on.sem_num = 0;
     op_on.sem_op = -1;
@@ -72,7 +72,7 @@
     op_off.sem_flg = SEM_UNDO;
 }
 
-static apr_status_t lock_cleanup(void *lock_)
+static apr_status_t sysv_cleanup(void *lock_)
 {
     apr_lock_t *lock=lock_;
     union semun ick;
@@ -84,28 +84,28 @@
     return APR_SUCCESS;
 }    
 
-apr_status_t apr_unix_create_inter_lock(apr_lock_t *new)
+static apr_status_t sysv_create(apr_lock_t *new)
 {
     union semun ick;
     
     new->interproc = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
 
     if (new->interproc < 0) {
-        lock_cleanup(new);
+        sysv_cleanup(new);
         return errno;
     }
     ick.val = 1;
     if (semctl(new->interproc, 0, SETVAL, ick) < 0) {
-        lock_cleanup(new);
+        sysv_cleanup(new);
         return errno;
     }
     new->curr_locked = 0;
-    apr_pool_cleanup_register(new->pool, (void *)new, lock_cleanup, 
+    apr_pool_cleanup_register(new->pool, (void *)new, sysv_cleanup, 
                               apr_pool_cleanup_null);
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_lock_inter(apr_lock_t *lock)
+static apr_status_t sysv_acquire(apr_lock_t *lock)
 {
     int rc;
 
@@ -119,7 +119,7 @@
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_unlock_inter(apr_lock_t *lock)
+static apr_status_t sysv_release(apr_lock_t *lock)
 {
     int rc;
 
@@ -133,48 +133,59 @@
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_destroy_inter_lock(apr_lock_t *lock)
+static apr_status_t sysv_destroy(apr_lock_t *lock)
 {
     apr_status_t stat;
 
-    if ((stat = lock_cleanup(lock)) == APR_SUCCESS) {
-        apr_pool_cleanup_kill(lock->pool, lock, lock_cleanup);
+    if ((stat = sysv_cleanup(lock)) == APR_SUCCESS) {
+        apr_pool_cleanup_kill(lock->pool, lock, sysv_cleanup);
         return APR_SUCCESS;
     }
     return stat;
 }
 
-apr_status_t apr_unix_child_init_lock(apr_lock_t **lock, apr_pool_t *cont, const char *fname)
+static apr_status_t sysv_child_init(apr_lock_t **lock, apr_pool_t *cont, const char *fname)
 {
     return APR_SUCCESS;
 }
 
-#elif (APR_USE_PROC_PTHREAD_SERIALIZE)  
+const struct apr_unix_lock_methods_t apr_unix_sysv_methods =
+{
+    sysv_create,
+    sysv_acquire,
+    sysv_release,
+    sysv_destroy,
+    sysv_child_init
+};
 
-void apr_unix_setup_lock(void)
+#endif /* SysV sem implementation */
+
+#if APR_HAVE_PROC_PTHREAD_SERIALIZE
+
+static void proc_pthread_setup(void)
 {
 }
 
-static apr_status_t lock_cleanup(void *lock_)
+static apr_status_t proc_pthread_cleanup(void *lock_)
 {
     apr_lock_t *lock=lock_;
     apr_status_t stat;
 
     if (lock->curr_locked == 1) {
-        if ((stat = pthread_mutex_unlock(lock->interproc))) {
+        if ((stat = pthread_mutex_unlock(lock->pthread_interproc))) {
 #ifdef PTHREAD_SETS_ERRNO
             stat = errno;
 #endif
             return stat;
         } 
-        if (munmap((caddr_t)lock->interproc, sizeof(pthread_mutex_t))){
+        if (munmap((caddr_t)lock->pthread_interproc, sizeof(pthread_mutex_t))){
             return errno;
         }
     }
     return APR_SUCCESS;
 }    
 
-apr_status_t apr_unix_create_inter_lock(apr_lock_t *new)
+static apr_status_t proc_pthread_create(apr_lock_t *new)
 {
     apr_status_t stat;
     int fd;
@@ -185,10 +196,10 @@
         return errno;
     }
 
-    new->interproc = (pthread_mutex_t *)mmap((caddr_t) 0, 
-                              sizeof(pthread_mutex_t), 
-                              PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 
-    if (new->interproc == (pthread_mutex_t *) (caddr_t) -1) {
+    new->pthread_interproc = (pthread_mutex_t *)mmap((caddr_t) 0, 
+                                                     sizeof(pthread_mutex_t), 
+                                                     PROT_READ | PROT_WRITE, MAP_SHARED,
fd, 0); 
+    if (new->pthread_interproc == (pthread_mutex_t *) (caddr_t) -1) {
         return errno;
     }
     close(fd);
@@ -196,22 +207,22 @@
 #ifdef PTHREAD_SETS_ERRNO
         stat = errno;
 #endif
-        lock_cleanup(new);
+        proc_pthread_cleanup(new);
         return stat;
     }
     if ((stat = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED))) {
 #ifdef PTHREAD_SETS_ERRNO
         stat = errno;
 #endif
-        lock_cleanup(new);
+        proc_pthread_cleanup(new);
         return stat;
     }
 
-    if ((stat = pthread_mutex_init(new->interproc, &mattr))) {
+    if ((stat = pthread_mutex_init(new->pthread_interproc, &mattr))) {
 #ifdef PTHREAD_SETS_ERRNO
         stat = errno;
 #endif
-        lock_cleanup(new);
+        proc_pthread_cleanup(new);
         return stat;
     }
 
@@ -219,21 +230,21 @@
 #ifdef PTHREAD_SETS_ERRNO
         stat = errno;
 #endif
-        lock_cleanup(new);
+        proc_pthread_cleanup(new);
         return stat;
     }
 
     new->curr_locked = 0;
-    apr_pool_register_cleanup(new->pool, (void *)new, lock_cleanup, 
+    apr_pool_cleanup_register(new->pool, (void *)new, proc_pthread_cleanup, 
                               apr_pool_cleanup_null);
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_lock_inter(apr_lock_t *lock)
+static apr_status_t proc_pthread_acquire(apr_lock_t *lock)
 {
     apr_status_t stat;
 
-    if ((stat = pthread_mutex_lock(lock->interproc))) {
+    if ((stat = pthread_mutex_lock(lock->pthread_interproc))) {
 #ifdef PTHREAD_SETS_ERRNO
         stat = errno;
 #endif
@@ -243,11 +254,11 @@
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_unlock_inter(apr_lock_t *lock)
+static apr_status_t proc_pthread_release(apr_lock_t *lock)
 {
     apr_status_t stat;
 
-    if ((stat = pthread_mutex_unlock(lock->interproc))) {
+    if ((stat = pthread_mutex_unlock(lock->pthread_interproc))) {
 #ifdef PTHREAD_SETS_ERRNO
         stat = errno;
 #endif
@@ -257,27 +268,40 @@
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_destroy_inter_lock(apr_lock_t *lock)
+static apr_status_t proc_pthread_destroy(apr_lock_t *lock)
 {
     apr_status_t stat;
-    if ((stat = lock_cleanup(lock)) == APR_SUCCESS) {
-        apr_pool_cleanup_kill(lock->pool, lock, lock_cleanup);
+    if ((stat = proc_pthread_cleanup(lock)) == APR_SUCCESS) {
+        apr_pool_cleanup_kill(lock->pool, lock, proc_pthread_cleanup);
         return APR_SUCCESS;
     }
     return stat;
 }
 
-apr_status_t apr_unix_child_init_lock(apr_lock_t **lock, apr_pool_t *cont, const char *fname)
+static apr_status_t proc_pthread_child_init(apr_lock_t **lock, apr_pool_t *cont, const char
*fname)
 {
     return APR_SUCCESS;
 }
+
+const struct apr_unix_lock_methods_t apr_unix_proc_pthread_methods =
+{
+    proc_pthread_create,
+    proc_pthread_acquire,
+    proc_pthread_release,
+    proc_pthread_destroy,
+    proc_pthread_child_init
+};
+
+#endif
 
-#elif (APR_USE_FCNTL_SERIALIZE)  
+#if APR_HAVE_FCNTL_SERIALIZE
 
 static struct flock lock_it;
 static struct flock unlock_it;
 
-void apr_unix_setup_lock(void)
+static apr_status_t fcntl_release(apr_lock_t *);
+
+static void fcntl_setup(void)
 {
     lock_it.l_whence = SEEK_SET;        /* from current point */
     lock_it.l_start = 0;                /* -"- */
@@ -291,17 +315,17 @@
     unlock_it.l_pid = 0;                /* pid not actually interesting */
 }
 
-static apr_status_t lock_cleanup(void *lock_)
+static apr_status_t fcntl_cleanup(void *lock_)
 {
     apr_lock_t *lock=lock_;
 
     if (lock->curr_locked == 1) {
-        return apr_unix_unlock_inter(lock);
+        return fcntl_release(lock);
     }
     return APR_SUCCESS;
 }    
 
-apr_status_t apr_unix_create_inter_lock(apr_lock_t *new)
+static apr_status_t fcntl_create(apr_lock_t *new)
 {
     if (new->fname) {
         new->interproc = open(new->fname, O_CREAT | O_WRONLY | O_EXCL, 0644);
@@ -312,18 +336,18 @@
     }
 
     if (new->interproc < 0) {
-        lock_cleanup(new);
+        fcntl_cleanup(new);
         return errno;
     }
 
     new->curr_locked=0;
     unlink(new->fname);
-    apr_pool_cleanup_register(new->pool, (void*)new, lock_cleanup, 
+    apr_pool_cleanup_register(new->pool, (void*)new, fcntl_cleanup, 
                               apr_pool_cleanup_null);
     return APR_SUCCESS; 
 }
 
-apr_status_t apr_unix_lock_inter(apr_lock_t *lock)
+static apr_status_t fcntl_acquire(apr_lock_t *lock)
 {
     int rc;
 
@@ -337,7 +361,7 @@
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_unlock_inter(apr_lock_t *lock)
+static apr_status_t fcntl_release(apr_lock_t *lock)
 {
     int rc;
 
@@ -351,41 +375,53 @@
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_destroy_inter_lock(apr_lock_t *lock)
+static apr_status_t fcntl_destroy(apr_lock_t *lock)
 {
     apr_status_t stat;
-    if ((stat = lock_cleanup(lock)) == APR_SUCCESS) {
-        apr_pool_cleanup_kill(lock->pool, lock, lock_cleanup);
+    if ((stat = fcntl_cleanup(lock)) == APR_SUCCESS) {
+        apr_pool_cleanup_kill(lock->pool, lock, fcntl_cleanup);
         return APR_SUCCESS;
     }
     return stat;
 }
 
-apr_status_t apr_unix_child_init_lock(apr_lock_t **lock, apr_pool_t *cont, 
-                                    const char *fname)
+static apr_status_t fcntl_child_init(apr_lock_t **lock, apr_pool_t *cont, 
+                                     const char *fname)
 {
     return APR_SUCCESS;
 }
 
+const struct apr_unix_lock_methods_t apr_unix_fcntl_methods =
+{
+    fcntl_create,
+    fcntl_acquire,
+    fcntl_release,
+    fcntl_destroy,
+    fcntl_child_init
+};
+
+#endif /* fcntl implementation */
 
-#elif (APR_USE_FLOCK_SERIALIZE)
+#if APR_HAVE_FLOCK_SERIALIZE
 
-void apr_unix_setup_lock(void)
+static apr_status_t flock_release(apr_lock_t *);
+
+static void flock_setup(void)
 {
 }
 
-static apr_status_t lock_cleanup(void *lock_)
+static apr_status_t flock_cleanup(void *lock_)
 {
     apr_lock_t *lock=lock_;
 
     if (lock->curr_locked == 1) {
-        return apr_unix_unlock_inter(lock);
+        return flock_release(lock);
     }
     unlink(lock->fname);
     return APR_SUCCESS;
 }    
 
-apr_status_t apr_unix_create_inter_lock(apr_lock_t *new)
+static apr_status_t flock_create(apr_lock_t *new)
 {
     if (new->fname) {
         new->interproc = open(new->fname, O_CREAT | O_WRONLY | O_EXCL, 0600);
@@ -396,16 +432,16 @@
     }
 
     if (new->interproc < 0) {
-        lock_cleanup(new);
+        flock_cleanup(new);
         return errno;
     }
     new->curr_locked = 0;
-    apr_pool_cleanup_register(new->pool, (void *)new, lock_cleanup,
+    apr_pool_cleanup_register(new->pool, (void *)new, flock_cleanup,
                               apr_pool_cleanup_null);
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_lock_inter(apr_lock_t *lock)
+static apr_status_t flock_acquire(apr_lock_t *lock)
 {
     int rc;
 
@@ -419,7 +455,7 @@
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_unlock_inter(apr_lock_t *lock)
+static apr_status_t flock_release(apr_lock_t *lock)
 {
     int rc;
 
@@ -433,18 +469,18 @@
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_destroy_inter_lock(apr_lock_t *lock)
+static apr_status_t flock_destroy(apr_lock_t *lock)
 {
     apr_status_t stat;
-    if ((stat = lock_cleanup(lock)) == APR_SUCCESS) {
-        apr_pool_cleanup_kill(lock->new, lock, lock_cleanup);
+    if ((stat = flock_cleanup(lock)) == APR_SUCCESS) {
+        apr_pool_cleanup_kill(lock->pool, lock, flock_cleanup);
         return APR_SUCCESS;
     }
     return stat;
 }
 
-apr_status_t apr_unix_child_init_lock(apr_lock_t **lock, apr_pool_t *cont, 
-                            const char *fname)
+static apr_status_t flock_child_init(apr_lock_t **lock, apr_pool_t *cont, 
+                                     const char *fname)
 {
     apr_lock_t *new;
 
@@ -453,18 +489,36 @@
     new->fname = apr_pstrdup(cont, fname);
     new->interproc = open(new->fname, O_WRONLY, 0600);
     if (new->interproc == -1) {
-        apr_unix_destroy_inter_lock(new);
+        flock_destroy(new);
         return errno;
     }
     *lock = new;
     return APR_SUCCESS;
 }
+
+const struct apr_unix_lock_methods_t apr_unix_flock_methods =
+{
+    flock_create,
+    flock_acquire,
+    flock_release,
+    flock_destroy,
+    flock_child_init
+};
+
+#endif /* flock implementation */
 
-#else
-/* No inter-process mutex on this platform.  Use at your own risk */
-#define create_inter_lock(x, y)
-#define lock_inter(x, y)
-#define unlock_inter(x, y)
-#define destroy_inter_lock(x, y)
-#define child_init_lock(x, y, z)
+void apr_unix_setup_lock(void)
+{
+#if APR_HAVE_SYSVSEM_SERIALIZE
+    sysv_setup();
+#endif
+#if APR_HAVE_PROC_PTHREAD_SERIALIZE
+    proc_pthread_setup();
 #endif
+#if APR_HAVE_FCNTL_SERIALIZE
+    fcntl_setup();
+#endif
+#if APR_HAVE_FLOCK_SERIALIZE
+    flock_setup();
+#endif
+}
Index: locks/unix/intraproc.c
===================================================================
RCS file: /home/cvs/apr/locks/unix/intraproc.c,v
retrieving revision 1.23
diff -u -r1.23 intraproc.c
--- locks/unix/intraproc.c	2001/06/05 16:46:52	1.23
+++ locks/unix/intraproc.c	2001/06/25 14:47:28
@@ -73,7 +73,7 @@
     return stat;
 }    
 
-apr_status_t apr_unix_create_intra_lock(apr_lock_t *new)
+static apr_status_t intra_create(apr_lock_t *new)
 {
     apr_status_t stat;
     pthread_mutexattr_t mattr;
@@ -113,7 +113,7 @@
     return APR_SUCCESS;
 }
 
-apr_status_t apr_unix_lock_intra(apr_lock_t *lock)
+static apr_status_t intra_acquire(apr_lock_t *lock)
 {
     apr_status_t stat;
 
@@ -126,7 +126,7 @@
     return stat;
 }
 
-apr_status_t apr_unix_unlock_intra(apr_lock_t *lock)
+static apr_status_t intra_release(apr_lock_t *lock)
 {
     apr_status_t status;
 
@@ -139,7 +139,7 @@
     return status;
 }
 
-apr_status_t apr_unix_destroy_intra_lock(apr_lock_t *lock)
+static apr_status_t intra_destroy(apr_lock_t *lock)
 {
     apr_status_t stat;
     if ((stat = lock_intra_cleanup(lock)) == APR_SUCCESS) {
@@ -148,5 +148,16 @@
     }
     return stat;
 }
-#endif
+
+#endif /* APR_USE_PTHREAD_SERIALIZE */
+
+const struct apr_unix_lock_methods_t apr_unix_intra_methods =
+{
+    intra_create,
+    intra_acquire,
+    intra_release,
+    intra_destroy,
+    NULL /* no child init */
+};
+
 #endif
Index: locks/unix/locks.c
===================================================================
RCS file: /home/cvs/apr/locks/unix/locks.c,v
retrieving revision 1.53
diff -u -r1.53 locks.c
--- locks/unix/locks.c	2001/06/14 18:51:58	1.53
+++ locks/unix/locks.c	2001/06/25 14:47:28
@@ -56,41 +56,128 @@
 #include "apr_strings.h"
 #include "apr_portable.h"
 
+#if !APR_PROCESS_LOCK_IS_GLOBAL && APR_HAVE_THREADS
+
+static apr_status_t lockall_create(apr_lock_t *new)
+{
+    apr_status_t rv;
+
+    if ((rv = new->inter_meth->create(new)) != APR_SUCCESS) {
+        return rv;
+    }
+    if ((rv = new->intra_meth->create(new)) != APR_SUCCESS) {
+        return rv;
+    }
+}
+
+static apr_status_t lockall_acquire(apr_lock_t *lock)
+{
+    apr_status_t rv;
+
+    if ((rv = new->intra_meth->acquire(lock)) != APR_SUCCESS) {
+        return rv;
+    }
+    if ((rv = new->inter_meth->acquire(lock)) != APR_SUCCESS) {
+        return rv;
+    }
+}
+
+static apr_status_t lockall_release(apr_lock_t *lock)
+{
+    apr_status_t rv;
+
+    if ((rv = new->intra_meth->release(lock)) != APR_SUCCESS) {
+        return rv;
+    }
+    if ((rv = new->inter_meth->release(lock)) != APR_SUCCESS) {
+        return rv;
+    }
+}
+
+static apr_status_t lockall_destroy(apr_lock_t *lock)
+{
+    apr_status_t rv;
+
+    if ((rv = new->intra_meth->destroy(lock)) != APR_SUCCESS) {
+        return rv;
+    }
+    if ((rv = new->inter_meth->destroy(lock)) != APR_SUCCESS) {
+        return rv;
+    }
+}
+
+static apr_status_t lockall_child_init(apr_lock_t **lock, apr_pool_t *pool,
+                                       const char *fname)
+{
+    apr_status_t rv;
+
+    /* no child init for intra lock */
+    if ((rv = (*lock)->inter_meth->child_init(lock, pool, fname)) != APR_SUCCESS) {
+        return rv;
+    }
+}
+
+static const struct apr_unix_lock_methods_t lockall_methods =
+{
+    lockall_create,
+    lockall_acquire,
+    lockall_release,
+    lockall_destroy,
+    lockall_child_init
+};
+
+#endif
+
 static apr_status_t create_lock(apr_lock_t *new, const char *fname)
 {
     apr_status_t stat;
 
+#if APR_USE_FLOCK_SERIALIZE
+    new->inter_meth = &apr_unix_flock_methods;
+#elif APR_USE_SYSVSEM_SERIALIZE
+    new->inter_meth = &apr_unix_sysv_methods;
+#elif APR_USE_FCNTL_SERIALIZE
+    new->inter_meth = &apr_unix_fcntl_methods;
+#elif APR_USE_PROC_PTHREAD_SERIALIZE
+    new->inter_method = &apr_unix_proc_pthread_methods;
+#else
+    return APR_ENOTIMPL;
+#endif
+
+    new->intra_meth = &apr_unix_intra_methods;
+
+    switch (new->scope) {
+    case APR_LOCKALL:
+#if APR_PROCESS_LOCK_IS_GLOBAL || !APR_HAVE_THREADS
+        /* XXX but how do we know that this particular mechanism has this
+         * property?
+         */
+        new->meth = new->inter_meth;
+#else
+        new->meth = lockall_methods;
+#endif
+        break;
+    case APR_CROSS_PROCESS:
+        new->meth = new->inter_meth;
+        break;
+    case APR_INTRAPROCESS:
+        new->meth = new->intra_meth;
+    }
+
     switch (new->type)
     {
     case APR_MUTEX:
-#if (APR_USE_FCNTL_SERIALIZE) || (APR_USE_FLOCK_SERIALIZE)
+/* XXX restore this optim: if (APR_USE_FCNTL_SERIALIZE) || (APR_USE_FLOCK_SERIALIZE) */
     /* file-based serialization primitives */
     if (new->scope != APR_INTRAPROCESS) {
         if (fname != NULL) {
             new->fname = apr_pstrdup(new->pool, fname);
         }
     }
-#endif
+/* XXX end of optim to restore */
 
-#if APR_PROCESS_LOCK_IS_GLOBAL /* don't need intra lock for APR_LOCKALL */
-    if (new->scope == APR_INTRAPROCESS) {
-#else
-    if (new->scope != APR_CROSS_PROCESS) {
-#endif
-#if APR_HAS_THREADS
-        if ((stat = apr_unix_create_intra_lock(new)) != APR_SUCCESS) {
-            return stat;
-        }
-#else
-        if (new->scope != APR_LOCKALL) {
-            return APR_ENOTIMPL;
-        }
-#endif
-    }
-    if (new->scope != APR_INTRAPROCESS) {
-        if ((stat = apr_unix_create_inter_lock(new)) != APR_SUCCESS) {
-            return stat;
-        }
+    if ((stat = new->meth->create(new)) != APR_SUCCESS) {
+        return stat;
     }
     break;
     case APR_READWRITE:
@@ -140,25 +227,10 @@
     switch (lock->type)
     {
     case APR_MUTEX:
-#if APR_PROCESS_LOCK_IS_GLOBAL /* don't need intra lock for APR_LOCKALL */
-    if (lock->scope == APR_INTRAPROCESS) {
-#else
-    if (lock->scope != APR_CROSS_PROCESS) {
-#endif
-#if APR_HAS_THREADS
-        if ((stat = apr_unix_lock_intra(lock)) != APR_SUCCESS) {
-            return stat;
-        }
-#else
-        /* must be APR_LOCKALL */
-#endif
-    }
-    if (lock->scope != APR_INTRAPROCESS) {
-        if ((stat = apr_unix_lock_inter(lock)) != APR_SUCCESS) {
+        if ((stat = lock->meth->acquire(lock)) != APR_SUCCESS) {
             return stat;
         }
-    }
-    break;
+        break;
     case APR_READWRITE:
         return APR_ENOTIMPL;
     }
@@ -214,25 +286,10 @@
     switch (lock->type)
     {
     case APR_MUTEX:
-#if APR_PROCESS_LOCK_IS_GLOBAL /* don't need intra lock for APR_LOCKALL */
-    if (lock->scope == APR_INTRAPROCESS) {
-#else
-    if (lock->scope != APR_CROSS_PROCESS) {
-#endif
-#if APR_HAS_THREADS
-        if ((stat = apr_unix_unlock_intra(lock)) != APR_SUCCESS) {
+        if ((stat = lock->meth->release(lock)) != APR_SUCCESS) {
             return stat;
         }
-#else
-        /* must be APR_LOCKALL */
-#endif
-    }
-    if (lock->scope != APR_INTRAPROCESS) {
-        if ((stat = apr_unix_unlock_inter(lock)) != APR_SUCCESS) {
-            return stat;
-        }
-    }
-    break;
+        break;
     case APR_READWRITE:
 #ifdef HAVE_PTHREAD_RWLOCK_INIT
         if ((stat = pthread_rwlock_unlock(&lock->rwlock)) != 0)
@@ -258,27 +315,10 @@
     switch (lock->type)
     {
     case APR_MUTEX:
-#if APR_PROCESS_LOCK_IS_GLOBAL /* don't need intra lock for APR_LOCKALL */
-    if (lock->scope == APR_INTRAPROCESS) {
-#else
-    if (lock->scope != APR_CROSS_PROCESS) {
-#endif
-#if APR_HAS_THREADS
-        if ((stat = apr_unix_destroy_intra_lock(lock)) != APR_SUCCESS) {
-            return stat;
-        }
-#else
-        if (lock->scope != APR_LOCKALL) {
-            return APR_ENOTIMPL;
-        }
-#endif
-    }
-    if (lock->scope != APR_INTRAPROCESS) {
-        if ((stat = apr_unix_destroy_inter_lock(lock)) != APR_SUCCESS) {
+        if ((stat = lock->meth->destroy(lock)) != APR_SUCCESS) {
             return stat;
         }
-    }
-    break;
+        break;
     case APR_READWRITE:
 #ifdef HAVE_PTHREAD_RWLOCK_INIT
     if ((stat = pthread_rwlock_destroy(&lock->rwlock)) != 0)
@@ -294,12 +334,8 @@
 apr_status_t apr_lock_child_init(apr_lock_t **lock, const char *fname, 
                                apr_pool_t *cont)
 {
-    apr_status_t stat;
-    if ((*lock)->scope != APR_INTRAPROCESS) {
-        if ((stat = apr_unix_child_init_lock(lock, cont, fname)) != APR_SUCCESS) {
-            return stat;
-        }
-    }
+    if ((*lock)->scope != APR_INTRAPROCESS)
+        return (*lock)->meth->child_init(lock, cont, fname);
     return APR_SUCCESS;
 }
 
@@ -336,6 +372,7 @@
         (*lock) = (apr_lock_t *)apr_pcalloc(pool, sizeof(apr_lock_t));
         (*lock)->pool = pool;
     }
+    /* XXX handle setting of handle for PROC_PTHREAD_SERIALIZE here */
     (*lock)->interproc = thelock->crossproc;
 #if APR_HAS_THREADS
 #if (APR_USE_PTHREAD_SERIALIZE)


-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...


Mime
View raw message