FFmpeg
Loading...
Searching...
No Matches
atomic.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19/*
20 * Test the stdatomic.h interface as used by FFmpeg, against whichever
21 * implementation configure selected, the native one or the compat fallback.
22 *
23 * The output is architecture independent: integer results are converted to
24 * long long before printing, and no pointer values are printed.
25 */
26
27#include <limits.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <stdatomic.h>
31
32static int called;
33
34static void callback(void)
35{
36 called++;
37}
38
39#define TEST(A, T, name, V1, V2) \
40do { \
41 A object = 0; \
42 T expected, r; \
43 atomic_store(&object, (T)(V1)); \
44 r = atomic_exchange(&object, (T)(V2)); \
45 printf(name " exchange %lld\n", (long long)r); \
46 r = atomic_fetch_add(&object, (T)1); \
47 printf(name " add %lld\n", (long long)r); \
48 r = atomic_fetch_sub(&object, (T)((uint64_t)(V2) + 1)); \
49 printf(name " sub %lld\n", (long long)r); \
50 r = atomic_fetch_or(&object, (T)0x10); \
51 printf(name " or %lld\n", (long long)r); \
52 r = atomic_fetch_xor(&object, (T)0x10); \
53 printf(name " xor %lld\n", (long long)r); \
54 r = atomic_fetch_and(&object, (T)1); \
55 printf(name " and %lld\n", (long long)r); \
56 atomic_store(&object, (T)0x5a); \
57 expected = (T)(V1); \
58 r = atomic_compare_exchange_strong(&object, &expected, (T)(V2)); \
59 printf(name " cas %lld %lld\n", \
60 (long long)r, (long long)expected); \
61 do { \
62 r = atomic_compare_exchange_weak(&object, &expected, (T)(V1)); \
63 } while (!r); \
64 printf(name " cas weak %lld %lld %lld\n", \
65 (long long)r, (long long)expected, \
66 (long long)atomic_load(&object)); \
67} while (0)
68
69int main(void)
70{
71 _Bool _Atomic b = 0;
72 _Bool be;
73 atomic_uint u32 = 0;
74 atomic_ullong u64 = 0;
75 atomic_uchar u8 = 0;
76 int x = 42;
77 int *_Atomic p = NULL;
78 int *pe;
79 void (*_Atomic fp)(void) = NULL;
80 const int *const arr[2] = { &x, NULL };
81 const int *const *_Atomic lst = NULL;
83 long long old;
84 int r1, r2, r3;
85 void (*loaded_callback)(void);
86
87 TEST(atomic_char, char, "char", 97, 122);
88 TEST(atomic_schar, signed char, "schar", -5, 42);
89 TEST(atomic_uchar, unsigned char, "uchar", 200, 42);
90 TEST(atomic_short, short, "short", -30000, 42);
91 TEST(atomic_ushort, unsigned short, "ushort", 60000, 42);
92 TEST(atomic_int, int, "int", -2000000000, 2000000000);
93 TEST(atomic_uint, unsigned int, "uint", 4000000000u, 7u);
94 TEST(atomic_long, long, "long", -2000000000L, 2000000000L);
95 TEST(atomic_ulong, unsigned long, "ulong", 4000000000UL, 7UL);
96 TEST(atomic_llong, long long, "llong", -9007199254740993LL, 9007199254740993LL);
97 TEST(atomic_ullong, unsigned long long, "ullong", 18446744073709551615ULL, 3ULL);
98 TEST(atomic_int_least16_t, int_least16_t, "int_least16", -30000, 42);
99 TEST(atomic_uint_least32_t, uint_least32_t, "uint_least32", 4000000000u, 7u);
100 TEST(atomic_int_least64_t, int_least64_t, "int_least64", -9007199254740993LL, 9007199254740993LL);
101 TEST(atomic_uint_least64_t, uint_least64_t, "uint_least64", 18446744073709551615ULL, 3ULL);
102 TEST(atomic_int_fast16_t, int_fast16_t, "int_fast16", -30000, 42);
103 TEST(atomic_uint_fast32_t, uint_fast32_t, "uint_fast32", 4000000000u, 7u);
104 TEST(atomic_intptr_t, intptr_t, "intptr", -123456, 42);
105 TEST(atomic_size_t, size_t, "size", 4000000000u, 7u);
106 TEST(atomic_intmax_t, intmax_t, "intmax", -9007199254740993LL, 9007199254740993LL);
107
108 /* unsigned wraparound */
109 atomic_store(&u32, UINT_MAX);
110 old = atomic_fetch_add(&u32, 1u);
111 printf("uint wrap %lld %lld\n",
112 old, (long long)atomic_load(&u32));
113 atomic_store(&u64, ULLONG_MAX);
114 old = (long long)atomic_fetch_add(&u64, 1);
115 printf("ullong add %lld %lld\n",
116 old, (long long)atomic_load(&u64));
117 atomic_store(&u64, 5);
118 old = (long long)atomic_fetch_sub(&u64, 10);
119 printf("ullong sub %lld %lld\n",
120 old, (long long)atomic_load(&u64));
121 atomic_store(&u8, 200);
122 old = atomic_fetch_add(&u8, 100);
123 printf("uchar add %lld %lld\n",
124 old, (long long)atomic_load(&u8));
125
126 /* bool, including the normalization of the desired value */
127 atomic_store(&b, 1);
128 be = 0;
129 r1 = atomic_load(&b);
130 r2 = atomic_exchange(&b, 0);
131 r3 = atomic_compare_exchange_strong(&b, &be, 2);
132 printf("bool %d %d %d %d %d\n", r1, r2, r3,
133 (int)be, (int)atomic_load(&b));
134
135 /* object pointers */
137 pe = NULL;
140 printf("ptr %d %d %d\n", r1, r2, pe == &x);
141 printf("ptr exch %d\n", atomic_exchange(&p, NULL) == &x);
142
143 /* function pointers, as in the log callback */
145 loaded_callback = atomic_load_explicit(&fp, memory_order_relaxed);
146 loaded_callback();
147
148 /* pointers to const, as in the device lists */
149 const int *const *l;
152 printf("lst %d %d\n", l == arr, **l == x);
153
158 printf("flag %d %d %d\n", r1, r2, r3);
159 printf("called %d\n", called);
160
161 {
162 int _Atomic i = 0;
163 atomic_init(&i, 43);
164 printf("init %d\n", (int)atomic_load(&i));
165 }
168 printf("kill_dep %d\n", (int)kill_dependency(1));
169
170 return 0;
171}
static int called
Definition atomic.c:32
int main(void)
Definition atomic.c:69
static void callback(void)
Definition atomic.c:34
#define TEST(A, T, name, V1, V2)
Definition atomic.c:39
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define NULL
Definition coverity.c:32
__device__ int printf(const char *,...)
static void callback(void *priv_data, int index, uint8_t *buf, int buf_size, int64_t time, enum dshowDeviceType devtype)
Definition dshow.c:342
#define b
Definition input.c:43
FF_ATOMIC_ALIGN64 int_least64_t atomic_int_least64_t
Definition stdatomic.h:79
#define atomic_store(object, desired)
Definition stdatomic.h:256
@ memory_order_seq_cst
Definition stdatomic.h:34
@ memory_order_relaxed
Definition stdatomic.h:29
@ memory_order_acquire
Definition stdatomic.h:31
unsigned char atomic_uchar
Definition stdatomic.h:60
FF_ATOMIC_ALIGN64 unsigned long long atomic_ullong
Definition stdatomic.h:68
int atomic_int
Definition stdatomic.h:63
#define atomic_compare_exchange_strong(object, expected, desired)
Definition stdatomic.h:281
#define atomic_exchange(object, desired)
Definition stdatomic.h:262
#define ATOMIC_FLAG_INIT
Definition stdatomic.h:331
FF_ATOMIC_ALIGN64 uint_least64_t atomic_uint_least64_t
Definition stdatomic.h:80
char atomic_char
Definition stdatomic.h:58
#define atomic_flag_clear(object)
Definition stdatomic.h:342
FF_ATOMIC_ALIGN64 long long atomic_llong
Definition stdatomic.h:67
intptr_t atomic_intptr_t
Definition stdatomic.h:89
#define atomic_fetch_sub(object, operand)
Definition stdatomic.h:315
unsigned int atomic_uint
Definition stdatomic.h:64
#define atomic_load_explicit(object, order)
Definition stdatomic.h:247
#define kill_dependency(y)
Definition stdatomic.h:121
#define atomic_signal_fence(order)
Definition stdatomic.h:138
#define atomic_thread_fence(order)
Definition stdatomic.h:137
short atomic_short
Definition stdatomic.h:61
unsigned short atomic_ushort
Definition stdatomic.h:62
long atomic_long
Definition stdatomic.h:65
size_t atomic_size_t
Definition stdatomic.h:91
unsigned long atomic_ulong
Definition stdatomic.h:66
#define _Atomic
Definition stdatomic.h:117
#define atomic_flag_test_and_set(object)
Definition stdatomic.h:336
#define atomic_load(object)
Definition stdatomic.h:250
#define atomic_store_explicit(object, desired, order)
Definition stdatomic.h:253
int_fast16_t atomic_int_fast16_t
Definition stdatomic.h:83
uint_fast32_t atomic_uint_fast32_t
Definition stdatomic.h:86
int_least16_t atomic_int_least16_t
Definition stdatomic.h:75
FF_ATOMIC_ALIGN64 intmax_t atomic_intmax_t
Definition stdatomic.h:93
#define atomic_fetch_add(object, operand)
Definition stdatomic.h:312
#define atomic_init(obj, value)
Definition stdatomic.h:119
signed char atomic_schar
Definition stdatomic.h:59
uint_least32_t atomic_uint_least32_t
Definition stdatomic.h:78