Mercury
mercury_atomic.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Argonne National Laboratory, Department of Energy,
3  *                    UChicago Argonne, LLC and The HDF Group.
4  * All rights reserved.
5  *
6  * The full copyright notice, including terms governing use, modification,
7  * and redistribution, is contained in the COPYING file that can be
8  * found at the root of the source code distribution tree.
9  */
10 
11 #ifndef MERCURY_ATOMIC_H
12 #define MERCURY_ATOMIC_H
13 
14 #include "mercury_util_config.h"
15 
16 #if defined(_WIN32)
17  #include <windows.h>
18  typedef struct { volatile LONG value; } hg_atomic_int32_t;
19 #elif defined(__APPLE__)
20  #include <libkern/OSAtomic.h>
21  typedef struct { volatile hg_util_int32_t value; } hg_atomic_int32_t;
22 #else
23  #include <opa_primitives.h>
24  typedef OPA_int_t hg_atomic_int32_t;
25 #endif
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
37 static HG_UTIL_INLINE void
38 hg_atomic_set32(hg_atomic_int32_t *ptr, hg_util_int32_t value)
39 {
40 #if defined(_WIN32) || defined(__APPLE__)
41  ptr->value = value;
42 #else
43  OPA_store_int(ptr, value);
44 #endif
45 }
46 
54 static HG_UTIL_INLINE hg_util_int32_t
56 {
57  hg_util_int32_t ret;
58 
59 #if defined(_WIN32) || defined(__APPLE__)
60  ret = ptr->value;
61 #else
62  ret = OPA_load_int(ptr);
63 #endif
64 
65  return ret;
66 }
67 
75 static HG_UTIL_INLINE hg_util_int32_t
77 {
78  hg_util_int32_t ret;
79 
80 #if defined(_WIN32)
81  ret = InterlockedIncrement(&ptr->value);
82 #elif defined(__APPLE__)
83  ret = OSAtomicIncrement32(&ptr->value);
84 #else
85  ret = OPA_fetch_and_incr_int(ptr) + 1;
86 #endif
87 
88  return ret;
89 }
90 
98 static HG_UTIL_INLINE hg_util_int32_t
100 {
101  hg_util_int32_t ret;
102 
103 #if defined(_WIN32)
104  ret = InterlockedDecrement(&ptr->value);
105 #elif defined(__APPLE__)
106  ret = OSAtomicDecrement32(&ptr->value);
107 #else
108  ret = OPA_fetch_and_decr_int(ptr) - 1;
109 #endif
110 
111  return ret;
112 }
113 
124 static HG_UTIL_INLINE hg_util_bool_t
125 hg_atomic_cas32(hg_atomic_int32_t *ptr, hg_util_int32_t compare_value,
126  hg_util_int32_t swap_value)
127 {
128  hg_util_bool_t ret;
129 
130 #if defined(_WIN32)
131  ret = (compare_value == InterlockedCompareExchange(&ptr->value, swap_value,
132  compare_value));
133 #elif defined(__APPLE__)
134  ret = OSAtomicCompareAndSwap32(compare_value, swap_value, &ptr->value);
135 #else
136  ret = (hg_util_bool_t) (compare_value == OPA_cas_int(ptr, compare_value, swap_value));
137 #endif
138 
139  return ret;
140 }
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 #endif /* MERCURY_ATOMIC_H */
static HG_UTIL_INLINE hg_util_int32_t hg_atomic_get32(hg_atomic_int32_t *ptr)
Get atomic value (32-bit integer).
OPA_int_t hg_atomic_int32_t
static HG_UTIL_INLINE hg_util_int32_t hg_atomic_decr32(hg_atomic_int32_t *ptr)
Decrement atomic value (32-bit integer).
static HG_UTIL_INLINE void hg_atomic_set32(hg_atomic_int32_t *ptr, hg_util_int32_t value)
Set atomic value (32-bit integer).
static HG_UTIL_INLINE hg_util_bool_t hg_atomic_cas32(hg_atomic_int32_t *ptr, hg_util_int32_t compare_value, hg_util_int32_t swap_value)
Compare and swap values (32-bit integer).
static HG_UTIL_INLINE hg_util_int32_t hg_atomic_incr32(hg_atomic_int32_t *ptr)
Increment atomic value (32-bit integer).