CPN
Computational Process Networks
AutoLock.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
25 #ifndef AUTOLOCK_H
26 #define AUTOLOCK_H
27 #pragma once
28 #include <cpn/common.h>
29 
34 template<class Lockable>
35 class AutoLock {
36 public:
41  AutoLock(Lockable& mutex_) : mutex(mutex_), count(0) {
42  Lock();
43  }
49  AutoLock(Lockable& mutex_, bool lock) : mutex(mutex_), count(0) {
50  if (lock) { Lock(); }
51  }
52 
54  while (count > 0)
55  Unlock();
56  }
57 
61  void Unlock() {
62  --count;
63  mutex.Unlock();
64  }
65 
69  void Lock() {
70  mutex.Lock();
71  ++count;
72  }
73 
74 private:
75  Lockable& mutex;
76  int count;
77 };
78 #endif
AutoLock(Lockable &mutex_)
Definition: AutoLock.h:41
void Unlock()
Definition: AutoLock.h:61
~AutoLock()
Definition: AutoLock.h:53
void Lock()
Definition: AutoLock.h:69
static PthreadMutex lock
Definition: NodeLoader.cc:36
int count
Definition: AutoLock.h:76
AutoLock(Lockable &mutex_, bool lock)
Definition: AutoLock.h:49
Lockable & mutex
Definition: AutoLock.h:75