File: lock_ops.h

package info (click to toggle)
kamailio 4.2.0-2+deb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (420 lines) | stat: -rw-r--r-- 11,058 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* $Id$ */
/*
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 *   ser locking library
 *   WARNING: do not include this file directly, use instead locking.h
 *   (unless you don't need to alloc/dealloc locks)
 *
 *  2002-12-16  created by andrei
 *  2003-02-20  s/gen_lock_t/gen_lock_t/ to avoid a type conflict 
 *               on solaris  (andrei)
 *  2003-03-05  lock set support added for FAST_LOCK & SYSV (andrei)
 *  2003-03-06  removed *_alloc,*_dealloc & moved them to lock_alloc.h
 *              renamed locking.h to lock_ops.h (all this to solve
 *              the locking.h<->shm_mem.h interdependency) (andrei)
 *  2003-03-10  lock set support added also for PTHREAD_MUTEX & POSIX_SEM
 *               (andrei)
 *  2003-03-17  possible signal interruptions treated for sysv (andrei)
 *  2004-07-28  s/lock_set_t/gen_lock_set_t/ because of a type conflict
 *              on darwin (andrei)
 *  2006-04-04  added lock_try(lock) and lock_set_try(s,i) (andrei)
 *  2007-05-13  added futex support (andrei)
 *
Implements:

	simple locks:
	-------------
	gen_lock_t* lock_init(gen_lock_t* lock); - inits the lock
	void    lock_destroy(gen_lock_t* lock);  - removes the lock (e.g sysv rmid)
	void    lock_get(gen_lock_t* lock);      - lock (mutex down)
	void    lock_release(gen_lock_t* lock);  - unlock (mutex up)
	int     lock_try(gen_lock_t* lock);      - tries to get the lock, returns
	                                            0 on success and !=0 on failure
	
	lock sets: 
	----------
	gen_lock_set_t* lock_set_init(gen_lock_set_t* set);  - inits the lock set
	void lock_set_destroy(gen_lock_set_t* s);        - removes the lock set
	void lock_set_get(gen_lock_set_t* s, int i);     - locks sem i from the set
	void lock_set_release(gen_lock_set_t* s, int i)  - unlocks sem i from the
	                                                   set
	int  lock_set_try(gen_lock_set_t* s, int i);    - tries to lock the sem i,
	                                                  returns 0 on success and
	                                                  !=0 on failure
	
	defines:
	--------
	GEN_LOCK_T_PREFERRED - defined if using  arrays of gen_lock_t is as good as
	                      using a lock set (gen_lock_set_t). 
						  In general is better to have the locks "close" or 
						  inside the protected data structure rather then 
						  having a separate array or lock set. However in some
						  case (e.g. SYSV_LOCKS) is better to use lock sets,
						  either due to lock number limitations, excesive 
						  performance or memory overhead. In this cases
						  GEN_LOCK_T_PREFERRED will not be defined.
	GEN_LOCK_T_UNLIMITED - defined if there is no system imposed limit on
	                       the number of locks (other then the memory).
	GEN_LOCK_SET_T_UNLIMITED
	                      - like above but for the size of a lock set.

WARNING: - lock_set_init may fail for large number of sems (e.g. sysv). 
         - signals are not treated! (some locks are "awakened" by the signals)
*/

#ifndef _lock_ops_h
#define _lock_ops_h

#ifdef USE_FUTEX
#include "futexlock.h"
/* if no native atomic ops support => USE_FUTEX will be undefined */
#endif


#ifdef USE_FUTEX

typedef futex_lock_t gen_lock_t;

#define lock_destroy(lock) /* do nothing */
#define lock_init(lock) futex_init(lock)
#define lock_try(lock)  futex_try(lock)
#define lock_get(lock)  futex_get(lock)
#define lock_release(lock) futex_release(lock)


#elif defined FAST_LOCK
#include "fastlock.h"

typedef fl_lock_t gen_lock_t;


#define lock_destroy(lock) /* do nothing */ 

inline static gen_lock_t* lock_init(gen_lock_t* lock)
{
	init_lock(*lock);
	return lock;
}

#define lock_try(lock) try_lock(lock)
#define lock_get(lock) get_lock(lock)
#define lock_release(lock) release_lock(lock)


