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