Browse Source

- new locking used in shm_mem
- some compile fixes in tm/lock.* (w/ sys v sems)
Notes: on linux use only FAST_LOCK or USE_SYSV_SEM (pthread & posix sems do not work)

Andrei Pelinescu-Onciul 23 years ago
parent
commit
e01354880c
7 changed files with 85 additions and 146 deletions
  1. 46 4
      Makefile.defs
  2. 8 13
      locking.h
  3. 9 0
      main.c
  4. 14 62
      mem/shm_mem.c
  5. 4 63
      mem/shm_mem.h
  6. 2 2
      modules/tm/lock.c
  7. 2 2
      modules/tm/lock.h

+ 46 - 4
Makefile.defs

@@ -8,7 +8,7 @@
 VERSION = 0
 VERSION = 0
 PATCHLEVEL = 8
 PATCHLEVEL = 8
 SUBLEVEL =   11
 SUBLEVEL =   11
-EXTRAVERSION = pre3-tcp0
+EXTRAVERSION = pre4-tcp0-locking
 
 
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
@@ -112,6 +112,13 @@ YACC := $(shell echo "$${YACC}")
 #		issues additional debugging information if lock/unlock is called
 #		issues additional debugging information if lock/unlock is called
 # -DFAST_LOCK
 # -DFAST_LOCK
 #		uses fast arhitecture specific locking (see the arh. specific section)
 #		uses fast arhitecture specific locking (see the arh. specific section)
+# -DUSE_SYSV_SEM
+#		uses sys v sems for locking (slower & limited number)
+# -DUSE_PTHREAD_MUTEX
+#		uses pthread mutexes, faster than sys v or posix sems, but do not 
+#		work on all systems inter-processes (e.g. linux)
+# -DUSE_POSIX_SEM
+#		uses posix semaphores for locking (faster than sys v)
 # -DBUSY_WAIT
 # -DBUSY_WAIT
 #		uses busy waiting on the lock
 #		uses busy waiting on the lock
 # -DADAPTIVE_WAIT
 # -DADAPTIVE_WAIT
@@ -276,21 +283,26 @@ endif
 
 
 # arh. specific definitions
 # arh. specific definitions
 ifeq ($(ARCH), i386)
 ifeq ($(ARCH), i386)
-	DEFS+= -DFAST_LOCK
+	use_fast_lock=yes
 endif
 endif
 
 
 ifeq ($(ARCH), sparc64)
 ifeq ($(ARCH), sparc64)
 ifeq 	($(CC_NAME), gcc)
 ifeq 	($(CC_NAME), gcc)
-		DEFS+= -DFAST_LOCK
+	use_fast_lock=yes
 endif
 endif
 endif
 endif
 
 
 ifeq ($(ARCH), arm)
 ifeq ($(ARCH), arm)
-	DEFS+= -DFAST_LOCK
+	use_fast_lock=yes
 endif
 endif
 
 
 ifeq ($(ARCH), ppc)
 ifeq ($(ARCH), ppc)
+	use_fast_lock=yes
+endif
+
+ifeq ($(use_fast_lock), yes)
 	DEFS+= -DFAST_LOCK
 	DEFS+= -DFAST_LOCK
+	found_lock_method=yes
 endif
 endif
 
 
 CFLAGS=
 CFLAGS=
@@ -514,10 +526,18 @@ LIBS= -lfl -ldl -lresolv
 #os specific stuff
 #os specific stuff
 ifeq ($(OS), linux)
 ifeq ($(OS), linux)
 	DEFS+=-DHAVE_GETHOSTBYNAME2 -DHAVE_UNION_SEMUN -DHAVE_SCHED_YIELD
 	DEFS+=-DHAVE_GETHOSTBYNAME2 -DHAVE_UNION_SEMUN -DHAVE_SCHED_YIELD
