stdatomic.h 8.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

#pragma once

#define WIN32_LEAN_AND_MEAN
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <windows.h>

#include <isc/util.h>

#ifndef __ATOMIC_RELAXED
#define __ATOMIC_RELAXED        0
#endif
#ifndef __ATOMIC_CONSUME
#define __ATOMIC_CONSUME        1
#endif
#ifndef __ATOMIC_ACQUIRE
#define __ATOMIC_ACQUIRE        2
#endif
#ifndef __ATOMIC_RELEASE
#define __ATOMIC_RELEASE        3
#endif
#ifndef __ATOMIC_ACQ_REL
#define __ATOMIC_ACQ_REL        4
#endif
#ifndef __ATOMIC_SEQ_CST
#define __ATOMIC_SEQ_CST        5
#endif

enum memory_order {
	memory_order_relaxed = __ATOMIC_RELAXED,
	memory_order_consume = __ATOMIC_CONSUME,
	memory_order_acquire = __ATOMIC_ACQUIRE,
	memory_order_release = __ATOMIC_RELEASE,
	memory_order_acq_rel = __ATOMIC_ACQ_REL,
	memory_order_seq_cst = __ATOMIC_SEQ_CST
};

typedef enum memory_order memory_order;

typedef int_fast32_t volatile	atomic_int_fast32_t;
typedef uint_fast32_t volatile	atomic_uint_fast32_t;
typedef int_fast64_t volatile	atomic_int_fast64_t;
typedef uint_fast64_t volatile	atomic_uint_fast64_t;

#define atomic_init(obj, desired)				\
	(*(obj) = (desired))

#define atomic_store_explicit32(obj, desired, order)		\
	(order == memory_order_relaxed				\
	 ? InterlockedExchangeNoFence((atomic_int_fast32_t *)obj, desired)	\
	 : (order == memory_order_acquire			\
	    ? InterlockedExchangeAcquire((atomic_int_fast32_t *)obj, desired) \
	    : InterlockedExchange((atomic_int_fast32_t *)obj, desired)))

67
#ifdef _WIN64
68 69 70 71 72 73
#define atomic_store_explicit64(obj, desired, order)		\
	(order == memory_order_relaxed				\
	 ? InterlockedExchangeNoFence64((atomic_int_fast64_t *)obj, desired) \
	 : (order == memory_order_acquire			\
	    ? InterlockedExchangeAcquire64((atomic_int_fast64_t *)obj, desired) \
	    : InterlockedExchange64((atomic_int_fast64_t *)obj, desired)))
74 75 76 77
#else
#define atomic_store_explicit64(obj, desired, order)		\
	InterlockedExchange64((atomic_int_fast64_t *)obj, desired)
#endif
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

static inline
void
atomic_store_abort() {
	INSIST(0);
	return;
}

#define atomic_store_explicit(obj, desired, order) 		\
	(sizeof(*obj) == 8					\
	 ? atomic_store_explicit64(obj, desired, order)		\
	 : (sizeof(*obj) == 4					\
	    ? atomic_store_explicit32(obj, desired, order)	\
	    : atomic_store_abort()))

#define atomic_store(obj, desired) \
	atomic_store(obj, desider, memory_order_seq_cst)

#define atomic_load_explicit32(obj, order)			\
	(order == memory_order_relaxed			\
	 ? (int32_t)InterlockedOrNoFence((atomic_int_fast32_t *)obj, 0)	\
	 : (order == memory_order_acquire			\
	    ? (int32_t)InterlockedOrAcquire((atomic_int_fast32_t *)obj, 0)	\
	    : (order == memory_order_release			\
	       ? (int32_t)InterlockedOrRelease((atomic_int_fast32_t *)obj, 0) \
	       : (int32_t)InterlockedOr((atomic_int_fast32_t *)obj, 0))))

105
#ifdef _WIN64
106 107 108 109 110 111 112 113
#define atomic_load_explicit64(obj, order)			\
	(order == memory_order_relaxed				\
	 ? InterlockedOr64NoFence((atomic_int_fast64_t *)obj, 0)	\
	 : (order == memory_order_acquire			\
	    ? InterlockedOr64Acquire((atomic_int_fast64_t *)obj, 0)	\
	    : (order == memory_order_release			\
	       ? InterlockedOr64Release((atomic_int_fast64_t *)obj, 0)	\
	       : InterlockedOr64((atomic_int_fast64_t *)obj, 0))))
114 115 116 117
#else
#define atomic_load_explicit64(obj, order)			\
	InterlockedOr64((atomic_int_fast64_t *)obj, 0)
#endif
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

static inline
int8_t
atomic_load_abort() {
	INSIST(0);
	return (0);
}

#define atomic_load_explicit(obj, order)			\
	(sizeof(*obj) == 8					\
	 ? atomic_load_explicit64(obj, order)			\
	 : (sizeof(*obj == 4)					\
	    ? atomic_load_explicit32(obj, order)		\
	    : atomic_load_abort()))

#define atomic_load(obj)					\
	atomic_load_explicit(obj, memory_order_seq_cst)

#define atomic_fetch_add_explicit32(obj, arg, order)		\
	(order == memory_order_relaxed				\
	 ? InterlockedExchangeAddNoFence((atomic_int_fast32_t *)obj, arg)	\
	 : (order == memory_order_acquire			\
	    ? InterlockedExchangeAddAcquire((atomic_int_fast32_t *)obj, arg) \
	    : (order == memory_order_release			\
	       ? InterlockedExchangeAddRelease((atomic_int_fast32_t *)obj, arg) \
	       : InterlockedExchange((atomic_int_fast32_t *)obj, arg))))

