MINOR: ssl: disable shared memory and locks on session cache if nbproc == 1

We don't needa to lock the memory when there is a single process. This can
make a difference on small systems where locking is much more expensive than
just a test.
diff --git a/include/proto/shctx.h b/include/proto/shctx.h
index 000cc05..862f6a0 100644
--- a/include/proto/shctx.h
+++ b/include/proto/shctx.h
@@ -56,9 +56,10 @@
 /* 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.
diff --git a/src/cfgparse.c b/src/cfgparse.c
index 7f03d64..6fbcd5b 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -6536,7 +6536,7 @@
 				continue;
 			}
 
-			if (shared_context_init(global.tune.sslcachesize) < 0) {
+			if (shared_context_init(global.tune.sslcachesize, (global.nbproc > 1) ? 1 : 0) < 0) {
 				Alert("Unable to allocate SSL session cache.\n");
 				cfgerr++;
 				continue;
diff --git a/src/shctx.c b/src/shctx.c
index 48d9541..23992a6 100644
--- a/src/shctx.c
+++ b/src/shctx.c
@@ -49,11 +49,11 @@
 
 /* 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__)
@@ -106,7 +106,7 @@
 
 #endif
 
-static inline void shared_context_lock(void)
+static inline void _shared_context_lock(void)
 {
 	unsigned int x;
 
@@ -122,7 +122,7 @@
 	}
 }
 
-static inline void shared_context_unlock(void)
+static inline void _shared_context_unlock(void)
 {
 	if (atomic_dec(&shctx->waiters)) {
 		shctx->waiters = 0;
@@ -130,10 +130,15 @@
 	}
 }
 
+#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
 
@@ -357,13 +362,14 @@
  * 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;
@@ -371,8 +377,11 @@
 	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;
@@ -385,6 +394,9 @@
 	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));