#elif defined USE_PTHREAD_MUTEX
#include <pthread.h>

typedef pthread_mutex_t gen_lock_t;

#define lock_destroy(lock) /* do nothing */ 

inline static gen_lock_t* lock_init(gen_lock_t* lock)
{
	if (pthread_mutex_init(lock, 0)==0) return lock;
	else return 0;
}

#define lock_try(lock) pthread_mutex_trylock(lock)
#define lock_get(lock) pthread_mutex_lock(lock)
#define lock_release(lock) pthread_mutex_unlock(lock)



#elif defined USE_POSIX_SEM
#include <semaphore.h>

typedef sem_t gen_lock_t;

#define lock_destroy(lock) /* do nothing */ 

inline static gen_lock_t* lock_init(gen_lock_t* lock)
{
	if (sem_init(lock, 1, 1)<0) return 0;
	return lock;
}

#define lock_try(lock) sem_trywait(lock)
#define lock_get(lock) sem_wait(lock)
#define lock_release(lock) sem_post(lock)


#elif defined USE_SYSV_SEM
#include <sys/ipc.h>
#include <sys/sem.h>

#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "dprint.h"
#include "globals.h" /* uid */

#if ((defined(HAVE_UNION_SEMUN) || defined(__GNU_LIBRARY__) )&& !defined(_SEM_SEMUN_UNDEFINED)) 
	
	/* union semun is defined by including sem.h */
#else
	/* according to X/OPEN we have to define it ourselves */
	union semun {
		int val;                      /* value for SETVAL */
		struct semid_ds *buf;         /* buffer for IPC_STAT, IPC_SET */
		unsigned short int *array;    /* array for GETALL, SETALL */
		struct seminfo *__buf;        /* buffer for IPC_INFO */
	};
#endif

typedef int gen_lock_t;




inline static gen_lock_t* lock_init(gen_lock_t* lock)
{
	union semun su;
	int euid;
	
	euid=geteuid();
	if (uid && uid!=euid)
		seteuid(uid); /* set euid to the cfg. requested one */
	*lock=semget(IPC_PRIVATE, 1, 0700);
	if (uid && uid!=euid)
		seteuid(euid); /* restore it */
	if (*lock==-1) return 0;
	su.val=1;
	if (semctl(*lock, 0, SETVAL, su)==-1){
		/* init error*/
		return 0;
	}
	return lock;
}

inline static void lock_destroy(gen_lock_t* lock)
{
	semctl(*lock, 0, IPC_RMID, (union semun)(int)0);
}


/* returns 0 if it got the lock, -1 otherwise */
inline static int lock_try(gen_lock_t* lock)
{
	struct sembuf sop;

	sop.sem_num=0;
	sop.sem_op=-1; /* down */
	sop.sem_flg=IPC_NOWAIT; 
tryagain:
	if (semop(*lock, &sop, 1)==-1){
		if (errno==EAGAIN){
			return -1;
		}else if (errno==EINTR){
			DBG("lock_get: signal received while waiting for on a mutex\n");
			goto tryagain;
		}else{
			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
			return -1;
		}
	}
	return 0;
}

inline static void lock_get(gen_lock_t* lock)
{
	struct sembuf sop;

	sop.sem_num=0;
	sop.sem_op=-1; /* down */
	sop.sem_flg=0; 
tryagain:
	if (semop(*lock, &sop, 1)==-1){
		if (errno==EINTR){
			DBG("lock_get: signal received while waiting for on a mutex\n");
			goto tryagain;
		}else{
			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
		}
	}
}

inline static void lock_release(gen_lock_t* lock)
{
	struct sembuf sop;
	
	sop.sem_num=0;
	sop.sem_op=1; /* up */
	sop.sem_flg=0; 
tryagain:
	if (semop(*lock, &sop, 1)==-1){
		if (errno==EINTR){
			/* very improbable*/
			DBG("lock_release: signal received while releasing a mutex\n");
			goto tryagain;
		}else{
			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
		}
	}
}


#else
#error "no locking method selected"
#endif


/* lock sets */

#if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || \
	defined(USE_POSIX_SEM) || defined(USE_FUTEX)
