CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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();}
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;
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) {}
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 
66 
68 
69 namespace Capri {
70 
71  // the global mutex...
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)){}
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>
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 
138  mutable boost::condition doit;
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> >
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
std::ostream & TSafeOstream()
int i
Definition: DBlmapReader.cc:9
M::scoped_lock m
Definition: MutexUtils.h:61
virtual void lock()=0
void lock()
Definition: MutexUtils.h:30
const Case & operator()() const
Definition: MutexUtils.h:121
static Mutex & myMutex(const T *)
a static safe class level mutex...
Definition: MutexUtils.h:83
void go(const Case &i)
Definition: MutexUtils.h:127
lockSentry(M &im)
Definition: MutexUtils.h:54
virtual void unlock()=0
boostFuture(int i)
Definition: MutexUtils.h:103
MutexI::scoped_lock scoped_lock
Definition: MutexUtils.h:39
void unlock()
Definition: MutexUtils.h:31
static T & instance(F &f=factory())
Definition: MutexUtils.h:156
lockSentry< boost::recursive_mutex > LockMutex
Definition: MutexUtils.h:65
static F & factory()
Definition: MutexUtils.h:166
virtual ~MutexI()
Definition: MutexUtils.h:16
LockMutex::Mutex glMutex
double f[11][100]
void unlock()
Definition: MutexUtils.h:42
M::scoped_lock & operator()()
Definition: MutexUtils.h:58
lockSentry< boost::mutex > SimpleLockMutex
Definition: MutexUtils.h:67
SimpleLockMutex::Mutex lock
Definition: MutexUtils.h:137
static T * buildMe(F &f)
Definition: MutexUtils.h:171
boost::condition doit
Definition: MutexUtils.h:138
boost::xtime xt
Definition: MutexUtils.h:101
AnyMutex(M &im)
Definition: MutexUtils.h:40
scoped_lock(MutexI &im)
Definition: MutexUtils.h:23
classLock(const T *me)
Definition: MutexUtils.h:95
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
~classLock()
Definition: MutexUtils.h:96
long double T
void lock()
Definition: MutexUtils.h:41
pthread_t thread_self_tid()
Definition: MutexUtils.h:112