VLC  4.0.0-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 #ifdef __cplusplus
22 # error Not implemented in C++.
23 #endif
24 
25 #ifndef VLC_ATOMIC_H
26 # define VLC_ATOMIC_H
27 
28 /**
29  * \file
30  * Atomic operations do not require locking, but they are not very powerful.
31  */
32 
33 # include <assert.h>
34 # include <stdatomic.h>
35 # include <vlc_common.h>
36 
37 typedef struct vlc_atomic_rc_t {
38  atomic_uintptr_t refs;
40 
41 /** Init the RC to 1 */
42 static inline void vlc_atomic_rc_init(vlc_atomic_rc_t *rc)
43 {
44  atomic_init(&rc->refs, 1);
45 }
46 
47 /** Increment the RC */
48 static inline void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
49 {
50  uintptr_t prev = atomic_fetch_add_explicit(&rc->refs, 1, memory_order_relaxed);
51  vlc_assert(prev);
52  VLC_UNUSED(prev);
53 }
54 
55 /** Decrement the RC and return true if it reaches 0 */
56 static inline bool vlc_atomic_rc_dec(vlc_atomic_rc_t *rc)
57 {
58  uintptr_t prev = atomic_fetch_sub_explicit(&rc->refs, 1, memory_order_acq_rel);
59  vlc_assert(prev);
60  return prev == 1;
61 }
62 
63 #endif
Definition: vlc_atomic.h:38
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:57
#define VLC_UNUSED(x)
Definition: vlc_common.h:1102
This file is a collection of common definitions and types.
atomic_uintptr_t refs
Definition: vlc_atomic.h:39
#define vlc_assert(pred)
Run-time assertion.
Definition: vlc_common.h:267
struct vlc_atomic_rc_t vlc_atomic_rc_t
static void vlc_atomic_rc_init(vlc_atomic_rc_t *rc)
Init the RC to 1.
Definition: vlc_atomic.h:43
static void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
Increment the RC.
Definition: vlc_atomic.h:49