]> granicus.if.org Git - apache/commitdiff
implement mutex control from shared memory... plain memory not
authorJim Jagielski <jim@apache.org>
Mon, 29 Dec 2008 15:55:58 +0000 (15:55 +0000)
committerJim Jagielski <jim@apache.org>
Mon, 29 Dec 2008 15:55:58 +0000 (15:55 +0000)
done yet... wonder if we should even do/include it?

Also looking at cleaning up the struct... do we really need
the sharedslotdesc element??

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@729921 13f79535-47bb-0310-9956-ffa450edef68

modules/mem/mod_plainmem.c
modules/mem/mod_sharedmem.c
modules/mem/sharedmem_util.c
modules/mem/sharedmem_util.h
modules/mem/slotmem.h

index 7a67f9eed3bb88d90db81816cfadb00a1a12a284..9ec06ec933116797e2fb0afeecd195c526f9df42 100644 (file)
  * This one uses plain memory.
  */
 
-#include "apr.h"
-#include "apr_pools.h"
-
-#include "httpd.h"
-#include "http_config.h"
-#include "http_log.h"
-
 #include  "slotmem.h"
 
 struct ap_slotmem {
index 5a5e527addf12011d2436dd160d3a102cb522be9..a1060e05b064e2c22c2e103cd968716e8ae55f68 100644 (file)
  * This one uses shared memory.
  */
 
-#include "apr.h"
-#include "apr_pools.h"
-#include "apr_shm.h"
-
-#include "httpd.h"
-#include "http_config.h"
-#include "http_log.h"
-
 #include  "slotmem.h"
 #include "sharedmem_util.h"
 
-/* make sure the shared memory is cleaned */
-static int initialize_cleanup(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
+/* 
+ * Create the shared mem mutex and
+ * make sure the shared memory is cleaned
+ */
+static int post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
 {
+    const char *temp_dir;
+    char *template;
+    apr_status_t rv;
+    void *data;
+    apr_file_t *fmutex;
+    const char *userdata_key = "sharedmem_post_config";
+
+    apr_pool_userdata_get(&data, userdata_key, s->process->pool);
+    if (!data) {
+        apr_pool_userdata_set((const void *)1, userdata_key,
+                               apr_pool_cleanup_null, s->process->pool);
+        return OK;
+    }
+
+    rv = apr_temp_dir_get(&temp_dir, p);
+    if (rv != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
+                     "sharedmem: search for temporary directory failed");
+        return rv;
+    }
+    apr_filepath_merge(&template, temp_dir, "sharedmem.lck.XXXXXX",
+                       APR_FILEPATH_NATIVE, p);
+    rv = apr_file_mktemp(&fmutex, template, 0, p);
+    if (rv != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
+                     "sharedmem: creation of mutex file in directory %s failed",
+                     temp_dir);
+        return rv;
+    }
+
+    rv = apr_file_name_get(&mutex_fname, fmutex);
+    if (rv != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
+                     "sharedmem: unable to get mutex fname");
+        return rv;
+    }
+
+    rv = apr_global_mutex_create(&sharedmem_mutex,
+                                 mutex_fname, APR_LOCK_DEFAULT, p);
+    if (rv != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
+                     "sharedmem: creation of mutex failed");
+        return rv;
+    }
+
+#ifdef AP_NEED_SET_MUTEX_PERMS
+    rv = ap_unixd_set_global_mutex_perms(sharedmem_mutex);
+    if (rv != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
+                     "sharedmem: failed to set mutex permissions");
+        return rv;
+    }
+#endif
+
     sharedmem_initialize_cleanup(p);
     return OK;
 }
@@ -52,12 +101,27 @@ static int pre_config(apr_pool_t *p, apr_pool_t *plog,
     return OK;
 }
 
