MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NdbSeqLock.hpp
1 /* Copyright (c) 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
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 #ifndef NDB_SEQLOCK_HPP
17 #define NDB_SEQLOCK_HPP
18 
19 #include <ndb_types.h>
20 #include "mt-asm.h"
21 
22 #if defined (NDB_HAVE_RMB) && defined(NDB_HAVE_WMB)
23 struct NdbSeqLock
24 {
25  NdbSeqLock() { m_seq = 0;}
26  volatile Uint32 m_seq;
27 
28  void write_lock();
29  void write_unlock();
30 
31  Uint32 read_lock();
32  bool read_unlock(Uint32 val) const;
33 };
34 
35 inline
36 void
37 NdbSeqLock::write_lock()
38 {
39  assert((m_seq & 1) == 0);
40  m_seq++;
41  wmb();
42 }
43 
44 inline
45 void
46 NdbSeqLock::write_unlock()
47 {
48  assert((m_seq & 1) == 1);
49  wmb();
50  m_seq++;
51 }
52 
53 inline
54 Uint32
55 NdbSeqLock::read_lock()
56 {
57 loop:
58  Uint32 val = m_seq;
59  rmb();
60  if (unlikely(val & 1))
61  {
62 #ifdef NDB_HAVE_CPU_PAUSE
63  cpu_pause();
64 #endif
65  goto loop;
66  }
67  return val;
68 }
69 
70 inline
71 bool
72 NdbSeqLock::read_unlock(Uint32 val) const
73 {
74  rmb();
75  return val == m_seq;
76 }
77 #else
82 struct NdbSeqLock
83 {
84  NdbSeqLock() { }
85 
86  void write_lock() {}
87  void write_unlock() {}
88 
89  Uint32 read_lock() { return 0; }
90  bool read_unlock(Uint32 val) const { return true;}
91 };
92 
93 #endif
94 
95 #endif