parser/sdp: more suggestive debug message
[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  * \file
29  * \brief SIP-router core :: 
30  * \ingroup core
31  * Module: \ref core
32  */
33
34
35 #include "atomic_ops_init.h"
36 #include "atomic_ops.h"
37
38 #if defined ATOMIC_OPS_USE_LOCK  || defined ATOMIC_OPS_USE_LOCK_SET || \
39         defined MEMBAR_USES_LOCK
40 #include "locking.h"
41 #endif
42
43 #ifdef MEMBAR_USES_LOCK
44 gen_lock_t* __membar_lock=0; /* init in atomic_ops.c */
45 #endif
46
47 #ifdef ATOMIC_OPS_USE_LOCK_SET
48 gen_lock_set_t* _atomic_lock_set=0;
49 #elif defined ATOMIC_OPS_USE_LOCK
50 gen_lock_t* _atomic_lock=0;
51 #endif
52
53
54 /* returns 0 on success, -1 on error */
55 int init_atomic_ops()
56 {
57         
58 #ifdef MEMBAR_USES_LOCK
59         if ((__membar_lock=lock_alloc())==0){
60                 goto error;
61         }
62         if (lock_init(__membar_lock)==0){
63                 lock_dealloc(__membar_lock);
64                 __membar_lock=0;
65                 goto error;
66         }
67         _membar_lock; /* start with the lock "taken" so that we can safely use
68                                          unlock/lock sequences on it later */
69 #endif
70 #ifdef ATOMIC_OPS_USE_LOCK_SET
71         if ((_atomic_lock_set=lock_set_alloc(_ATOMIC_LS_SIZE))==0){
72                 goto error;
73         }
74         if (lock_set_init(_atomic_lock_set)==0){
75                 lock_set_dealloc(_atomic_lock_set);
76                 _atomic_lock_set=0;
77                 goto error;
78         }
79 #elif defined ATOMIC_OPS_USE_LOCK
80         if ((_atomic_lock=lock_alloc())==0){
81                 goto error;
82         }
83         if (lock_init(_atomic_lock)==0){
84                 lock_dealloc(_atomic_lock);
85                 _atomic_lock=0;
86                 goto error;
87         }
88 #endif /* ATOMIC_OPS_USE_LOCK_SET/ATOMIC_OPS_USE_LOCK */
89         return 0;
90 #if defined MEMBAR_USES_LOCK || defined ATOMIC_OPS_USE_LOCK || \
91         defined ATOMIC_OPS_USE_LOCK_SET
92 error:
93         destroy_atomic_ops();
94         return -1;
95 #endif
96 }
97
98
99
100 void destroy_atomic_ops()
101 {
102 #ifdef MEMBAR_USES_LOCK
103         if (__membar_lock!=0){
104                 lock_destroy(__membar_lock);
105                 lock_dealloc(__membar_lock);
106                 __membar_lock=0;
107         }
108 #endif
109 #ifdef ATOMIC_OPS_USE_LOCK_SET
110         if (_atomic_lock_set!=0){
111                 lock_set_destroy(_atomic_lock_set);
112                 lock_set_dealloc(_atomic_lock_set);
113                 _atomic_lock_set=0;
114         }
115 #elif defined ATOMIC_OPS_USE_LOCK
116         if (_atomic_lock!=0){
117                 lock_destroy(_atomic_lock);
118                 lock_dealloc(_atomic_lock);
119                 _atomic_lock=0;
120         }
121 #endif /* ATOMIC_OPS_USE_LOCK_SET / ATOMIC_OPS_USE_LOCK*/
122 }