Browse code

- 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 authored on 16/01/2003 19:22:09
Showing 7 changed files
... ...
@@ -8,7 +8,7 @@
8 8
 VERSION = 0
9 9
 PATCHLEVEL = 8
10 10
 SUBLEVEL =   11
11
-EXTRAVERSION = pre3-tcp0
11
+EXTRAVERSION = pre4-tcp0-locking
12 12
 
13 13
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
14 14
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
... ...
@@ -112,6 +112,13 @@ YACC := $(shell echo "$${YACC}")
112 112
 #		issues additional debugging information if lock/unlock is called
113 113
 # -DFAST_LOCK
114 114
 #		uses fast arhitecture specific locking (see the arh. specific section)
115
+# -DUSE_SYSV_SEM
116
+#		uses sys v sems for locking (slower & limited number)
117
+# -DUSE_PTHREAD_MUTEX
118
+#		uses pthread mutexes, faster than sys v or posix sems, but do not 
119
+#		work on all systems inter-processes (e.g. linux)
120
+# -DUSE_POSIX_SEM
121
+#		uses posix semaphores for locking (faster than sys v)
115 122
 # -DBUSY_WAIT
116 123
 #		uses busy waiting on the lock
117 124
 # -DADAPTIVE_WAIT
... ...
@@ -276,21 +283,26 @@ endif
276 276
 
277 277
 # arh. specific definitions
278 278
 ifeq ($(ARCH), i386)
279
-	DEFS+= -DFAST_LOCK
279
+	use_fast_lock=yes
280 280
 endif
281 281
 
282 282
 ifeq ($(ARCH), sparc64)
283 283
 ifeq 	($(CC_NAME), gcc)
284
-		DEFS+= -DFAST_LOCK
284
+	use_fast_lock=yes
285 285
 endif
286 286
 endif
287 287
 
288 288
 ifeq ($(ARCH), arm)
289
-	DEFS+= -DFAST_LOCK
289
+	use_fast_lock=yes
290 290
 endif
291 291
 
292 292
 ifeq ($(ARCH), ppc)
293
+	use_fast_lock=yes
294
+endif
295
+
296
+ifeq ($(use_fast_lock), yes)
293 297
 	DEFS+= -DFAST_LOCK
298
+	found_lock_method=yes
294 299
 endif
295 300
 
296 301
 CFLAGS=
... ...
@@ -514,10 +526,18 @@ LIBS= -lfl -ldl -lresolv
514 514
 #os specific stuff
515 515
 ifeq ($(OS), linux)
516 516
 	DEFS+=-DHAVE_GETHOSTBYNAME2 -DHAVE_UNION_SEMUN -DHAVE_SCHED_YIELD
517
+	ifneq ($(found_lock_method), yes)
518
+		DEFS+= -DUSE_SYSV_SEM  # try posix sems
519
+		found_lock_method=yes
520
+	endif
517 521
 endif
518 522
 
519 523
 ifeq  ($(OS), solaris)
520 524
 	DEFS+= -DHAVE_GETIPNODEBYNAME -DHAVE_SYS_SOCKIO_H -DHAVE_SCHED_YIELD
525
+	ifneq ($(found_lock_method), yes)
526
+		DEFS+= -DUSE_PTHREAD_MUTEX  # try pthread sems
527
+		found_lock_method=yes
528
+	endif
521 529
 	ifeq ($(mode), release)
522 530
 		#use these only if you're using gcc with Solaris ld
523 531
 		#LDFLAGS=-O2 $(PROFILE)
... ...
@@ -539,6 +559,10 @@ endif
539 539
 ifeq ($(OS), freebsd)
540 540
 	DEFS+=-DHAVE_SOCKADDR_SA_LEN -DHAVE_GETHOSTBYNAME2 -DHAVE_UNION_SEMUN \
541 541
 		-DHAVE_SCHED_YIELD
542
+	ifneq ($(found_lock_method), yes)
543
+		DEFS+= -DUSE_PTHREAD_MUTEX  # try pthread sems
544
+		found_lock_method=yes
545
+	endif
542 546
 	YACC=yacc
543 547
 	LIBS= -lfl  #dlopen is in libc
544 548
 endif
... ...
@@ -546,6 +570,10 @@ endif
546 546
 ifeq ($(OS), openbsd)
547 547
 	DEFS+=-DHAVE_SOCKADDR_SA_LEN -DDLSYM_PREFIX='"_"' -DHAVE_GETHOSTBYNAME2 \
