1 /*
2 *
3 * Copyright (C) 2001-2003 FhG Fokus
4 *
5 * This file is part of Kamailio, a free SIP server.
6 *
7 * Kamailio 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 Kamailio 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 * Kamailio 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 /*!
28 * \file
29 * \brief Kamailio core :: Kamailio locking library
30 * \author andrei
31 * \ingroup core
32 * Module: \ref core
33 *
34 * WARNING: don't include this directly include instead locking.h!
35 *
36 Implements: (see also locking.h)
37
38 simple locks:
39 -------------
40 gen_lock_t* lock_alloc(); - allocates a lock in shared mem.
41 void lock_dealloc(gen_lock_t* lock); - deallocates the lock's shared m.
42
43 lock sets: [implemented only for FL & SYSV so far]
44 ----------
45 gen_lock_set_t* lock_set_alloc(no) - allocs a lock set in shm.
46 void lock_set_dealloc(gen_lock_set_t* s); - deallocs the lock set shm.
47
48 */
49
50 #ifndef _lock_alloc_h
51 #define _lock_alloc_h
52
53 /*shm_{malloc, free}*/
54 #include "mem/mem.h"
55 #include "mem/shm_mem.h"
56
57 #if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM)
58 /* simple locks*/
59 #define lock_alloc() shm_malloc(sizeof(gen_lock_t))
60 #define lock_dealloc(lock) shm_free((void*)lock)
61 /* lock sets */
62
lock_set_alloc(int n)63 inline static gen_lock_set_t* lock_set_alloc(int n)
64 {
65 gen_lock_set_t* ls;
66 ls=(gen_lock_set_t*)shm_malloc(sizeof(gen_lock_set_t)+n*sizeof(gen_lock_t));
67 if (ls==0){
68 SHM_MEM_CRITICAL;
69 }else{
70 ls->locks=(gen_lock_t*)((char*)ls+sizeof(gen_lock_set_t));
71 ls->size=n;
72 }
73 return ls;
74 }
75
76 #define lock_set_dealloc(lock_set) shm_free((void*)lock_set)
77
78 #elif defined USE_SYSV_SEM
79
80 /*simple locks*/
81 #define lock_alloc() shm_malloc(sizeof(gen_lock_t))
82 #define lock_dealloc(lock) shm_free((void*)lock)
83 /* lock sets */
84
lock_set_alloc(int n)85 inline static gen_lock_set_t* lock_set_alloc(int n)
86 {
87 gen_lock_set_t* ls;
88 ls=(gen_lock_set_t*)shm_malloc(sizeof(gen_lock_set_t));
89 if (ls==0){
90 SHM_MEM_CRITICAL;
91 }else{
92 ls->size=n;
93 ls->semid=-1;
94 }
95 return ls;
96 }
97
98
99 #define lock_set_dealloc(lock_set) shm_free((void*)lock_set)
100
101
102 #else
103 #error "no locking method selected"
104 #endif
105
106
107 #endif
108