Logo ROOT   6.10/09
Reference Guide
TMCAutoLock.h
Go to the documentation of this file.
1 // @(#)root/vmc:$Id$
2 // Author: Ivana Hrivnacova, 24/03/2017
3 
4 /*************************************************************************
5  * Copyright (C) 2014, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef TMCAUTOLOCK_HH
13 #define TMCAUTOLOCK_HH
14 
15 //------------------------------------------------
16 // The Geant4 Virtual Monte Carlo package
17 // Copyright (C) 2013, 2014 Ivana Hrivnacova
18 // All rights reserved.
19 //
20 // For the licensing terms see geant4_vmc/LICENSE.
21 // Contact: root-vmc@cern.ch
22 //-------------------------------------------------
23 
24 /// \file TMCAutoLock.h
25 /// \brief Definition of the TMCTemplateAutoLock and TMCImpMutexAutoLock classes
26 ///
27 /// \author I. Hrivnacova; IPN Orsay
28 
29 //
30 // ********************************************************************
31 // * License and Disclaimer *
32 // * *
33 // * The Geant4 software is copyright of the Copyright Holders of *
34 // * the Geant4 Collaboration. It is provided under the terms and *
35 // * conditions of the Geant4 Software License, included in the file *
36 // * LICENSE and available at http://cern.ch/geant4/license . These *
37 // * include a list of copyright holders. *
38 // * *
39 // * Neither the authors of this software system, nor their employing *
40 // * institutes,nor the agencies providing financial support for this *
41 // * work make any representation or warranty, express or implied, *
42 // * regarding this software system or assume any liability for its *
43 // * use. Please see the license in the file LICENSE and URL above *
44 // * for the full disclaimer and the limitation of liability. *
45 // * *
46 // * This code implementation is the result of the scientific and *
47 // * technical work of the GEANT4 collaboration. *
48 // * By using, copying, modifying or distributing the software (or *
49 // * any work based on the software) you agree to acknowledge its *
50 // * use in resulting scientific publications, and indicate your *
51 // * acceptance of all terms of the Geant4 Software license. *
52 // ********************************************************************
53 //
54 // $Id$
55 //
56 // ---------------------------------------------------------------
57 // GEANT 4 class header file
58 //
59 // Class Description:
60 //
61 // This class provides a mechanism to create a mutex and locks/unlocks it.
62 // Can be used by applications to implement in a portable way a mutexing logic.
63 // Usage Example:
64 //
65 // #include "G4Threading.hh"
66 // #include "G4AutoLock.hh"
67 // /* somehwere */
68 // G4Mutex aMutex = G4MUTEX_INITIALIZER;
69 // /*
70 // somewhere else:
71 // The G4AutoLock instance will automatically unlock the mutex when it
72 // goes out of scope, lock and unlock method are anyway available for
73 // explicit handling of mutex lock. */
74 // G4AutoLock l(&aMutex);
75 // ProtectedCode();
76 // l.unlock(); //explicit unlock
77 // UnprotectedCode();
78 // l.lock(); //explicit lock
79 //
80 // Note that G4AutoLock is defined also for a sequential Geant4 build,
81 // but has no effect.
82 
83 // ---------------------------------------------------------------
84 // Author: Andrea Dotti (15 Feb 2013): First Implementation
85 // ---------------------------------------------------------------
86 
87 #define TMCMULTITHREADED 1
88 
89 #if defined(TMCMULTITHREADED)
90 
91 #include <pthread.h>
92 typedef pthread_mutex_t TMCMutex;
93 #define TMCMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
94 #define TMCMUTEXLOCK pthread_mutex_lock
95 #define TMCMUTEXUNLOCK pthread_mutex_unlock
96 typedef int (*thread_lock)(TMCMutex *);
97 typedef int (*thread_unlock)(TMCMutex *);
98 #else
99 typedef int TMCMutex;
100 #define TMCMUTEX_INITIALIZER 1
101 #define TMCMUTEXLOCK fake_mutex_lock_unlock
102 #define TMCMUTEXUNLOCK fake_mutex_lock_unlock
103 typedef int (*thread_lock)(TMCMutex *);
104 typedef int (*thread_unlock)(TMCMutex *);
105 #endif
106 
107 /// \brief Template classe which provides a mechanism to create a mutex and
108 /// locks/unlocks it.
109 ///
110 /// Extracted from G4AutoLock implementation for Linux
111 /// Note: Note that G4TemplateAutoLock by itself is not thread-safe and
112 /// cannot be shared among threads due to the locked switch
113 
114 template <class M, typename L, typename U>
116 public:
117  TMCTemplateAutoLock(M *mtx, L l, U u) : locked(false), _m(mtx), _l(l), _u(u) { lock(); }
118 
119  virtual ~TMCTemplateAutoLock() { unlock(); }
120 
121  inline void unlock()
122  {
123  if (!locked) return;
124  _u(_m);
125  locked = false;
126  }
127 
128  inline void lock()
129  {
130  if (locked) return;
131  _l(_m);
132  locked = true;
133  }
134 
135 private:
136  // Disable copy and assignement operators
137  //
140 
141 private:
142  bool locked;
143  M *_m;
144  L _l;
145  U _u;
146 };
147 
148 /// \brief Realization of TMCTemplateAutoLock with TMCMutex
149 ///
150 /// Extracted from G4AutoLock implementation for Linux
151 
152 struct TMCImpMutexAutoLock : public TMCTemplateAutoLock<TMCMutex, thread_lock, thread_unlock> {
155  {
156  }
157 };
159 
160 #endif // TMCAUTOLOCK_HH
Template classe which provides a mechanism to create a mutex and locks/unlocks it.
Definition: TMCAutoLock.h:115
RooArgList L(const RooAbsArg &v1)
TMCImpMutexAutoLock TMCAutoLock
Definition: TMCAutoLock.h:158
#define TMCMUTEXLOCK
Definition: TMCAutoLock.h:94
virtual ~TMCTemplateAutoLock()
Definition: TMCAutoLock.h:119
TLine * l
Definition: textangle.C:4
#define TMCMUTEXUNLOCK
Definition: TMCAutoLock.h:95
pthread_mutex_t TMCMutex
Definition: TMCAutoLock.h:92
TMCImpMutexAutoLock(TMCMutex *mtx)
Definition: TMCAutoLock.h:153
int(* thread_lock)(TMCMutex *)
Definition: TMCAutoLock.h:96
int(* thread_unlock)(TMCMutex *)
Definition: TMCAutoLock.h:97
Realization of TMCTemplateAutoLock with TMCMutex.
Definition: TMCAutoLock.h:152
TMCTemplateAutoLock & operator=(const TMCTemplateAutoLock &rhs)
TMCTemplateAutoLock(M *mtx, L l, U u)
Definition: TMCAutoLock.h:117