HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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_API.h"
15 #include "SYS_Types.h"
16 #include "SYS_AtomicInt.h"
17 
18 #include <cstddef>
19 
20 template <typename T>
22 {
23 private:
24 public:
25  typedef T value_type;
26  typedef T * pointer;
27 
28 
29  explicit SYS_AtomicPtr(T *value = 0)
30  : myValue(reinterpret_cast<ptrdiff_t>(value))
31  {
32  }
33 
34  /// Atomically assigns val to myValue, returning the prior value of
35  /// myValue.
36  inline T *exchange(T *val);
37 
38  /// Atomically compares (myValue == expected), and if true, replaces
39  /// myValue with desired. Returns prior myValue.
40  inline T *compare_swap(T *expected, T *desired);
41 
42  /// Atomically compares (ptr_ref == expected), and if true, replaces
43  /// ptr_ref with desired and returns true. Else, returns false.
44  static bool compare_exchange_strong(
45  T * volatile * ptr_addr,
46  T *expected, T *desired);
47 
48  /// Atomically obtain myValue
49  operator T *() const;
50 
51  /// Uses T *() for atomic indirection
52  T * operator->() const { return (T *)*this; }
53 
54  inline T *relaxedLoad() const
55  {
56  return reinterpret_cast<T *>(myValue.relaxedLoad());
57  }
58  inline void relaxedStore(T *val)
59  {
60  myValue.relaxedStore(reinterpret_cast<ptrdiff_t>(val));
61  }
62 
63 private:
65 
66 private:
67  // Prohibited operators
69  SYS_AtomicPtr &operator=(const SYS_AtomicPtr &);
70 };
71 
72 /// Atomically assigns val to myValue, returning the prior value of
73 /// myValue.
74 template <typename T>
75 T *
77 {
78  return reinterpret_cast<T *>(
79  myValue.exchange(reinterpret_cast<ptrdiff_t>(val)));
80 }
81 
82 template <typename T>
83 T *
84 SYS_AtomicPtr<T>::compare_swap(T *expected, T *desired)
85 {
86  return reinterpret_cast<T *>(
87  myValue.compare_swap(
88  reinterpret_cast<ptrdiff_t>(expected),
89  reinterpret_cast<ptrdiff_t>(desired)));
90 }
91 
92 template <typename T>
93 bool
95  T * volatile * ptr_addr, T *expected, T *desired)
96 {
97  T *old = reinterpret_cast<T *>(
99  reinterpret_cast<volatile ptrdiff_t *>(ptr_addr),
100  reinterpret_cast<ptrdiff_t>(expected),
101  reinterpret_cast<ptrdiff_t>(desired)));
102 
103  return (old == expected);
104 }
105 
106 template <typename T>
108 {
109  return reinterpret_cast<T *>(myValue.load());
110 }
111 
112 
113 #endif
T compare_and_swap(volatile T *addr, T oldval, T newval)
T * relaxedLoad() const
Definition: SYS_AtomicPtr.h:54
T * exchange(T *val)
Definition: SYS_AtomicPtr.h:76
T * compare_swap(T *expected, T *desired)
Definition: SYS_AtomicPtr.h:84
GLsizei const GLfloat * value
Definition: glcorearb.h:823
T * operator->() const
Uses T *() for atomic indirection.
Definition: SYS_AtomicPtr.h:52
static bool compare_exchange_strong(T *volatile *ptr_addr, T *expected, T *desired)
Definition: SYS_AtomicPtr.h:94
GLuint GLfloat * val
Definition: glcorearb.h:1607
SYS_AtomicPtr(T *value=0)
Definition: SYS_AtomicPtr.h:29
SYS_FORCE_INLINE void relaxedStore(T val)
Definition: SYS_AtomicInt.h:68
SYS_FORCE_INLINE T relaxedLoad() const
Definition: SYS_AtomicInt.h:61
void relaxedStore(T *val)
Definition: SYS_AtomicPtr.h:58