xref: /aosp_15_r20/external/libdav1d/include/compat/msvc/stdatomic.h (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
1*c0909341SAndroid Build Coastguard Worker /*
2*c0909341SAndroid Build Coastguard Worker * Copyright © 2018, VideoLAN and dav1d authors
3*c0909341SAndroid Build Coastguard Worker * All rights reserved.
4*c0909341SAndroid Build Coastguard Worker *
5*c0909341SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*c0909341SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met:
7*c0909341SAndroid Build Coastguard Worker *
8*c0909341SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright notice, this
9*c0909341SAndroid Build Coastguard Worker *    list of conditions and the following disclaimer.
10*c0909341SAndroid Build Coastguard Worker *
11*c0909341SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright notice,
12*c0909341SAndroid Build Coastguard Worker *    this list of conditions and the following disclaimer in the documentation
13*c0909341SAndroid Build Coastguard Worker *    and/or other materials provided with the distribution.
14*c0909341SAndroid Build Coastguard Worker *
15*c0909341SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16*c0909341SAndroid Build Coastguard Worker * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17*c0909341SAndroid Build Coastguard Worker * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18*c0909341SAndroid Build Coastguard Worker * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19*c0909341SAndroid Build Coastguard Worker * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20*c0909341SAndroid Build Coastguard Worker * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21*c0909341SAndroid Build Coastguard Worker * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22*c0909341SAndroid Build Coastguard Worker * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*c0909341SAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24*c0909341SAndroid Build Coastguard Worker * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*c0909341SAndroid Build Coastguard Worker */
26*c0909341SAndroid Build Coastguard Worker 
27*c0909341SAndroid Build Coastguard Worker #ifndef MSCVER_STDATOMIC_H_
28*c0909341SAndroid Build Coastguard Worker #define MSCVER_STDATOMIC_H_
29*c0909341SAndroid Build Coastguard Worker 
30*c0909341SAndroid Build Coastguard Worker #if !defined(__cplusplus) && defined(_MSC_VER)
31*c0909341SAndroid Build Coastguard Worker 
32*c0909341SAndroid Build Coastguard Worker #pragma warning(push)
33*c0909341SAndroid Build Coastguard Worker #pragma warning(disable:4067)    /* newline for __has_include_next */
34*c0909341SAndroid Build Coastguard Worker 
35*c0909341SAndroid Build Coastguard Worker #if defined(__clang__) && __has_include_next(<stdatomic.h>)
36*c0909341SAndroid Build Coastguard Worker    /* use the clang stdatomic.h with clang-cl*/
37*c0909341SAndroid Build Coastguard Worker #  include_next <stdatomic.h>
38*c0909341SAndroid Build Coastguard Worker #else /* ! stdatomic.h */
39*c0909341SAndroid Build Coastguard Worker 
40*c0909341SAndroid Build Coastguard Worker #include <windows.h>
41*c0909341SAndroid Build Coastguard Worker 
42*c0909341SAndroid Build Coastguard Worker #include "common/attributes.h"
43*c0909341SAndroid Build Coastguard Worker 
44*c0909341SAndroid Build Coastguard Worker typedef volatile LONG  atomic_int;
45*c0909341SAndroid Build Coastguard Worker typedef volatile ULONG atomic_uint;
46*c0909341SAndroid Build Coastguard Worker 
47*c0909341SAndroid Build Coastguard Worker typedef enum {
48*c0909341SAndroid Build Coastguard Worker     memory_order_relaxed,
49*c0909341SAndroid Build Coastguard Worker     memory_order_acquire
50*c0909341SAndroid Build Coastguard Worker } msvc_atomic_memory_order;
51*c0909341SAndroid Build Coastguard Worker 
52*c0909341SAndroid Build Coastguard Worker #define atomic_init(p_a, v)           do { *(p_a) = (v); } while(0)
53*c0909341SAndroid Build Coastguard Worker #define atomic_store(p_a, v)          InterlockedExchange((LONG*)p_a, v)
54*c0909341SAndroid Build Coastguard Worker #define atomic_load(p_a)              InterlockedCompareExchange((LONG*)p_a, 0, 0)
55*c0909341SAndroid Build Coastguard Worker #define atomic_exchange(p_a, v)       InterlockedExchange(p_a, v)
56*c0909341SAndroid Build Coastguard Worker #define atomic_load_explicit(p_a, mo) atomic_load(p_a)
57*c0909341SAndroid Build Coastguard Worker 
atomic_compare_exchange_strong_int(LONG * obj,LONG * expected,LONG desired)58*c0909341SAndroid Build Coastguard Worker static inline int atomic_compare_exchange_strong_int(LONG *obj, LONG *expected,
59*c0909341SAndroid Build Coastguard Worker                                                      LONG desired)
60*c0909341SAndroid Build Coastguard Worker {
61*c0909341SAndroid Build Coastguard Worker     LONG orig = *expected;
62*c0909341SAndroid Build Coastguard Worker     *expected = InterlockedCompareExchange(obj, desired, orig);
63*c0909341SAndroid Build Coastguard Worker     return *expected == orig;
64*c0909341SAndroid Build Coastguard Worker }
65*c0909341SAndroid Build Coastguard Worker #define atomic_compare_exchange_strong(p_a, expected, desired) atomic_compare_exchange_strong_int((LONG *)p_a, (LONG *)expected, (LONG)desired)
66*c0909341SAndroid Build Coastguard Worker 
67*c0909341SAndroid Build Coastguard Worker /*
68*c0909341SAndroid Build Coastguard Worker  * TODO use a special call to increment/decrement
69*c0909341SAndroid Build Coastguard Worker  * using InterlockedIncrement/InterlockedDecrement
70*c0909341SAndroid Build Coastguard Worker  */
71*c0909341SAndroid Build Coastguard Worker #define atomic_fetch_add(p_a, inc)    InterlockedExchangeAdd(p_a, inc)
72*c0909341SAndroid Build Coastguard Worker #define atomic_fetch_sub(p_a, dec)    InterlockedExchangeAdd(p_a, -(dec))
73*c0909341SAndroid Build Coastguard Worker #define atomic_fetch_or(p_a, v)       InterlockedOr(p_a, v)
74*c0909341SAndroid Build Coastguard Worker #define atomic_fetch_add_explicit(p_a, inc, mo) atomic_fetch_add(p_a, inc)
75*c0909341SAndroid Build Coastguard Worker 
76*c0909341SAndroid Build Coastguard Worker #endif /* ! stdatomic.h */
77*c0909341SAndroid Build Coastguard Worker 
78*c0909341SAndroid Build Coastguard Worker #pragma warning(pop)
79*c0909341SAndroid Build Coastguard Worker 
80*c0909341SAndroid Build Coastguard Worker #endif /* !defined(__cplusplus) && defined(_MSC_VER) */
81*c0909341SAndroid Build Coastguard Worker 
82*c0909341SAndroid Build Coastguard Worker #endif /* MSCVER_STDATOMIC_H_ */
83