Motr  M0
__sync_atomic.h
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #pragma once
24 
25 #ifndef __MOTR_LIB___SYNC_ATOMIC_H__
26 #define __MOTR_LIB___SYNC_ATOMIC_H__
27 
28 #include "lib/types.h"
29 #include "lib/assert.h"
30 
57 struct m0_atomic64 {
58  long a_value;
59 };
60 
61 static inline void m0_atomic64_set(struct m0_atomic64 *a, int64_t num)
62 {
63  M0_CASSERT(sizeof a->a_value == sizeof num);
64 
65  a->a_value = num;
66 }
67 
71 static inline int64_t m0_atomic64_get(const struct m0_atomic64 *a)
72 {
73  return a->a_value;
74 }
75 
83 static inline void m0_atomic64_inc(struct m0_atomic64 *a)
84 {
85  __sync_fetch_and_add(&a->a_value, 1);
86 }
87 
95 static inline void m0_atomic64_dec(struct m0_atomic64 *a)
96 {
97  __sync_fetch_and_sub(&a->a_value, 1);
98 }
99 
103 static inline void m0_atomic64_add(struct m0_atomic64 *a, int64_t num)
104 {
105  __sync_fetch_and_add(&a->a_value, num);
106 }
107 
111 static inline void m0_atomic64_sub(struct m0_atomic64 *a, int64_t num)
112 {
113  __sync_fetch_and_sub(&a->a_value, num);
114 }
115 
123 static inline int64_t m0_atomic64_add_return(struct m0_atomic64 *a,
124  int64_t delta)
125 {
126  return __sync_add_and_fetch(&a->a_value, delta);
127 }
128 
136 static inline int64_t m0_atomic64_sub_return(struct m0_atomic64 *a,
137  int64_t delta)
138 {
139  return m0_atomic64_add_return(a, -delta);
140 }
141 
142 static inline bool m0_atomic64_inc_and_test(struct m0_atomic64 *a)
143 {
144  return __sync_add_and_fetch(&a->a_value, 1) == 0;
145 }
146 
147 static inline bool m0_atomic64_dec_and_test(struct m0_atomic64 *a)
148 {
149  return __sync_sub_and_fetch(&a->a_value, 1) == 0;
150 }
151 
152 static inline bool
153 m0_atomic64_cas(int64_t * loc, int64_t oldval, int64_t newval)
154 {
155  return __sync_bool_compare_and_swap(loc, oldval, newval);
156 }
157 
158 static inline void m0_mb(void)
159 {
160  __sync_synchronize();
161 }
162 
164 #endif /* __MOTR_LIB___SYNC_ATOMIC_H__ */
165 
166 /*
167  * Local variables:
168  * c-indentation-style: "K&R"
169  * c-basic-offset: 8
170  * tab-width: 8
171  * fill-column: 80
172  * scroll-step: 1
173  * End:
174  */
static bool m0_atomic64_cas(int64_t *loc, int64_t oldval, int64_t newval)
static void m0_mb(void)
#define M0_CASSERT(cond)
static bool m0_atomic64_inc_and_test(struct m0_atomic64 *a)
static int64_t m0_atomic64_add_return(struct m0_atomic64 *a, int64_t delta)
static void m0_atomic64_sub(struct m0_atomic64 *a, int64_t num)
static void m0_atomic64_set(struct m0_atomic64 *a, int64_t num)
Definition: __sync_atomic.h:61
static void m0_atomic64_inc(struct m0_atomic64 *a)
Definition: __sync_atomic.h:83
static void m0_atomic64_add(struct m0_atomic64 *a, int64_t num)
static int64_t m0_atomic64_sub_return(struct m0_atomic64 *a, int64_t delta)
static int64_t m0_atomic64_get(const struct m0_atomic64 *a)
Definition: __sync_atomic.h:71
static void m0_atomic64_dec(struct m0_atomic64 *a)
Definition: __sync_atomic.h:95
int num
Definition: bulk_if.c:54
static bool m0_atomic64_dec_and_test(struct m0_atomic64 *a)