Main Page
Namespaces
Classes
Package Documentation
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Pages
src
Utilities
General
interface
MutexUtils.h
Go to the documentation of this file.
1
#ifndef UTILITIES_GENERAL_MUTEXUTILS_H
2
#define UTILITIES_GENERAL_MUTEXUTILS_H
3
//
4
// thread Utilities...
5
//
6
#include <boost/thread/recursive_mutex.hpp>
7
#include <boost/thread/mutex.hpp>
8
#include <boost/thread/xtime.hpp>
9
#include <boost/thread/condition.hpp>
10
#include <boost/thread/tss.hpp>
11
12
#include <iosfwd>
13
14
class
MutexI
{
15
public
:
16
virtual
~MutexI
(){}
17
virtual
void
lock
()=0;
18
virtual
void
unlock
()=0;
19
20
class
scoped_lock
{
21
MutexI
&
m
;
22
public
:
23
scoped_lock
(
MutexI
& im) :
m
(im) {
m
.
lock
();}
24
~scoped_lock
() {
m
.
unlock
();}
25
};
26
27
};
28
29
struct
DummyMutex
:
public
MutexI
{
30
void
lock
(){}
31
void
unlock
(){}
32
};
33
34
template
<
class
M=boost::recursive_mutex>
35
class
AnyMutex
:
public
MutexI
{
36
M &
m
;
37
public
:
38
typedef
M
Mutex
;
39
typedef
MutexI::scoped_lock
scoped_lock
;
40
AnyMutex
(M & im) :
m
(im){}
41
void
lock
() {
m
.lock();}
42
void
unlock
() {
m
.unlock();}
43
};
44
45
46
std::ostream &
TSafeOstream
();
47
48
49
template
<
class
M>
50
class
lockSentry
{
51
public
:
52
typedef
M
Mutex
;
53
54
explicit
lockSentry
(M& im) :
m
(im) {}
55
~lockSentry
() {}
56
57
operator
typename
M::scoped_lock & () {
return
m
;}
58
typename
M::scoped_lock &
operator()
() {
return
m
;}
59
60
private
:
61
typename
M::scoped_lock
m
;
62
63
};
64
65
typedef
lockSentry<boost::recursive_mutex>
LockMutex
;
66
67
typedef
lockSentry<boost::mutex>
SimpleLockMutex
;
68
69
namespace
Capri {
70
71
// the global mutex...
72
extern
LockMutex::Mutex
glMutex
;
73
74
}
75
76
77
template
<
class
M=boost::recursive_mutex>
78
struct
ThreadTraits
{
79
80
typedef
M
Mutex
;
81
83
template
<
class
T>
static
Mutex
&
myMutex
(
const
T*) {
84
static
Mutex
locm;
85
return
locm;
86
}
87
88
89
};
90
91
92
template
<
class
T,
class
M=boost::recursive_mutex>
93
class
classLock
:
private
lockSentry
<M> {
94
public
:
95
explicit
classLock
(
const
T* me) :
lockSentry
<M>(
ThreadTraits
<M>::myMutex(me)){}
96
~classLock
(){}
97
};
98
99
100
struct
boostFuture
{
101
boost::xtime
xt
;
102
103
boostFuture
(
int
i
) {
104
boost::xtime_get(&
xt
, boost::TIME_UTC);
105
xt
.sec +=
i
;
106
}
107
108
operator
const
boost::xtime & ()
const
{
return
xt
;}
109
110
};
111
112
inline
pthread_t
thread_self_tid
() {
return
pthread_self();}
113
114
115
116
template
<
typename
T>
117
class
ThreadMessage
{
118
public
:
119
typedef
T
Case
;
120
121
const
Case
&
operator()
()
const
{
122
SimpleLockMutex
gl(
lock
);
123
doit
.wait(gl());
124
return
it
;
125
}
126
127
void
go
(
const
Case
&
i
) {
128
SimpleLockMutex
gl(
lock
);
129
it
=
i
;
130
doit
.notify_all();
131
}
132
133
const
Case
&
get
()
const
{
return
it
;}
134
135
private
:
136
137
mutable
SimpleLockMutex::Mutex
lock
;
138
mutable
boost::condition
doit
;
139
Case
it
;
140
};
141
142
namespace
{
143
template
<
typename
T>
144
struct
defF {
145
T * operator()() {
146
return
new
T();
147
}
148
};
149
150
}
151
152
template
<
typename
T,
typename
F=defF<T> >
153
class
ThreadSingleton
{
154
public
:
155
156
static
T &
instance
(F&
f
=
factory
()) {
157
static
boost::thread_specific_ptr<T> me;
158
if
(!me.get()) {
159
T *
t
=
buildMe
(
f
);
160
me.reset(t);
161
}
162
return
*me;
163
}
164
165
private
:
166
static
F &
factory
() {
167
static
F
f
;
168
return
f
;
169
}
170
171
static
T *
buildMe
(F&
f
) {
172
return
f
();
173
}
174
175
};
176
177
#endif // UTILITIES_THREADS_MUTEXUTILS_H
TSafeOstream
std::ostream & TSafeOstream()
i
int i
Definition:
DBlmapReader.cc:9
ThreadMessage
Definition:
MutexUtils.h:117
lockSentry::m
M::scoped_lock m
Definition:
MutexUtils.h:61
classLock
Definition:
MutexUtils.h:93
MutexI::lock
virtual void lock()=0
DummyMutex::lock
void lock()
Definition:
MutexUtils.h:30
ThreadTraits
Definition:
MutexUtils.h:78
boostFuture
Definition:
MutexUtils.h:100
ThreadMessage::operator()
const Case & operator()() const
Definition:
MutexUtils.h:121
ThreadTraits::myMutex
static Mutex & myMutex(const T *)
a static safe class level mutex...
Definition:
MutexUtils.h:83
MutexI::scoped_lock::~scoped_lock
~scoped_lock()
Definition:
MutexUtils.h:24
lockSentry
Definition:
MutexUtils.h:50
ThreadMessage::go
void go(const Case &i)
Definition:
MutexUtils.h:127
lockSentry::lockSentry
lockSentry(M &im)
Definition:
MutexUtils.h:54
AnyMutex::Mutex
M Mutex
Definition:
MutexUtils.h:38
MutexI::unlock
virtual void unlock()=0
lockSentry::Mutex
M Mutex
Definition:
MutexUtils.h:52
DummyMutex
Definition:
MutexUtils.h:29
boostFuture::boostFuture
boostFuture(int i)
Definition:
MutexUtils.h:103
MutexI::scoped_lock::m
MutexI & m
Definition:
MutexUtils.h:21
ThreadTraits::Mutex
M Mutex
Definition:
MutexUtils.h:80
AnyMutex::scoped_lock
MutexI::scoped_lock scoped_lock
Definition:
MutexUtils.h:39
DummyMutex::unlock
void unlock()
Definition:
MutexUtils.h:31
ThreadSingleton::instance
static T & instance(F &f=factory())
Definition:
MutexUtils.h:156
AnyMutex::m
M & m
Definition:
MutexUtils.h:36
MutexI
Definition:
MutexUtils.h:14
MutexI::scoped_lock
Definition:
MutexUtils.h:20
LockMutex
lockSentry< boost::recursive_mutex > LockMutex
Definition:
MutexUtils.h:65
matplotRender.t
tuple t
Definition:
matplotRender.py:569
ThreadMessage::Case
T Case
Definition:
MutexUtils.h:119
ThreadSingleton::factory
static F & factory()
Definition:
MutexUtils.h:166
MutexI::~MutexI
virtual ~MutexI()
Definition:
MutexUtils.h:16
AnyMutex
Definition:
MutexUtils.h:35
Capri::glMutex
LockMutex::Mutex glMutex
f
double f[11][100]
Definition:
MuScleFitUtils.cc:79
AnyMutex::unlock
void unlock()
Definition:
MutexUtils.h:42
lockSentry::operator()
M::scoped_lock & operator()()
Definition:
MutexUtils.h:58
SimpleLockMutex
lockSentry< boost::mutex > SimpleLockMutex
Definition:
MutexUtils.h:67
ThreadMessage::lock
SimpleLockMutex::Mutex lock
Definition:
MutexUtils.h:137
ThreadSingleton::buildMe
static T * buildMe(F &f)
Definition:
MutexUtils.h:171
ThreadMessage::doit
boost::condition doit
Definition:
MutexUtils.h:138
boostFuture::xt
boost::xtime xt
Definition:
MutexUtils.h:101
AnyMutex::AnyMutex
AnyMutex(M &im)
Definition:
MutexUtils.h:40
ThreadSingleton
Definition:
MutexUtils.h:153
MutexI::scoped_lock::scoped_lock
scoped_lock(MutexI &im)
Definition:
MutexUtils.h:23
ThreadMessage::it
Case it
Definition:
MutexUtils.h:139
classLock::classLock
classLock(const T *me)
Definition:
MutexUtils.h:95
classLock::~classLock
~classLock()
Definition:
MutexUtils.h:96
lockSentry::~lockSentry
~lockSentry()
Definition:
MutexUtils.h:55
AnyMutex::lock
void lock()
Definition:
MutexUtils.h:41
thread_self_tid
pthread_t thread_self_tid()
Definition:
MutexUtils.h:112
Generated for CMSSW Reference Manual by
1.8.5