Main MRPT website > C++ reference for MRPT 1.5.6
atomic_incr.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/synch/atomic_incr.h>
13 
14 using namespace mrpt::synch;
15 
16 #ifdef MRPT_OS_WINDOWS
17  #include <windows.h>
18 #elif defined( __clang__ ) && defined( MRPT_OS_APPLE )
19  //no include
20 #elif defined( __GNUC__ )
21  #if ( __GNUC__ * 100 + __GNUC_MINOR__ >= 402 )
22  # include <ext/atomicity.h>
23  #else
24  # include <bits/atomicity.h>
25  #endif
26 #else
27  #error This is not Windows and compiler is not GCC: Non implemented atomic_incr for this case
28 #endif
29 
30 
31 #ifdef MRPT_OS_WINDOWS
33  {
34  InterlockedIncrement(&m_value);
35  }
36 
38  {
39  return InterlockedDecrement(&m_value);
40  }
41 
42  CAtomicCounter::operator CAtomicCounter::atomic_num_t() const
43  {
44  return static_cast<long const volatile &>( m_value );
45  }
46 
47 #else
48  // This should be GCC:
49  #if ( defined( __i386__ ) || defined( __x86_64__ ) )
51  {
52  __asm__
53  (
54  "lock\n\t"
55  "incl %0":
56  "+m"( m_value ): // output (%0)
57  : // inputs
58  "cc" // clobbers
59  );
60  }
61 
62  int my_atomic_exchange_and_add( int * pw, int dv )
63  {
64  // int r = *pw;
65  // *pw += dv;
66  // return r;
67  int r;
68  __asm__ __volatile__
69  (
70  "lock\n\t"
71  "xadd %1, %0":
72  "+m"( *pw ), "=r"( r ): // outputs (%0, %1)
73  "1"( dv ): // inputs (%2 == %1)
74  "memory", "cc" // clobbers
75  );
76  return r;
77  }
78 
80  {
81  return my_atomic_exchange_and_add( &m_value, -1 ) - 1;
82  }
83 
84  CAtomicCounter::operator CAtomicCounter::atomic_num_t() const
85  {
86  return my_atomic_exchange_and_add( &m_value, 0 );
87  }
88 
89  #else
90  #if defined(__GLIBCXX__) // g++ 3.4+
91  using __gnu_cxx::__atomic_add;
92  using __gnu_cxx::__exchange_and_add;
93  #endif
94 
96  {
97  __atomic_add(&m_value, 1);
98  }
99 
101  {
102  return __exchange_and_add(&m_value, -1) - 1;
103  }
104 
105  CAtomicCounter::operator CAtomicCounter::atomic_num_t() const
106  {
107  return __exchange_and_add(&m_value, 0);
108  }
109  #endif
110 #endif
111 
void operator++()
Atomic increment of value.
Definition: atomic_incr.cpp:95
This namespace provides multitask, synchronization utilities.
Definition: atomic_incr.h:29
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
atomic_num_t operator--()
Atomic decrement of value and return new value.



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019