/* Allocate shared memory context.
* size is maximum cached sessions.
* if set less or equal to 0, SHCTX_DEFAULT_SIZE is used.
+ * use_shared_memory is set to 1 to use a mapped share memory
* Returns: -1 on alloc failure, size if it performs context alloc,
* and 0 if cache is already allocated */
-int shared_context_init(int size);
+int shared_context_init(int size, int use_shared_memory);
/* Set shared cache callbacks on an ssl context.
* Set session cache mode to server and disable openssl internal cache.
/* Static shared context */
static struct shared_context *shctx = NULL;
+static int use_shared_mem = 0;
/* Callbacks */
static void (*shared_session_new_cbk)(unsigned char *session, unsigned int session_len, long cdate);
-
/* Lock functions */
#ifdef USE_SYSCALL_FUTEX
#if defined (__i586__) || defined (__x86_64__)
#endif
-static inline void shared_context_lock(void)
+static inline void _shared_context_lock(void)
{
unsigned int x;
}
}
-static inline void shared_context_unlock(void)
+static inline void _shared_context_unlock(void)
{
if (atomic_dec(&shctx->waiters)) {
shctx->waiters = 0;
}
}
+#define shared_context_lock(v) if (use_shared_mem) _shared_context_lock()
+
+#define shared_context_unlock(v) if (use_shared_mem) _shared_context_unlock()
+
#else /* USE_SYSCALL_FUTEX */
-#define shared_context_lock(v) pthread_mutex_lock(&shctx->mutex)
-#define shared_context_unlock(v) pthread_mutex_unlock(&shctx->mutex)
+#define shared_context_lock(v) if (use_shared_mem) pthread_mutex_lock(&shctx->mutex)
+
+#define shared_context_unlock(v) if (use_shared_mem) pthread_mutex_unlock(&shctx->mutex)
#endif
* if set less or equal to 0, SHCTX_DEFAULT_SIZE is used.
* Returns: -1 on alloc failure, size if it performs context alloc,
* and 0 if cache is already allocated */
-int shared_context_init(int size)
+int shared_context_init(int size, int shared)
{
int i;
#ifndef USE_SYSCALL_FUTEX
pthread_mutexattr_t attr;
#endif /* USE_SYSCALL_FUTEX */
struct shared_session *prev,*cur;
+ int maptype = MAP_PRIVATE;
if (shctx)
return 0;
if (size<=0)
size = SHCTX_DEFAULT_SIZE;
+ if (shared)
+ maptype = MAP_SHARED;
+
shctx = (struct shared_context *)mmap(NULL, sizeof(struct shared_context)+(size*sizeof(struct shared_session)),
- PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
+ PROT_READ | PROT_WRITE, maptype | MAP_ANON, -1, 0);
if (!shctx || shctx == MAP_FAILED) {
shctx = NULL;
return -1;
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&shctx->mutex, &attr);
#endif
+ if (maptype == MAP_SHARED)
+ use_shared_mem = 1;
+
memset(&shctx->active.key, 0, sizeof(struct ebmb_node));
memset(&shctx->free.key, 0, sizeof(struct ebmb_node));