[23684b7] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[23684b7] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[174156fd] | 29 | /** @addtogroup kernel_generic
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_ATOMIC_H_
|
---|
| 36 | #define KERN_ATOMIC_H_
|
---|
[23684b7] | 37 |
|
---|
[4621d23] | 38 | #include <stdbool.h>
|
---|
[d99c1d2] | 39 | #include <typedefs.h>
|
---|
[4621d23] | 40 | #include <stdatomic.h>
|
---|
[23684b7] | 41 |
|
---|
[c53e813] | 42 | #define atomic_predec(val) \
|
---|
| 43 | (atomic_fetch_sub((val), 1) - 1)
|
---|
[4621d23] | 44 |
|
---|
[c53e813] | 45 | #define atomic_preinc(val) \
|
---|
| 46 | (atomic_fetch_add((val), 1) + 1)
|
---|
[4621d23] | 47 |
|
---|
[c53e813] | 48 | #define atomic_postdec(val) \
|
---|
| 49 | atomic_fetch_sub((val), 1)
|
---|
[4621d23] | 50 |
|
---|
[c53e813] | 51 | #define atomic_postinc(val) \
|
---|
| 52 | atomic_fetch_add((val), 1)
|
---|
[4621d23] | 53 |
|
---|
[c53e813] | 54 | #define atomic_dec(val) \
|
---|
| 55 | ((void) atomic_fetch_sub(val, 1))
|
---|
[4621d23] | 56 |
|
---|
[c53e813] | 57 | #define atomic_inc(val) \
|
---|
| 58 | ((void) atomic_fetch_add(val, 1))
|
---|
[618d02a] | 59 |
|
---|
| 60 | #define local_atomic_exchange(var_addr, new_val) \
|
---|
[8591b31] | 61 | atomic_exchange_explicit( \
|
---|
| 62 | (_Atomic typeof(*(var_addr)) *) (var_addr), \
|
---|
| 63 | (new_val), memory_order_relaxed)
|
---|
[618d02a] | 64 |
|
---|
[b2ec5cf] | 65 | #if __64_BITS__
|
---|
| 66 |
|
---|
| 67 | typedef struct {
|
---|
| 68 | atomic_uint_fast64_t value;
|
---|
| 69 | } atomic_time_stat_t;
|
---|
| 70 |
|
---|
| 71 | #define ATOMIC_TIME_INITIALIZER() (atomic_time_stat_t) {}
|
---|
| 72 |
|
---|
| 73 | static inline void atomic_time_increment(atomic_time_stat_t *time, int a)
|
---|
| 74 | {
|
---|
| 75 | /*
|
---|
| 76 | * We require increments to be synchronized with each other, so we
|
---|
| 77 | * can use ordinary reads and writes instead of a more expensive atomic
|
---|
| 78 | * read-modify-write operations.
|
---|
| 79 | */
|
---|
| 80 | uint64_t v = atomic_load_explicit(&time->value, memory_order_relaxed);
|
---|
| 81 | atomic_store_explicit(&time->value, v + a, memory_order_relaxed);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | static inline uint64_t atomic_time_read(atomic_time_stat_t *time)
|
---|
| 85 | {
|
---|
| 86 | return atomic_load_explicit(&time->value, memory_order_relaxed);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | #else
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * A monotonically increasing 64b time statistic.
|
---|
| 93 | * Increments must be synchronized with each other (or limited to a single
|
---|
| 94 | * thread/CPU), but reads can be performed from any thread.
|
---|
| 95 | *
|
---|
| 96 | */
|
---|
| 97 | typedef struct {
|
---|
| 98 | uint64_t true_value;
|
---|
| 99 | atomic_uint_fast32_t high1;
|
---|
| 100 | atomic_uint_fast32_t high2;
|
---|
| 101 | atomic_uint_fast32_t low;
|
---|
| 102 | } atomic_time_stat_t;
|
---|
| 103 |
|
---|
| 104 | #define ATOMIC_TIME_INITIALIZER() (atomic_time_stat_t) {}
|
---|
| 105 |
|
---|
| 106 | static inline void atomic_time_increment(atomic_time_stat_t *time, int a)
|
---|
| 107 | {
|
---|
| 108 | /*
|
---|
| 109 | * On 32b architectures, we can't rely on 64b memory reads/writes being
|
---|
| 110 | * architecturally atomic, but we also don't want to pay the cost of
|
---|
| 111 | * emulating atomic reads/writes, so instead we split value in half
|
---|
| 112 | * and perform some ordering magic to make sure readers always get
|
---|
| 113 | * consistent value.
|
---|
| 114 | */
|
---|
| 115 |
|
---|
| 116 | /* true_value is only used by the writer, so this need not be atomic. */
|
---|
| 117 | uint64_t val = time->true_value;
|
---|
| 118 | uint32_t old_high = val >> 32;
|
---|
| 119 | val += a;
|
---|
| 120 | uint32_t new_high = val >> 32;
|
---|
| 121 | time->true_value = val;
|
---|
| 122 |
|
---|
| 123 | /* Tell GCC that the first branch is far more likely than the second. */
|
---|
| 124 | if (__builtin_expect(old_high == new_high, 1)) {
|
---|
| 125 | /* If the high half didn't change, we need not bother with barriers. */
|
---|
| 126 | atomic_store_explicit(&time->low, (uint32_t) val, memory_order_relaxed);
|
---|
| 127 | } else {
|
---|
| 128 | /*
|
---|
| 129 | * If both halves changed, extra ordering is necessary.
|
---|
| 130 | * The idea is that if reader reads high1 and high2 with the same value,
|
---|
| 131 | * it is guaranteed that they read the correct low half for that value.
|
---|
| 132 | *
|
---|
| 133 | * This is the same sequence that is used by userspace to read clock.
|
---|
| 134 | */
|
---|
| 135 | atomic_store_explicit(&time->high1, new_high, memory_order_relaxed);
|
---|
| 136 | atomic_store_explicit(&time->low, (uint32_t) val, memory_order_release);
|
---|
| 137 | atomic_store_explicit(&time->high2, new_high, memory_order_release);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | static inline uint64_t atomic_time_read(atomic_time_stat_t *time)
|
---|
| 142 | {
|
---|
| 143 | uint32_t high2 = atomic_load_explicit(&time->high2, memory_order_acquire);
|
---|
| 144 | uint32_t low = atomic_load_explicit(&time->low, memory_order_acquire);
|
---|
| 145 | uint32_t high1 = atomic_load_explicit(&time->high1, memory_order_relaxed);
|
---|
| 146 |
|
---|
| 147 | if (high1 != high2)
|
---|
| 148 | low = 0;
|
---|
| 149 |
|
---|
| 150 | /* If the values differ, high1 is always the newer value. */
|
---|
| 151 | return (uint64_t) high1 << 32 | (uint64_t) low;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | #endif /* __64_BITS__ */
|
---|
| 155 |
|
---|
[23684b7] | 156 | #endif
|
---|
[b45c443] | 157 |
|
---|
[42d3be3] | 158 | /** @}
|
---|
[b45c443] | 159 | */
|
---|