]> git.sesse.net Git - vlc/blob - src/misc/atomic.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / misc / atomic.c
1 /*****************************************************************************
2  * atomic.c:
3  *****************************************************************************
4  * Copyright (C) 2010 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <vlc_common.h>
26 #include <vlc_atomic.h>
27
28 #if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
29 /* GCC intrinsics */
30
31 uintptr_t vlc_atomic_get (const vlc_atomic_t *atom)
32 {
33     __sync_synchronize ();
34     return atom->u;
35 }
36
37 uintptr_t vlc_atomic_set (vlc_atomic_t *atom, uintptr_t v)
38 {
39     atom->u = v;
40     __sync_synchronize ();
41     return v;
42 }
43
44 uintptr_t vlc_atomic_add (vlc_atomic_t *atom, uintptr_t v)
45 {
46     return __sync_add_and_fetch (&atom->u, v);
47 }
48
49 #else
50 /* Worst-case fallback implementation with a mutex */
51
52 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
53
54 uintptr_t vlc_atomic_get (const vlc_atomic_t *atom)
55 {
56     uintptr_t v;
57
58     vlc_mutex_lock (&lock);
59     v = atom->u;
60     vlc_mutex_unlock (&lock);
61     return v;
62 }
63
64 uintptr_t vlc_atomic_set (vlc_atomic_t *atom, uintptr_t v)
65 {
66     vlc_mutex_lock (&lock);
67     atom->u = v;
68     vlc_mutex_unlock (&lock);
69     return v;
70 }
71
72 uintptr_t vlc_atomic_add (vlc_atomic_t *atom, uintptr_t v)
73 {
74     vlc_mutex_lock (&lock);
75     atom->u += v;
76     v = atom->u;
77     vlc_mutex_unlock (&lock);
78     return v;
79 }
80
81 #endif