BeThread.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
11 
12 #include "BeCriticalSection.h"
13 #include "BeAtomic.h"
14 
15 #if defined (_WIN32) && defined(_MANAGED)
16  //=======================================================================================
17  //
18  // BEMUTEX_DATA_ARRAY_LENGTH = sizeof (std::recursive_mutex) / sizeof (void*)
19  // BECONDITIONVARIABLE_DATA_ARRAY_LENGTH = sizeof (std::condition_variable_any) / sizeof (void*)
20  // Array of void* is used to force pointer type alignment
21  //=======================================================================================
22  #if (_MSC_VER >= 1900)
23  #if defined(_M_X64)
24  #define BEMUTEX_DATA_ARRAY_LENGTH (80 / sizeof(void*))
25  #define BECONDITIONVARIABLE_DATA_ARRAY_LENGTH (88 / sizeof(void*))
26  #else
27  #define BEMUTEX_DATA_ARRAY_LENGTH (48 / sizeof(void*))
28  #define BECONDITIONVARIABLE_DATA_ARRAY_LENGTH (48 / sizeof(void*))
29  #endif
30  #else
31  #define BEMUTEX_DATA_ARRAY_LENGTH 1
32  #define BECONDITIONVARIABLE_DATA_ARRAY_LENGTH 2
33  #endif
34 #else
35  //=======================================================================================
36  // Sane platforms...
37  //=======================================================================================
38  #include <mutex>
39  #include <condition_variable>
40  #define BEMUTEX_DATA_ARRAY_LENGTH sizeof(std::recursive_mutex) / sizeof(void*)
41  #define BECONDITIONVARIABLE_DATA_ARRAY_LENGTH sizeof(std::condition_variable_any) / sizeof(void*)
42 #endif
43 
44 
46 
49 
50 
51 //=======================================================================================
55 // @bsiclass Bentley Systems
56 //=======================================================================================
57 struct BeMutex
58 {
59 private:
60  BeMutex(BeMutex const&) = delete;
61  BeMutex& operator=(BeMutex const&) = delete;
62  void* m_osMutex[BEMUTEX_DATA_ARRAY_LENGTH];
63 
64 public:
66  BENTLEYDLL_EXPORT ~BeMutex();
67 
69  BENTLEYDLL_EXPORT void lock();
70  void Enter() {lock();}
71 
73  BENTLEYDLL_EXPORT void unlock();
74  void Leave() {unlock();}
75 };
76 
77 //=======================================================================================
80 // @bsiclass Bentley Systems
81 //=======================================================================================
83 {
84 private:
85  BeMutexHolder(BeMutexHolder const&) = delete;
86  BeMutexHolder& operator=(BeMutexHolder const&) = delete;
87 
88  BeMutex* m_mutex;
89 
90  bool m_owns;
91 
92 public:
93  enum class Lock : bool {No=false, Yes=true};
94 
98  BENTLEYDLL_EXPORT BeMutexHolder(BeMutex& mutex, Lock lock=Lock::Yes);
99 
102 
104  BeMutex* GetMutex() {return m_mutex;}
106  BENTLEYDLL_EXPORT void unlock();
108  BENTLEYDLL_EXPORT void lock();
110  BENTLEYDLL_EXPORT bool owns_lock();
111 };
112 
113 //=======================================================================================
115 // @bsiclass Bentley Systems
116 //=======================================================================================
118 {
121  virtual bool _TestCondition(struct BeConditionVariable &cv) = 0;
122 };
123 
124 //=======================================================================================
130 // @bsiclass Bentley Systems
131 //=======================================================================================
133 {
134 private:
136  mutable BeMutex m_mutex;
137  BeConditionVariable(BeConditionVariable const&) = delete;
139 
140 public:
141  BENTLEYDLL_EXPORT void InfiniteWait(BeMutexHolder& holder);
142  BENTLEYDLL_EXPORT bool RelativeWait(BeMutexHolder& holder, uint32_t timeoutMillis);
143 
144  static const uint32_t Infinite = 0xffffffff;
145 
148 
149  // Get the mutex
150  BeMutex& GetMutex() const {return m_mutex;}
151 
154  BENTLEYDLL_EXPORT bool ProtectedWaitOnCondition(BeMutexHolder&, IConditionVariablePredicate* predicate, uint32_t timeoutMillis);
155 
170  bool WaitOnCondition(IConditionVariablePredicate* predicate, uint32_t timeoutMillis)
171  {
172  BeMutexHolder holder(m_mutex);
173  return ProtectedWaitOnCondition(holder, predicate, timeoutMillis);
174  }
175 
177  BENTLEYDLL_EXPORT void notify_one();
179  BENTLEYDLL_EXPORT void notify_all();
180 };
181 
182 
183 
184 
185 /*=================================================================================**/
188 
190  {
191  typedef unsigned (__stdcall *T_ThreadStart)(void*);
192 
197  BENTLEYDLL_EXPORT static uintptr_t StartNewThread (int stackSize, T_ThreadStart startAddr, void* arg);
198 
201  BENTLEYDLL_EXPORT static void SetCurrentThreadName(Utf8CP newName);
202 
204  BENTLEYDLL_EXPORT static intptr_t GetCurrentThreadId();
205 
208  BENTLEYDLL_EXPORT static void BeSleep (UInt32 millis);
209  };
210 
212 
#define BENTLEYDLL_EXPORT
Definition: Bentley.h:249
A synchronization primitive that can be used to block a thread, or multiple threads at the same time...
Definition: BeThread.h:132
A BeMutex ownership wrapper.
Definition: BeThread.h:82
void Enter()
Definition: BeThread.h:70
Together with the BeThread.h file provides Bentley specific thread handling functions (Bentley/BeCrit...
uint32_t UInt32
Definition: Bentley.r.h:128
bstdmap & operator=(const bstdmap &__rhs)
Definition: stdcxx/bstdmap.h:170
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
void Leave()
Definition: BeThread.h:74
BeMutex & GetMutex() const
Definition: BeThread.h:150
Utilities for dealing with threads !
Definition: BeThread.h:189
BeMutex * GetMutex()
Get the BeMutex associated with this BeMutexHolder.
Definition: BeThread.h:104
Lock
Definition: BeThread.h:93
A synchronization primitive that can be used to protect shared data from being simultaneously accesse...
Definition: BeThread.h:57
#define BECONDITIONVARIABLE_DATA_ARRAY_LENGTH
Definition: BeThread.h:41
bool WaitOnCondition(IConditionVariablePredicate *predicate, uint32_t timeoutMillis)
Enters the mutex, calls the predicate, and then waits on the condition if the predicate returns false...
Definition: BeThread.h:170
#define BEMUTEX_DATA_ARRAY_LENGTH
Definition: BeThread.h:40
Utf8Char const * Utf8CP
Definition: Bentley.h:229
Provides Bentley specific implementation of the std::atomic template class (Bentley/BeAtomic.h).
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
unsigned int uint32_t
Definition: Bentley.r.h:93
Provides implementation of predicate for a BeConditionVariable.
Definition: BeThread.h:117

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.