1 /*
2 * Copyright (C) 2006 iptelorg GmbH
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /*!
18 * \file
19 * \brief Kamailio core :: atomic operations init
20 * \ingroup core
21 * Module: \ref core
22 */
23
24
25 #include "atomic_ops_init.h"
26 #include "atomic_ops.h"
27
28 #if defined ATOMIC_OPS_USE_LOCK || defined ATOMIC_OPS_USE_LOCK_SET || \
29 defined MEMBAR_USES_LOCK
30 #include "locking.h"
31 #endif
32
33 #ifdef MEMBAR_USES_LOCK
34 gen_lock_t* __membar_lock=0; /* init in atomic_ops.c */
35 #endif
36
37 #ifdef ATOMIC_OPS_USE_LOCK_SET
38 gen_lock_set_t* _atomic_lock_set=0;
39 #elif defined ATOMIC_OPS_USE_LOCK
40 gen_lock_t* _atomic_lock=0;
41 #endif
42
43
44 /* returns 0 on success, -1 on error */
init_atomic_ops()45 int init_atomic_ops()
46 {
47
48 #ifdef MEMBAR_USES_LOCK
49 if ((__membar_lock=lock_alloc())==0){
50 goto error;
51 }
52 if (lock_init(__membar_lock)==0){
53 lock_dealloc(__membar_lock);
54 __membar_lock=0;
55 goto error;
56 }
57 _membar_lock; /* start with the lock "taken" so that we can safely use
58 unlock/lock sequences on it later */
59 #endif
60 #ifdef ATOMIC_OPS_USE_LOCK_SET
61 if ((_atomic_lock_set=lock_set_alloc(_ATOMIC_LS_SIZE))==0){
62 goto error;
63 }
64 if (lock_set_init(_atomic_lock_set)==0){
65 lock_set_dealloc(_atomic_lock_set);
66 _atomic_lock_set=0;
67 goto error;
68 }
69 #elif defined ATOMIC_OPS_USE_LOCK
70 if ((_atomic_lock=lock_alloc())==0){
71 goto error;
72 }
73 if (lock_init(_atomic_lock)==0){
74 lock_dealloc(_atomic_lock);
75 _atomic_lock=0;
76 goto error;
77 }
78 #endif /* ATOMIC_OPS_USE_LOCK_SET/ATOMIC_OPS_USE_LOCK */
79 return 0;
80 #if defined MEMBAR_USES_LOCK || defined ATOMIC_OPS_USE_LOCK || \
81 defined ATOMIC_OPS_USE_LOCK_SET
82 error:
83 destroy_atomic_ops();
84 return -1;
85 #endif
86 }
87
88
89
destroy_atomic_ops()90 void destroy_atomic_ops()
91 {
92 #ifdef MEMBAR_USES_LOCK
93 if (__membar_lock!=0){
94 lock_destroy(__membar_lock);
95 lock_dealloc(__membar_lock);
96 __membar_lock=0;
97 }
98 #endif
99 #ifdef ATOMIC_OPS_USE_LOCK_SET
100 if (_atomic_lock_set!=0){
101 lock_set_destroy(_atomic_lock_set);
102 lock_set_dealloc(_atomic_lock_set);
103 _atomic_lock_set=0;
104 }
105 #elif defined ATOMIC_OPS_USE_LOCK
106 if (_atomic_lock!=0){
107 lock_destroy(_atomic_lock);
108 lock_dealloc(_atomic_lock);
109 _atomic_lock=0;
110 }
111 #endif /* ATOMIC_OPS_USE_LOCK_SET / ATOMIC_OPS_USE_LOCK*/
112 }
113