548 548
 		-DHAVE_UNION_SEMUN
549
+	ifneq ($(found_lock_method), yes)
550
+		DEFS+= -DUSE_PTHREAD_MUTEX  # try pthread sems
551
+		found_lock_method=yes
552
+	endif
549 553
 	# (symbols on openbsd are prefixed by "_")
550 554
 	YACC=yacc
551 555
 	# no sched_yield on openbsd unless linking with c_r (not recommended)
... ...
@@ -555,10 +583,24 @@ endif
555 555
 	
556 556
 ifeq ($(OS), netbsd)
557 557
 	DEFS+=-DHAVE_SOCKADDR_SA_LEN -DHAVE_GETHOSTBYNAME2 
558
+	ifneq ($(found_lock_method), yes)
559
+		DEFS+= -DUSE_SYSV_SEM  # try pthread sems
560
+		found_lock_method=yes
561
+	endif
558 562
 	YACC=yacc
559 563
 	LIBS= -lfl 
560 564
 endif
561 565
 
562 566
 ifneq (,$(findstring CYGWIN, $(OS)))
563 567
 	#cygwin is the same as common
568
+	ifneq ($(found_lock_method), yes)
569
+		DEFS+= -DUSE_SYSV_SEM  # try sys v sems
570
+		found_lock_method=yes
571
+	endif
572
+endif
573
+
574
+ifneq ($(found_lock_method), yes)
575
+$(warning	No locking method found so far, trying SYS V sems)
576
+		DEFS+= -DUSE_SYSV_SEM  # try sys v sems
577
+		found_lock_method=yes
564 578
 endif
... ...
@@ -105,7 +105,7 @@ inline static lock_t* lock_init(lock_t* lock)
105 105
 }
106 106
 
107 107
 #define lock_get(lock) sem_wait(lock)
108
-#define lock_release(lock) sem_release(lock)
108
+#define lock_release(lock) sem_post(lock)
109 109
 
110 110
 
111 111
 #elif defined USE_SYSV_SEM
... ...
@@ -127,21 +127,16 @@ inline static lock_t* lock_init(lock_t* lock)
127 127
 
128 128
 typedef int lock_t;
129 129
 
130
-inline static lock_t* lock_alloc()
131
-{
132
-	lock_t* l;
133
-	
134
-	l=shm_malloc(sizeof(lock_t));
135
-	if (l==0) return 0;
136
-	*l=semget(IPC_PRIVATE, 1, 0700);
137
-	if (*l==-1) return 0;
138
-	return l;
139
-}
130
+#define lock_alloc() shm_malloc(sizeof(lock_t))
131
+#define lock_dealloc(lock) shm_free(lock)
140 132
 
141 133
 
142 134
 inline static lock_t* lock_init(lock_t* lock)
