HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SYS_AtomicPtr.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: SYS_AtomicPtr.h ( UT Library, C++)
7  *
8  * COMMENTS: Thread-safe operations on pointer types.
9  */
10 
11 #ifndef __SYS_AtomicPtr__
12 #define __SYS_AtomicPtr__
13 
14 #include "SYS_Types.h"
15 #include "SYS_AtomicInt.h"
16 
17 #include <cstddef>
18 
19 template <typename T>
21 {
22 private:
23 public:
24  typedef T value_type;
25  typedef T * pointer;
26 
27 
28  explicit SYS_AtomicPtr(T *value = 0)
29  : myValue(reinterpret_cast<ptrdiff_t>(value))
30  {
31  }
32 
33  /// Atomically assigns val to myValue, returning the prior value of
34  /// myValue.
35  inline T *exchange(T *val);
36 
37  /// Atomically compares (myValue == expected), and if true, replaces
38  /// myValue with desired. Returns prior myValue.
39  inline T *compare_swap(T *expected, T *desired);
40 
41  /// Atomically compares (ptr_ref == expected), and if true, replaces
42  /// ptr_ref with desired and returns true. Else, returns false.
43  static bool compare_exchange_strong(
44  T * volatile * ptr_addr,
45  T *expected, T *desired);
46 
47  /// Atomically obtain myValue
48  operator T *() const;
49 
50  /// Uses T *() for atomic indirection
51  T * operator->() const { return (T *)*this; }
52 
53  inline T *relaxedLoad() const
54  {
55  return reinterpret_cast<T *>(myValue.relaxedLoad());
56  }
57  inline void relaxedStore(T *val)
58  {
59  myValue.relaxedStore(reinterpret_cast<ptrdiff_t>(val));
60  }
61 
62 private:
64 
65 private:
66  // Prohibited operators
68  SYS_AtomicPtr &operator=(const SYS_AtomicPtr &);
69 };
70 
71 /// Atomically assigns val to myValue, returning the prior value of
72 /// myValue.
73 template <typename T>
74 T *
76 {
77  return reinterpret_cast<T *>(
78  myValue.exchange(reinterpret_cast<ptrdiff_t>(val)));
79 }
80 
81 template <typename T>
82 T *
83 SYS_AtomicPtr<T>::compare_swap(T *expected, T *desired)
84 {
85  return reinterpret_cast<T *>(
86  myValue.compare_swap(
87  reinterpret_cast<ptrdiff_t>(expected),
88  reinterpret_cast<ptrdiff_t>(desired)));
89 }
90 
91 template <typename T>
92 bool
94  T * volatile * ptr_addr, T *expected, T *desired)
95 {
96  T *old = reinterpret_cast<T *>(
98  reinterpret_cast<volatile ptrdiff_t *>(ptr_addr),
99  reinterpret_cast<ptrdiff_t>(expected),
100  reinterpret_cast<ptrdiff_t>(desired)));
101 
102  return (old == expected);
103 }
104 
105 template <typename T>
107 {
108  return reinterpret_cast<T *>(myValue.load());
109 }
110 
111 
112 #endif
T compare_and_swap(volatile T *addr, T oldval, T newval)
T * relaxedLoad() const
Definition: SYS_AtomicPtr.h:53
T * exchange(T *val)
Definition: SYS_AtomicPtr.h:75
T * compare_swap(T *expected, T *desired)
Definition: SYS_AtomicPtr.h:83
GLsizei const GLfloat * value
Definition: glcorearb.h:823
T * operator->() const
Uses T *() for atomic indirection.
Definition: SYS_AtomicPtr.h:51
static bool compare_exchange_strong(T *volatile *ptr_addr, T *expected, T *desired)
Definition: SYS_AtomicPtr.h:93
GLuint GLfloat * val
Definition: glcorearb.h:1607
SYS_AtomicPtr(T *value=0)
Definition: SYS_AtomicPtr.h:28
SYS_FORCE_INLINE void relaxedStore(T val)
Definition: SYS_AtomicInt.h:67
SYS_FORCE_INLINE T relaxedLoad() const
Definition: SYS_AtomicInt.h:60
void relaxedStore(T *val)
Definition: SYS_AtomicPtr.h:57