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