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#define VLC_STATIC_RC { \
42 .refs = (uintptr_t) 1 \
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#ifndef __cplusplus
53 atomic_init(&rc->refs, (uintptr_t)1);
54#else
55 rc->refs = (uintptr_t)1;
56#endif
57}
58
59/** Increment the RC */
60static inline void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
62 uintptr_t prev = atomic_fetch_add_explicit(&rc->refs, (uintptr_t)1,
63 memory_order_relaxed);
64 vlc_assert(prev);
65 VLC_UNUSED(prev);
66}
67
68/** Decrement the RC and return true if it reaches 0 */
69static inline bool vlc_atomic_rc_dec(vlc_atomic_rc_t *rc)
71 uintptr_t prev = atomic_fetch_sub_explicit(&rc->refs, (uintptr_t)1,
72 memory_order_acq_rel);
73 vlc_assert(prev);
74 return prev == 1;
75}
76
77/** Returns the current reference count.
78 * This is not safe to use for logic and must only be used for debugging or
79 * assertion purposes */
80static inline uintptr_t vlc_atomic_rc_get(const vlc_atomic_rc_t* rc)
82 return atomic_load_explicit(&rc->refs, memory_order_relaxed);
83}
84
85/**
86 * Waits on an address.
87 *
88 * Puts the calling thread to sleep if a specific unsigned 32-bits value is
89 * stored at a specified address. The thread will sleep until it is woken up by
90 * a call to vlc_atomic_notify_one() or vlc_atomic_notify_all() in another
91 * thread, or spuriously.
92 *
93 * If the value does not match, do nothing and return immediately.
94 *
95 * \param addr address to check for
96 * \param val value to match at the address
97 */
98VLC_API void vlc_atomic_wait(void *addr, unsigned val);
99
100/**
101 * Waits on an address with a time-out.
102 *
103 * This function operates as vlc_atomic_wait() but provides an additional
104 * time-out. If the deadline is reached, the thread resumes and the function
105 * returns.
106 *
107 * \param addr address to check for
108 * \param val value to match at the address
109 * \param deadline deadline to wait until
110 *
111 * \retval 0 the function was woken up before the time-out
112 * \retval ETIMEDOUT the deadline was reached
113 */
115int vlc_atomic_timedwait(void *addr, unsigned val, vlc_tick_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:292
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
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:70
static uintptr_t vlc_atomic_rc_get(const vlc_atomic_rc_t *rc)
Returns the current reference count.
Definition vlc_atomic.h:81
static void vlc_atomic_rc_inc(vlc_atomic_rc_t *rc)
Increment the RC.
Definition vlc_atomic.h:61
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:1047
int64_t vlc_tick_t
High precision date or time interval.
Definition vlc_tick.h:48