+	ifneq ($(found_lock_method), yes)
+		DEFS+= -DUSE_SYSV_SEM  # try posix sems
+		found_lock_method=yes
+	endif
 endif
 endif
 
 
 ifeq  ($(OS), solaris)
 ifeq  ($(OS), solaris)
 	DEFS+= -DHAVE_GETIPNODEBYNAME -DHAVE_SYS_SOCKIO_H -DHAVE_SCHED_YIELD
 	DEFS+= -DHAVE_GETIPNODEBYNAME -DHAVE_SYS_SOCKIO_H -DHAVE_SCHED_YIELD
+	ifneq ($(found_lock_method), yes)
+		DEFS+= -DUSE_PTHREAD_MUTEX  # try pthread sems
+		found_lock_method=yes
+	endif
 	ifeq ($(mode), release)
 	ifeq ($(mode), release)
 		#use these only if you're using gcc with Solaris ld
 		#use these only if you're using gcc with Solaris ld
 		#LDFLAGS=-O2 $(PROFILE)
 		#LDFLAGS=-O2 $(PROFILE)
@@ -539,6 +559,10 @@ endif
 ifeq ($(OS), freebsd)
 ifeq ($(OS), freebsd)
 	DEFS+=-DHAVE_SOCKADDR_SA_LEN -DHAVE_GETHOSTBYNAME2 -DHAVE_UNION_SEMUN \
 	DEFS+=-DHAVE_SOCKADDR_SA_LEN -DHAVE_GETHOSTBYNAME2 -DHAVE_UNION_SEMUN \
 		-DHAVE_SCHED_YIELD
 		-DHAVE_SCHED_YIELD
+	ifneq ($(found_lock_method), yes)
+		DEFS+= -DUSE_PTHREAD_MUTEX  # try pthread sems
+		found_lock_method=yes
+	endif
 	YACC=yacc
 	YACC=yacc
 	LIBS= -lfl  #dlopen is in libc
 	LIBS= -lfl  #dlopen is in libc
 endif
 endif
@@ -546,6 +570,10 @@ endif
 ifeq ($(OS), openbsd)
 ifeq ($(OS), openbsd)
 	DEFS+=-DHAVE_SOCKADDR_SA_LEN -DDLSYM_PREFIX='"_"' -DHAVE_GETHOSTBYNAME2 \
 	DEFS+=-DHAVE_SOCKADDR_SA_LEN -DDLSYM_PREFIX='"_"' -DHAVE_GETHOSTBYNAME2 \
 		-DHAVE_UNION_SEMUN
 		-DHAVE_UNION_SEMUN
+	ifneq ($(found_lock_method), yes)
+		DEFS+= -DUSE_PTHREAD_MUTEX  # try pthread sems
+		found_lock_method=yes
+	endif
 	# (symbols on openbsd are prefixed by "_")
 	# (symbols on openbsd are prefixed by "_")
 	YACC=yacc
 	YACC=yacc
 	# no sched_yield on openbsd unless linking with c_r (not recommended)
 	# no sched_yield on openbsd unless linking with c_r (not recommended)
@@ -555,10 +583,24 @@ endif
 	
 	
 ifeq ($(OS), netbsd)
 ifeq ($(OS), netbsd)
 	DEFS+=-DHAVE_SOCKADDR_SA_LEN -DHAVE_GETHOSTBYNAME2 
 	DEFS+=-DHAVE_SOCKADDR_SA_LEN -DHAVE_GETHOSTBYNAME2 
+	ifneq ($(found_lock_method), yes)
+		DEFS+= -DUSE_SYSV_SEM  # try pthread sems
+		found_lock_method=yes
+	endif
 	YACC=yacc
 	YACC=yacc
 	LIBS= -lfl 
 	LIBS= -lfl 
 endif
 endif
 
 
 ifneq (,$(findstring CYGWIN, $(OS)))
 ifneq (,$(findstring CYGWIN, $(OS)))
 	#cygwin is the same as common
 	#cygwin is the same as common
+	ifneq ($(found_lock_method), yes)
+		DEFS+= -DUSE_SYSV_SEM  # try sys v sems
+		found_lock_method=yes
+	endif
+endif
+
+ifneq ($(found_lock_method), yes)
+$(warning	No locking method found so far, trying SYS V sems)
+		DEFS+= -DUSE_SYSV_SEM  # try sys v sems
+		found_lock_method=yes
 endif
 endif

