atomic.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2011 Equinor ASA, Norway.
3
4 The file 'atomic.h' is part of ERT - Ensemble based Reservoir Tool.
5
6 ERT is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 ERT is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.
14
15 See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
16 for more details.
17*/
18
19/*
20 This whole file was something I found on the internet - it was
21 posted as beeing in the public domain. The essential functions
22 __sync_add_and_fetch() and do on are (as I understand it) built in
23 gcc functions. Only available in reasonably new gcc versions
24 (4.1???).
25*/
26
27#ifndef _ATOMIC_H
28#define _ATOMIC_H
29
30
31
36typedef struct {
37 volatile int counter;
38} atomic_t;
39
40#define ATOMIC_INIT(i) �{ (i) }
41
48#define atomic_read(v) ((v)->counter)
49
55#define atomic_set(v,i) (((v)->counter) = (i))
56
62static inline void atomic_add( int i, atomic_t *v )
63{
64 (void)__sync_add_and_fetch(&v->counter, i);
65}
66
74static inline void atomic_sub( int i, atomic_t *v )
75{
76 (void)__sync_sub_and_fetch(&v->counter, i);
77}
78
88static inline int atomic_sub_and_test( int i, atomic_t *v )
89{
90 return !(__sync_sub_and_fetch(&v->counter, i));
91}
92
99static inline void atomic_inc( atomic_t *v )
100{
101 (void)__sync_fetch_and_add(&v->counter, 1);
102}
103
111static inline void atomic_dec( atomic_t *v )
112{
113 (void)__sync_fetch_and_sub(&v->counter, 1);
114}
115
124static inline int atomic_dec_and_test( atomic_t *v )
125{
126 return !(__sync_sub_and_fetch(&v->counter, 1));
127}
128
137static inline int atomic_inc_and_test( atomic_t *v )
138{
139 return !(__sync_add_and_fetch(&v->counter, 1));
140}
141
151static inline int atomic_add_negative( int i, atomic_t *v )
152{
153 return (__sync_add_and_fetch(&v->counter, i) < 0);
154}
155
156#endif
157
158/*****************************************************************/
159/*****************************************************************/
160/*****************************************************************/
161
164//static inline int atomic_add( int i, atomic_t *v )
165//{
166// return __sync_add_and_fetch(&v->counter, i);
167//}
168//
171//�* @brief compare and swap
172//�* @param v pointer of type atomic_t
173//�*
174//�* If the current value of @b v is @b oldval,
175//�* then write @b newval into @b v. Returns #TRUE if
176//�* the comparison is successful and @b newval was
177//�* written.
178//�*/
179//static inline int atomic_cas( atomic_t *v, int oldval, int newval )
180//{
181// return __sync_bool_compare_and_swap(&v->counter, oldval, newval);
182//}
183
static void atomic_dec(atomic_t *v)
decrement atomic variable �*
Definition: atomic.h:111
static int atomic_dec_and_test(atomic_t *v)
Decrement and test �*.
Definition: atomic.h:124
static void atomic_add(int i, atomic_t *v)
Definition: atomic.h:62
static void atomic_inc(atomic_t *v)
Definition: atomic.h:99
static int atomic_sub_and_test(int i, atomic_t *v)
Definition: atomic.h:88
static int atomic_add_negative(int i, atomic_t *v)
add and test if negative �*
Definition: atomic.h:151
static int atomic_inc_and_test(atomic_t *v)
Increment and test �*.
Definition: atomic.h:137
static void atomic_sub(int i, atomic_t *v)
Definition: atomic.h:74
Definition: atomic.h:36
volatile int counter
Definition: atomic.h:37