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  * @defgroup atomic Kamailio atomic operations
19  * @brief  Kamailio atomic operations and memory barriers support
20  *
21  * Kamailio atomic operations and memory barriers support for different CPU
22  * architectures implemented in assembler. It also provides some generic
23  * fallback code for architectures not currently supported.
24  */
25 
26 /**
27  * @file
28  * @brief Common part for all the atomic operations
29  *
30  * Common part for all the atomic operations (atomic_t and common operations)
31  * see atomic_ops.h for more info.
32  * @ingroup atomic
33  */
34 
35 #ifndef __atomic_common
36 #define __atomic_common
37 
38 /**
39  * @brief atomic_t defined as a struct to easily catch non atomic operations on it.
40  *
41  * atomic_t defined as a struct to easily catch non atomic operations on it,
42  * e.g. atomic_t foo; foo++  will generate a compile error.
43  */
44 typedef struct{ volatile int val; } atomic_t;
45 
46 
47 /**
48  * @name Atomic load and store operations
49  * Atomic store and load operations are atomic on all cpus, note however that they
50  * don't include memory barriers so if you want to use atomic_{get,set}
51  * to implement mutexes you must use the mb_* versions or explicitely use
52  * the barriers
53  */
54 
55 /*@{ */
56 
57 #define atomic_set_int(pvar, i) (*(int*)(pvar)=i)
58 #define atomic_set_long(pvar, i) (*(long*)(pvar)=i)
59 #define atomic_get_int(pvar) (*(int*)(pvar))
60 #define atomic_get_long(pvar) (*(long*)(pvar))
61 
62 #define atomic_set(at_var, value)	(atomic_set_int(&((at_var)->val), (value)))
63 
atomic_get(atomic_t * v)64 inline static int atomic_get(atomic_t *v)
65 {
66 	return atomic_get_int(&(v->val));
67 }
68 
69 /*@} */
70 
71 #endif
72