summaryrefslogtreecommitdiffstats
path: root/modules/mem
diff options
context:
space:
mode:
authorJim Jagielski <jim@apache.org>2008-12-29 16:55:58 +0100
committerJim Jagielski <jim@apache.org>2008-12-29 16:55:58 +0100
commit5228be187eb395e3afa1264fb08f9f4caa6fe710 (patch)
tree44f262932aefd0eb57fd2a6f127ce22c043218df /modules/mem
parentupdate transformation (diff)
downloadapache2-5228be187eb395e3afa1264fb08f9f4caa6fe710.tar.xz
apache2-5228be187eb395e3afa1264fb08f9f4caa6fe710.zip
implement mutex control from shared memory... plain memory not
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
Diffstat (limited to 'modules/mem')
-rw-r--r--modules/mem/mod_plainmem.c7
-rw-r--r--modules/mem/mod_sharedmem.c86
-rw-r--r--modules/mem/sharedmem_util.c35
-rw-r--r--modules/mem/sharedmem_util.h2
-rw-r--r--modules/mem/slotmem.h34
5 files changed, 126 insertions, 38 deletions
diff --git a/modules/mem/mod_plainmem.c b/modules/mem/mod_plainmem.c
index 7a67f9eed3..9ec06ec933 100644
--- a/modules/mem/mod_plainmem.c
+++ b/modules/mem/mod_plainmem.c
@@ -18,13 +18,6 @@
* 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 {
diff --git a/modules/mem/mod_sharedmem.c b/modules/mem/mod_sharedmem.c
index 5a5e527add..a1060e05b0 100644
--- a/modules/mem/mod_sharedmem.c
+++ b/modules/mem/mod_sharedmem.c
@@ -18,20 +18,69 @@
* 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 = {
diff --git a/modules/mem/sharedmem_util.c b/modules/mem/sharedmem_util.c
index d40fd412c6..6ddc9cafc3 100644
--- a/modules/mem/sharedmem_util.c
+++ b/modules/mem/sharedmem_util.c
@@ -18,16 +18,6 @@
* 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 */
diff --git a/modules/mem/sharedmem_util.h b/modules/mem/sharedmem_util.h
index 66afa7b350..d0db9dce72 100644
--- a/modules/mem/sharedmem_util.h
+++ b/modules/mem/sharedmem_util.h
@@ -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;
diff --git a/modules/mem/slotmem.h b/modules/mem/slotmem.h
index 2e230ef226..c4b8d78ddc 100644
--- a/modules/mem/slotmem.h
+++ b/modules/mem/slotmem.h
@@ -28,15 +28,25 @@
* @{
*/
+#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;