VLC  4.0.0-dev
vlc_atomic.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vlc_atomic.h:
3  *****************************************************************************
4  * Copyright (C) 2010 RĂ©mi Denis-Courmont
5  *
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.
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 Lesser General Public License for more details.
15  *
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  *****************************************************************************/
20 
21 #ifndef VLC_ATOMIC_H
22 # define VLC_ATOMIC_H
23 
24 /**
25  * \file
26  * Atomic operations do not require locking, but they are not very powerful.
27  */
28 
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>
39 
40 typedef struct vlc_atomic_rc_t {
41  atomic_uintptr_t refs;
43 
44 /** Init the RC to 1 */
45 static inline void vlc_atomic_rc_init(vlc_atomic_rc_t *rc)
46 {
47  atomic_init(&rc->refs, (uintptr_t)1);
48 }
49 
50 /** Increment the RC */
51 static inline void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
52 {
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);
57 }
58 
59 /** Decrement the RC and return true if it reaches 0 */
60 static inline bool vlc_atomic_rc_dec(vlc_atomic_rc_t *rc)
61 {
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;
66 }
67 
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)
72 {
73  return atomic_load_explicit(&rc->refs, memory_order_relaxed);
74 }
75 
76 #endif
#define vlc_assert(pred)
Run-time assertion.
Definition: vlc_common.h:272
Definition: vlc_atomic.h:41
atomic_uintptr_t refs
Definition: vlc_atomic.h:42
static void vlc_atomic_rc_init(vlc_atomic_rc_t *rc)
Init the RC to 1.
Definition: vlc_atomic.h:46
struct vlc_atomic_rc_t vlc_atomic_rc_t
static bool vlc_atomic_rc_dec(vlc_atomic_rc_t *rc)
Decrement the RC and return true if it reaches 0.
Definition: vlc_atomic.h:61
static uintptr_t vlc_atomic_rc_get(const vlc_atomic_rc_t *rc)
Returns the current reference count.
Definition: vlc_atomic.h:72
static void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
Increment the RC.
Definition: vlc_atomic.h:52
This file is a collection of common definitions and types.
#define VLC_UNUSED(x)
Definition: vlc_common.h:1106