... | ... |
@@ -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 | 283 |
|
277 | 284 |
# arh. specific definitions |
278 | 285 |
ifeq ($(ARCH), i386) |
279 |
- DEFS+= -DFAST_LOCK |
|
286 |
+ use_fast_lock=yes |
|
280 | 287 |
endif |
281 | 288 |
|
282 | 289 |
ifeq ($(ARCH), sparc64) |
283 | 290 |
ifeq ($(CC_NAME), gcc) |
284 |
- DEFS+= -DFAST_LOCK |
|
291 |
+ use_fast_lock=yes |
|
285 | 292 |
endif |
286 | 293 |
endif |
287 | 294 |
|
288 | 295 |
ifeq ($(ARCH), arm) |
289 |
- DEFS+= -DFAST_LOCK |
|
296 |
+ use_fast_lock=yes |
|
290 | 297 |
endif |
291 | 298 |
|
292 | 299 |
ifeq ($(ARCH), ppc) |
300 |
+ use_fast_lock=yes |
|
301 |
+endif |
|
302 |
+ |
|
303 |
+ifeq ($(use_fast_lock), yes) |
|
293 | 304 |
DEFS+= -DFAST_LOCK |
305 |
+ found_lock_method=yes |
|
294 | 306 |
endif |
295 | 307 |
|
296 | 308 |
CFLAGS= |
... | ... |
@@ -514,10 +526,18 @@ LIBS= -lfl -ldl -lresolv |
514 | 526 |
#os specific stuff |
515 | 527 |
ifeq ($(OS), linux) |
516 | 528 |
DEFS+=-DHAVE_GETHOSTBYNAME2 -DHAVE_UNION_SEMUN -DHAVE_SCHED_YIELD |
529 |
+ ifneq ($(found_lock_method), yes) |
|
530 |
+ DEFS+= -DUSE_SYSV_SEM # try posix sems |
|
531 |
+ found_lock_method=yes |
|
532 |
+ endif |
|
517 | 533 |
endif |
518 | 534 |
|
519 | 535 |
ifeq ($(OS), solaris) |
520 | 536 |
DEFS+= -DHAVE_GETIPNODEBYNAME -DHAVE_SYS_SOCKIO_H -DHAVE_SCHED_YIELD |
537 |
+ ifneq ($(found_lock_method), yes) |
|
538 |
+ DEFS+= -DUSE_PTHREAD_MUTEX # try pthread sems |
|
539 |
+ found_lock_method=yes |
|
540 |
+ endif |
|
521 | 541 |
ifeq ($(mode), release) |
522 | 542 |
#use these only if you're using gcc with Solaris ld |
523 | 543 |
#LDFLAGS=-O2 $(PROFILE) |
... | ... |
@@ -539,6 +559,10 @@ endif |
539 | 559 |
ifeq ($(OS), freebsd) |
540 | 560 |
DEFS+=-DHAVE_SOCKADDR_SA_LEN -DHAVE_GETHOSTBYNAME2 -DHAVE_UNION_SEMUN \ |
541 | 561 |
-DHAVE_SCHED_YIELD |
562 |
+ ifneq ($(found_lock_method), yes) |
|
563 |
+ DEFS+= -DUSE_PTHREAD_MUTEX # try pthread sems |
|
564 |
+ found_lock_method=yes |
|
565 |
+ endif |
|
542 | 566 |
YACC=yacc |
543 | 567 |
LIBS= -lfl #dlopen is in libc |
544 | 568 |
endif |
... | ... |
@@ -546,6 +570,10 @@ endif |
546 | 570 |
ifeq ($(OS), openbsd) |
547 | 571 |
DEFS+=-DHAVE_SOCKADDR_SA_LEN -DDLSYM_PREFIX='"_"' -DHAVE_GETHOSTBYNAME2 \ |
548 | 572 |
-DHAVE_UNION_SEMUN |
573 |
+ ifneq ($(found_lock_method), yes) |
|
574 |
+ DEFS+= -DUSE_PTHREAD_MUTEX # try pthread sems |
|
575 |
+ found_lock_method=yes |
|
576 |
+ endif |
|
549 | 577 |
# (symbols on openbsd are prefixed by "_") |
550 | 578 |
YACC=yacc |
551 | 579 |
# no sched_yield on openbsd unless linking with c_r (not recommended) |
... | ... |
@@ -555,10 +583,24 @@ endif |
555 | 583 |
|
556 | 584 |
ifeq ($(OS), netbsd) |
557 | 585 |
DEFS+=-DHAVE_SOCKADDR_SA_LEN -DHAVE_GETHOSTBYNAME2 |
586 |
+ ifneq ($(found_lock_method), yes) |
|
587 |
+ DEFS+= -DUSE_SYSV_SEM # try pthread sems |
|
588 |
+ found_lock_method=yes |
|
589 |
+ endif |
|
558 | 590 |
YACC=yacc |
559 | 591 |
LIBS= -lfl |
560 | 592 |
endif |
561 | 593 |
|
562 | 594 |
ifneq (,$(findstring CYGWIN, $(OS))) |
563 | 595 |
#cygwin is the same as common |
596 |
+ ifneq ($(found_lock_method), yes) |
|
597 |
+ DEFS+= -DUSE_SYSV_SEM # try sys v sems |
|
598 |
+ found_lock_method=yes |
|
599 |
+ endif |
|
600 |
+endif |
|
601 |
+ |
|
602 |
+ifneq ($(found_lock_method), yes) |
|
603 |
+$(warning No locking method found so far, trying SYS V sems) |
|
604 |
+ DEFS+= -DUSE_SYSV_SEM # try sys v sems |
|
605 |
+ found_lock_method=yes |
|
564 | 606 |
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 | 152 |
|
158 | 153 |
#define lock_dealloc(lock) shm_free(lock) |
159 | 154 |
|
160 |
-inline void lock_get(lock_t* lock) |
|
155 |
+inline static void lock_get(lock_t* lock) |
|
161 | 156 |
{ |
162 | 157 |
struct sembuf sop; |
163 | 158 |
|
... | ... |
@@ -167,7 +162,7 @@ inline void lock_get(lock_t* lock) |
167 | 162 |
semop(*lock, &sop, 1); |
168 | 163 |
} |
169 | 164 |
|
170 |
-inline void lock_release(lock_t* lock) |
|
165 |
+inline static void lock_release(lock_t* lock) |
|
171 | 166 |
{ |
172 | 167 |
struct sembuf sop; |
173 | 168 |
|
... | ... |
@@ -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 | 167 |
int shm_mem_init() |
192 | 168 |
{ |
193 | 169 |
|
194 |
-#ifndef FAST_LOCK |
|
195 |
- union semun su; |
|
196 |
- int ret; |
|
197 |
-#endif |
|
198 | 170 |
#ifdef SHM_MMAP |
199 | 171 |
int fd; |
200 | 172 |
#else |
... | ... |
@@ -238,25 +210,6 @@ int shm_mem_init() |
238 | 210 |
return -1; |
239 | 211 |
} |
240 | 212 |
|
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 | 213 |
/* init it for malloc*/ |
261 | 214 |
# ifdef VQ_MALLOC |
262 | 215 |
shm_block=vqm_malloc_init(shm_mempool, /* SHM_MEM_SIZE */ shm_mem_size ); |
... | ... |
@@ -271,10 +224,17 @@ int shm_mem_init() |
271 | 224 |
shm_mem_destroy(); |
272 | 225 |
return -1; |
273 | 226 |
} |
274 |
-#ifdef FAST_LOCK |
|
275 |
- mem_lock=shm_malloc_unsafe(sizeof(fl_lock_t)); |
|
276 |
- init_lock(*mem_lock); |
|
277 |
-#endif |
|
227 |
+ mem_lock=shm_malloc_unsafe(sizeof(lock_t)); /* skip lock_alloc, race cond*/ |
|
228 |
+ if (mem_lock==0){ |
|
229 |
+ LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate lock\n"); |
|
230 |
+ shm_mem_destroy(); |
|
231 |
+ return -1; |
|
232 |
+ } |
|
233 |
+ if (lock_init(mem_lock)==0){ |
|
234 |
+ LOG(L_CRIT, "ERROR: shm_mem_init: could not initialize lock\n"); |
|
235 |
+ shm_mem_destroy(); |
|
236 |
+ return -1; |
|
237 |
+ } |
|
278 | 238 |
|
279 | 239 |
DBG("shm_mem_init: success\n"); |
280 | 240 |
|
... | ... |
@@ -288,9 +248,6 @@ void shm_mem_destroy() |
288 | 248 |
#ifndef SHM_MMAP |
289 | 249 |
struct shmid_ds shm_info; |
290 | 250 |
#endif |
291 |
-#ifndef FAST_LOCK |
|
292 |
- union semun zero_un; |
|
293 |
-#endif |
|
294 | 251 |
|
295 | 252 |
DBG("shm_mem_destroy\n"); |
296 | 253 |
if (shm_mempool && (shm_mempool!=(void*)-1)) { |
... | ... |
@@ -307,13 +264,8 @@ void shm_mem_destroy() |
307 | 264 |
shm_shmid=-1; |
308 | 265 |
} |
309 | 266 |
#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 |
|
267 |
+ if (mem_lock) lock_destroy(mem_lock); /* we don't need to dealloc it*/ |
|
268 |
+ |
|
317 | 269 |
} |
318 | 270 |
|
319 | 271 |
|
... | ... |
@@ -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 | 72 |
# define MY_STATUS qm_status |
72 | 73 |
#endif |
73 | 74 |
|
74 |
-#ifdef FAST_LOCK |
|
75 |
-#include "../fastlock.h" |
|
76 | 75 |
|
77 |
- extern fl_lock_t* mem_lock; |
|
78 |
-#else |
|
79 |
-extern int shm_semid; |
|
80 |
-#endif |
|
76 |
+ extern lock_t* mem_lock; |
|
81 | 77 |
|
82 | 78 |
|
83 | 79 |
int shm_mem_init(); |
84 | 80 |
void shm_mem_destroy(); |
85 | 81 |
|
86 | 82 |
|
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 | 83 |
|
143 |
-/* ret -1 on erro*/ |
|
144 |
-#endif |
|
84 |
+#define shm_lock() lock_get(mem_lock) |
|
85 |
+#define shm_unlock() lock_release(mem_lock) |
|
145 | 86 |
|
146 | 87 |
|
147 | 88 |
|
... | ... |
@@ -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{ |