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 #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 atomic_uint_least32_t vlc_atomic_float;
38 
39 static inline void vlc_atomic_init_float(vlc_atomic_float *var, float f)
40 {
41  union { float f; uint32_t i; } u;
42  u.f = f;
43  atomic_init(var, u.i);
44 }
45 
46 /** Helper to retrieve a single precision from an atom. */
47 static inline float vlc_atomic_load_float(vlc_atomic_float *atom)
48 {
49  union { float f; uint32_t i; } u;
50  u.i = atomic_load(atom);
51  return u.f;
52 }
53 
54 /** Helper to store a single precision into an atom. */
55 static inline void vlc_atomic_store_float(vlc_atomic_float *atom, float f)
56 {
57  union { float f; uint32_t i; } u;
58  u.f = f;
59  atomic_store(atom, u.i);
60 }
61 
62 typedef struct vlc_atomic_rc_t {
63  atomic_uint refs;
65 
66 /** Init the RC to 1 */
67 static inline void vlc_atomic_rc_init(vlc_atomic_rc_t *rc)
68 {
69  atomic_init(&rc->refs, 1);
70 }
71 
72 /** Increment the RC */
73 static inline void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
74 {
75  unsigned prev = atomic_fetch_add_explicit(&rc->refs, 1, memory_order_relaxed);
76  vlc_assert(prev);
77  VLC_UNUSED(prev);
78 }
79 
80 /** Decrement the RC and return true if it reaches 0 */
81 static inline bool vlc_atomic_rc_dec(vlc_atomic_rc_t *rc)
82 {
83  unsigned prev = atomic_fetch_sub_explicit(&rc->refs, 1, memory_order_acq_rel);
84  vlc_assert(prev);
85  return prev == 1;
86 }
87 
88 #endif
Definition: vlc_atomic.h:63
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:82
#define VLC_UNUSED(x)
Definition: vlc_common.h:1085
This file is a collection of common definitions and types.
static float vlc_atomic_load_float(vlc_atomic_float *atom)
Helper to retrieve a single precision from an atom.
Definition: vlc_atomic.h:48
const char var[sizeof("video")]
Definition: player.c:1699
#define vlc_assert(pred)
Run-time assertion.
Definition: vlc_common.h:267
static void vlc_atomic_store_float(vlc_atomic_float *atom, float f)
Helper to store a single precision into an atom.
Definition: vlc_atomic.h:56
struct vlc_atomic_rc_t vlc_atomic_rc_t
static void vlc_atomic_init_float(vlc_atomic_float *var, float f)
Definition: vlc_atomic.h:40
atomic_uint refs
Definition: vlc_atomic.h:64
static void vlc_atomic_rc_init(vlc_atomic_rc_t *rc)
Init the RC to 1.
Definition: vlc_atomic.h:68
atomic_uint_least32_t vlc_atomic_float
Definition: vlc_atomic.h:38
static void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
Increment the RC.
Definition: vlc_atomic.h:74