Arcane  v3.14.10.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
Atomic.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* Atomic.cc (C) 2000-2024 */
9/* */
10/* Types atomiques pour le multi-threading. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/Atomic.h"
15
16#ifdef ARCANE_HAS_CXX20
17#include <atomic>
18#else
19#include <glib.h>
20#endif
21
22#include <iostream>
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane
28{
29
30namespace
31{
32 void _setValue(volatile Int32* ptr, Int32 value)
33 {
34#ifdef ARCANE_HAS_CXX20
35 std::atomic_ref<Int32> r(*const_cast<Int32*>(ptr));
36 r.store(value);
37#else
38 g_atomic_int_set(ptr, value);
39#endif
40 }
41 Int32 _getValue(volatile Int32* ptr)
42 {
43#ifdef ARCANE_HAS_CXX20
44 std::atomic_ref<Int32> r(*const_cast<Int32*>(ptr));
45 return r.load();
46#else
47 return g_atomic_int_get(ptr);
48#endif
49 }
50 Int32 _atomicAdd(volatile Int32* ptr)
51 {
52#ifdef ARCANE_HAS_CXX20
53 std::atomic_ref<Int32> r(*const_cast<Int32*>(ptr));
54 return r.fetch_add(1) + 1;
55#else
56 return g_atomic_int_add(ptr, 1) + 1;
57#endif
58 }
59 Int32 _atomicSub(volatile Int32* ptr)
60 {
61#ifdef ARCANE_HAS_CXX20
62 std::atomic_ref<Int32> r(*const_cast<Int32*>(ptr));
63 return r.fetch_sub(1) - 1;
64#else
65 return g_atomic_int_add(ptr, -1) - 1;
66#endif
67 }
68} // namespace
69
70/*---------------------------------------------------------------------------*/
71/*---------------------------------------------------------------------------*/
72
73/*
74 * Pour l'instant, utilise les fonctions de la glib. A terme, il faudra
75 * utiliser le type std::atomic de la stl de la norme C++0x.
76 *
77 * Pour supporter les veilles versions de la glib, il ne faut
78 * utiliser que les fonctions suivantes:
79 *
80 * gint g_atomic_int_exchange_and_add(gint *atomic, gint val);
81 * void g_atomic_int_add(gint *atomic,gint val);
82 * gboolean g_atomic_int_compare_and_exchange(gint *atomic,gint oldval, gint newval);
83 * gint g_atomic_int_get(gint *atomic);
84 */
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
89AtomicInt32(int v)
90{
91 _setValue(&m_value, v);
92}
93
94Int32 AtomicInt32::
95operator++()
96{
97 return _atomicAdd(&m_value);
98}
99
100Int32 AtomicInt32::
101operator--()
102{
103 return _atomicSub(&m_value);
104}
105
106void AtomicInt32::
107operator=(Int32 v)
108{
109 _setValue(&m_value, v);
110}
111
112/*---------------------------------------------------------------------------*/
113/*---------------------------------------------------------------------------*/
114
115Int32 AtomicInt32::
116value() const
117{
118 return _getValue(&m_value);
119}
120
121Int32 AtomicInt32::
122increment(volatile Int32* v)
123{
124 return _atomicAdd(v);
125}
126
127Int32 AtomicInt32::
128decrement(volatile Int32* v)
129{
130 return _atomicSub(v);
131}
132
133void AtomicInt32::
134setValue(volatile Int32* v, Int32 new_v)
135{
136 _setValue(v, new_v);
137}
138
139Int32 AtomicInt32::
140getValue(volatile Int32* v)
141{
142 return _getValue(v);
143}
144
145/*---------------------------------------------------------------------------*/
146/*---------------------------------------------------------------------------*/
147
148} // End namespace Arcane
149
150/*---------------------------------------------------------------------------*/
151/*---------------------------------------------------------------------------*/
AtomicInt32()
Constructeur: attention, aucune initialisation.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-