+static void child_init(apr_pool_t *p, server_rec *s)
+{
+    apr_status_t rv;
+
+    rv = apr_global_mutex_child_init(&sharedmem_mutex,
+                                     mutex_fname, p);
+    if (rv != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
+                     "Failed to initialise global mutex %s in child process %"
+                     APR_PID_T_FMT ".",
+                     mutex_fname, getpid());
+    }
+}
+
 static void ap_sharedmem_register_hook(apr_pool_t *p)
 {
     const slotmem_storage_method *storage = sharedmem_getstorage();
     ap_register_provider(p, SLOTMEM_STORAGE, "shared", "0", storage);
-    ap_hook_post_config(initialize_cleanup, NULL, NULL, APR_HOOK_LAST);
+    ap_hook_post_config(post_config, NULL, NULL, APR_HOOK_LAST);
     ap_hook_pre_config(pre_config, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_child_init(child_init, NULL, NULL, APR_HOOK_MIDDLE);
 }
 
 module AP_MODULE_DECLARE_DATA sharedmem_module = {
index d40fd412c635cfba7c563d731ebbee20329511a0..6ddc9cafc343c1566484f0e9aa19d56e580257b3 100644 (file)
  * This one uses shared memory.
  */
 
-#include "apr.h"
-#include "apr_file_io.h"
-#include "apr_strings.h"
-#include "apr_pools.h"
-#include "apr_shm.h"
-
-#include "httpd.h"
-#include "http_config.h"
-#include "http_log.h"
-
 #include "slotmem.h"
 #include "sharedmem_util.h"
 
@@ -44,6 +34,7 @@ struct ap_slotmem {
     apr_size_t size;
     int num;
     apr_pool_t *globalpool;
+    apr_global_mutex_t *sharedmem_mutex;
     struct ap_slotmem *next;
 };
 
@@ -247,6 +238,7 @@ static apr_status_t ap_slotmem_create(ap_slotmem_t **new, const char *name, apr_
     res->size = item_size;
     res->num = item_num;
     res->globalpool = globalpool;
+    res->sharedmem_mutex = sharedmem_mutex;
     res->next = NULL;
     if (globallistmem == NULL) {
        globallistmem = res;
@@ -317,6 +309,7 @@ static apr_status_t ap_slotmem_attach(ap_slotmem_t **new, const char *name, apr_
     res->size = desc.item_size;
     res->num = desc.item_num;
     res->globalpool = globalpool;
+    res->sharedmem_mutex = sharedmem_mutex;
     res->next = NULL;
     if (globallistmem == NULL) {
        globallistmem = res;
@@ -330,19 +323,19 @@ static apr_status_t ap_slotmem_attach(ap_slotmem_t **new, const char *name, apr_
     *item_num = desc.item_num;
     return APR_SUCCESS;
 }
-static apr_status_t ap_slotmem_mem(ap_slotmem_t *score, int id, void **mem)
+static apr_status_t ap_slotmem_mem(ap_slotmem_t *slot, int id, void **mem)
 {
 
     void *ptr;
 
-    if (!score) {
+    if (!slot) {
        return APR_ENOSHMAVAIL;
     }
-    if (id < 0 || id > score->num) {
+    if (id < 0 || id > slot->num) {
        return APR_ENOSHMAVAIL;
     }
 
-    ptr = score->base + score->size * id;
+    ptr = slot->base + slot->size * id;
     if (!ptr) {
        return APR_ENOSHMAVAIL;
     }
@@ -350,11 +343,23 @@ static apr_status_t ap_slotmem_mem(ap_slotmem_t *score, int id, void **mem)
     return APR_SUCCESS;
 }
 
+static apr_status_t ap_slotmem_lock(ap_slotmem_t *slot)
+{
+    return (apr_global_mutex_lock(slot->sharedmem_mutex));    
+}
+
+static apr_status_t ap_slotmem_unlock(ap_slotmem_t *slot)
+{
+    return (apr_global_mutex_unlock(slot->sharedmem_mutex));
+}
+
 static const slotmem_storage_method storage = {
     &ap_slotmem_do,
     &ap_slotmem_create,
     &ap_slotmem_attach,
-    &ap_slotmem_mem
+    &ap_slotmem_mem,
+    &ap_slotmem_lock,
+    &ap_slotmem_unlock
 };
 
 /* make the storage usuable from outside */
index 66afa7b3501952866dcfd64165a205a35509244e..d0db9dce7214de25fb17bbaa7f518a2e60650cf3 100644 (file)
@@ -20,3 +20,5 @@
 const slotmem_storage_method *sharedmem_getstorage(void);
 void sharedmem_initglobalpool(apr_pool_t *p);
 void sharedmem_initialize_cleanup(apr_pool_t *p);
+apr_global_mutex_t *sharedmem_mutex;
+const char *mutex_fname;
index 2e230ef226b38afa898b03968b96eb7bb3d4c151..c4b8d78ddc238ad5f5ca079bc43b7804d6867238 100644 (file)
  * @{
  */
 
+#include "httpd.h"
+#include "http_config.h"
+#include "http_log.h"
+#include "ap_provider.h"
+
 #include "apr.h"
 #include "apr_strings.h"
 #include "apr_pools.h"
 #include "apr_shm.h"
+#include "apr_global_mutex.h"
+#include "apr_file_io.h"
 
-#include "httpd.h"
-#include "http_config.h"
-#include "http_log.h"
-#include "ap_provider.h"
+#ifdef AP_NEED_SET_MUTEX_PERMS
+#include "unixd.h"
+#endif
+
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>         /* for getpid() */
+#endif
 
 #define SLOTMEM_STORAGE "slotmem"
 
@@ -92,7 +102,21 @@ AP_DECLARE(apr_status_t) (* ap_slotmem_attach)(ap_slotmem_t **new, const char *n
  * @param mem address to store the pointer to the slot
  * @return APR_SUCCESS if all went well
  */
-AP_DECLARE(apr_status_t) (* ap_slotmem_mem)(ap_slotmem_t *s, int item_id, void**mem); 
+AP_DECLARE(apr_status_t) (* ap_slotmem_mem)(ap_slotmem_t *s, int item_id, void**mem);
+/**
+ * lock the memory segment
+ * NOTE: All slots share the same mutex
+ * @param s ap_slotmem_t to use
+ * @return APR_SUCCESS if all went well
+ */
+AP_DECLARE(apr_status_t) (* ap_slotmem_lock)(ap_slotmem_t *s);
+/**
+ * unlock the memory segment
+ * NOTE: All slots share the same mutex
+ * @param s ap_slotmem_t to use.
+ * @return APR_SUCCESS if all went well
+ */
+AP_DECLARE(apr_status_t) (* ap_slotmem_unlock)(ap_slotmem_t *s);
 };
 
 typedef struct slotmem_storage_method slotmem_storage_method;