RCFProto
 All Classes Functions Typedefs
ThreadLibrary.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_THREADLIBRARY_HPP
20 #define INCLUDE_RCF_THREADLIBRARY_HPP
21 
22 #include <boost/noncopyable.hpp>
23 #include <boost/shared_ptr.hpp>
24 
25 #include <RCF/Config.hpp>
26 #include <RCF/Export.hpp>
27 
28 #include <RCF/AsioFwd.hpp>
29 #include <boost/cstdint.hpp>
30 #include <boost/noncopyable.hpp>
31 #include <boost/throw_exception.hpp>
32 
33 #ifdef BOOST_WINDOWS
34 #include <windows.h>
35 #endif
36 
37 namespace RCF {
38  namespace detail {
39  typedef boost::noncopyable noncopyable;
40  }
41 }
42 
43 #include <RCF/thread/event.hpp>
44 #include <RCF/thread/mutex.hpp>
45 #include <RCF/thread/thread.hpp>
46 
47 #ifdef RCF_USE_BOOST_TLS
48 #include <boost/thread/tss.hpp>
49 #else
50 #include <RCF/thread/tss_ptr.hpp>
51 #endif
52 
53 namespace RCF {
54 
55  // Multi-threading primitives, based on the ones in asio.
56 
57  typedef RCF::detail::thread Thread;
58  typedef RCF::detail::mutex Mutex;
59  typedef RCF::detail::mutex::scoped_lock Lock;
60  typedef RCF::detail::event Condition;
61 
62 #ifdef BOOST_WINDOWS
63  typedef int ThreadId;
64 #else
65  typedef pthread_t ThreadId;
66 #endif
67 
68 #ifdef RCF_USE_BOOST_TLS
69 
70  template<typename T>
71  class ThreadSpecificPtr : public boost::thread_specific_ptr<T>
72  {
73  };
74 
75 #else
76 
77  template<typename T>
78  class ThreadSpecificPtr : public RCF::detail::tss_ptr<T>
79  {
80  public:
81  void reset(T * pt = NULL)
82  {
83  RCF::detail::tss_ptr<T>::operator =(pt);
84  }
85 
86  T * get() const
87  {
88  return RCF::detail::tss_ptr<T>::operator T*();
89  }
90 
91  T * operator->() const
92  {
93  return RCF::detail::tss_ptr<T>::operator T*();
94  }
95 
96  typedef ThreadSpecificPtr Val;
97  };
98 
99 #endif
100 
101  // Simple read-write mutex.
102 
103  class ReadWriteMutex;
104 
105  class RCF_EXPORT ReadLock : boost::noncopyable
106  {
107  public:
108  ReadLock(ReadWriteMutex &rwm);
109  ~ReadLock();
110  void lock();
111  void unlock();
112 
113  private:
114  ReadWriteMutex & rwm;
115  bool locked;
116  };
117 
118  class RCF_EXPORT WriteLock : boost::noncopyable
119  {
120  public:
121  WriteLock(ReadWriteMutex &rwm);
122  ~WriteLock();
123  void lock();
124  void unlock();
125 
126  private:
127  ReadWriteMutex & rwm;
128  Lock readLock;
129  Lock writeLock;
130  bool locked;
131  };
132 
133  enum ReadWritePriority
134  {
135  ReaderPriority,
136  WriterPriority
137  };
138 
139  class RCF_EXPORT ReadWriteMutex : boost::noncopyable
140  {
141  public:
142  ReadWriteMutex(ReadWritePriority rwsp);
143 
144  private:
145 
146  void waitOnReadUnlock(Lock &lock);
147  void notifyReadUnlock(Lock &lock);
148 
149  Mutex readMutex;
150  Mutex writeMutex;
151  Condition readUnlockEvent;
152  int readerCount;
153 
154  public:
155 
156  friend class ReadLock;
157  friend class WriteLock;
158 
159  };
160 
161 #ifdef BOOST_WINDOWS
162 
163  // On Windows critical sections are automatically recursive. All
164  // this class does is disable the recursive locking assert in win_mutex.
165  class RCF_EXPORT RecursiveMutex : public RCF::detail::win_mutex
166  {
167  public:
168  RecursiveMutex();
169  ~RecursiveMutex();
170  };
171 
172  typedef Lock RecursiveLock;
173 
174 #else
175 
176  // Some pthreads versions have built in recursive mutexes
177  // (PTHREAD_MUTEX_RECURSIVE). For portability we're using
178  // a custom implemenation instead.
179 
180  class RCF_EXPORT RecursiveMutex : boost::noncopyable
181  {
182  public:
183  RecursiveMutex();
184  ~RecursiveMutex();
185 
186  private:
187 
188  friend class RCF::detail::scoped_lock<RecursiveMutex>;
189 
190  void lock();
191  void unlock();
192 
193  Mutex mMutex;
194  Condition mCondition;
195  bool mIsLocked;
196  ThreadId mOwner;
197  std::size_t mLockCount;
198 
199  };
200 
201  typedef RCF::detail::scoped_lock<RecursiveMutex> RecursiveLock;
202 
203 #endif
204 
205  typedef boost::shared_ptr<RecursiveLock> RecursiveLockPtr;
206  typedef boost::shared_ptr<RecursiveMutex> RecursiveMutexPtr;
207 
208  typedef boost::shared_ptr<Thread> ThreadPtr;
209  typedef boost::shared_ptr<ReadWriteMutex> ReadWriteMutexPtr;
210  typedef boost::shared_ptr<Mutex> MutexPtr;
211  typedef boost::shared_ptr<Lock> LockPtr;
212  typedef boost::shared_ptr<Condition> ConditionPtr;
213 
214 
215  RCF_EXPORT ThreadId getCurrentThreadId();
216 
217  // Time in ms since ca 1970, modulo 65536 s (turns over every ~18.2 hrs).
218  RCF_EXPORT boost::uint32_t getCurrentTimeMs();
219 
220  // Generate a timeout value for the given ending time.
221  // Returns zero if endTime <= current time <= endTime+10%of timer resolution, otherwise returns a nonzero duration in ms.
222  // Timer resolution as above (18.2 hrs).
223  static const unsigned int MaxTimeoutMs = (((unsigned int)-1)/10)*9;
224  RCF_EXPORT boost::uint32_t generateTimeoutMs(unsigned int endTimeMs);
225 
226  RCF_EXPORT Mutex & getRootMutex();
227 
228  RCF_EXPORT void sleepMs(boost::uint32_t msec);
229 }
230 
231 #endif // ! INCLUDE_RCF_THREADLIBRARY_HPP