#define GEN_LOCK_T_PREFERRED
#define GEN_LOCK_T_PREFERED  /* backwards compat. */
#define GEN_LOCK_T_UNLIMITED
#define GEN_LOCK_SET_T_UNLIMITED

struct gen_lock_set_t_ {
	long size;
	gen_lock_t* locks;
}; /* must be  aligned (32 bits or 64 depending on the arch)*/
typedef struct gen_lock_set_t_ gen_lock_set_t;


#define lock_set_destroy(lock_set) /* do nothing */

inline static gen_lock_set_t* lock_set_init(gen_lock_set_t* s)
{
	int r;
	for (r=0; r<s->size; r++) if (lock_init(&s->locks[r])==0) return 0;
	return s;
}

/* WARNING: no boundary checks!*/
#define lock_set_try(set, i) lock_try(&set->locks[i])
#define lock_set_get(set, i) lock_get(&set->locks[i])
#define lock_set_release(set, i) lock_release(&set->locks[i])

#elif defined(USE_SYSV_SEM)
#undef GEN_LOCK_T_PREFERRED
#undef GEN_LOCK_T_PREFERED  /* backwards compat. */
#undef GEN_LOCK_T_UNLIMITED
#undef GEN_LOCK_SET_T_UNLIMITED
#define GEN_LOCK_T_LIMITED
#define GEN_LOCK_SET_T_LIMITED

struct gen_lock_set_t_ {
	int size;
	int semid;
};


typedef struct gen_lock_set_t_ gen_lock_set_t;
inline static gen_lock_set_t* lock_set_init(gen_lock_set_t* s)
{
	union semun su;
	int r;
	int euid;

	euid=geteuid();
	if (uid && uid!=euid)
		seteuid(uid); /* set euid to the cfg. requested one */
	s->semid=semget(IPC_PRIVATE, s->size, 0700);
	if (uid && uid!=euid)
		seteuid(euid); /* restore euid */
	if (s->semid==-1){
		LM_CRIT("(SYSV): semget (..., %d, 0700) failed: %s\n",
				s->size, strerror(errno));
		return 0;
	}
	su.val=1;
	for (r=0; r<s->size; r++){
		if (semctl(s->semid, r, SETVAL, su)==-1){
			LM_CRIT("(SYSV): semctl failed on sem %d: %s\n", r, strerror(errno));
			semctl(s->semid, 0, IPC_RMID, (union semun)(int)0);
			return 0;
		}
	}
	return s;
}

inline static void lock_set_destroy(gen_lock_set_t* s)
{
	semctl(s->semid, 0, IPC_RMID, (union semun)(int)0);
}


/* returns 0 if it "gets" the lock, -1 otherwise */
inline static int lock_set_try(gen_lock_set_t* s, int n)
{
	struct sembuf sop;
	
	sop.sem_num=n;
	sop.sem_op=-1; /* down */
	sop.sem_flg=IPC_NOWAIT; 
tryagain:
	if (semop(s->semid, &sop, 1)==-1){
		if (errno==EAGAIN){
			return -1;
		}else if (errno==EINTR){
			DBG("lock_get: signal received while waiting for on a mutex\n");
			goto tryagain;
		}else{
			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
			return -1;
		}
	}
	return 0;
}


inline static void lock_set_get(gen_lock_set_t* s, int n)
{
	struct sembuf sop;
	sop.sem_num=n;
	sop.sem_op=-1; /* down */
	sop.sem_flg=0;
tryagain:
	if (semop(s->semid, &sop, 1)==-1){
		if (errno==EINTR){
			DBG("lock_set_get: signal received while waiting on a mutex\n");
			goto tryagain;
		}else{
			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
		}
	}
}

inline static void lock_set_release(gen_lock_set_t* s, int n)
{
	struct sembuf sop;
	sop.sem_num=n;
	sop.sem_op=1; /* up */
	sop.sem_flg=0;
tryagain:
	if (semop(s->semid, &sop, 1)==-1){
		if (errno==EINTR){
			/* very improbable */
			DBG("lock_set_release: signal received while releasing mutex\n");
			goto tryagain;
		}else{
			LM_CRIT("sysv: %s (%d)\n", strerror(errno), errno);
		}
	}
}
#else 
#error "no lock set method selected"
#endif


#endif