Subordination
A framework for distributed programming
static_lock.hh
1 #ifndef SUBORDINATION_BASE_STATIC_LOCK_HH
2 #define SUBORDINATION_BASE_STATIC_LOCK_HH
3 
4 namespace sbn {
5 
6  template <class M1, class M2=M1>
7  class static_lock {
8 
9  private:
10  M1* _m1;
11  M2* _m2;
12 
13  public:
14 
15  inline
16  static_lock(M1* a, M2* b) {
17  if (a < b || !b) {
18  this->_m1 = a;
19  this->_m2 = b;
20  } else {
21  this->_m1 = b;
22  this->_m2 = a;
23  }
24  this->lock();
25  }
26 
27  inline
28  ~static_lock() {
29  this->unlock();
30  }
31 
32  inline void
33  lock() {
34  this->_m1->lock();
35  if (this->_m2) {
36  this->_m2->lock();
37  }
38  }
39 
40  inline void
41  unlock() {
42  if (this->_m2) {
43  this->_m2->unlock();
44  }
45  this->_m1->unlock();
46  }
47 
48  };
49 
50 }
51 
52 #endif // vim:filetype=cpp
Definition: static_lock.hh:7