demux: adaptive: pass sequence to chunks
[vlc.git] / include / vlc_atomic.h
blobb393fafb36276c257b7289ae09dc8dc1d8ec8bec
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 #ifndef VLC_ATOMIC_H
22 # define VLC_ATOMIC_H
24 /**
25 * \file
26 * Atomic operations do not require locking, but they are not very powerful.
29 # include <assert.h>
30 #ifndef __cplusplus
31 # include <stdatomic.h>
32 #else
33 # include <atomic>
34 using std::atomic_uintptr_t;
35 using std::memory_order_relaxed;
36 using std::memory_order_acq_rel;
37 #endif
38 # include <vlc_common.h>
40 typedef struct vlc_atomic_rc_t {
41 atomic_uintptr_t refs;
42 } vlc_atomic_rc_t;
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);
55 vlc_assert(prev);
56 VLC_UNUSED(prev);
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);
64 vlc_assert(prev);
65 return prev == 1;
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);
76 #endif