+ 8 - 13
locking.h

@@ -105,7 +105,7 @@ inline static lock_t* lock_init(lock_t* lock)
 }
 }
 
 
 #define lock_get(lock) sem_wait(lock)
 #define lock_get(lock) sem_wait(lock)
-#define lock_release(lock) sem_release(lock)
+#define lock_release(lock) sem_post(lock)
 
 
 
 
 #elif defined USE_SYSV_SEM
 #elif defined USE_SYSV_SEM
@@ -127,21 +127,16 @@ inline static lock_t* lock_init(lock_t* lock)
 
 
 typedef int lock_t;
 typedef int lock_t;
 
 
-inline static lock_t* lock_alloc()
-{
-	lock_t* l;
-	
-	l=shm_malloc(sizeof(lock_t));
-	if (l==0) return 0;
-	*l=semget(IPC_PRIVATE, 1, 0700);
-	if (*l==-1) return 0;
-	return l;
-}
+#define lock_alloc() shm_malloc(sizeof(lock_t))
+#define lock_dealloc(lock) shm_free(lock)
 
 
 
 
 inline static lock_t* lock_init(lock_t* lock)
 inline static lock_t* lock_init(lock_t* lock)
 {
 {
 	union semun su;
 	union semun su;
+	
+	*lock=semget(IPC_PRIVATE, 1, 0700);
+	if (*lock==-1) return 0;
 	su.val=1;
 	su.val=1;
 	if (semctl(*lock, 0, SETVAL, su)==-1){
 	if (semctl(*lock, 0, SETVAL, su)==-1){
 		/* init error*/
 		/* init error*/
@@ -157,7 +152,7 @@ inline static void lock_destroy(lock_t* lock)
 
 
 #define lock_dealloc(lock) shm_free(lock)
 #define lock_dealloc(lock) shm_free(lock)
 
 
-inline void lock_get(lock_t* lock)
+inline static void lock_get(lock_t* lock)
 {
 {
 	struct sembuf sop;
 	struct sembuf sop;
 
 
@@ -167,7 +162,7 @@ inline void lock_get(lock_t* lock)
 	semop(*lock, &sop, 1);
 	semop(*lock, &sop, 1);
 }
 }
 
 
-inline void lock_release(lock_t* lock)
+inline static void lock_release(lock_t* lock)
 {
 {
 	struct sembuf sop;
 	struct sembuf sop;
 	
 	

+ 9 - 0
main.c

@@ -141,6 +141,15 @@ static char flags[]=
 #ifdef BUSY_WAIT
 #ifdef BUSY_WAIT
 "-BUSY_WAIT"
 "-BUSY_WAIT"
 #endif
 #endif
+#ifdef USE_PTHREAD_MUTEX
+", USE_PTHREAD_MUTEX"
+#endif
+#ifdef USE_POSIX_SEM
+", USE_POSIX_SEM"
+#endif
+#ifdef USE_SYSV_SEM
+", USE_SYSV_SEM"
+#endif
 #ifdef ADAPTIVE_WAIT
 #ifdef ADAPTIVE_WAIT
 "-ADAPTIVE_WAIT"
 "-ADAPTIVE_WAIT"
 #endif
 #endif

+ 14 - 62
mem/shm_mem.c

@@ -45,37 +45,13 @@
 
 
 #endif
 #endif
 
 
-#ifdef FAST_LOCK
-#include "../fastlock.h"
-#endif
-
-
-
-
-/* define semun */
-#if defined(HAVE_UNION_SEMUN) && !defined(_SEM_SEMUN_UNDEFINED)
-	/* union semun is defined by including <sys/sem.h> */
-#else
-	/* according to X/OPEN we have to define it ourselves */
-	union semun {
-		int val;                    /* value for SETVAL */
-		struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
-		unsigned short int *array;  /* array for GETALL, SETALL */
-		struct seminfo *__buf;      /* buffer for IPC_INFO */
-	};
-#endif
-
 
 
 
 
 #ifndef SHM_MMAP
 #ifndef SHM_MMAP
 static int shm_shmid=-1; /*shared memory id*/
 static int shm_shmid=-1; /*shared memory id*/
 #endif
 #endif
 
 
-#ifdef FAST_LOCK
-fl_lock_t* mem_lock=0;
-#else
-int shm_semid=-1; /*semaphore id*/
-#endif
+lock_t* mem_lock=0;
 
 
 static void* shm_mempool=(void*)-1;
 static void* shm_mempool=(void*)-1;
 #ifdef VQ_MALLOC
 #ifdef VQ_MALLOC
@@ -191,10 +167,6 @@ void* _shm_resize( void* p , unsigned int s)
 int shm_mem_init()
 int shm_mem_init()
 {
 {
 
 
-#ifndef FAST_LOCK
-	union semun su;
-	int ret;
-#endif
 #ifdef SHM_MMAP
 #ifdef SHM_MMAP
 	int fd;
 	int fd;
 #else
 #else
@@ -238,25 +210,6 @@ int shm_mem_init()
 		return -1;
 		return -1;
 	}
 	}
 
 
-#ifndef FAST_LOCK
-	/* alloc a semaphore (for malloc)*/
-	shm_semid=semget(IPC_PRIVATE, 1, 0700);
-	if (shm_semid==-1){
-		LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate semaphore: %s\n",
-				strerror(errno));
-		shm_mem_destroy();
-		return -1;
-	}
-	/* set its value to 1 (mutex)*/
-	su.val=1;
-	ret=semctl(shm_semid, 0, SETVAL, su);
-	if (ret==-1){
-		LOG(L_CRIT, "ERROR: shm_mem_init: could not set initial semaphore"
-				" value: %s\n", strerror(errno));
-		shm_mem_destroy();
-		return -1;
-	}
-#endif
 	/* init it for malloc*/
 	/* init it for malloc*/
 #	ifdef VQ_MALLOC
 #	ifdef VQ_MALLOC
 		shm_block=vqm_malloc_init(shm_mempool, /* SHM_MEM_SIZE */ shm_mem_size );
 		shm_block=vqm_malloc_init(shm_mempool, /* SHM_MEM_SIZE */ shm_mem_size );
@@ -271,10 +224,17 @@ int shm_mem_init()
 		shm_mem_destroy();
 		shm_mem_destroy();
 		return -1;
 		return -1;
 	}
 	}
-#ifdef FAST_LOCK
-	mem_lock=shm_malloc_unsafe(sizeof(fl_lock_t));
-	init_lock(*mem_lock);
-#endif
+	mem_lock=shm_malloc_unsafe(sizeof(lock_t)); /* skip lock_alloc, race cond*/
+	if (mem_lock==0){
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate lock\n");
+		shm_mem_destroy();
+		return -1;
+	}
+	if (lock_init(mem_lock)==0){
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not initialize lock\n");
+		shm_mem_destroy();
+		return -1;
+	}
 	
 	
 	DBG("shm_mem_init: success\n");
 	DBG("shm_mem_init: success\n");
 	
 	
@@ -288,9 +248,6 @@ void shm_mem_destroy()
 #ifndef SHM_MMAP
 #ifndef SHM_MMAP
 	struct shmid_ds shm_info;
 	struct shmid_ds shm_info;
 #endif
 #endif
-#ifndef FAST_LOCK
-	union semun zero_un;
-#endif
 	
 	
 	DBG("shm_mem_destroy\n");
 	DBG("shm_mem_destroy\n");
 	if (shm_mempool && (shm_mempool!=(void*)-1)) {
 	if (shm_mempool && (shm_mempool!=(void*)-1)) {
@@ -307,13 +264,8 @@ void shm_mem_destroy()
 		shm_shmid=-1;
 		shm_shmid=-1;
 	}
 	}
 #endif
 #endif
-#ifndef FAST_LOCK
-	if (shm_semid!=-1) {
-		zero_un.val=0;
-		semctl(shm_semid, 0, IPC_RMID, zero_un);
-		shm_semid=-1;
-	}
-#endif
+	if (mem_lock) lock_destroy(mem_lock); /* we don't need to dealloc it*/
+	
 }
 }
 
 
 
 

+ 4 - 63
mem/shm_mem.h

@@ -50,6 +50,7 @@
 
 
 
 
 #include "../dprint.h"
 #include "../dprint.h"
+#include "../locking.h"
 
 
 #ifdef VQ_MALLOC
 #ifdef VQ_MALLOC
 #	include "vq_malloc.h"
 #	include "vq_malloc.h"
@@ -71,77 +72,17 @@
 #	define MY_STATUS qm_status
 #	define MY_STATUS qm_status
 #endif
 #endif
 
 
-#ifdef FAST_LOCK
-#include "../fastlock.h"
 	
 	
-	extern fl_lock_t* mem_lock;
-#else
-extern  int shm_semid;
-#endif
+	extern lock_t* mem_lock;
 
 
 
 
 int shm_mem_init();
 int shm_mem_init();
 void shm_mem_destroy();
 void shm_mem_destroy();
 
 
 
 
-#ifdef FAST_LOCK
-
-#define shm_lock()    get_lock(mem_lock)
-#define shm_unlock()  release_lock(mem_lock)
-
-#else
-/* inline functions (do not move them to *.c, they won't be inlined anymore) */
-static inline void shm_lock()
-{
-
-	struct sembuf sop;
-	int ret;
-	
-	sop.sem_num=0;
-	sop.sem_op=-1; /*down*/
-	sop.sem_flg=0 /*SEM_UNDO*/;
-again:
-	ret=semop(shm_semid, &sop, 1);
-
-	switch(ret){
-		case 0: /*ok*/
-			break;
-		case EINTR: /*interrupted by signal, try again*/
-			DBG("sh_lock: interrupted by signal, trying again...\n");
-			goto again;
-		default:
-			LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
-					strerror(errno));
-	}
-}
-
-
-
-static inline void shm_unlock()
-{
-	struct sembuf sop;
-	int ret;
-	
-	sop.sem_num=0;
-	sop.sem_op=1; /*up*/
-	sop.sem_flg=0 /*SEM_UNDO*/;
-again:
-	ret=semop(shm_semid, &sop, 1);
-	/*should ret immediately*/
-	switch(ret){
-		case 0: /*ok*/
-			break;
-		case EINTR: /*interrupted by signal, try again*/
-			DBG("sh_lock: interrupted by signal, trying again...\n");
-			goto again;
-		default:
-			LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
-					strerror(errno));
-	}
-}
 
 
-/* ret -1 on erro*/
-#endif
+#define shm_lock()    lock_get(mem_lock)
+#define shm_unlock()  lock_release(mem_lock)
 
 
 
 
 
 

