Arcane  v4.1.0.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
Atomic.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 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-2025 */
9/* */
10/* Types atomiques pour le multi-threading. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/Atomic.h"
15
16#include <atomic>
17
18#include <iostream>
19
20/*---------------------------------------------------------------------------*/
21/*---------------------------------------------------------------------------*/
22
23namespace Arcane
24{
25
26namespace
27{
28 void _setValue(volatile Int32* ptr, Int32 value)
29 {
30 std::atomic_ref<Int32> r(*const_cast<Int32*>(ptr));
31 r.store(value);
32 }
33 Int32 _getValue(volatile Int32* ptr)
34 {
35 std::atomic_ref<Int32> r(*const_cast<Int32*>(ptr));
36 return r.load();
37 }
38 Int32 _atomicAdd(volatile Int32* ptr)
39 {
40 std::atomic_ref<Int32> r(*const_cast<Int32*>(ptr));
41 return r.fetch_add(1) + 1;
42 }
43 Int32 _atomicSub(volatile Int32* ptr)
44 {
45 std::atomic_ref<Int32> r(*const_cast<Int32*>(ptr));
46 return r.fetch_sub(1) - 1;
47 }
48} // namespace
49
50/*---------------------------------------------------------------------------*/
51/*---------------------------------------------------------------------------*/
52
54AtomicInt32(int v)
55{
56 _setValue(&m_value, v);
57}
58
59Int32 AtomicInt32::
60operator++()
61{
62 return _atomicAdd(&m_value);
63}
64
65Int32 AtomicInt32::
66operator--()
67{
68 return _atomicSub(&m_value);
69}
70
71void AtomicInt32::
72operator=(Int32 v)
73{
74 _setValue(&m_value, v);
75}
76
77/*---------------------------------------------------------------------------*/
78/*---------------------------------------------------------------------------*/
79
80Int32 AtomicInt32::
81value() const
82{
83 return _getValue(&m_value);
84}
85
86Int32 AtomicInt32::
87increment(volatile Int32* v)
88{
89 return _atomicAdd(v);
90}
91
92Int32 AtomicInt32::
93decrement(volatile Int32* v)
94{
95 return _atomicSub(v);
96}
97
98void AtomicInt32::
99setValue(volatile Int32* v, Int32 new_v)
100{
101 _setValue(v, new_v);
102}
103
104Int32 AtomicInt32::
105getValue(volatile Int32* v)
106{
107 return _getValue(v);
108}
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/
112
113} // End namespace Arcane
114
115/*---------------------------------------------------------------------------*/
116/*---------------------------------------------------------------------------*/
AtomicInt32()
Constructeur: attention, aucune initialisation.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
std::int32_t Int32
Type entier signé sur 32 bits.