"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/include/isc/stdatomic.h" (7 Sep 2020, 5249 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stdatomic.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 #pragma once
   13 
   14 #if !defined(__has_feature)
   15 #define __has_feature(x) 0
   16 #endif
   17 
   18 #if !defined(__has_extension)
   19 #define __has_extension(x) __has_feature(x)
   20 #endif
   21 
   22 #if !defined(__GNUC_PREREQ__)
   23 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
   24 #define __GNUC_PREREQ__(maj, min)                    \
   25     ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
   26 #else
   27 #define __GNUC_PREREQ__(maj, min) 0
   28 #endif
   29 #endif
   30 
   31 #if !defined(__CLANG_ATOMICS) && !defined(__GNUC_ATOMICS)
   32 #if __has_extension(c_atomic) || __has_extension(cxx_atomic)
   33 #define __CLANG_ATOMICS
   34 #elif __GNUC_PREREQ__(4, 7)
   35 #define __GNUC_ATOMICS
   36 #elif !defined(__GNUC__)
   37 /* cppcheck-suppress preprocessorErrorDirective */
   38 #error "isc/stdatomic.h does not support your compiler"
   39 #endif
   40 #endif
   41 
   42 #ifndef __ATOMIC_RELAXED
   43 #define __ATOMIC_RELAXED        0
   44 #endif
   45 #ifndef __ATOMIC_CONSUME
   46 #define __ATOMIC_CONSUME        1
   47 #endif
   48 #ifndef __ATOMIC_ACQUIRE
   49 #define __ATOMIC_ACQUIRE        2
   50 #endif
   51 #ifndef __ATOMIC_RELEASE
   52 #define __ATOMIC_RELEASE        3
   53 #endif
   54 #ifndef __ATOMIC_ACQ_REL
   55 #define __ATOMIC_ACQ_REL        4
   56 #endif
   57 #ifndef __ATOMIC_SEQ_CST
   58 #define __ATOMIC_SEQ_CST        5
   59 #endif
   60 
   61 
   62 enum memory_order {
   63     memory_order_relaxed = __ATOMIC_RELAXED,
   64     memory_order_consume = __ATOMIC_CONSUME,
   65     memory_order_acquire = __ATOMIC_ACQUIRE,
   66     memory_order_release = __ATOMIC_RELEASE,
   67     memory_order_acq_rel = __ATOMIC_ACQ_REL,
   68     memory_order_seq_cst = __ATOMIC_SEQ_CST
   69 };
   70 
   71 typedef enum memory_order memory_order;
   72 
   73 typedef int_fast32_t    atomic_int_fast32_t;
   74 typedef uint_fast32_t   atomic_uint_fast32_t;
   75 typedef int_fast64_t    atomic_int_fast64_t;
   76 typedef uint_fast64_t   atomic_uint_fast64_t;
   77 
   78 #if defined(__CLANG_ATOMICS) /* __c11_atomic builtins */
   79 #define atomic_init(obj, desired)       \
   80     __c11_atomic_init(obj, desired)
   81 #define atomic_load_explicit(obj, order)    \
   82     __c11_atomic_load(obj, order)
   83 #define atomic_store_explicit(obj, desired, order)  \
   84     __c11_atomic_store(obj, desired, order)
   85 #define atomic_fetch_add_explicit(obj, arg, order)  \
   86     __c11_atomic_fetch_add(obj, arg, order)
   87 #define atomic_fetch_sub_explicit(obj, arg, order)  \
   88     __c11_atomic_fetch_sub(obj, arg, order)
   89 #define atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail) \
   90     __c11_atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail)
   91 #define atomic_compare_exchange_weak_explicit(obj, expected, desired, succ, fail)   \
   92     __c11_atomic_compare_exchange_weak_explicit(obj, expected, desired, succ, fail)
   93 #elif defined(__GNUC_ATOMICS) /* __atomic builtins */
   94 #define atomic_init(obj, desired)           \
   95     (*obj = desired)
   96 #define atomic_load_explicit(obj, order)        \
   97     __atomic_load_n(obj, order)
   98 #define atomic_store_explicit(obj, desired, order)  \
   99     __atomic_store_n(obj, desired, order)
  100 #define atomic_fetch_add_explicit(obj, arg, order)  \
  101     __atomic_fetch_add(obj, arg, order)
  102 #define atomic_fetch_sub_explicit(obj, arg, order)  \
  103     __atomic_fetch_sub(obj, arg, order)
  104 #define atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail) \
  105     __atomic_compare_exchange_n(obj, expected, desired, 0, succ, fail)
  106 #define atomic_compare_exchange_weak_explicit(obj, expected, desired, succ, fail)   \
  107     __atomic_compare_exchange_n(obj, expected, desired, 1, succ, fail)
  108 #else /* __sync builtins */
  109 #define atomic_init(obj, desired)           \
  110     (*obj = desired)
  111 #define atomic_load_explicit(obj, order)        \
  112     __sync_fetch_and_add(obj, 0)
  113 #define atomic_store_explicit(obj, desired, order)  \
  114     do {                        \
  115         __sync_synchronize();           \
  116         *obj = desired;             \
  117         __sync_synchronize();           \
  118     } while (0);
  119 #define atomic_fetch_add_explicit(obj, arg, order) \
  120     __sync_fetch_and_add(obj, arg)
  121 #define atomic_fetch_sub_explicit(obj, arg, order) \
  122     __sync_fetch_and_sub(obj, arg, order)
  123 #define atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail) \
  124     ({                                  \
  125         __typeof__(obj) __v;                        \
  126         _Bool __r;                          \
  127         __v = __sync_val_compare_and_swap(obj, *(expected), desired);   \
  128         __r = *(expected) == __v;                   \
  129         *(expected) = __v;                      \
  130         __r;                                \
  131     })
  132 #define atomic_compare_exchange_weak_explicit(obj, expected, desired, succ, fail)   \
  133     atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail)
  134 #endif
  135 
  136 #define atomic_load(obj) \
  137     atomic_load_explicit(obj, memory_order_seq_cst)
  138 #define atomic_store(obj) \
  139     atomic_store_explicit(obj, memory_order_seq_cst)
  140 #define atomic_fetch_add(obj) \
  141     atomic_fetch_add_explicit(obj, arg, memory_order_seq_cst)
  142 #define atomic_fetch_sub(obj) \
  143     atomic_fetch_sub_explicit(obj, arg, memory_order_seq_cst)
  144 #define atomic_compare_exchange_strong(obj, expected, desired)  \
  145     atomic_compare_exchange_strong_explicit(obj, expected, desired, memory_order_seq_cst, memory_order_seq_cst)
  146 #define atomic_compare_exchange_weak(obj, expected, desired)    \
  147     atomic_compare_exchange_weak_explicit(obj, expected, desired, memory_order_seq_cst, memory_order_seq_cst)