143 135
 {
144 136
 	union semun su;
137
+	
138
+	*lock=semget(IPC_PRIVATE, 1, 0700);
139
+	if (*lock==-1) return 0;
145 140
 	su.val=1;
146 141
 	if (semctl(*lock, 0, SETVAL, su)==-1){
147 142
 		/* init error*/
... ...
@@ -157,7 +152,7 @@ inline static void lock_destroy(lock_t* lock)
157 157
 
158 158
 #define lock_dealloc(lock) shm_free(lock)
159 159
 
160
-inline void lock_get(lock_t* lock)
160
+inline static void lock_get(lock_t* lock)
161 161
 {
162 162
 	struct sembuf sop;
163 163
 
... ...
@@ -167,7 +162,7 @@ inline void lock_get(lock_t* lock)
167 167
 	semop(*lock, &sop, 1);
168 168
 }
169 169
 
170
-inline void lock_release(lock_t* lock)
170
+inline static void lock_release(lock_t* lock)
171 171
 {
172 172
 	struct sembuf sop;
173 173
 	
... ...
@@ -141,6 +141,15 @@ static char flags[]=
141 141
 #ifdef BUSY_WAIT
142 142
 "-BUSY_WAIT"
143 143
 #endif
144
+#ifdef USE_PTHREAD_MUTEX
145
+", USE_PTHREAD_MUTEX"
146
+#endif
147
+#ifdef USE_POSIX_SEM
148
+", USE_POSIX_SEM"
149
+#endif
150
+#ifdef USE_SYSV_SEM
151
+", USE_SYSV_SEM"
152
+#endif
144 153
 #ifdef ADAPTIVE_WAIT
145 154
 "-ADAPTIVE_WAIT"
146 155
 #endif
... ...
@@ -45,37 +45,13 @@
45 45
 
46 46
 #endif
47 47
 
48
-#ifdef FAST_LOCK
49
-#include "../fastlock.h"
50
-#endif
51
-
52
-
53
-
54
-
55
-/* define semun */
56
-#if defined(HAVE_UNION_SEMUN) && !defined(_SEM_SEMUN_UNDEFINED)
57
-	/* union semun is defined by including <sys/sem.h> */
58
-#else
59
-	/* according to X/OPEN we have to define it ourselves */
60
-	union semun {
61
-		int val;                    /* value for SETVAL */
62
-		struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
63
-		unsigned short int *array;  /* array for GETALL, SETALL */
64
-		struct seminfo *__buf;      /* buffer for IPC_INFO */
65
-	};
66
-#endif
67
-
68 48
 
69 49
 
70 50
 #ifndef SHM_MMAP
71 51
 static int shm_shmid=-1; /*shared memory id*/
72 52
 #endif
73 53
 
74
-#ifdef FAST_LOCK
75
-fl_lock_t* mem_lock=0;
76
-#else
77
-int shm_semid=-1; /*semaphore id*/
78
-#endif
54
+lock_t* mem_lock=0;
79 55
 
80 56
 static void* shm_mempool=(void*)-1;
81 57
 #ifdef VQ_MALLOC
... ...
@@ -191,10 +167,6 @@ void* _shm_resize( void* p , unsigned int s)
191 191
 int shm_mem_init()
192 192
 {
193 193
 
194
-#ifndef FAST_LOCK
195
-	union semun su;
196
-	int ret;
197
-#endif
198 194
 #ifdef SHM_MMAP
199 195
 	int fd;
200 196
 #else
... ...
@@ -238,25 +210,6 @@ int shm_mem_init()
238 238
 		return -1;
239 239
 	}
240 240
 
241
-#ifndef FAST_LOCK
242
-	/* alloc a semaphore (for malloc)*/
243
-	shm_semid=semget(IPC_PRIVATE, 1, 0700);
244
-	if (shm_semid==-1){
245
-		LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate semaphore: %s\n",
246
-				strerror(errno));
247
-		shm_mem_destroy();
248
-		return -1;
249
-	}
250
-	/* set its value to 1 (mutex)*/
251
-	su.val=1;
252
-	ret=semctl(shm_semid, 0, SETVAL, su);
253
-	if (ret==-1){
254
-		LOG(L_CRIT, "ERROR: shm_mem_init: could not set initial semaphore"
255
-				" value: %s\n", strerror(errno));
256
-		shm_mem_destroy();
257
-		return -1;
258
-	}
259
-#endif
260 241
 	/* init it for malloc*/
261 242
 #	ifdef VQ_MALLOC
262 243
 		shm_block=vqm_malloc_init(shm_mempool, /* SHM_MEM_SIZE */ shm_mem_size );
... ...
@@ -271,10 +224,17 @@ int shm_mem_init()
271 271
 		shm_mem_destroy();
272 272
 		return -1;
273 273
 	}
274
-#ifdef FAST_LOCK
275
-	mem_lock=shm_malloc_unsafe(sizeof(fl_lock_t));
276
-	init_lock(*mem_lock);
277
-#endif
274
+	mem_lock=shm_malloc_unsafe(sizeof(lock_t)); /* skip lock_alloc, race cond*/
275
+	if (mem_lock==0){
276
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate lock\n");
277
+		shm_mem_destroy();
278
+		return -1;
279
+	}
280
+	if (lock_init(mem_lock)==0){
281
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not initialize lock\n");
282
+		shm_mem_destroy();
283
+		return -1;
284
+	}
278 285
 	
279 286
 	DBG("shm_mem_init: success\n");
280 287
 	
... ...
@@ -288,9 +248,6 @@ void shm_mem_destroy()
288 288
 #ifndef SHM_MMAP
289 289
 	struct shmid_ds shm_info;
290 290
 #endif
291
-#ifndef FAST_LOCK
292
-	union semun zero_un;
293
-#endif
294 291
 	
295 292
 	DBG("shm_mem_destroy\n");
296 293
 	if (shm_mempool && (shm_mempool!=(void*)-1)) {
... ...
@@ -307,13 +264,8 @@ void shm_mem_destroy()
307 307
 		shm_shmid=-1;
308 308
 	}
309 309
 #endif
310
-#ifndef FAST_LOCK
311
-	if (shm_semid!=-1) {
312
-		zero_un.val=0;
313
-		semctl(shm_semid, 0, IPC_RMID, zero_un);
314
-		shm_semid=-1;
315
-	}
316
-#endif
310
+	if (mem_lock) lock_destroy(mem_lock); /* we don't need to dealloc it*/
311
+	
317 312
 }
