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