Logo ROOT   6.16/01
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#ifndef _WIN32
88#define TMCMULTITHREADED 1
89#endif
90
91#if defined(TMCMULTITHREADED)
92
93#include <pthread.h>
94typedef pthread_mutex_t TMCMutex;
95#define TMCMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
96#define TMCMUTEXLOCK pthread_mutex_lock
97#define TMCMUTEXUNLOCK pthread_mutex_unlock
98typedef int (*TMCthread_lock)(TMCMutex *);
99typedef int (*TMCthread_unlock)(TMCMutex *);
100#else
101typedef int TMCMutex;
103#define TMCMUTEX_INITIALIZER 1
104#define TMCMUTEXLOCK fake_mutex_lock_unlock
105#define TMCMUTEXUNLOCK fake_mutex_lock_unlock
106typedef int (*TMCthread_lock)(TMCMutex *);
107typedef int (*TMCthread_unlock)(TMCMutex *);
108#endif
109
110/// \brief Template classe which provides a mechanism to create a mutex and
111/// locks/unlocks it.
112///
113/// Extracted from G4AutoLock implementation for Linux
114/// Note: Note that G4TemplateAutoLock by itself is not thread-safe and
115/// cannot be shared among threads due to the locked switch
116
117template <class M, typename L, typename U>
119public:
120 TMCTemplateAutoLock(M *mtx, L l, U u) : locked(false), _m(mtx), _l(l), _u(u) { lock(); }
121
123
124 inline void unlock()
125 {
126 if (!locked) return;
127 _u(_m);
128 locked = false;
129 }
130
131 inline void lock()
132 {
133 if (locked) return;
134 _l(_m);
135 locked = true;
136 }
137
138private:
139 // Disable copy and assignement operators
140 //
143
144private:
145 bool locked;
146 M *_m;
148 U _u;
149};
150
151/// \brief Realization of TMCTemplateAutoLock with TMCMutex
152///
153/// Extracted from G4AutoLock implementation for Linux
154
155struct TMCImpMutexAutoLock : public TMCTemplateAutoLock<TMCMutex, TMCthread_lock, TMCthread_unlock> {
158 {
159 }
160};
162
163#endif // TMCAUTOLOCK_HH
int fake_mutex_lock_unlock(TMCMutex *)
Definition: TMCAutoLock.cxx:56
#define TMCMUTEXUNLOCK
Definition: TMCAutoLock.h:97
int(* TMCthread_lock)(TMCMutex *)
Definition: TMCAutoLock.h:98
int(* TMCthread_unlock)(TMCMutex *)
Definition: TMCAutoLock.h:99
TMCImpMutexAutoLock TMCAutoLock
Definition: TMCAutoLock.h:161
#define TMCMUTEXLOCK
Definition: TMCAutoLock.h:96
pthread_mutex_t TMCMutex
Definition: TMCAutoLock.h:94
Template classe which provides a mechanism to create a mutex and locks/unlocks it.
Definition: TMCAutoLock.h:118
TMCTemplateAutoLock(const TMCTemplateAutoLock &rhs)
virtual ~TMCTemplateAutoLock()
Definition: TMCAutoLock.h:122
TMCTemplateAutoLock & operator=(const TMCTemplateAutoLock &rhs)
TMCTemplateAutoLock(M *mtx, L l, U u)
Definition: TMCAutoLock.h:120
static constexpr double L
Realization of TMCTemplateAutoLock with TMCMutex.
Definition: TMCAutoLock.h:155
TMCImpMutexAutoLock(TMCMutex *mtx)
Definition: TMCAutoLock.h:156
auto * l
Definition: textangle.C:4