27 #ifndef __IDTHREADING__ 28 #define __IDTHREADING__ 30 #include "IDThreadingPrimitives.h" 36 typedef uint32 ExecutionContextID;
37 extern RUNTIME_DECL
const ExecutionContextID kInvalidExecutionContextID;
43 RUNTIME_DECL ExecutionContextID CurrentExecutionContextId ();
48 RUNTIME_DECL
void SetThreadName(ThreadID
id,
const char* name);
56 RUNTIME_DECL
void BeginPublicThreadBottleneck ();
63 RUNTIME_DECL
void EndPublicThreadBottleneck ();
65 typedef uint32 PublicThreadLocalStorageKey;
66 typedef uint64 PublicThreadLocalStorageValue;
68 RUNTIME_DECL PublicThreadLocalStorageKey PublicThreadLocalStorageAllocKey ();
69 RUNTIME_DECL
void PublicThreadLocalStorageRealeaseKey(PublicThreadLocalStorageKey key);
70 RUNTIME_DECL
void PublicThreadLocalStorageSet (PublicThreadLocalStorageKey key, PublicThreadLocalStorageValue value);
71 RUNTIME_DECL PublicThreadLocalStorageValue PublicThreadLocalStorageGet (PublicThreadLocalStorageKey key, PublicThreadLocalStorageValue initialVal);
73 typedef uint32 PublicThreadLocalSmartPointerKey;
74 typedef boost::shared_ptr<void> PublicThreadLocalSmartPointerValue;
77 RUNTIME_DECL PublicThreadLocalSmartPointerKey PublicThreadLocalSmartPointerAllocKey ();
78 RUNTIME_DECL
void PublicThreadLocalSmartPointerRealeaseKey(PublicThreadLocalSmartPointerKey key);
79 RUNTIME_DECL
void PublicThreadLocalSmartPointerSet (PublicThreadLocalSmartPointerKey key, PublicThreadLocalSmartPointerValue value);
80 RUNTIME_DECL PublicThreadLocalSmartPointerValue PublicThreadLocalSmartPointerGet (PublicThreadLocalSmartPointerKey key);
93 fKey (PublicThreadLocalStorageAllocKey ()),
100 PublicThreadLocalStorageRealeaseKey(fKey);
106 return T (PublicThreadLocalStorageGet (fKey, fInitialVal));
112 PublicThreadLocalStorageValue value =
114 PublicThreadLocalStorageSet (fKey, value);
129 ThreadLocal<T>& operator -=(T x)
134 ThreadLocal<T>& operator ++()
136 return operator+=(1);
138 ThreadLocal<T>& operator --()
140 return operator-=(1);
143 PublicThreadLocalStorageKey fKey;
144 PublicThreadLocalStorageValue fInitialVal;
159 fKey (PublicThreadLocalSmartPointerAllocKey ()),
164 fKey (PublicThreadLocalSmartPointerAllocKey ()),
165 fInitialVal(
new T(intVal))
172 PublicThreadLocalSmartPointerRealeaseKey(fKey);
179 const T *Get ()
const 184 const T& GetReference ()
const 189 void Set (
const T& val)
202 PublicThreadLocalSmartPointerValue value =
203 PublicThreadLocalSmartPointerGet (fKey);
204 T *p = (T*) (value.get());
208 p = fInitialVal?
new T(*fInitialVal) :
new T;
211 (PublicThreadLocalSmartPointerValue) (p);
212 PublicThreadLocalSmartPointerSet (fKey, value);
217 PublicThreadLocalSmartPointerKey fKey;
229 fKey (PublicThreadLocalSmartPointerAllocKey ()),
236 PublicThreadLocalSmartPointerRealeaseKey(fKey);
244 const T *Get ()
const 255 const T *begin ()
const 262 return _get () + fSize;
265 const T *end ()
const 267 return _get () + fSize;
278 PublicThreadLocalSmartPointerValue value =
279 PublicThreadLocalSmartPointerGet (fKey);
280 std::vector<T> *p = (std::vector<T> *) (value.get());
284 p =
new std::vector<T>(fSize);
286 (PublicThreadLocalSmartPointerValue) (p);
287 PublicThreadLocalSmartPointerSet (fKey, value);
294 PublicThreadLocalSmartPointerKey fKey;
299 RUNTIME_DECL
void TestHook ();
304 inline bool operator<(ThreadLocal<T>
const& left,
ThreadLocal<T> const& right)
306 return left.
Get() < right.Get();
310 inline bool operator==(ThreadLocal<T>
const& left, ThreadLocal<T>
const& right)
312 return left.Get() == right.Get();
316 inline bool operator<(ThreadLocal<T>
const& left, T right)
318 return left.Get() < right;
322 inline bool operator<(T left, ThreadLocal<T>
const& right)
324 return left < right.Get();
328 inline bool operator==(ThreadLocal<T>
const& left, T right)
330 return left.Get() == right;
334 inline bool operator==(T left, ThreadLocal<T>
const& right)
336 return left == right.Get();
340 inline bool operator!=(ThreadLocal<T>
const& left, T right)
342 return left.Get() != right;
346 inline bool operator!=(T left, ThreadLocal<T>
const& right)
348 return left != right.Get();
355 RUNTIME_DECL
void Sleep( uint32 milliseconds );
368 template <
class Mutex>
376 : fMutex(mx), fLocked(false)
385 : fMutex(mx), fLocked(true)
407 ASSERT_MSG (!fLocked,
"lock_guard::lock called on an already owned mutex!");
415 ASSERT_MSG (fLocked,
"lock_guard::unlock called on a mutex we don't own!");
421 bool is_locked()
const {
return fLocked; }
427 class spin_mutex_impl;
460 spin_mutex_impl* fIsLocked;
464 #endif // __IDTHREADING__