summaryrefslogtreecommitdiff
path: root/storage/ndb/src/kernel/vm/Mutex.hpp
blob: 1a39ef10996e3d355217441014490c513542cbcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* Copyright (c) 2003-2005 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */

#ifndef BLOCK_MUTEX_HPP
#define BLOCK_MUTEX_HPP

#include "Callback.hpp"
#include "SimulatedBlock.hpp"

class Mutex;

/**
 * MutexHandle - A "reference" to a mutex
 *             - Should be used together with Mutex
 */
class MutexHandle {
  friend class Mutex;
public:
  MutexHandle(Uint32 id);
  
  bool isNull() const;
  void release(SimulatedBlock::MutexManager & mgr);

private:
  const Uint32 m_mutexId;
  Uint32 m_activeMutexPtrI;
};

/**
 * MutexHandle2 - A template-based "reference" to a mutex
 */
template<Uint32 MutexId>
class MutexHandle2 {
  friend class Mutex;
public:
  MutexHandle2();
  
  bool isNull() const;
  void release(SimulatedBlock::MutexManager & mgr);

private:
  Uint32 m_activeMutexPtrI;
};

/**
 * A mutex - Used together with a MutexHandle to be put on the stack
 */
class Mutex {
public:
  Mutex(Signal*, SimulatedBlock::MutexManager & mgr, MutexHandle &);
  
  template<Uint32 MutexId>
  Mutex(Signal*, SimulatedBlock::MutexManager & mgr, MutexHandle2<MutexId> &);
  
  ~Mutex();

  void release();
  bool isNull() const ;
  
  bool lock(SimulatedBlock::Callback & callback);
  bool trylock(SimulatedBlock::Callback & callback);
  void unlock(SimulatedBlock::Callback & callback);
  void unlock(); // Ignore callback
  
  bool create(SimulatedBlock::Callback & callback);
  bool destroy(SimulatedBlock::Callback & callback);

private:
  Signal* m_signal;
  SimulatedBlock::MutexManager & m_mgr;
  const Uint32 m_mutexId;
  Uint32 & m_srcPtrI;
  SimulatedBlock::MutexManager::ActiveMutexPtr m_ptr;
  
public:
  static void release(SimulatedBlock::MutexManager&, 
		      Uint32 activePtrI, Uint32 mutexId);
};

inline
MutexHandle::MutexHandle(Uint32 id) : m_mutexId(id) { 
  m_activeMutexPtrI = RNIL;
}

inline
bool
MutexHandle::isNull() const {
  return m_activeMutexPtrI == RNIL;
}

inline
void 
MutexHandle::release(SimulatedBlock::MutexManager & mgr){
  if(!isNull()){
    Mutex::release(mgr, m_activeMutexPtrI, m_mutexId);
    m_activeMutexPtrI = RNIL;
  }
}

template<Uint32 MutexId>
inline
MutexHandle2<MutexId>::MutexHandle2() { 
  m_activeMutexPtrI = RNIL;
}
  
template<Uint32 MutexId>
inline
bool 
MutexHandle2<MutexId>::isNull() const {
  return m_activeMutexPtrI == RNIL;
}


template<Uint32 MutexId>
inline
void 
MutexHandle2<MutexId>::release(SimulatedBlock::MutexManager & mgr){
  if(!isNull()){
    Mutex::release(mgr, m_activeMutexPtrI, MutexId);
    m_activeMutexPtrI = RNIL;
  }
}


inline
Mutex::Mutex(Signal* signal, SimulatedBlock::MutexManager & mgr, 
	     MutexHandle & mh)
  : m_signal(signal),
    m_mgr(mgr),
    m_mutexId(mh.m_mutexId),
    m_srcPtrI(mh.m_activeMutexPtrI){

  m_ptr.i = m_srcPtrI;

}

template<Uint32 MutexId>
inline
Mutex::Mutex(Signal* signal, SimulatedBlock::MutexManager & mgr, 
	     MutexHandle2<MutexId> & mh)
  : m_signal(signal),
    m_mgr(mgr),
    m_mutexId(MutexId),
    m_srcPtrI(mh.m_activeMutexPtrI){
  
  m_ptr.i = m_srcPtrI;

}

inline
Mutex::~Mutex(){
  m_srcPtrI = m_ptr.i;
}

inline
void
Mutex::release(){
  if(!m_ptr.isNull()){
    Mutex::release(m_mgr, m_ptr.i, m_mutexId);
    m_ptr.setNull();
  }
}

inline
bool
Mutex::isNull() const {
  return m_ptr.isNull();
}

inline
bool
Mutex::lock(SimulatedBlock::Callback & callback){
  if(m_ptr.isNull()){
    if(m_mgr.seize(m_ptr)){
      m_ptr.p->m_mutexId = m_mutexId;
      m_ptr.p->m_callback = callback;
      m_mgr.lock(m_signal, m_ptr);
      return true;
    }
    return false;
  }
  ErrorReporter::handleAssert("Mutex::lock mutex alreay inuse", 
			      __FILE__, __LINE__);
  return false;
}

inline
bool
Mutex::trylock(SimulatedBlock::Callback & callback){
  if(m_ptr.isNull()){
    if(m_mgr.seize(m_ptr)){
      m_ptr.p->m_mutexId = m_mutexId;
      m_ptr.p->m_callback = callback;
      m_mgr.lock(m_signal, m_ptr);
      return true;
    }
    return false;
  }
  ErrorReporter::handleAssert("Mutex::trylock mutex alreay inuse", 
			      __FILE__, __LINE__);
  return false;
}

inline
void
Mutex::unlock(SimulatedBlock::Callback & callback){
  if(!m_ptr.isNull()){
    m_mgr.getPtr(m_ptr);
    if(m_ptr.p->m_mutexId == m_mutexId){
      m_ptr.p->m_callback = callback;
      m_mgr.unlock(m_signal, m_ptr);
      return;
    }
  }
  ErrorReporter::handleAssert("Mutex::unlock invalid mutex", 
			      __FILE__, __LINE__);
}

inline
bool
Mutex::create(SimulatedBlock::Callback & callback){
  if(m_ptr.isNull()){
    if(m_mgr.seize(m_ptr)){
      m_ptr.p->m_mutexId = m_mutexId;
      m_ptr.p->m_callback = callback;
      m_mgr.create(m_signal, m_ptr);
      return true;
    }
    return false;
  }
  ErrorReporter::handleAssert("Mutex::create mutex alreay inuse", 
			      __FILE__, __LINE__);
  return false;
}

inline
bool
Mutex::destroy(SimulatedBlock::Callback & callback){
  if(m_ptr.isNull()){
    if(m_mgr.seize(m_ptr)){
      m_ptr.p->m_mutexId = m_mutexId;
      m_ptr.p->m_callback = callback;
      m_mgr.destroy(m_signal, m_ptr);
      return true;
    }
    return false;
  }
  ErrorReporter::handleAssert("Mutex::destroy mutex alreay inuse", 
			      __FILE__, __LINE__);
  return false;
}


#endif