145
#ifdef _WIN64
146 147 148 149 150 151 152 153
#define atomic_fetch_add_explicit64(obj, arg, order)		\
	(order == memory_order_relaxed				\
	 ? InterlockedExchangeAddNoFence64((atomic_int_fast64_t *)obj, arg) \
	 : (order == memory_order_acquire			\
	    ? InterlockedExchangeAddAcquire64((atomic_int_fast64_t *)obj, arg) \
	    : (order == memory_order_release			\
	       ? InterlockedExchangeAddRelease64((atomic_int_fast64_t *)obj, arg) \
	       : InterlockedExchange64((atomic_int_fast64_t *)obj, arg))))
154 155 156 157
#else
#define atomic_fetch_add_explicit64(obj, arg, order)	\
	InterlockedExchange64((atomic_int_fast64_t *)obj, arg)
#endif
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

static inline
int8_t
atomic_add_abort() {
	INSIST(0);
	return (0);
}

#define atomic_fetch_add_explicit(obj, arg, order)		\
	(sizeof(*obj) == 8					\
	 ? atomic_fetch_add_explicit64(obj, arg, order)		\
	 : (sizeof(*obj) == 4					\
	    ? atomic_fetch_add_explicit32(obj, arg, order)	\
	    : atomic_add_abort()))

#define atomic_fetch_add(obj, arg)				\
	atomic_fetch_add_explicit(obj, arg, memory_order_seq_cst)

#define atomic_fetch_sub_explicit(obj, arg, order)		\
	atomic_fetch_add_explicit(obj, -arg, order)

#define atomic_fetch_sub(obj, arg)				\
	atomic_fetch_sub_explicit(obj, arg, memory_order_seq_cst)

static inline bool
atomic_compare_exchange_strong_explicit32(atomic_int_fast32_t *obj,
					  int32_t *expected,
					  int32_t desired,
					  memory_order succ,
					  memory_order fail) {
	bool __r;
	int32_t __v;
	REQUIRE(succ == fail);
	switch (succ) {
	case memory_order_relaxed:
		__v = InterlockedCompareExchangeNoFence((atomic_int_fast32_t *)obj, desired, *expected);
		break;
	case memory_order_acquire:
		__v = InterlockedCompareExchangeAcquire((atomic_int_fast32_t *)obj, desired, *expected);
		break;
	case memory_order_release:
		__v = InterlockedCompareExchangeRelease((atomic_int_fast32_t *)obj, desired, *expected);
		break;
	default:
		__v = InterlockedCompareExchange((atomic_int_fast32_t *)obj, desired, *expected);
		break;
	}
	__r = (*(expected) == __v);
	if (!__r) {
		*(expected) = __v;
	}
	return (__r);
}

static inline bool
atomic_compare_exchange_strong_explicit64(atomic_int_fast64_t *obj,
					  int64_t *expected,
					  int64_t desired,
					  memory_order succ,
					  memory_order fail) {
	bool __r;
	int64_t __v;
	REQUIRE(succ == fail);
221
#ifdef _WIN64
222 223
	switch (succ) {
	case memory_order_relaxed:
224
		__v = InterlockedCompareExchangeNoFence64((atomic_int_fast64_t *)obj, desired, *expected);
225 226
		break;
	case memory_order_acquire:
227
		__v = InterlockedCompareExchangeAcquire64((atomic_int_fast64_t *)obj, desired, *expected);
228 229
		break;
	case memory_order_release:
230
		__v = InterlockedCompareExchangeRelease64((atomic_int_fast64_t *)obj, desired, *expected);
231 232 233 234 235
		break;
	default:
		__v = InterlockedCompareExchange64((atomic_int_fast64_t *)obj, desired, *expected);
		break;
	}
236 237 238
#else
	__v = InterlockedCompareExchange64((atomic_int_fast64_t *)obj, desired, *expected);
#endif
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	__r = (*(expected) == __v);
	if (!__r) {
		*(expected) = __v;
	}
	return (__r);
}

static inline
bool
atomic_compare_exchange_abort() {
	INSIST(0);
	return (false);
}

#define atomic_compare_exchange_strong_explicit(obj, expected, desired, \
						succ, fail)		\
	(sizeof(*obj) == 8						\
	 ? atomic_compare_exchange_strong_explicit64(obj, expected,	\
						     desired,		\
						     succ, fail)	\
	 : (sizeof(*obj) == 4						\
	    ? atomic_compare_exchange_strong_explicit32(obj, expected,	\
							desired,	\
							succ, fail)	\
	    : atomic_compare_exchange_abort()))

#define atomic_compare_exchange_strong(obj, expected, desired,		\
				       succ, fail)			\
	atomic_compare_exchange_strong_explicit(obj, expected, desired, \
						memory_order_cst_seq,	\
						memory_order_cst_seq)

#define atomic_compare_exchange_weak_explicit(obj, expected, desired,	\
					      succ, fail)		\
	atomic_compare_exchange_strong_explicit(obj, expected, desired, \
						succ, fail)

#define atomic_compare_exchange_weak(obj, expected, desired)		\
	atomic_compare_exchange_weak_explicit(obj, expected, desired,	\
					      memory_order_cst_seq,	\
					      memory_order_cst_seq)