Orocos Real-Time Toolkit  2.8.3
oro_arch.h
Go to the documentation of this file.
1 #include "../../rtt-config.h"
2 #ifndef __ARCH_MSVC_ORO_ATOMIC__
3 #define __ARCH_MSVC_ORO_ATOMIC__
4 
5 #include <windows.h>
6 #undef interface
7 #include <intrin.h>
8 
9 typedef volatile long oro_atomic_t;
10 
11 #define ORO_ATOMIC_SETUP oro_atomic_set
12 #define ORO_ATOMIC_CLEANUP(a_int)
13 
14 #define oro_atomic_read(a_int) (*(a_int))
15 
16 #define oro_atomic_set(a_int,n) (*(a_int) = (n))
17 
18 static __forceinline void oro_atomic_add(oro_atomic_t *a_int, int n)
19 {
20  _InterlockedExchangeAdd((long *)a_int, n);
21 }
22 
23 static __forceinline void oro_atomic_sub(oro_atomic_t *a_int, int n)
24 {
25  oro_atomic_add(a_int, -n);
26 }
27 
28 static __forceinline int oro_atomic_sub_and_test(oro_atomic_t *a_int, int n)
29 {
30  return ((_InterlockedExchangeAdd((long *)a_int, -n) - n) == 0);
31 }
32 
33 static __forceinline void oro_atomic_inc(oro_atomic_t *a_int)
34 {
35  _InterlockedIncrement((long *)a_int);
36 }
37 
38 static __forceinline void oro_atomic_dec(oro_atomic_t *a_int)
39 {
40  _InterlockedDecrement((long *)a_int);
41 }
42 
43 static __forceinline int oro_atomic_dec_and_test(oro_atomic_t *a_int)
44 {
45  return (_InterlockedDecrement((long *)a_int) == 0);
46 }
47 
48 static __forceinline int oro_atomic_inc_and_test(oro_atomic_t *a_int)
49 {
50  return (_InterlockedIncrement((long *)a_int) == 0);
51 }
52 
53 static __forceinline int oro_atomic_add_negative(oro_atomic_t *a_int, int n)
54 {
55  return ((_InterlockedExchangeAdd((long *)a_int, n) + n) < 0);
56 }
57 
58 static __forceinline int oro_atomic_add_return(oro_atomic_t *a_int, int n)
59 {
60  return _InterlockedExchangeAdd((long *)a_int, n) + n;
61 }
62 
63 static __forceinline int oro_atomic_sub_return(oro_atomic_t *a_int, int n)
64 {
65  return oro_atomic_add_return(a_int, -n);
66 }
67 
68 static __forceinline int oro_atomic_inc_return(oro_atomic_t *a_int)
69 {
70  return _InterlockedIncrement((long *)a_int);
71 }
72 
73 static __forceinline int oro_atomic_dec_return(oro_atomic_t *a_int)
74 {
75  return _InterlockedDecrement((long *)a_int);
76 }
77 
78 static __forceinline int oro_atomic_clear_mask(oro_atomic_t *a_int, int mask)
79 {
80  return _InterlockedAnd((long *)a_int, ~mask);
81 }
82 
83 static __forceinline int oro_atomic_set_mask(oro_atomic_t *a_int, int mask)
84 {
85  return _InterlockedOr((long *)a_int, mask);
86 }
87 
88 #pragma warning(push)
89 #pragma warning(disable : 4715) // Disable warning on "specified function can potentially not return a value"
90 
91 template<typename T> inline T oro_cmpxchg(volatile void * ptr, T old, T _new)
92 {
93  switch(sizeof(T))
94  {
95  case 2:
96  return (T)(_InterlockedCompareExchange16((short *)ptr, (short)_new, (short)old));
97  case 4:
98  return (T)(_InterlockedCompareExchange((long *)ptr, (long)_new, (long)old));
99  case 8:
100  return (T)(_InterlockedCompareExchange64((__int64 *)ptr, (__int64)_new, (__int64)old));
101  }
102 }
103 
104 #pragma warning(pop)
105 
106 #endif
int oro_atomic_inc_and_test(oro_atomic_t *a)
Increment a atomically and test for zero.
void oro_atomic_inc(oro_atomic_t *a)
Increment a atomically.
volatile long oro_atomic_t
Definition: oro_arch.h:9
void oro_atomic_sub(int n, oro_atomic_t *a, int n)
Substract n from a.
int oro_atomic_dec_and_test(oro_atomic_t *a)
Decrement a atomically and test for zero.
int oro_atomic_sub_and_test(oro_atomic_t *a, int n)
Substract n from a and test for zero.
#define oro_atomic_add_negative(a, v)
Definition: oro_atomic.h:160
T oro_cmpxchg(volatile void *ptr, T old, T _new)
Definition: oro_arch.h:91
void oro_atomic_dec(oro_atomic_t *a)
Decrement a atomically.
Structure that contains an int for atomic operations.
Definition: oro_arch.h:10
void oro_atomic_add(oro_atomic_t *a, int n)
Add n to a.