Geant4  10.02.p02
G4AutoLock.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id$
27 //
28 // ---------------------------------------------------------------
29 // GEANT 4 class header file
30 //
31 // Class Description:
32 //
33 // This class provides a mechanism to create a mutex and locks/unlocks it.
34 // Can be used by applications to implement in a portable way a mutexing logic.
35 // Usage Example:
36 //
37 // #include "G4Threading.hh"
38 // #include "G4AutoLock.hh"
39 // /* somehwere */
40 // G4Mutex aMutex = G4MUTEX_INITIALIZER;
41 // /*
42 // somewhere else:
43 // The G4AutoLock instance will automatically unlock the mutex when it
44 // goes out of scope, lock and unlock method are anyway available for
45 // explicit handling of mutex lock. */
46 // G4AutoLock l(&aMutex);
47 // ProtectedCode();
48 // l.unlock(); //explicit unlock
49 // UnprotectedCode();
50 // l.lock(); //explicit lock
51 //
52 // Note that G4AutoLock is defined also for a sequential Geant4 build,
53 // but has no effect.
54 
55 // ---------------------------------------------------------------
56 // Author: Andrea Dotti (15 Feb 2013): First Implementation
57 // ---------------------------------------------------------------
58 
59 #ifndef G4AUTOLOCK_HH
60 #define G4AUTOLOCK_HH
61 
62 #include "G4Threading.hh"
63 
64 // Note: Note that G4TemplateAutoLock by itself is not thread-safe and
65 // cannot be shared among threads due to the locked switch
66 //
67 template<class M, typename L, typename U>
69 {
70  public:
71 
72  G4TemplateAutoLock(M* mtx, L l, U u) : locked(false), _m(mtx), _l(l), _u(u)
73  {
74  lock();
75  }
76 
78  {
79  unlock();
80  }
81 
82  inline void unlock() {
83  if ( !locked ) return;
84  _u(_m);
85  locked = false;
86  }
87 
88  inline void lock() {
89  if ( locked ) return;
90  _l(_m);
91  locked = true;
92  }
93 
94  private:
95 
96  // Disable copy and assignement operators
97  //
100 
101  private:
103  M* _m;
104  L _l;
105  U _u;
106 };
107 
109  : public G4TemplateAutoLock<G4Mutex,thread_lock,thread_unlock>
110 {
113  (mtx, &G4MUTEXLOCK, &G4MUTEXUNLOCK) {}
114 };
116 
117 #endif //G4AUTOLOCK_HH
#define G4MUTEXUNLOCK
Definition: G4Threading.hh:180
G4int(* thread_lock)(G4Mutex *)
Definition: G4Threading.hh:186
virtual ~G4TemplateAutoLock()
Definition: G4AutoLock.hh:77
G4ImpMutexAutoLock(G4Mutex *mtx)
Definition: G4AutoLock.hh:111
static const double L
Definition: G4SIunits.hh:123
bool G4bool
Definition: G4Types.hh:79
#define G4MUTEXLOCK
Definition: G4Threading.hh:179
G4int(* thread_unlock)(G4Mutex *)
Definition: G4Threading.hh:187
G4TemplateAutoLock(M *mtx, L l, U u)
Definition: G4AutoLock.hh:72
G4int G4Mutex
Definition: G4Threading.hh:173
G4ImpMutexAutoLock G4AutoLock
Definition: G4AutoLock.hh:115
G4TemplateAutoLock & operator=(const G4TemplateAutoLock &rhs)