6 #include "Honey/Thread/platform/Thread.h"
23 StoreId(
int id = -1) : id(id), reclaim(0) {}
30 Store() : ptr(nullptr), reclaim(-1), fin(bind(finalize<void>(), ptr)) {}
33 function<void ()> fin;
37 class InterruptWait : mt::NoCopy
40 InterruptWait(Thread& thread, Condition& cond, Mutex& mutex);
60 inline void spin(
int count) {
for (
int i = 0; i < count; ++i)
pause(); }
72 typedef function<T* ()>
Init;
73 typedef function<void (T*)>
Fin;
76 Local(
const Init& init = []{
return new T(); },
const Fin& fin =
finalize<T>());
82 operator const T&()
const {
return get(); }
83 operator T&() {
return get(); }
89 const T&
get()
const {
const_cast<Local*
>(
this)->
get(); }
148 Thread(
const Entry& entry,
int stackSize = 0);
153 Thread& operator=(Thread&& rhs);
156 static Thread&
current() {
return static_cast<Thread&
>(Super::current()); }
192 ThreadId
threadId()
const {
return Super::threadId(); }
198 typedef thread::priv::StoreId StoreId;
199 typedef thread::priv::Store Store;
202 Thread(
bool external,
int stackSize);
209 static StoreId allocStore();
211 static void freeStore(StoreId
id);
213 Store& store(
const StoreId&
id) {
const_cast<Thread*
>(
this)->expandStore(
id.
id);
return _stores[
id.id]; }
215 void expandStore(
int id) {
if (
id < (
int)_stores.size())
return; _stores.resize(
id*2+1); }
218 UniquePtr<SpinLock> _lock;
221 UniquePtr<ConditionLock> _doneCond;
222 UniquePtr<ConditionLock> _sleepCond;
224 bool _interruptEnable;
225 Exception::ConstPtr _interruptEx;
226 Condition* _interruptCond;
227 Mutex* _interruptMutex;
229 vector<Store> _stores;
236 vector<StoreId> storeIds;
238 UniquePtr<SpinLock> storeLock;
240 static mt_global(Static, getStatic,);
257 if (store.reclaim != _id.reclaim)
260 store.reclaim = _id.reclaim;
266 return *
reinterpret_cast<T*
>(store.ptr);
T & operator*()
Definition: Thread.h:87
static int priorityMin()
Definition: Thread.h:180
void yield()
Give up this thread's time slice to allow other threads to execute.
Definition: Thread.h:52
bool interruptEnabled()
Check whether interrupts are enabled for this thread.
Definition: Thread.cpp:44
const T & operator*() const
Definition: Thread.h:86
friend class platform::Thread
Definition: Thread.h:131
static TimePoint now()
Get current time.
Definition: Clock.h:48
Thread class.
Definition: Thread.h:128
static int priorityNormal()
Definition: Thread.h:179
A thread lock where the lock is acquired through a busy wait loop.
Definition: Spin.h:17
InterruptEnable(bool enable)
Definition: Thread.cpp:58
Inherit to declare that class is not copyable.
Definition: Meta.h:286
void setPriority(int priority)
Set thread execution scheduling priority.
Definition: Thread.h:185
~InterruptEnable()
Definition: Thread.cpp:63
static int concurrency()
Get number of threads that can be executed concurrently on the device.
Definition: Thread.h:195
Super::TimePoint TimePoint
Definition: Clock.h:40
void spin(int count)
Suspend this thread momentarily without giving up its time slice. The thread will pause count times...
Definition: Thread.h:60
Thread(const Entry &entry, int stackSize=0)
Construct a thread.
Definition: Thread.cpp:89
function< void(T *)> Fin
Definition: Thread.h:73
const T & get() const
Definition: Thread.h:89
Lock that is bound to a single condition. This is the common usage case of condition variables...
Definition: Lock.h:33
void pause()
Perform a no-op without giving up this thread's time slice. This no-op momentarily frees resources fo...
Definition: Thread.h:54
static int priorityMax()
Definition: Thread.h:181
#define EXCEPTION(Class)
Declares methods required for every subclass of honey::Exception.
Definition: Exception.h:17
void sleep(MonoClock::Duration time)
Suspend this thread for an amount of time.
Definition: Thread.cpp:35
Super::ThreadId ThreadId
Definition: Thread.h:140
const T * operator->() const
Definition: Thread.h:84
Local & operator=(const T &rhs)
Assign thread-local object to rhs.
Definition: Thread.h:80
static Thread & current()
Get the thread object of the calling thread.
Definition: Thread.h:156
function< T *()> Init
Definition: Thread.h:72
~Local()
Definition: Thread.h:250
Local thread storage. Multiple threads can access one Local object, but each thread will only see its...
Definition: Thread.h:69
Interrupted exception.
Definition: Thread.h:101
Base exception class. Exceptions inherited from this class provide debug info and can be thrown polym...
Definition: Exception.h:45
Enable / disable interrupts in the current thread's scope.
Definition: Thread.h:113
Functor to delete a pointer.
Definition: Allocator.h:161
T * operator->()
Definition: Thread.h:85
static const ThreadId threadIdInvalid
Invalid thread id.
Definition: Thread.h:190
void interrupt(const Exception::ConstPtr &e=new thread::Interrupted)
Request an interrupt in the thread.
Definition: Thread.cpp:156
friend class thread::priv::InterruptWait
Definition: Thread.h:137
ThreadId threadId() const
Get the thread unique platform id.
Definition: Thread.h:192
int getPriority() const
Get thread execution scheduling priority.
Definition: Thread.h:187
TimePoint::Duration Duration
Definition: Clock.h:41
Local(const Init &init=[]{return new T();}, const Fin &fin=finalize< T >())
Init / Finalize func is called once per thread to create/destroy local object instance.
Definition: Thread.h:247
bool interruptRequested() const
Check whether an interrupt has been requested for the thread.
Definition: Thread.cpp:167
bool join(MonoClock::Duration time)
Try to join for an amount of time. Returns true if joined and thread execution is complete...
Definition: Thread.h:164
Global Honeycomb namespace.
void join()
Wait until thread execution is complete.
Definition: Thread.h:162
void interruptPoint()
Throw an exception if interrupt is enabled and has been requested in this thread. ...
Definition: Thread.cpp:46
function< void()> Entry
Definition: Thread.h:141
void start()
Begin execution of this thread. The entry function will be called.
Definition: Thread.cpp:128