diff options
author | chai <chaifix@163.com> | 2019-03-27 22:18:14 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-03-27 22:18:14 +0800 |
commit | 69f7d1bd745ed5680b9bc4e3cfdd882ff2a5ad26 (patch) | |
tree | 729e563da8fea6cf8c5455f3afdb3c6ce0aecde4 /source/libs/asura-lib-utils/threading/thread.cpp | |
parent | 66c5fdc564dd892ed265132d6c1378dbe3cebcee (diff) |
+threading
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(); + } } } |