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 *****************************************************************************/
26 * Atomic operations do not require locking, but they are not very powerful.
31 # include <stdatomic.h>
34 using std::atomic_uintptr_t
;
35 using std::memory_order_relaxed
;
36 using std::memory_order_acq_rel
;
38 # include <vlc_common.h>
40 typedef struct vlc_atomic_rc_t
{
41 atomic_uintptr_t refs
;
44 /** Init the RC to 1 */
45 static inline void vlc_atomic_rc_init(vlc_atomic_rc_t
*rc
)
47 atomic_init(&rc
->refs
, (uintptr_t)1);
50 /** Increment the RC */
51 static inline void vlc_atomic_rc_inc(vlc_atomic_rc_t
*rc
)
53 uintptr_t prev
= atomic_fetch_add_explicit(&rc
->refs
, (uintptr_t)1,
54 memory_order_relaxed
);
59 /** Decrement the RC and return true if it reaches 0 */
60 static inline bool vlc_atomic_rc_dec(vlc_atomic_rc_t
*rc
)
62 uintptr_t prev
= atomic_fetch_sub_explicit(&rc
->refs
, (uintptr_t)1,
63 memory_order_acq_rel
);
68 /** Returns the current reference count.
69 * This is not safe to use for logic and must only be used for debugging or
70 * assertion purposes */
71 static inline uintptr_t vlc_atomic_rc_get(const vlc_atomic_rc_t
* rc
)
73 return atomic_load_explicit(&rc
->refs
, memory_order_relaxed
);