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