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