+ 2 - 2
modules/tm/lock.c

@@ -140,7 +140,7 @@ tryagain:
 			DBG("signal received in a semaphore\n");
 			DBG("signal received in a semaphore\n");
 			goto tryagain;
 			goto tryagain;
 		} else {
 		} else {
-			LOG(L_CRIT, "ERROR: change_semaphore(%x, %x, 1) : %s\n",
+			LOG(L_CRIT, "ERROR: change_semaphore(%x, %p, 1) : %s\n",
 					s->semaphore_set, &pbuf,
 					s->semaphore_set, &pbuf,
 					strerror(errno));
 					strerror(errno));
 		}
 		}
@@ -252,7 +252,7 @@ again:
 		if (errno==EINVAL || errno==ENOSPC ) {
 		if (errno==EINVAL || errno==ENOSPC ) {
 			DBG("DEBUG:lock_initialize: reply semaphore initialization"
 			DBG("DEBUG:lock_initialize: reply semaphore initialization"
 				" failure: %s\n", strerror(errno));
 				" failure: %s\n", strerror(errno));
-			probe_run==1;
+			probe_run=1;
 			i--;
 			i--;
 			goto again;
 			goto again;
 		}else{
 		}else{

+ 2 - 2
modules/tm/lock.h

@@ -27,8 +27,8 @@
 
 
 
 
 
 
-#ifndef _SEMAPHORE_H
-#define _SEMAPHORE_H
+#ifndef __lock_h
+#define __lock_h
 
 
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/ipc.h>