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>
34using std::atomic_uintptr_t;
35using std::memory_order_relaxed;
36using std::memory_order_acq_rel;
37#endif
38# include <vlc_common.h>
39# include <vlc_tick.h>
40
41#define VLC_STATIC_RC { \
42 .refs = ATOMIC_VAR_INIT(0) \
43}
44
45typedef struct vlc_atomic_rc_t {
46 atomic_uintptr_t refs;
49/** Init the RC to 1 */
50static inline void vlc_atomic_rc_init(vlc_atomic_rc_t *rc)
52 atomic_init(&rc->refs, (uintptr_t)1);
53}
54
55/** Increment the RC */
56static inline void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
58 uintptr_t prev = atomic_fetch_add_explicit(&rc->refs, (uintptr_t)1,
59 memory_order_relaxed);
60 vlc_assert(prev);
61 VLC_UNUSED(prev);
62}
63
64/** Decrement the RC and return true if it reaches 0 */
65static inline bool vlc_atomic_rc_dec(vlc_atomic_rc_t *rc)
67 uintptr_t prev = atomic_fetch_sub_explicit(&rc->refs, (uintptr_t)1,
68 memory_order_acq_rel);
69 vlc_assert(prev);
70 return prev == 1;
71}
72
73/** Returns the current reference count.
74 * This is not safe to use for logic and must only be used for debugging or
75 * assertion purposes */
76static inline uintptr_t vlc_atomic_rc_get(const vlc_atomic_rc_t* rc)
78 return atomic_load_explicit(&rc->refs, memory_order_relaxed);
79}
80
81/**
82 * Waits on an address.
83 *
84 * Puts the calling thread to sleep if a specific unsigned 32-bits value is
85 * stored at a specified address. The thread will sleep until it is woken up by
86 * a call to vlc_atomic_notify_one() or vlc_atomic_notify_all() in another
87 * thread, or spuriously.
88 *
89 * If the value does not match, do nothing and return immediately.
90 *
91 * \param addr address to check for
92 * \param val value to match at the address
93 */
94VLC_API void vlc_atomic_wait(void *addr, unsigned val);
95
96/**
97 * Waits on an address with a time-out.
98 *
99 * This function operates as vlc_atomic_wait() but provides an additional
100 * time-out. If the deadline is reached, the thread resumes and the function
101 * returns.
102 *
103 * \param addr address to check for
104 * \param val value to match at the address
105 * \param deadline deadline to wait until
106 *
107 * \retval 0 the function was woken up before the time-out
108 * \retval ETIMEDOUT the deadline was reached
109 */
111int vlc_atomic_timedwait(void *addr, unsigned val, vlc_tick_t deadline);
112
113int vlc_atomic_timedwait_daytime(void *addr, unsigned val, time_t deadline);
114
115/**
116 * Wakes up one thread on an address.
117 *
118 * Wakes up (at least) one of the thread sleeping on the specified address.
119 * The address must be equal to the first parameter given by at least one
120 * thread sleeping within the vlc_atomic_wait() or vlc_atomic_timedwait()
121 * functions. If no threads are found, this function does nothing.
122 *
123 * \param addr address identifying which threads may be woken up
124 */
125VLC_API void vlc_atomic_notify_one(void *addr);
126
127/**
128 * Wakes up all thread on an address.
129 *
130 * Wakes up all threads sleeping on the specified address (if any).
131 * Any thread sleeping within a call to vlc_atomic_wait() or
132 * vlc_atomic_timedwait() with the specified address as first call parameter
133 * will be woken up.
134 *
135 * \param addr address identifying which threads to wake up
136 */
137VLC_API void vlc_atomic_notify_all(void *addr);
138
139#endif
#define VLC_API
Definition: fourcc_gen.c:31
#define vlc_assert(pred)
Run-time assertion.
Definition: vlc_common.h:301
Definition: vlc_atomic.h:46
atomic_uintptr_t refs
Definition: vlc_atomic.h:47
int vlc_atomic_timedwait(void *addr, unsigned val, vlc_tick_t deadline)
Waits on an address with a time-out.
Definition: thread.c:92
static void vlc_atomic_rc_init(vlc_atomic_rc_t *rc)
Init the RC to 1.
Definition: vlc_atomic.h:51
int vlc_atomic_timedwait_daytime(void *addr, unsigned val, time_t deadline)
Definition: thread.c:133
struct vlc_atomic_rc_t vlc_atomic_rc_t
void vlc_atomic_wait(void *addr, unsigned val)
Waits on an address.
Definition: thread.c:85
void vlc_atomic_notify_one(void *addr)
Wakes up one thread on an address.
Definition: thread.c:75
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:66
static uintptr_t vlc_atomic_rc_get(const vlc_atomic_rc_t *rc)
Returns the current reference count.
Definition: vlc_atomic.h:77
static void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
Increment the RC.
Definition: vlc_atomic.h:57
void vlc_atomic_notify_all(void *addr)
Wakes up all thread on an address.
Definition: thread.c:80
This file is a collection of common definitions and types.
#define VLC_UNUSED(x)
Definition: vlc_common.h:1131
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_tick.h:45