Browse code

- added lock sets to locking.h - locking.h splitted in 2: lock_alloc & lock_ops to solve some interdependency between shm_mem.h & locking.h (WARNING: include only locking.h unless you know what you're doing).

Andrei Pelinescu-Onciul authored on 06/03/2003 16:22:10
Showing 5 changed files
... ...
@@ -15,7 +15,7 @@
15 15
 VERSION = 0
16 16
 PATCHLEVEL = 8
17 17
 SUBLEVEL =   11
18
-EXTRAVERSION = pre6-tcp9-tm
18
+EXTRAVERSION = pre6-tcp10-lck
19 19
 
20 20
 RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
21 21
 OS = $(shell uname -s | sed -e s/SunOS/solaris/ | tr "[A-Z]" "[a-z]")
22 22
new file mode 100644
... ...
@@ -0,0 +1,107 @@
0
+/* $Id$ */
1
+/*
2
+ *
3
+ * Copyright (C) 2001-2003 Fhg Fokus
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+
27
+/*
28
+ *   ser locking library
29
+ *   WARNING: don't include this directly include instead locking.h!
30
+ *
31
+ *  2002-03-06  created by andrei (contains parts of the original locking.h)
32
+ *
33
+Implements: (see also locking.h)
34
+
35
+	simple locks:
36
+	-------------
37
+	gen_lock_t* lock_alloc();                - allocates a lock in shared mem.
38
+	void    lock_dealloc(gen_lock_t* lock);  - deallocates the lock's shared m.
39
+	
40
+	lock sets: [implemented only for FL & SYSV so far]
41
+	----------
42
+	lock_set_t* lock_set_alloc(no)               - allocs a lock set in shm.
43
+	void lock_set_dealloc(lock_set_t* s);        - deallocs the lock set shm.
44
+
45
+*/
46
+
47
+#ifndef _lock_alloc_h
48
+#define _lock_alloc_h
49
+
50
+/*shm_{malloc, free}*/
51
+#include "mem/mem.h"
52
+#ifdef SHM_MEM
53
+#include "mem/shm_mem.h"
54
+#else
55
+#error "locking requires shared memroy support"
56
+#endif
57
+
58
+#if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM)
59
+/* simple locks*/
60
+#define lock_alloc() shm_malloc(sizeof(gen_lock_t))
61
+#define lock_dealloc(lock) shm_free(lock)
62
+/* lock sets */
63
+
64
+inline static lock_set_t* lock_set_alloc(int n)
65
+{
66
+	lock_set_t* ls;
67
+	ls=(lock_set_t*)shm_malloc(sizeof(lock_set_t)+n*sizeof(gen_lock_t));
68
+	if (ls==0){
69
+		LOG(L_CRIT, "ERROR: lock_set_alloc (FL): could not allocate lock_set\n");
70
+	}else{
71
+		ls->locks=(gen_lock_t*)((char*)ls+sizeof(lock_set_t));
72
+		ls->size=n;
73
+	}
74
+	return ls;
75
+}
76
+
77
+#define lock_set_dealloc(lock_set) shm_free(lock_set)
78
+
79
+#elif defined USE_SYSV_SEM
80
+
81
+/*simple locks*/
82
+#define lock_alloc() shm_malloc(sizeof(gen_lock_t))
83
+#define lock_dealloc(lock) shm_free(lock)
84
+/* lock sets */
85
+
86
+inline static lock_set_t* lock_set_alloc(int n)
87
+{
88
+	lock_set_t* ls;
89
+	ls=(lock_set_t*)shm_malloc(sizeof(lock_set_t));
90
+	if (ls){
91
+		ls->size=n;
92
+		ls->semid=-1;
93
+	};
94
+	return ls;
95
+}
96
+
97
+
98
+#define lock_set_dealloc(lock_set) shm_free(lock_set)
99
+
100
+
101
+#else
102
+#error "no locking method selected"
103
+#endif
104
+
105
+
106
+#endif
0 107
new file mode 100644
... ...
@@ -0,0 +1,256 @@
0
+/* $Id$ */
1
+/*
2
+ *
3
+ * Copyright (C) 2001-2003 Fhg Fokus
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License 
23
+ * along with this program; if not, write to the Free Software 
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ */
26
+
27
+/*
28
+ *   ser locking library
29
+ *   WARNING: do not include this file directly, use instead locking.h
30
+ *   (unless you don't need to alloc/dealloc locks)
31
+ *
32
+ *  2002-12-16  created by andrei
33
+ *  2003-02-20  s/gen_lock_t/gen_lock_t/ to avoid a type conflict 
34
+ *               on solaris  (andrei)
35
+ *  2003-03-05  lock set support added for FAST_LOCK & SYSV (andrei)
36
+ *  2003-03-06  removed *_alloc,*_dealloc & moved them to lock_alloc.h
37
+ *              renamed locking.h to lock_ops.h (all this to solve
38
+ *              the locking.h<->shm_mem.h interdependency) (andrei)
39
+ *
40
+Implements:
41
+
42
+	simple locks:
43
+	-------------
44
+	gen_lock_t* lock_init(gen_lock_t* lock); - inits the lock
45
+	void    lock_destroy(gen_lock_t* lock);  - removes the lock (e.g sysv rmid)
46
+	void    lock_get(gen_lock_t* lock);      - lock (mutex down)
47
+	void    lock_release(gen_lock_t* lock);  - unlock (mutex up)
48
+	
49
+	lock sets: [implemented only for FL & SYSV so far]
50
+	----------
51
+	lock_set_t* lock_set_init(lock_set_t* set);  - inits the lock set
52
+	void lock_set_destroy(lock_set_t* s);        - removes the lock set
53
+	void lock_set_get(lock_set_t* s, int i);     - locks sem i from the set
54
+	void lock_set_release(lock_set_t* s, int i)  - unlocks sem i from the set
55
+
56
+WARNING: signals are not treated! (some locks are "awakened" by the signals)
57
+*/
58
+
59
+#ifndef _lock_ops_h
60
+#define _lock_ops_h
61
+
62
+
63
+#ifdef FAST_LOCK
64
+#include "fastlock.h"
65
+
66
+typedef fl_lock_t gen_lock_t;
67
+
68
+struct lock_set_t_ {
69
+	long size;
70
+	gen_lock_t* locks;
71
+}; /* must be  aligned (32 bits or 64 depending on the arch)*/
72
+typedef struct lock_set_t_ lock_set_t;
73
+
74
+#define lock_destroy(lock) /* do nothing */ 
75
+
76
+inline static gen_lock_t* lock_init(gen_lock_t* lock)
77
+{
78
+	init_lock(*lock);
79
+	return lock;
80
+}
81
+
82
+#define lock_get(lock) get_lock(lock)
83
+#define lock_release(lock) release_lock(lock)
84
+
85
+/* lock sets */
86
+
87
+#define lock_set_destroy(lock_set) /* do nothing */
88
+
89
+inline static lock_set_t* lock_set_init(lock_set_t* s)
90
+{
91
+	int r;
92
+	for (r=0; r<s->size; r++) lock_init(&s->locks[r]);
93
+	return s;
94
+}
95
+
96
+/* WARNING: no boundary checks!*/
97
+#define lock_set_get(set, i) lock_get(&set->locks[i])
98
+#define lock_set_release(set, i) lock_release(&set->locks[i])
99
+
100
+#elif defined USE_PTHREAD_MUTEX
101
+#include <pthread.h>
102
+
103
+typedef pthread_mutex_t gen_lock_t;
104
+
105
+#define lock_destroy(lock) /* do nothing */ 
106
+
107
+inline static gen_lock_t* lock_init(gen_lock_t* lock)
108
+{
109
+	if (pthread_mutex_init(lock, 0)==0) return lock;
110
+	else return 0;
111
+}
112
+
113
+#define lock_get(lock) pthread_mutex_lock(lock)
114
+#define lock_release(lock) pthread_mutex_unlock(lock)
115
+
116
+
117
+
118
+#elif defined USE_POSIX_SEM
119
+#include <semaphore.h>
120
+
121
+typedef sem_t gen_lock_t;
122
+
123
+#define lock_destroy(lock) /* do nothing */ 
124
+
125
+inline static gen_lock_t* lock_init(gen_lock_t* lock)
126
+{
127
+	if (sem_init(lock, 1, 1)<0) return 0;
128
+	return lock;
129
+}
130
+
131
+#define lock_get(lock) sem_wait(lock)
132
+#define lock_release(lock) sem_post(lock)
133
+
134
+
135
+#elif defined USE_SYSV_SEM
136
+#include <sys/ipc.h>
137
+#include <sys/sem.h>
138
+
139
+#if ((defined(HAVE_UNION_SEMUN) || defined(__GNU_LIBRARY__) )&& !defined(_SEM_SEMUN_UNDEFINED)) 
140
+	
141
+	/* union semun is defined by including sem.h */
142
+#else
143
+	/* according to X/OPEN we have to define it ourselves */
144
+	union semun {
145
+		int val;                      /* value for SETVAL */
146
+		struct semid_ds *buf;         /* buffer for IPC_STAT, IPC_SET */
147
+		unsigned short int *array;    /* array for GETALL, SETALL */
148
+		struct seminfo *__buf;        /* buffer for IPC_INFO */
149
+	};
150
+#endif
151
+
152
+typedef int gen_lock_t;
153
+
154
+struct lock_set_t_ {
155
+	int size;
156
+	int semid;
157
+};
158
+typedef struct lock_set_t_ lock_set_t;
159
+
160
+
161
+
162
+inline static gen_lock_t* lock_init(gen_lock_t* lock)
163
+{
164
+	union semun su;
165
+	
166
+	*lock=semget(IPC_PRIVATE, 1, 0700);
167
+	if (*lock==-1) return 0;
168
+	su.val=1;
169
+	if (semctl(*lock, 0, SETVAL, su)==-1){
170
+		/* init error*/
171
+		return 0;
172
+	}
173
+	return lock;
174
+}
175
+
176
+inline static void lock_destroy(gen_lock_t* lock)
177
+{
178
+	semctl(*lock, 0, IPC_RMID, (union semun)(int)0);
179
+}
180
+
181
+
182
+inline static void lock_get(gen_lock_t* lock)
183
+{
184
+	struct sembuf sop;
185
+
186
+	sop.sem_num=0;
187
+	sop.sem_op=-1; /* down */
188
+	sop.sem_flg=0; 
189
+	semop(*lock, &sop, 1);
190
+}
191
+
192
+inline static void lock_release(gen_lock_t* lock)
193
+{
194
+	struct sembuf sop;
195
+	
196
+	sop.sem_num=0;
197
+	sop.sem_op=1; /* up */
198
+	sop.sem_flg=0; 
199
+	semop(*lock, &sop, 1);
200
+}
201
+
202
+/* lock sets */
203
+
204
+inline static lock_set_t* lock_set_init(lock_set_t* s)
205
+{
206
+	union semun su;
207
+	int r;
208
+	
209
+	s->semid=semget(IPC_PRIVATE, s->size, 0700);
210
+	if (s->semid==-1){
211
+		LOG(L_CRIT, "ERROR: lock_set_init (SYSV): semget failed: %s\n",
212
+				strerror(errno));
213
+		return 0;
214
+	}
215
+	su.val=1;
216
+	for (r=0; r<s->size; r++){
217
+		if (semctl(s->semid, r, SETVAL, su)==-1){
218
+			LOG(L_CRIT, "ERROR: lock_set_init (SYSV): semctl failed on sem %d"
219
+					": %s\n", r, strerror(errno));
220
+			semctl(s->semid, 0, IPC_RMID, (union semun)(int)0);
221
+			return 0;
222
+		}
223
+	}
224
+	return s;
225
+}
226
+
227
+inline static void lock_set_destroy(lock_set_t* s)
228
+{
229
+	semctl(s->semid, 0, IPC_RMID, (union semun)(int)0);
230
+}
231
+
232
+inline static void lock_set_get(lock_set_t* s, int n)
233
+{
234
+	struct sembuf sop;
235
+	sop.sem_num=n;
236
+	sop.sem_op=-1; /* down */
237
+	sop.sem_flg=0;
238
+	semop(s->semid, &sop, 1);
239
+}
240
+
241
+inline static void lock_set_release(lock_set_t* s, int n)
242
+{
243
+	struct sembuf sop;
244
+	sop.sem_num=n;
245
+	sop.sem_op=1; /* up */
246
+	sop.sem_flg=0;
247
+	semop(s->semid, &sop, 1);
248
+}
249
+
250
+#else
251
+#error "no locking method selected"
252
+#endif
253
+
254
+
255
+#endif
... ...
@@ -31,155 +31,38 @@
31 31
  *  2002-12-16  created by andrei
32 32
  *  2003-02-20  s/gen_lock_t/gen_lock_t/ to avoid a type conflict 
33 33
  *               on solaris  (andrei)
34
+ *  2003-03-05  lock set support added for FAST_LOCK & SYSV (andrei)
35
+ *  2003-03-06  splited in two: lock_ops.h & lock_alloc.h, to avoid
36
+ *               shm_mem.h<->locking.h interdependency (andrei)
34 37
  *
35
-Implements:
36
-
37
-	gen_lock_t* lock_alloc();               - allocates a lock in shared mem.
38
-	gen_lock_t* lock_init(gen_lock_t* lock);    - inits the lock
39
-	void    lock_destroy(gen_lock_t* lock); - removes the lock (e.g sysv rmid)
40
-	void    lock_dealloc(gen_lock_t* lock); - deallocates the lock's shared m.
41
-	void    lock_get(gen_lock_t* lock);     - lock (mutex down)
42
-	void    lock_release(gen_lock_t* lock); - unlock (mutex up)
38
+Implements (in lock_ops.h & lock_alloc.h):
39
+
40
+	simple locks:
41
+	-------------
42
+	gen_lock_t* lock_alloc();                - allocates a lock in shared mem.
43
+	gen_lock_t* lock_init(gen_lock_t* lock); - inits the lock
44
+	void    lock_destroy(gen_lock_t* lock);  - removes the lock (e.g sysv rmid)
45
+	void    lock_dealloc(gen_lock_t* lock);  - deallocates the lock's shared m.
46
+	void    lock_get(gen_lock_t* lock);      - lock (mutex down)
47
+	void    lock_release(gen_lock_t* lock);  - unlock (mutex up)
48
+	
49
+	lock sets: [implemented only for FL & SYSV so far]
50
+	----------
51
+	lock_set_t* lock_set_alloc(no)               - allocs a lock set in shm.
52
+	lock_set_t* lock_set_init(lock_set_t* set);  - inits the lock set
53
+	void lock_set_destroy(lock_set_t* s);        - removes the lock set
54
+	void lock_set_dealloc(lock_set_t* s);        - deallocs the lock set shm.
55
+	void lock_set_get(lock_set_t* s, int i);     - locks sem i from the set
56
+	void lock_set_release(lock_set_t* s, int i)  - unlocks sem i from the set
57
+
58
+WARNING: signals are not treated! (some locks are "awakened" by the signals)
43 59
 */
44 60
 
45 61
 #ifndef _locking_h
46 62
 #define _locking_h
47 63
 
48
-
49
-#ifdef FAST_LOCK
50
-#include "fastlock.h"
51
-
52
-typedef fl_lock_t gen_lock_t;
53
-
54
-#define lock_alloc() shm_malloc(sizeof(gen_lock_t))
55
-#define lock_destroy(lock) /* do nothing */ 
56
-#define lock_dealloc(lock) shm_free(lock)
57
-
58
-inline static gen_lock_t* lock_init(gen_lock_t* lock)
59
-{
60
-	init_lock(*lock);
61
-	return lock;
62
-}
63
-
64
-#define lock_get(lock) get_lock(lock)
65
-#define lock_release(lock) release_lock(lock)
66
-
67
-
68
-
69
-#elif defined USE_PTHREAD_MUTEX
70
-#include <pthread.h>
71
-
72
-typedef pthread_mutex_t gen_lock_t;
73
-
74
-#define lock_alloc() shm_malloc(sizeof(gen_lock_t))
75
-#define lock_destroy(lock) /* do nothing */ 
76
-#define lock_dealloc(lock) shm_free(lock)
77
-
78
-inline static gen_lock_t* lock_init(gen_lock_t* lock)
79
-{
80
-	if (pthread_mutex_init(lock, 0)==0) return lock;
81
-	else return 0;
82
-}
83
-
84
-#define lock_get(lock) pthread_mutex_lock(lock)
85
-#define lock_release(lock) pthread_mutex_unlock(lock)
86
-
87
-
88
-
89
-#elif defined USE_POSIX_SEM
90
-#include <semaphore.h>
91
-
92
-typedef sem_t gen_lock_t;
93
-
94
-#define lock_alloc() shm_malloc(sizeof(gen_lock_t))
95
-#define lock_destroy(lock) /* do nothing */ 
96
-#define lock_dealloc(lock) shm_free(lock)
97
-
98
-inline static gen_lock_t* lock_init(gen_lock_t* lock)
99
-{
100
-	if (sem_init(lock, 1, 1)<0) return 0;
101
-	return lock;
102
-}
103
-
104
-#define lock_get(lock) sem_wait(lock)
105
-#define lock_release(lock) sem_post(lock)
106
-
107
-
108
-#elif defined USE_SYSV_SEM
109
-#include <sys/ipc.h>
110
-#include <sys/sem.h>
111
-
112
-#if ((defined(HAVE_UNION_SEMUN) || defined(__GNU_LIBRARY__) )&& !defined(_SEM_SEMUN_UNDEFINED)) 
113
-	
114
-	/* union semun is defined by including sem.h */
115
-#else
116
-	/* according to X/OPEN we have to define it ourselves */
117
-	union semun {
118
-		int val;                      /* value for SETVAL */
119
-		struct semid_ds *buf;         /* buffer for IPC_STAT, IPC_SET */
120
-		unsigned short int *array;    /* array for GETALL, SETALL */
121
-		struct seminfo *__buf;        /* buffer for IPC_INFO */
122
-	};
123
-#endif
124
-
125
-typedef int gen_lock_t;
126
-
127
-#define lock_alloc() shm_malloc(sizeof(gen_lock_t))
128
-#define lock_dealloc(lock) shm_free(lock)
129
-
130
-
131
-inline static gen_lock_t* lock_init(gen_lock_t* lock)
132
-{
133
-	union semun su;
134
-	
135
-	*lock=semget(IPC_PRIVATE, 1, 0700);
136
-	if (*lock==-1) return 0;
137
-	su.val=1;
138
-	if (semctl(*lock, 0, SETVAL, su)==-1){
139
-		/* init error*/
140
-		return 0;
141
-	}
142
-	return lock;
143
-}
144
-
145
-inline static void lock_destroy(gen_lock_t* lock)
146
-{
147
-	semctl(*lock, 0, IPC_RMID, (union semun)(int)0);
148
-}
149
-
150
-#define lock_dealloc(lock) shm_free(lock)
151
-
152
-inline static void lock_get(gen_lock_t* lock)
153
-{
154
-	struct sembuf sop;
155
-
156
-	sop.sem_num=0;
157
-	sop.sem_op=-1; /* down */
158
-	sop.sem_flg=0; /*SEM_UNDO*/
159
-	semop(*lock, &sop, 1);
160
-}
161
-
162
-inline static void lock_release(gen_lock_t* lock)
163
-{
164
-	struct sembuf sop;
165
-	
166
-	sop.sem_num=0;
167
-	sop.sem_op=1; /* up */
168
-	sop.sem_flg=0; /* SEM_UNDO*/
169
-	semop(*lock, &sop, 1);
170
-}
171
-
172
-#else
173
-#error "no locking method selected"
174
-#endif
175
-
176
-
177
-/*shm_{malloc, free}*/
178
-#include "mem/mem.h"
179
-#ifdef SHM_MEM
180
-#include "mem/shm_mem.h"
181
-#else
182
-#error "locking requires shared memroy support"
183
-#endif
64
+/* the order is important */
65
+#include "lock_ops.h"
66
+#include "lock_alloc.h" 
184 67
 
185 68
 #endif
... ...
@@ -50,7 +50,7 @@
50 50
 
51 51
 
52 52
 #include "../dprint.h"
53
-#include "../locking.h"
53
+#include "../lock_ops.h" /* we don't include locking.h on purpose */
54 54
 
55 55
 #ifdef VQ_MALLOC
56 56
 #	include "vq_malloc.h"