diff options
Diffstat (limited to 'source/libs/asura-lib-utils/threading/thread.cpp')
-rw-r--r-- | source/libs/asura-lib-utils/threading/thread.cpp | 81 |
1 files changed, 80 insertions, 1 deletions
diff --git a/source/libs/asura-lib-utils/threading/thread.cpp b/source/libs/asura-lib-utils/threading/thread.cpp index c77f3ab..d1b055d 100644 --- a/source/libs/asura-lib-utils/threading/thread.cpp +++ b/source/libs/asura-lib-utils/threading/thread.cpp @@ -1,13 +1,92 @@ #include "thread.h" +#include "thread_impl_win32.h" +#include "thread_impl_posix.h" +#include "thread_impl_sdl.h" +#include "thread_impl_std.h" + namespace AsuraEngine { namespace Threading { - bool Thread::Enqueue(ThreadTask* task) + Thread::Thread(const std::string& name) + : mName(name) + { + } + + Thread::~Thread() + { + delete mImpl; + } + +#define try_start_thread(impl)\ + if (!mImpl) \ + { \ + mImpl = new impl(); \ + if (!mImpl->Start(this, stacksize)) \ + { \ + delete mImpl; \ + mImpl = nullptr; \ + } \ + } + + bool Thread::AddTask(ThreadTask* task) + { + mTaskQueue.push(task); + return true; + } + + void Thread::Start(uint32 stacksize) + { +#if ASURA_THREAD_WIN32 + try_start_thread(ThreadImplWin32); +#endif + + assert(mImpl); + } + + void Thread::Join() + { + assert(mImpl); + mImpl->Join(); + } + + void Thread::Kill() + { + assert(mImpl); + mImpl->Kill(); + } + + bool Thread::IsRunning() + { + assert(mImpl); + return mImpl->IsRunning(); + } + + bool Thread::IsCurrent() + { + assert(mImpl); + return mImpl->IsCurrent(); + } + + const std::string& Thread::GetName() + { + return mName; + } + + void Thread::Execute() { + while (!mTaskQueue.empty()) + { + ThreadTask* task = mTaskQueue.front(); + if (task->Execute()) + task->Invoke(); + mMutex.Lock(); + mTaskQueue.pop(); + mMutex.Unlock(); + } } } |