MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pfs_atomic.h
Go to the documentation of this file.
1 /* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 #ifndef PFS_ATOMIC_H
17 #define PFS_ATOMIC_H
18 
24 #include <my_atomic.h>
25 
28 {
29 public:
31  static void init();
33  static void cleanup();
34 
36  static inline int32 load_32(volatile int32 *ptr)
37  {
38  int32 result;
39  rdlock(ptr);
40  result= my_atomic_load32(ptr);
41  rdunlock(ptr);
42  return result;
43  }
44 
46  static inline int64 load_64(volatile int64 *ptr)
47  {
48  int64 result;
49  rdlock(ptr);
50  result= my_atomic_load64(ptr);
51  rdunlock(ptr);
52  return result;
53  }
54 
56  static inline uint32 load_u32(volatile uint32 *ptr)
57  {
58  uint32 result;
59  rdlock(ptr);
60  result= (uint32) my_atomic_load32((int32*) ptr);
61  rdunlock(ptr);
62  return result;
63  }
64 
66  static inline uint64 load_u64(volatile uint64 *ptr)
67  {
68  uint64 result;
69  rdlock(ptr);
70  result= (uint64) my_atomic_load64((int64*) ptr);
71  rdunlock(ptr);
72  return result;
73  }
74 
76  static inline void store_32(volatile int32 *ptr, int32 value)
77  {
78  wrlock(ptr);
79  my_atomic_store32(ptr, value);
80  wrunlock(ptr);
81  }
82 
84  static inline void store_64(volatile int64 *ptr, int64 value)
85  {
86  wrlock(ptr);
87  my_atomic_store64(ptr, value);
88  wrunlock(ptr);
89  }
90 
92  static inline void store_u32(volatile uint32 *ptr, uint32 value)
93  {
94  wrlock(ptr);
95  my_atomic_store32((int32*) ptr, (int32) value);
96  wrunlock(ptr);
97  }
98 
100  static inline void store_u64(volatile uint64 *ptr, uint64 value)
101  {
102  wrlock(ptr);
103  my_atomic_store64((int64*) ptr, (int64) value);
104  wrunlock(ptr);
105  }
106 
108  static inline int32 add_32(volatile int32 *ptr, int32 value)
109  {
110  int32 result;
111  wrlock(ptr);
112  result= my_atomic_add32(ptr, value);
113  wrunlock(ptr);
114  return result;
115  }
116 
118  static inline int64 add_64(volatile int64 *ptr, int64 value)
119  {
120  int64 result;
121  wrlock(ptr);
122  result= my_atomic_add64(ptr, value);
123  wrunlock(ptr);
124  return result;
125  }
126 
128  static inline uint32 add_u32(volatile uint32 *ptr, uint32 value)
129  {
130  uint32 result;
131  wrlock(ptr);
132  result= (uint32) my_atomic_add32((int32*) ptr, (int32) value);
133  wrunlock(ptr);
134  return result;
135  }
136 
138  static inline uint64 add_u64(volatile uint64 *ptr, uint64 value)
139  {
140  uint64 result;
141  wrlock(ptr);
142  result= (uint64) my_atomic_add64((int64*) ptr, (int64) value);
143  wrunlock(ptr);
144  return result;
145  }
146 
148  static inline bool cas_32(volatile int32 *ptr, int32 *old_value,
149  int32 new_value)
150  {
151  bool result;
152  wrlock(ptr);
153  result= my_atomic_cas32(ptr, old_value, new_value);
154  wrunlock(ptr);
155  return result;
156  }
157 
159  static inline bool cas_64(volatile int64 *ptr, int64 *old_value,
160  int64 new_value)
161  {
162  bool result;
163  wrlock(ptr);
164  result= my_atomic_cas64(ptr, old_value, new_value);
165  wrunlock(ptr);
166  return result;
167  }
168 
170  static inline bool cas_u32(volatile uint32 *ptr, uint32 *old_value,
171  uint32 new_value)
172  {
173  bool result;
174  wrlock(ptr);
175  result= my_atomic_cas32((int32*) ptr, (int32*) old_value,
176  (uint32) new_value);
177  wrunlock(ptr);
178  return result;
179  }
180 
182  static inline bool cas_u64(volatile uint64 *ptr, uint64 *old_value,
183  uint64 new_value)
184  {
185  bool result;
186  wrlock(ptr);
187  result= my_atomic_cas64((int64*) ptr, (int64*) old_value,
188  (uint64) new_value);
189  wrunlock(ptr);
190  return result;
191  }
192 
193 private:
194  static my_atomic_rwlock_t m_rwlock_array[256];
195 
200  static inline my_atomic_rwlock_t *get_rwlock(volatile void *ptr)
201  {
202  /*
203  Divide an address by 8 to remove alignment,
204  modulo 256 to fall in the array.
205  */
206  uint index= (((intptr) ptr) >> 3) & 0xFF;
207  my_atomic_rwlock_t *result= &m_rwlock_array[index];
208  return result;
209  }
210 
215  static inline void rdlock(volatile void *ptr)
216  {
217  my_atomic_rwlock_rdlock(get_rwlock(ptr));
218  }
219 
224  static inline void wrlock(volatile void *ptr)
225  {
226  my_atomic_rwlock_wrlock(get_rwlock(ptr));
227  }
228 
233  static inline void rdunlock(volatile void *ptr)
234  {
235  my_atomic_rwlock_rdunlock(get_rwlock(ptr));
236  }
237 
242  static inline void wrunlock(volatile void *ptr)
243  {
244  my_atomic_rwlock_wrunlock(get_rwlock(ptr));
245  }
246 };
247 
248 #endif
249