SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Guard.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 
31 ///
32 ///@file Guard.h
33 ///@brief Automatically lock/unlock a mutex or crowdmonitor.
34 ///
35 ///@author Steve Parker
36 /// Department of Computer Science
37 /// University of Utah
38 ///@date June 1997
39 ///
40 
41 #ifndef Core_Thread_Guard_h
42 #define Core_Thread_Guard_h
43 
45 
46 namespace SCIRun {
47 
48 class Mutex;
49 class RecursiveMutex;
50 class CrowdMonitor;
51 
52 /**************************************
53 
54 @class
55  Guard
56 
57  KEYWORDS
58  Thread
59 
60 @details
61  Utility class to lock and unlock a <b>Mutex</b> or a <b>CrowdMonitor</b>.
62  The constructor of the <b>Guard</b> object will lock the mutex
63  (or <b>CrowdMonitor</b>), and the destructor will unlock it.
64  <p>
65  This would be used like this:
66  <blockquote><pre>
67  {
68  <blockquote>Guard mlock(&mutex); // Acquire the mutex
69  ... critical section ...</blockquote>
70  } // mutex is released when mlock goes out of scope
71  </pre></blockquote>
72 
73 ****************************************/
74 class SCISHARE Guard {
75 public:
76  //////////
77  /// Attach the <b>Guard</b> object to the <i>mutex</i>, and
78  /// acquire the mutex.
79  Guard(Mutex* mutex);
80 
81  Guard(RecursiveMutex* rmutex);
82 
83  enum Which {
85  Write
86  };
87 
88  //////////
89  /// Attach the <b>Guard</b> to the <i>CrowdMonitor</pre> and
90  /// acquire one of the locks. If <i>action</i> is
91  /// <b>Guard::Read</b>, the read lock will be acquired, and if
92  /// <i>action</i> is <b>Write</b>, then the write lock will be
93  /// acquired. The appropriate lock will then be released by the
94  /// destructor
95  Guard(CrowdMonitor* crowdMonitor, Which action);
96 
97  //////////
98  /// Release the lock acquired by the constructor.
99  ~Guard();
100 private:
101  Mutex* mutex_;
102  RecursiveMutex* rmutex_;
103  CrowdMonitor* monitor_;
104  Which action_;
105 
106  // Cannot copy them
107  Guard(const Guard&);
108  Guard& operator=(const Guard&);
109 };
110 } // End namespace SCIRun
111 
112 #endif
113 
Definition: RecursiveMutex.h:68
Multiple reader/single writer locks, implementation for pthreads simply wrapping pthread_rwlock_t.
#define SCISHARE
Definition: share.h:39
Definition: Mutex.h:65
Definition: CrowdMonitor.h:78
Definition: Guard.h:74
Definition: Guard.h:84
boost::lock_guard< boost::mutex > Guard
Definition: Mutex.h:55
boost::mutex Mutex
Definition: SchedulingWithBoostGraph.cc:434
Which
Definition: Guard.h:83