1/* Copyright 2016 Google Inc.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License. */
14
15#ifndef NSYNC_PUBLIC_NSYNC_ATOMIC_H_
16#define NSYNC_PUBLIC_NSYNC_ATOMIC_H_
17
18#include "nsync_cpp.h"
19
20/* This file is not to be included directly by the client. It exists because
21 on some platforms, one cannot use a simple uint32_t with atomic operations.
22 */
23#if NSYNC_ATOMIC_TYPECHECK
24#include <inttypes.h>
25NSYNC_CPP_START_
26typedef struct { uint32_t value; } nsync_atomic_uint32_;
27NSYNC_CPP_END_
28#define NSYNC_ATOMIC_UINT32_INIT_ { 0 }
29#define NSYNC_ATOMIC_UINT32_LOAD_(p) ((p)->value)
30#define NSYNC_ATOMIC_UINT32_STORE_(p,v) ((p)->value = (v))
31#define NSYNC_ATOMIC_UINT32_PTR_(p) (&(p)->value)
32
33#elif NSYNC_ATOMIC_C11
34#include <stdatomic.h>
35NSYNC_CPP_START_
36typedef atomic_uint_least32_t nsync_atomic_uint32_;
37NSYNC_CPP_END_
38#define NSYNC_ATOMIC_UINT32_INIT_ 0
39#define NSYNC_ATOMIC_UINT32_LOAD_(p) (*(p))
40#define NSYNC_ATOMIC_UINT32_STORE_(p,v) (*(p) = (v))
41#define NSYNC_ATOMIC_UINT32_PTR_(p) (p)
42
43#elif NSYNC_ATOMIC_CPP11
44#include <atomic>
45NSYNC_CPP_START_
46typedef std::atomic<uint32_t> nsync_atomic_uint32_;
47NSYNC_CPP_END_
48#define NSYNC_ATOMIC_UINT32_INIT_ ATOMIC_VAR_INIT (0)
49#define NSYNC_ATOMIC_UINT32_LOAD_(p) (std::atomic_load (p))
50#define NSYNC_ATOMIC_UINT32_STORE_(p,v) (std::atomic_store ((p), (uint32_t) (v)))
51#define NSYNC_ATOMIC_UINT32_PTR_(p) (p)
52
53#else
54#include <inttypes.h>
55NSYNC_CPP_START_
56typedef uint32_t nsync_atomic_uint32_;
57NSYNC_CPP_END_
58#define NSYNC_ATOMIC_UINT32_INIT_ 0
59#define NSYNC_ATOMIC_UINT32_LOAD_(p) (*(p))
60#define NSYNC_ATOMIC_UINT32_STORE_(p,v) (*(p) = (v))
61#define NSYNC_ATOMIC_UINT32_PTR_(p) (p)
62#endif
63
64#endif /*NSYNC_PUBLIC_NSYNC_ATOMIC_H_*/
65