318 313
 
319 314
 
... ...
@@ -50,6 +50,7 @@
50 50
 
51 51
 
52 52
 #include "../dprint.h"
53
+#include "../locking.h"
53 54
 
54 55
 #ifdef VQ_MALLOC
55 56
 #	include "vq_malloc.h"
... ...
@@ -71,77 +72,17 @@
71 71
 #	define MY_STATUS qm_status
72 72
 #endif
73 73
 
74
-#ifdef FAST_LOCK
75
-#include "../fastlock.h"
76 74
 	
77
-	extern fl_lock_t* mem_lock;
78
-#else
79
-extern  int shm_semid;
80
-#endif
75
+	extern lock_t* mem_lock;
81 76
 
82 77
 
83 78
 int shm_mem_init();
84 79
 void shm_mem_destroy();
85 80
 
86 81
 
87
-#ifdef FAST_LOCK
88
-
89
-#define shm_lock()    get_lock(mem_lock)
90
-#define shm_unlock()  release_lock(mem_lock)
91
-
92
-#else
93
-/* inline functions (do not move them to *.c, they won't be inlined anymore) */
94
-static inline void shm_lock()
95
-{
96
-
97
-	struct sembuf sop;
98
-	int ret;
99
-	
100
-	sop.sem_num=0;
101
-	sop.sem_op=-1; /*down*/
102
-	sop.sem_flg=0 /*SEM_UNDO*/;
103
-again:
104
-	ret=semop(shm_semid, &sop, 1);
105
-
106
-	switch(ret){
107
-		case 0: /*ok*/
108
-			break;
109
-		case EINTR: /*interrupted by signal, try again*/
110
-			DBG("sh_lock: interrupted by signal, trying again...\n");
111
-			goto again;
112
-		default:
113
-			LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
114
-					strerror(errno));
115
-	}
116
-}
117
-
118
-
119
-
120
-static inline void shm_unlock()
121
-{
122
-	struct sembuf sop;
123
-	int ret;
124
-	
125
-	sop.sem_num=0;
126
-	sop.sem_op=1; /*up*/
127
-	sop.sem_flg=0 /*SEM_UNDO*/;
128
-again:
129
-	ret=semop(shm_semid, &sop, 1);
130
-	/*should ret immediately*/
131
-	switch(ret){
132
-		case 0: /*ok*/
133
-			break;
134
-		case EINTR: /*interrupted by signal, try again*/
135
-			DBG("sh_lock: interrupted by signal, trying again...\n");
136
-			goto again;
137
-		default:
138
-			LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
139
-					strerror(errno));
140
-	}
141
-}
142 82
 
143
-/* ret -1 on erro*/
144
-#endif
83
+#define shm_lock()    lock_get(mem_lock)
84
+#define shm_unlock()  lock_release(mem_lock)
145 85
 
146 86
 
147 87
 
... ...
@@ -140,7 +140,7 @@ tryagain:
140 140
 			DBG("signal received in a semaphore\n");
141 141
 			goto tryagain;
142 142
 		} else {
143
-			LOG(L_CRIT, "ERROR: change_semaphore(%x, %x, 1) : %s\n",
143
+			LOG(L_CRIT, "ERROR: change_semaphore(%x, %p, 1) : %s\n",
144 144
 					s->semaphore_set, &pbuf,
145 145
 					strerror(errno));
146 146
 		}
... ...
@@ -252,7 +252,7 @@ again:
252 252
 		if (errno==EINVAL || errno==ENOSPC ) {
253 253
 			DBG("DEBUG:lock_initialize: reply semaphore initialization"
254 254
 				" failure: %s\n", strerror(errno));
255
-			probe_run==1;
255
+			probe_run=1;
256 256
 			i--;
257 257
 			goto again;
258 258
 		}else{
... ...
@@ -27,8 +27,8 @@
27 27
 
28 28
 
29 29
 
30
-#ifndef _SEMAPHORE_H
31
-#define _SEMAPHORE_H
30
+#ifndef __lock_h
31
+#define __lock_h
32 32
 
33 33
 #include <sys/types.h>
34 34
 #include <sys/ipc.h>