Browse code

- preliminary general locking stuff (uses fast lock, pthread sems, posix sems or sys v sems depending on what USE_* is defined)

Andrei Pelinescu-Onciul authored on 13/01/2003 17:33:31
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,186 @@
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
+    - created 16.12.2003 (andrei)
30
+
31
+Implements:
32
+
33
+	lock_t* lock_alloc();                    - allocates a lock in shared mem.
34
+	lock_t* lock_init(lock_t* lock);         - inits the lock
35
+	void    lock_destroy(lock_t* lock);      - removes the lock (e.g sysv rmid)
36
+	void    lock_dealloc(lock_t* lock);      - deallocates the lock's shared m.
37
+	void    lock_get(lock_t* lock);          - lock (mutex down)
38
+	void    lock_release(lock_t* lock);      - unlock (mutex up)
39
+*/
40
+
41
+#ifndef _locking_h
42
+#define _locking_h
43
+
44
+#include "mem/mem.h"
45
+#ifdef SHM_MEM
46
+#include "mem/shm_mem.h"
47
+#else
48
+#error "locking requires shared memroy support"
49
+#endif
50
+
51
+#ifdef FAST_LOCK
52
+#include "fastlock.h"
53
+
54
+typedef fl_lock_t lock_t;
55
+
56
+#define lock_alloc() shm_malloc(sizeof(lock_t))
57
+#define lock_destroy(lock) /* do nothing */ 
58
+#define lock_dealloc(lock) shm_free(lock)
59
+
60
+inline static lock_t* lock_init(lock_t* lock)
61
+{
62
+	init_lock(*lock);
63
+	return lock;
64
+}
65
+
66
+#define lock_get(lock) get_lock(lock)
67
+#define lock_release(lock) release_lock(lock)
68
+
69
+
70
+
71
+#elif defined USE_PTHREAD_MUTEX
72
+#include <pthread.h>
73
+
74
+typedef pthread_mutex_t lock_t;
75
+
76
+#define lock_alloc() shm_malloc(sizeof(lock_t))
77
+#define lock_destroy(lock) /* do nothing */ 
78
+#define lock_dealloc(lock) shm_free(lock)
79
+
80
+inline static lock_t* lock_init(lock_t* lock)
81
+{
82
+	if (pthread_mutex_init(lock, 0)==0) return lock;
83
+	else return 0;
84
+}
85
+
86
+#define lock_get(lock) pthread_mutex_lock(lock)
87
+#define lock_release(lock) pthread_mutex_unlock(lock)
88
+
89
+
90
+
91
+#elif defined USE_POSIX_SEM
92
+#include <semaphore.h>
93
+
94
+typedef sem_t lock_t;
95
+
96
+#define lock_alloc() shm_malloc(sizeof(lock_t))
97
+#define lock_destroy(lock) /* do nothing */ 
98
+#define lock_dealloc(lock) shm_free(lock)
99
+
100
+inline static lock_t* lock_init(lock_t* lock)
101
+{
102
+	if (sem_init(lock, 0, 1)<0) return 0;
103
+	return lock;
104
+}
105
+
106
+#define lock_get(lock) sem_wait(lock)
107
+#define lock_release(lock) sem_release(lock)
108
+
109
+
110
+#elif defined USE_SYSV_SEM
111
+#include <sys/ipc.h>
112
+#include <sys/sem.h>
113
+
114
+#if ((defined(HAVE_UNION_SEMUN) || defined(__GNU_LIBRARY__) )&& !defined(_SEM_SEMUN_UNDEFINED)) 
115
+	
116
+	/* union semun is defined by including sem.h */
117
+#else
118
+	/* according to X/OPEN we have to define it ourselves */
119
+	union semun {
120
+		int val;                      /* value for SETVAL */
121
+		struct semid_ds *buf;         /* buffer for IPC_STAT, IPC_SET */
122
+		unsigned short int *array;    /* array for GETALL, SETALL */
123
+		struct seminfo *__buf;        /* buffer for IPC_INFO */
124
+	};
125
+#endif
126
+
127
+typedef int lock_t;
128
+
129
+inline static lock_t* lock_alloc()
130
+{
131
+	lock_t* l;
132
+	
133
+	l=shm_malloc(sizeof(lock_t));
134
+	if (l==0) return 0;
135
+	*l=semget(IPC_PRIVATE, 1, 0700);
136
+	if (*l==-1) return 0;
137
+	return l;
138
+}
139
+
140
+
141
+inline static lock_t* lock_init(lock_t* lock)
142
+{
143
+	union semun su;
144
+	su.val=1;
145
+	if (semctl(*lock, 0, SETVAL, su)==-1){
146
+		/* init error*/
147
+		return 0;
148
+	}
149
+	return lock;
150
+}
151
+
152
+inline static void lock_destroy(lock_t* lock)
153
+{
154
+	semctl(*lock, 0, IPC_RMID, (union semun)(int)0);
155
+}
156
+
157
+#define lock_dealloc(lock) shm_free(lock)
158
+
159
+inline void lock_get(lock_t* lock)
160
+{
161
+	struct sembuf sop;
162
+
163
+	sop.sem_num=0;
164
+	sop.sem_op=-1; /* down */
165
+	sop.sem_flg=0; /*SEM_UNDO*/
166
+	semop(*lock, &sop, 1);
167
+}
168
+
169
+inline void lock_release(lock_t* lock)
170
+{
171
+	struct sembuf sop;
172
+	
173
+	sop.sem_num=0;
174
+	sop.sem_op=1; /* up */
175
+	sop.sem_flg=0; /* SEM_UNDO*/
176
+	semop(*lock, &sop, 1);
177
+}
178
+
179
+#else
180
+#error "no locking method selected"
181
+#endif
182
+
183
+
184
+
185
+#endif