demux: mp4: use struct for coreaudio layout params
[vlc.git] / include / vlc_atomic.h
blobe2d4fe5360a530d8d18c9578ae0b1c75470a1667
1 /*****************************************************************************
2 * vlc_atomic.h:
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 *****************************************************************************/
21 #ifdef __cplusplus
22 # error Not implemented in C++.
23 #endif
25 #ifndef VLC_ATOMIC_H
26 # define VLC_ATOMIC_H
28 /**
29 * \file
30 * Atomic operations do not require locking, but they are not very powerful.
33 # include <assert.h>
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;
42 u.f = f;
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);
51 return u.f;
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;
58 u.f = f;
59 atomic_store(atom, u.i);
62 typedef struct vlc_atomic_rc_t {
63 atomic_uint refs;
64 } 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);
76 vlc_assert(prev);
77 VLC_UNUSED(prev);
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);
84 vlc_assert(prev);
85 return prev == 1;
88 #endif