1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2010 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
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 Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
22 # error Not implemented in C++.
30 * Atomic operations do not require locking, but they are not very powerful.
34 # include <stdatomic.h>
35 # include <vlc_common.h>
37 typedef atomic_uint_least32_t vlc_atomic_float
;
39 static inline void vlc_atomic_init_float(vlc_atomic_float
*var
, float f
)
41 union { float f
; uint32_t i
; } u
;
43 atomic_init(var
, u
.i
);
46 /** Helper to retrieve a single precision from an atom. */
47 static inline float vlc_atomic_load_float(vlc_atomic_float
*atom
)
49 union { float f
; uint32_t i
; } u
;
50 u
.i
= atomic_load(atom
);
54 /** Helper to store a single precision into an atom. */
55 static inline void vlc_atomic_store_float(vlc_atomic_float
*atom
, float f
)
57 union { float f
; uint32_t i
; } u
;
59 atomic_store(atom
, u
.i
);
62 typedef struct vlc_atomic_rc_t
{
66 /** Init the RC to 1 */
67 static inline void vlc_atomic_rc_init(vlc_atomic_rc_t
*rc
)
69 atomic_init(&rc
->refs
, 1);
72 /** Increment the RC */
73 static inline void vlc_atomic_rc_inc(vlc_atomic_rc_t
*rc
)
75 unsigned prev
= atomic_fetch_add_explicit(&rc
->refs
, 1, memory_order_relaxed
);
80 /** Decrement the RC and return true if it reaches 0 */
81 static inline bool vlc_atomic_rc_dec(vlc_atomic_rc_t
*rc
)
83 unsigned prev
= atomic_fetch_sub_explicit(&rc
->refs
, 1, memory_order_acq_rel
);