/usr/include/casacore/casa/OS/Mutex.h is in casacore-dev 2.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | //# Mutex.h: Classes to handle mutexes and (un)locking
//# Copyright (C) 2011
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: aips2-request@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
//#
//# $Id$
#ifndef CASA_MUTEX_H
#define CASA_MUTEX_H
#include <casacore/casa/aips.h>
//# Mostly copied from the LOFAR software.
namespace casacore {
// <summary>Wrapper around a pthreads mutex</summary>
// <use visibility=export>
//
// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
// </reviewed>
//
// <synopsis>
// This class is a wrapper around a phtreads mutex.
// <br>Although the Mutex class has a lock function, class ScopedMutexLock
// should be used to obtain a lock, because it makes locking exception-safe.
// </synopsis>
class Mutex
{
public:
// Define the type of mutex.
// (see phtread_mutexattr_settype for their meaning).
// In Debug mode, type Auto will use PTHREAD_MUTEX_ERRORCHECK,
// otherwise PTHREAD_MUTEX_DEFAULT.
enum Type {Normal, ErrorCheck, Recursive, Default, Auto};
// Create the mutex.
Mutex (Type type=Auto);
// Destroy the mutex.
~Mutex();
// Set a lock on the mutex. It waits till it gets the lock.
void lock();
// Unlock the mutex.
void unlock();
// Try to lock the mutex. True is returned if it succeeded.
bool trylock();
private:
// Forbid copy constructor.
Mutex (const Mutex&);
// Forbid assignment.
Mutex& operator= (const Mutex&);
//# Data members
//# Use void*, because we cannot forward declare pthread_mutex_t.
void* itsMutex;
};
// <summary>Exception-safe lock/unlock of a mutex</summary>
// <use visibility=export>
//
// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
// </reviewed>
//
// <synopsis>
// The constructor of this class locks a mutex, while the destructor
// unlocks it. In this way the user does not need to take care of
// unlocking a mutex and is mutex locking fully exception-safe
// </synopsis>
class ScopedMutexLock
{
public:
// Create a lock on the mutex.
ScopedMutexLock (Mutex& mutex)
: itsMutexRef(mutex)
{ itsMutexRef.lock(); }
// The destructor automatically unlocks the mutex.
~ScopedMutexLock()
{ itsMutexRef.unlock(); }
private:
// Forbid copy constructor.
ScopedMutexLock (const ScopedMutexLock&);
// Forbid assignment.
ScopedMutexLock& operator= (const ScopedMutexLock&);
Mutex& itsMutexRef;
};
// <summary>Thread-safe initialization of global variables</summary>
// <use visibility=export>
//
// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
// </reviewed>
//
// <synopsis>
// This class does a double checked lock.
// <br>
// Often data needs to be initialized once and accessed many times. To do
// this in a thread-safe way, a mutex lock needs to be used. However, that
// is relatively expensive.
// The double checked lock idiom overcomes this problem. A Bool flag tells
// if an operation needs to be done. If so, a lock is set and the flag is
// tested again in case another thread happened to do the operation between
// the test and acquiring the lock.
// At the end of the operation the flag is cleared.
//
//
// The flag needs to be declared volatile to avoid execution ordering
// problems in case a compiler optimizes too much.
// <note role-warning>
// This idiom is not fully portable, because on more exotic machines the
// caches in different cores may not be synchronized well.
// </note>
// </synopsis>
//
// <example>
// <srcblock>
// // Declare static variables.
// static volatile Bool needInit = True;
// static Mutex mutex;
// // Execute the code in a scope, so the destructor is called automatically.
// {
// CheckedMutexLock locker(mutex, needInit);
// if (locker.doIt()) {
// .. do the initialization
// }
// }
// </srcblock>
// </example>
class MutexedInit
{
public:
// Define the initialization function to call.
typedef void InitFunc (void*);
// Create the mutex and set that the initialization should be done.
MutexedInit (InitFunc* func, void* arg=0, Mutex::Type type=Mutex::Auto);
// Execute the initialization function if not done yet.
void exec()
{ if (itsDoExec) doExec(); }
// Get the mutex (to make it possible to lock for other purposes).
Mutex& mutex()
{ return itsMutex; }
private:
// Forbid copy constructor.
MutexedInit (const MutexedInit&);
// Forbid assignment.
MutexedInit& operator= (const MutexedInit&);
// Thread-safe execution of the initialization function (if still needed).
void doExec();
//# Data members
Mutex itsMutex;
InitFunc* itsFunc;
void* itsArg;
volatile Bool itsDoExec;
};
} // namespace casacore
#endif
|