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, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 #ifndef NDB_SEQLOCK_HPP
24 #define NDB_SEQLOCK_HPP
25 
26 #include <ndb_types.h>
27 #include "mt-asm.h"
28 
29 #if defined (NDB_HAVE_RMB) && defined(NDB_HAVE_WMB)
30 struct NdbSeqLock
31 {
NdbSeqLockNdbSeqLock32   NdbSeqLock() { m_seq = 0;}
33   volatile Uint32 m_seq;
34 
35   void write_lock();
36   void write_unlock();
37 
38   Uint32 read_lock();
39   bool read_unlock(Uint32 val) const;
40 };
41 
42 inline
43 void
write_lock()44 NdbSeqLock::write_lock()
45 {
46   assert((m_seq & 1) == 0);
47   m_seq++;
48   wmb();
49 }
50 
51 inline
52 void
write_unlock()53 NdbSeqLock::write_unlock()
54 {
55   assert((m_seq & 1) == 1);
56   wmb();
57   m_seq++;
58 }
59 
60 inline
61 Uint32
read_lock()62 NdbSeqLock::read_lock()
63 {
64 loop:
65   Uint32 val = m_seq;
66   rmb();
67   if (unlikely(val & 1))
68   {
69 #ifdef NDB_HAVE_CPU_PAUSE
70     cpu_pause();
71 #endif
72     goto loop;
73   }
74   return val;
75 }
76 
77 inline
78 bool
read_unlock(Uint32 val) const79 NdbSeqLock::read_unlock(Uint32 val) const
80 {
81   rmb();
82   return val == m_seq;
83 }
84 #else /** ! rmb() or wmb() */
85 /**
86  * Only for ndbd...
87  */
88 
89 struct NdbSeqLock
90 {
NdbSeqLockNdbSeqLock91   NdbSeqLock() { }
92 
write_lockNdbSeqLock93   void write_lock() {}
write_unlockNdbSeqLock94   void write_unlock() {}
95 
read_lockNdbSeqLock96   Uint32 read_lock() { return 0; }
read_unlockNdbSeqLock97   bool read_unlock(Uint32 val) const { return true;}
98 };
99 
100 #endif
101 
102 #endif
103