Free Electron
Mutex.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #define FE_MTX_COUNT (FE_CODEGEN<FE_DEBUG)
8 
9 #ifndef __platform_mutex_h__
10 #define __platform_mutex_h__
11 
12 #if 0
13 #include "boost/thread.hpp"
14 #define FE_YIELD boost::thread::yield
15 #define FE_SLEEP(s, ns) \
16  struct boost::xtime boost_time; \
17  boost_time.sec=seconds; \
18  boost_time.nsec=nanoseconds; \
19  boost::thread::sleep(boost_time);
20 #endif
21 
22 #if 0
23 #include "boost/thread.hpp"
24 #include "boost/thread/thread.hpp"
25 #include "boost/thread/tss.hpp"
26 #include "boost/thread/condition.hpp"
27 typedef boost::shared_mutex mutex;
28 typedef boost::recursive_mutex recursive_mutex;
29 #define FE_YIELD boost::thread::yield
30 #define FE_SLEEP(s, ns) \
31  struct boost::xtime boost_time; \
32  boost_time.sec=seconds; \
33  boost_time.nsec=nanoseconds; \
34  boost::thread::sleep(boost_time);
35 #define FE_LOCK(s, m) s.lock()
36 #define FE_UNLOCK(s) s.unlock()
37 #define FE_M_LOCK(m) m->lock()
38 #define FE_M_UNLOCK(m) m->unlock()
39 #define FE_M_TRY_LOCK(m) m->try_lock()
40 #define FE_M_LOCK_SHARED(m) m->lock_shared()
41 #define FE_M_UNLOCK_SHARED(m) m->unlock_shared()
42 #define FE_M_TRY_LOCK_SHARED(m) m->try_lock_shared()
43 #endif
44 
45 #if 0
46 #include "tbb/mutex.h"
47 #include "tbb/recursive_mutex.h"
48 #include "tbb/spin_rw_mutex.h"
49 #include "tbb/tick_count.h"
50 #include "tbb/tbb_thread.h"
51 typedef tbb::spin_rw_mutex mutex;
52 typedef tbb::recursive_mutex recursive_mutex;
53 #define FE_YIELD tbb::this_tbb_thread::yield
54 #define FE_SLEEP(s, ns) \
55  tbb::tick_count::interval_t t(((double)s) + (((double)ns) * 1.0e-9)); \
56  tbb::this_tbb_thread::sleep(t)
57 #define FE_LOCK(s, m) s.acquire(m)
58 #define FE_UNLOCK(s) s.release()
59 #define FE_M_LOCK(m) m->lock()
60 #define FE_M_UNLOCK(m) m->unlock()
61 #define FE_M_TRY_LOCK(m) m->try_lock()
62 #define FE_M_LOCK_SHARED(m) m->lock_read()
63 #define FE_M_UNLOCK_SHARED(m) m->unlock()
64 #define FE_M_TRY_LOCK_SHARED(m) m->try_lock_read()
65 #endif
66 
67 namespace fe
68 {
69 
70 /*
71  boost::mutex m_jobMonitor;
72  boost::mutex::scoped_lock lock(m_jobMonitor);
73 
74  boost::condition m_jobAvailable;
75  m_jobAvailable.wait(lock);
76  m_jobAvailable.notify_one();
77  m_barrierHit.notify_all();
78 */
79 
80 extern "C"
81 {
82 
83 typedef void* (FE_CDECL MutexInitFunction)(bool a_recursive);
84 typedef bool (FE_CDECL MutexLockFunction)(bool a_recursive,void* a_pRawMutex,
85  bool a_un,bool a_try,bool a_readOnly);
86 typedef void (FE_CDECL MutexFinishFunction)(bool a_recursive,void* a_pRawMutex);
87 
88 typedef void* (FE_CDECL MutexGuardInitFunction)(
89  bool a_recursive,void* a_pRawMutex,bool a_readOnly);
90 typedef bool (FE_CDECL MutexGuardLockFunction)(bool a_recursive,void* a_pRawGuard,
91  bool a_un,bool a_try,bool a_readOnly);
92 typedef void (FE_CDECL MutexGuardFinishFunction)(bool a_recursive,void* a_pRawGuard,
93  bool a_readOnly);
94 
95 typedef void* (FE_CDECL MutexConditionInitFunction)(void);
96 typedef bool (FE_CDECL MutexConditionWaitFunction)(void* a_pRawCondition,
97  bool a_recursive,void* a_pRawGuard,bool a_readOnly);
98 typedef bool (FE_CDECL MutexConditionNotifyFunction)(void* a_pRawCondition,bool a_all);
99 typedef void (FE_CDECL MutexConditionFinishFunction)(void* a_pRawCondition);
100 
101 } /* extern "C" */
102 
103 FE_MEM_PORT extern MutexInitFunction* gs_fnMutexInit;
104 FE_MEM_PORT extern MutexLockFunction* gs_fnMutexLock;
105 FE_MEM_PORT extern MutexFinishFunction* gs_fnMutexFinish;
106 
107 FE_MEM_PORT extern MutexGuardInitFunction* gs_fnMutexGuardInit;
108 FE_MEM_PORT extern MutexGuardLockFunction* gs_fnMutexGuardLock;
109 FE_MEM_PORT extern MutexGuardFinishFunction* gs_fnMutexGuardFinish;
110 
111 FE_MEM_PORT extern MutexConditionInitFunction* gs_fnMutexConditionInit;
112 FE_MEM_PORT extern MutexConditionWaitFunction* gs_fnMutexConditionWait;
113 FE_MEM_PORT extern MutexConditionNotifyFunction* gs_fnMutexConditionNotify;
114 FE_MEM_PORT extern MutexConditionFinishFunction* gs_fnMutexConditionFinish;
115 
116 FE_MEM_PORT extern String gs_mutexSupport;
117 FE_MEM_PORT extern BWORD gs_mutexChecked;
118 FE_MEM_PORT extern DL_Loader* gs_pMutexLoader;
119 
120 class FE_DL_PUBLIC FE_DL_EXPORT Mutex
121 {
122  public:
123  Mutex(BWORD a_recursive=FALSE);
124  ~Mutex(void);
125 
126 static BWORD supported(void) { return (gs_fnMutexInit!=NULL); }
127 static String support(void) { return gs_mutexSupport; }
128 
129  BWORD lock(void);
130  BWORD unlock(void);
131  BWORD tryLock(void);
132  BWORD lockReadOnly(void);
133  BWORD unlockReadOnly(void);
134  BWORD tryLockReadOnly(void);
135 
136  class Condition;
137  class Guard
138  {
139  friend class Mutex::Condition;
140 
141  public:
142  Guard(Mutex& a_rMutex,BWORD a_readOnly=FALSE,
143  BWORD a_truly=TRUE);
144  ~Guard(void);
145 
146  BWORD lock(void);
147  BWORD unlock(void);
148  BWORD tryLock(void);
149 
150  private:
151  Mutex* m_pMutex;
152  void* m_pRawGuard;
153  bool m_readOnly;
154  };
155 
156  class Condition
157  {
158  public:
159  Condition(void);
160  ~Condition(void);
161 
162  BWORD wait(Guard& a_rGuard);
163  BWORD notifyOne(void);
164  BWORD notifyAll(void);
165 
166  private:
167  void* raw(void);
168 
169  void* m_pRawCondition;
170  };
171 
172 static void replaceInitFunction(MutexInitFunction* a_fnInit)
173  { gs_fnMutexInit=a_fnInit; }
174 static void replaceLockFunction(MutexLockFunction* a_fnLock)
175  { gs_fnMutexLock=a_fnLock; }
176 static void replaceFinishFunction(MutexFinishFunction* a_fnFinish)
177  { gs_fnMutexFinish=a_fnFinish; }
178 
179 static void replaceGuardInitFunction(MutexGuardInitFunction* a_fnGuardInit)
180  { gs_fnMutexGuardInit=a_fnGuardInit; }
181 static void replaceGuardLockFunction(MutexGuardLockFunction* a_fnGuardLock)
182  { gs_fnMutexGuardLock=a_fnGuardLock; }
183 static void replaceGuardFinishFunction(
184  MutexGuardFinishFunction* a_fnGuardFinish)
185  { gs_fnMutexGuardFinish=a_fnGuardFinish; }
186 
187 static void replaceConditionInitFunction(
188  MutexConditionInitFunction* a_fnConditionInit)
189  { gs_fnMutexConditionInit=a_fnConditionInit; }
190 static void replaceConditionWaitFunction(
191  MutexConditionWaitFunction* a_fnConditionWait)
192  { gs_fnMutexConditionWait=a_fnConditionWait; }
193 static void replaceConditionNotifyFunction(
194  MutexConditionNotifyFunction* a_fnConditionNotify)
195  { gs_fnMutexConditionNotify=a_fnConditionNotify; }
196 static void replaceConditionFinishFunction(
197  MutexConditionFinishFunction* a_fnConditionFinish)
198  { gs_fnMutexConditionFinish=a_fnConditionFinish; }
199 
200 static BWORD confirm(String a_hint="");
201 static BWORD clearFunctionPointers(void);
202 static BWORD dismiss(void);
203 
204  protected:
205 
206  BWORD m_recursive;
207 
208  private:
209 
210  void* raw(void);
211 
212  bool m_initializing;
213  void* m_pRawMutex;
214 
215 #if FE_MTX_COUNT
216  I32 m_fakeCount;
217  I32 m_lockCount;
218 #endif
219 };
220 
221 class FE_DL_PUBLIC FE_DL_EXPORT RecursiveMutex: public Mutex
222 {
223  public:
224  RecursiveMutex(void):
225  Mutex(TRUE) {}
226 };
227 
228 } /* namespace */
229 
230 #endif /* __platform_mutex_h__ */
kernel
Definition: namespace.dox:3