From c302f5ae5f9e30a28e487e8a764d9cc31546bbea Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 29 Mar 2019 22:51:04 +0800 Subject: *rename --- .../threading/binding/_coroutine.cpp | 40 --- .../asura-lib-utils/threading/binding/_thread.cpp | 210 ---------------- .../libs/asura-lib-utils/threading/coroutine.cpp | 16 -- source/libs/asura-lib-utils/threading/coroutine.h | 40 --- source/libs/asura-lib-utils/threading/mutex.cpp | 106 -------- source/libs/asura-lib-utils/threading/mutex.h | 126 ---------- .../libs/asura-lib-utils/threading/semaphore.cpp | 88 ------- source/libs/asura-lib-utils/threading/semaphore.h | 70 ------ source/libs/asura-lib-utils/threading/task.cpp | 12 - source/libs/asura-lib-utils/threading/task.h | 45 ---- source/libs/asura-lib-utils/threading/thread.cpp | 272 --------------------- source/libs/asura-lib-utils/threading/thread.h | 221 ----------------- .../threading/thread_impl_posix.cpp | 9 - .../asura-lib-utils/threading/thread_impl_posix.h | 0 .../asura-lib-utils/threading/thread_impl_sdl.cpp | 0 .../asura-lib-utils/threading/thread_impl_sdl.h | 0 .../asura-lib-utils/threading/thread_impl_std.cpp | 0 .../asura-lib-utils/threading/thread_impl_std.h | 43 ---- .../threading/thread_impl_win32.cpp | 76 ------ .../asura-lib-utils/threading/thread_impl_win32.h | 47 ---- .../libs/asura-lib-utils/threading/thread_task.cpp | 0 .../libs/asura-lib-utils/threading/thread_task.h | 44 ---- 22 files changed, 1465 deletions(-) delete mode 100644 source/libs/asura-lib-utils/threading/binding/_coroutine.cpp delete mode 100644 source/libs/asura-lib-utils/threading/binding/_thread.cpp delete mode 100644 source/libs/asura-lib-utils/threading/coroutine.cpp delete mode 100644 source/libs/asura-lib-utils/threading/coroutine.h delete mode 100644 source/libs/asura-lib-utils/threading/mutex.cpp delete mode 100644 source/libs/asura-lib-utils/threading/mutex.h delete mode 100644 source/libs/asura-lib-utils/threading/semaphore.cpp delete mode 100644 source/libs/asura-lib-utils/threading/semaphore.h delete mode 100644 source/libs/asura-lib-utils/threading/task.cpp delete mode 100644 source/libs/asura-lib-utils/threading/task.h delete mode 100644 source/libs/asura-lib-utils/threading/thread.cpp delete mode 100644 source/libs/asura-lib-utils/threading/thread.h delete mode 100644 source/libs/asura-lib-utils/threading/thread_impl_posix.cpp delete mode 100644 source/libs/asura-lib-utils/threading/thread_impl_posix.h delete mode 100644 source/libs/asura-lib-utils/threading/thread_impl_sdl.cpp delete mode 100644 source/libs/asura-lib-utils/threading/thread_impl_sdl.h delete mode 100644 source/libs/asura-lib-utils/threading/thread_impl_std.cpp delete mode 100644 source/libs/asura-lib-utils/threading/thread_impl_std.h delete mode 100644 source/libs/asura-lib-utils/threading/thread_impl_win32.cpp delete mode 100644 source/libs/asura-lib-utils/threading/thread_impl_win32.h delete mode 100644 source/libs/asura-lib-utils/threading/thread_task.cpp delete mode 100644 source/libs/asura-lib-utils/threading/thread_task.h (limited to 'source/libs/asura-lib-utils/threading') diff --git a/source/libs/asura-lib-utils/threading/binding/_coroutine.cpp b/source/libs/asura-lib-utils/threading/binding/_coroutine.cpp deleted file mode 100644 index 7f74cca..0000000 --- a/source/libs/asura-lib-utils/threading/binding/_coroutine.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "../coroutine.h" - -using namespace std; - -namespace AsuraEngine -{ - namespace Threading - { - - LUAX_REGISTRY(Coroutine) - { - LUAX_REGISTER_METHODS(state, - { "New", _New }, - { "Run", _Run } - ); - } - - LUAX_POSTPROCESS(Coroutine) - { - - } - - // Coroutine.New() - LUAX_IMPL_METHOD(Coroutine, _New) - { - LUAX_STATE(L); - - return 0; - } - - // coroutine:Run() - LUAX_IMPL_METHOD(Coroutine, _Run) - { - LUAX_PREPARE(L, Coroutine); - - return 0; - } - - } -} diff --git a/source/libs/asura-lib-utils/threading/binding/_thread.cpp b/source/libs/asura-lib-utils/threading/binding/_thread.cpp deleted file mode 100644 index a5aff03..0000000 --- a/source/libs/asura-lib-utils/threading/binding/_thread.cpp +++ /dev/null @@ -1,210 +0,0 @@ -#include "../thread.h" - -using namespace std; - -namespace AsuraEngine -{ - namespace Threading - { - - LUAX_REGISTRY(Thread) - { - LUAX_REGISTER_METHODS(state, - { "New", _New }, - { "AddTask", _AddTask }, - { "Start", _Start }, - { "Idle", _Idle }, - { "Pause", _Pause }, - { "Resume", _Resume }, - { "Stop", _Stop }, - { "Join", _Join }, - { "IsRunning", _IsRunning }, - { "IsPaused", _IsPaused }, - { "IsStopped", _IsStopped }, - { "IsCurrent", _IsCurrent }, - { "Sleep", _Sleep }, - { "Post", _Post }, - { "GetName", _GetName }, - { "GetType", _GetType }, - { "GetState", _GetState } - ); - } - - LUAX_POSTPROCESS(Thread) - { - LUAX_REGISTER_ENUM(state, "EThreadType", - { "DEFERRED", THREAD_TYPE_DEFERRED }, - { "IMMEDIATE", THREAD_TYPE_IMMEDIATE } - ); - LUAX_REGISTER_ENUM(state, "EThreadState", - { "READY", THREAD_STATE_IDLE }, - { "RUNNING", THREAD_STATE_RUNNING }, - { "PAUSED", THREAD_STATE_PAUSED }, - { "STOPPED", THREAD_STATE_STOPPED } - ); - } - - // thread = Thread.New(thread_type, sleepTime, name) - LUAX_IMPL_METHOD(Thread, _New) - { - LUAX_STATE(L); - - ThreadType type = (ThreadType)state.GetValue(1, THREAD_TYPE_DEFERRED); - uint sleepTime = state.GetValue(2,1); - cc8* name = state.GetValue(3, ""); - - Thread* thread = new Thread(state, type, sleepTime, name); - thread->PushLuaxUserdata(state); - - return 1; - } - - // thread:AddTask(task) - LUAX_IMPL_METHOD(Thread, _AddTask) - { - LUAX_PREPARE(L, Thread); - - Task* task = state.GetUserdata(2); - self->AddTask(task); - self->LuaxRetain(state, task); - return 0; - } - - // successed = thread:Start(isDeamon, stackSize) - LUAX_IMPL_METHOD(Thread, _Start) - { - LUAX_PREPARE(L, Thread); - - bool isDaemon = state.GetValue(2, true); - uint stackSize = state.GetValue(3, 0); - - state.Push(self->Start(isDaemon, stackSize)); - return 1; - } - - // thread:Idle() - LUAX_IMPL_METHOD(Thread, _Idle) - { - LUAX_PREPARE(L, Thread); - self->Idle(); - return 0; - } - - // thread:Pause() - LUAX_IMPL_METHOD(Thread, _Pause) - { - LUAX_PREPARE(L, Thread); - self->Pause(); - return 0; - } - - // thread:Resume() - LUAX_IMPL_METHOD(Thread, _Resume) - { - LUAX_PREPARE(L, Thread); - self->Resume(); - return 0; - } - - // thread:Stop() - LUAX_IMPL_METHOD(Thread, _Stop) - { - LUAX_PREPARE(L, Thread); - self->Stop(); - return 0; - } - - // thread:Join() - LUAX_IMPL_METHOD(Thread, _Join) - { - LUAX_PREPARE(L, Thread); - self->Join(); - return 0; - } - - // thread:IsRunning() - LUAX_IMPL_METHOD(Thread, _IsRunning) - { - LUAX_PREPARE(L, Thread); - state.Push(self->IsRunning()); - return 1; - } - - // thread:IsPaused() - LUAX_IMPL_METHOD(Thread, _IsPaused) - { - LUAX_PREPARE(L, Thread); - state.Push(self->IsPaused()); - return 1; - } - - // thread:IsStopped() - LUAX_IMPL_METHOD(Thread, _IsStopped) - { - LUAX_PREPARE(L, Thread); - state.Push(self->IsStopped()); - return 1; - } - - // thread:IsCurrent() - LUAX_IMPL_METHOD(Thread, _IsCurrent) - { - LUAX_PREPARE(L, Thread); - state.Push(self->IsCurrent()); - return 1; - } - - // Thread.Sleep(ms) - LUAX_IMPL_METHOD(Thread, _Sleep) - { - LUAX_STATE(L); - uint ms = state.GetValue(1, 0); -#ifdef _WIN32 - ::Sleep(ms); -#endif - return 0; - } - - // thread:Post() - LUAX_IMPL_METHOD(Thread, _Post) - { - LUAX_PREPARE(L, Thread); - self->Post(); - return 0; - } - - // thread:GetName() - LUAX_IMPL_METHOD(Thread, _GetName) - { - LUAX_PREPARE(L, Thread); - state.Push(self->GetName()); - return 1; - } - - // thread:GetType() - LUAX_IMPL_METHOD(Thread, _GetType) - { - LUAX_PREPARE(L, Thread); - state.Push(self->mType); - return 1; - } - - // thread:GetState() - LUAX_IMPL_METHOD(Thread, _GetState) - { - LUAX_PREPARE(L, Thread); - state.Push(self->mState); - return 1; - } - - // thread:SetSleepTime(sleepTime) - LUAX_IMPL_METHOD(Thread, _SetSleepTime) - { - LUAX_PREPARE(L, Thread); - uint time = state.CheckValue(2); - self->SetSleepTime(time); - return 0; - } - - } -} diff --git a/source/libs/asura-lib-utils/threading/coroutine.cpp b/source/libs/asura-lib-utils/threading/coroutine.cpp deleted file mode 100644 index 9f65c5f..0000000 --- a/source/libs/asura-lib-utils/threading/coroutine.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "coroutine.h" - -namespace AsuraEngine -{ - namespace Threading - { -/* - Coroutine::Coroutine() - { - - } -*/ - - - } -} diff --git a/source/libs/asura-lib-utils/threading/coroutine.h b/source/libs/asura-lib-utils/threading/coroutine.h deleted file mode 100644 index 01af654..0000000 --- a/source/libs/asura-lib-utils/threading/coroutine.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef __ASURA_COROUTINE_H__ -#define __ASURA_COROUTINE_H__ - -#include "../scripting/portable.hpp" - -namespace AsuraEngine -{ - namespace Threading - { - - /// - /// lua协程,用来做一些逻辑并发操作。 - /// - class Coroutine ASURA_FINAL - : public AEScripting::Portable - { - public: - - LUAX_DECL_FACTORY(Coroutine); - - - - private: - - /// - /// 当前协程的state - /// - lua_State* mThreadState; - - LUAX_DECL_METHOD(_New); - LUAX_DECL_METHOD(_Run); - - }; - - } -} - -namespace AEThreading = AsuraEngine::Threading; - -#endif \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/mutex.cpp b/source/libs/asura-lib-utils/threading/mutex.cpp deleted file mode 100644 index 663ac28..0000000 --- a/source/libs/asura-lib-utils/threading/mutex.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include - -#include "mutex.h" - -namespace AsuraEngine -{ - namespace Threading - { - -#define try_create_mutex(impl)\ - if (!mImpl) \ - { \ - try \ - { \ - mImpl = new impl(); \ - } \ - catch (Exception& e) \ - { \ - mImpl = nullptr; \ - } \ - } - - Mutex::Mutex() - : mImpl(nullptr) - { -#if ASURA_MUTEX_WIN32_CRITICLE_SECTION - try_create_mutex(MutexImplWin32_CS); -#endif -#if ASURA_MUTEX_WIN32_KERNAL_MUTEX - try_create_mutex(MutexImplWin32_KM); -#endif - ASSERT(mImpl); - } - - Mutex::~Mutex() - { - delete mImpl; - } - - void Mutex::Lock() - { - ASSERT(mImpl); - - mImpl->Lock(); - } - - void Mutex::Unlock() - { - ASSERT(mImpl); - - mImpl->Unlock(); - } - -#if ASURA_MUTEX_WIN32_CRITICLE_SECTION - - MutexImplWin32_CS::MutexImplWin32_CS() - { - ::InitializeCriticalSection(&mMutex); - } - - MutexImplWin32_CS::~MutexImplWin32_CS() - { - ::DeleteCriticalSection(&mMutex); - } - - void MutexImplWin32_CS::Lock() - { - ::EnterCriticalSection(&mMutex); - } - - void MutexImplWin32_CS::Unlock() - { - ::LeaveCriticalSection(&mMutex); - } - -#endif // ASURA_MUTEX_WIN32_CRITICLE_SECTION - -#if ASURA_MUTEX_WIN32_KERNAL_MUTEX - - MutexImplWin32_KM::MutexImplWin32_KM() - { - mHandle = ::CreateMutex(NULL, FALSE, NULL); - if (!mHandle) - throw Exception("Cant use win32 mutex."); - } - - MutexImplWin32_KM::~MutexImplWin32_KM() - { - ::CloseHandle(mHandle); - mHandle = NULL; - } - - void MutexImplWin32_KM::Lock() - { - ::WaitForSingleObject(mHandle, INFINITE); - } - - void MutexImplWin32_KM::Unlock() - { - ::ReleaseMutex(mHandle); - } - -#endif // ASURA_MUTEX_WIN32_KERNAL_MUTEX - - } -} diff --git a/source/libs/asura-lib-utils/threading/mutex.h b/source/libs/asura-lib-utils/threading/mutex.h deleted file mode 100644 index 7e7d877..0000000 --- a/source/libs/asura-lib-utils/threading/mutex.h +++ /dev/null @@ -1,126 +0,0 @@ -#ifndef __ASURA_MUTEX_H__ -#define __ASURA_MUTEX_H__ - -#include - -#include "../utils_config.h" - -#if ASURA_THREAD_WIN32 - #include -#endif - -namespace AsuraEngine -{ - namespace Threading - { - - class MutexImpl; - - class Mutex - { - public: - - Mutex(); - ~Mutex(); - - void Lock(); - void Unlock(); - - private: - - MutexImpl* mImpl; - - }; - - class _mutex_locker - { - public: - _mutex_locker(Mutex& mutex) - : m(mutex) - { - m.Lock(); - }; - ~_mutex_locker() - { - m.Unlock(); - } - private: - void* operator new(size_t); - Mutex& m; - }; - -// 将所在的栈从此位置开始到退栈处作为临界区锁定。 -#define lock(mutex) _mutex_locker _asura_scoped_lock_0x0(mutex) -#define lock2(mutex) _mutex_locker _asura_scoped_lock_0x1(mutex) -#define lock3(mutex) _mutex_locker _asura_scoped_lock_0x2(mutex) -#define lock4(mutex) _mutex_locker _asura_scoped_lock_0x3(mutex) -#define lock5(mutex) _mutex_locker _asura_scoped_lock_0x4(mutex) - - ASURA_ABSTRACT class MutexImpl - { - public: - - MutexImpl() {}; - virtual ~MutexImpl() {}; - - virtual void Lock() = 0; - virtual void Unlock() = 0; - - }; - -#if ASURA_MUTEX_WIN32_CRITICLE_SECTION - - //https://blog.csdn.net/l799623787/article/details/18259949 - class MutexImplWin32_CS ASURA_FINAL : public MutexImpl - { - public: - - MutexImplWin32_CS(); - ~MutexImplWin32_CS(); - - void Lock() override; - void Unlock() override; - - private: - - //HANDLE mHandle; - CRITICAL_SECTION mMutex; - - }; - -#endif // ASURA_MUTEX_WIN32_CRITICLE_SECTION - -#if ASURA_MUTEX_WIN32_KERNAL_MUTEX - - class MutexImplWin32_KM ASURA_FINAL : public MutexImpl - { - public: - - MutexImplWin32_KM(); - ~MutexImplWin32_KM(); - - void Lock() override; - void Unlock() override; - - private: - - HANDLE mHandle; - - }; - -#endif // ASURA_MUTEX_WIN32_KERNAL_MUTEX - -#if ASURA_THREAD_STD - - class MutexImplSTD ASURA_FINAL : public MutexImpl - { - }; - -#endif // ASURA_THREAD_STD - - } -} - -namespace AEThreading = AsuraEngine::Threading; - -#endif \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/semaphore.cpp b/source/libs/asura-lib-utils/threading/semaphore.cpp deleted file mode 100644 index d59ec78..0000000 --- a/source/libs/asura-lib-utils/threading/semaphore.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "../exceptions/exception.h" -#include "../type.h" - -#include "semaphore.h" - -namespace AsuraEngine -{ - namespace Threading - { - -#define try_create_semaphore(impl) \ - if (!mImpl) \ - { \ - try \ - { \ - mImpl = new impl(init_count); \ - } \ - catch (Exception& e) \ - { \ - mImpl = nullptr; \ - } \ - } - - Semaphore::Semaphore(unsigned int init_count) - : mImpl(nullptr) - { -#ifdef ASURA_THREAD_WIN32 - try_create_semaphore(SemaphoreWin32); -#endif - ASSERT(mImpl); - } - - Semaphore::~Semaphore() - { - if (mImpl) delete mImpl; - } - - void Semaphore::Signal() - { - ASSERT(mImpl); - mImpl->Signal(); - } - - void Semaphore::Wait(int timeout) - { - ASSERT(mImpl); - mImpl->Wait(timeout); - } - -#if ASURA_THREAD_WIN32 - - SemaphoreWin32::SemaphoreWin32(unsigned int init_value) - : SemaphoreImpl(init_value) - { - mSem = CreateSemaphore(NULL, init_value, UINT_MAX, NULL); - if (!mSem) - throw Exception("Cant use win32 semaphore."); - } - - SemaphoreWin32::~SemaphoreWin32() - { - CloseHandle(mSem); - } - - void SemaphoreWin32::Signal() - { - InterlockedIncrement(&mCount); - if (ReleaseSemaphore(mSem, 1, NULL) == FALSE) - InterlockedDecrement(&mCount); - } - - bool SemaphoreWin32::Wait(int timeout) - { - int result; - result = WaitForSingleObject(mSem, timeout < 0 ? INFINITE : timeout); - if (result == WAIT_OBJECT_0) - { - InterlockedDecrement(&mCount); - return true; - } - else - return false; - } - -#endif // ASURA_THREAD_WIN32 - - } -} \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/semaphore.h b/source/libs/asura-lib-utils/threading/semaphore.h deleted file mode 100644 index 80773d8..0000000 --- a/source/libs/asura-lib-utils/threading/semaphore.h +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef __ASURA_SEMAPHORE_H__ -#define __ASURA_SEMAPHORE_H__ - -#include "../utils_config.h" - -#if ASURA_THREAD_WIN32 -#include -#endif - -namespace AsuraEngine -{ - namespace Threading - { - - class SemaphoreImpl; - - /// - /// 信号量 - /// - class Semaphore - { - public: - - Semaphore(unsigned int init_count = 1); - ~Semaphore(); - - void Signal(); - void Wait(int timeout = 0); - - private: - SemaphoreImpl* mImpl; - }; - - class SemaphoreImpl - { - public: - SemaphoreImpl(unsigned int init_value) - : mCount(init_value) - { - }; - virtual ~SemaphoreImpl() {}; - virtual void Signal() = 0; - virtual bool Wait(int timeout) = 0; - inline int Current() { return mCount; } - protected: - unsigned int mCount; - }; - -#define wait(sem) sem.Wait(); -#define signal(sem) sem.Signal(); - -#if ASURA_THREAD_WIN32 - - class SemaphoreWin32 : public SemaphoreImpl - { - public: - SemaphoreWin32(unsigned int init_value); - ~SemaphoreWin32(); - void Signal() override; - bool Wait(int timeout) override; - private: - HANDLE mSem; - }; - -#endif // ASURA_THREAD_WIN32 - - } -} - -#endif \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/task.cpp b/source/libs/asura-lib-utils/threading/task.cpp deleted file mode 100644 index 2e84ed4..0000000 --- a/source/libs/asura-lib-utils/threading/task.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "task.h" -#include "../scripting/lua_env.h" - -using namespace AEScripting; - -namespace AsuraEngine -{ - namespace Threading - { - - } -} diff --git a/source/libs/asura-lib-utils/threading/task.h b/source/libs/asura-lib-utils/threading/task.h deleted file mode 100644 index fb7aa5f..0000000 --- a/source/libs/asura-lib-utils/threading/task.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef __ASURA_THRAD_TASK_H__ -#define __ASURA_THRAD_TASK_H__ - -#include -#include - -namespace AsuraEngine -{ - namespace Threading - { - - /// - /// 希望放在另一个线程处理的任务,继承Task并重写Execute方法。 - /// - ASURA_ABSTRACT class Task - : public virtual AEScripting::NativeAccessor - { - public: - - Task() {}; - virtual ~Task() {}; - - /// - /// 执行任务,完成后返回true,调用回调函数。 - /// - virtual bool Execute() = 0; - - /// - /// 调用回调。在invoke thread里面回调。 - /// - virtual void Invoke(lua_State* invokeThreaad) = 0; - - protected: - - // 取回调函数 - Luax::LuaxMemberRef mCallback; - - }; - - } -} - -namespace AEThreading = AsuraEngine::Threading; - -#endif \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/thread.cpp b/source/libs/asura-lib-utils/threading/thread.cpp deleted file mode 100644 index 0f4f5da..0000000 --- a/source/libs/asura-lib-utils/threading/thread.cpp +++ /dev/null @@ -1,272 +0,0 @@ -#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 - { - - Thread::Thread(lua_State* luaThread, ThreadType type /*= THREAD_TYPE_DEFERRED*/, uint sleepTime /*= 0*/, const std::string& name /*= ""*/) - : mName(name) - , mState(THREAD_STATE_IDLE) - , mType(type) - , mLuaThread(luaThread) - , mCallbackThread(nullptr) - , mSleepTime(sleepTime) - { - LUAX_STATE(luaThread); - if (type == THREAD_TYPE_IMMEDIATE) - { - Luax::LuaxVM* vm = state.GetVM(); - ASSERT(vm); - mCallbackThread = vm->CreateThread(); - ASSERT(mCallbackThread); - SetLuaxMemberRef(state, mCallbackThreadRef, -1); - state.Pop(); // callback thread - } - } - - Thread::~Thread() - { - if (mImpl) - { - delete mImpl; - mImpl = nullptr; - } - } - - bool Thread::AddTask(Task* task) - { - lock(mTaskQueueMutex); - mTaskQueue.push(task); - return true; - } - - uint Thread::GetTaskCount() - { - return mTaskQueue.size(); - } - - void Thread::Idle() - { - mState = THREAD_STATE_IDLE; - } - -#define try_start_thread(impl)\ - if (!mImpl) \ - { \ - mImpl = new impl(); \ - if (!mImpl->Start(this, stacksize)) \ - { \ - delete mImpl; \ - mImpl = nullptr; \ - } \ - } - - bool Thread::Start(bool isDaemon /*= true*/, uint32 stacksize /*= 0*/) - { - if (mState != THREAD_STATE_IDLE) - return false; - - // 如果已经存在一个之前创建的,关闭它。 - if (mImpl) - { - delete mImpl; - mImpl = nullptr; - } - -#if ASURA_THREAD_WIN32 - try_start_thread(ThreadImplWin32); -#endif - - if (!mImpl) - return false; - - mIsDaemon = isDaemon; - mStateMutex.Lock(); - mState = THREAD_STATE_RUNNING; - mStateMutex.Unlock(); - } - - void Thread::Pause() - { - ASSERT(mImpl); - - lock(mStateMutex); - mState = THREAD_STATE_PAUSED; - } - - void Thread::Resume() - { - ASSERT(mImpl); - - lock(mStateMutex); - if(mState == THREAD_STATE_PAUSED) - mState = THREAD_STATE_RUNNING; - } - - void Thread::Stop() - { - ASSERT(mImpl); - - lock(mStateMutex); - mState = THREAD_STATE_STOPPED; - } - - void Thread::PauseSync() - { - Pause(); - wait(mSemPause); - } - - void Thread::ResumeSync() - { - Resume(); - wait(mSemResume); - } - - void Thread::StopSync() - { - Stop(); - wait(mSemStop); - } - - void Thread::Join() - { - ASSERT(mImpl); - mImpl->Join(); - } - - ThreadState Thread::GetState() - { - ThreadState state; - mStateMutex.Lock(); - state = mState; - mStateMutex.Unlock(); - return state; - } - - bool Thread::IsRunning() - { - ASSERT(mImpl); - - return GetState() == THREAD_STATE_RUNNING; - } - - bool Thread::IsPaused() - { - ASSERT(mImpl); - - return GetState() == THREAD_STATE_PAUSED; - } - - bool Thread::IsStopped() - { - ASSERT(mImpl); - - return GetState() == THREAD_STATE_STOPPED; - } - - bool Thread::IsCurrent() - { - ASSERT(mImpl); - - return mImpl->IsCurrent(); - } - - const std::string& Thread::GetName() - { - return mName; - } - - void Thread::Process() - { - LUAX_STATE(mLuaThread); - - do{ - if (IsRunning()) - { - while (!mTaskQueue.empty()) - { - Task* task = mTaskQueue.front(); - if (task && task->Execute()) - { - if (mType == THREAD_TYPE_DEFERRED) - { - mFinishedMutex.Lock(); - mFinishedTasks.push(task); - mFinishedMutex.Unlock(); - } - else if (mType == THREAD_TYPE_IMMEDIATE) - { - task->Invoke(mCallbackThread); - this->LuaxRelease(state, task); - } - mTaskQueueMutex.Lock(); - mTaskQueue.pop(); - mTaskQueueMutex.Unlock(); - } - } - } - - // 退出循环 - if (IsStopped()) - break; - - // 降低CPU使用率 - Sleep(mSleepTime); - - } while (mIsDaemon); - - // 非守护线程,先切到stop状态 - if (!mIsDaemon) - Stop(); - - signal(mSemStop); - - // 重置状态为Idle - Idle(); - } - - /// - /// 延迟模式主动发布回调、 - /// - void Thread::Post() - { - ASSERT(mType == THREAD_TYPE_DEFERRED); - - LUAX_STATE(mLuaThread); - while (!mFinishedTasks.empty()) - { - Task* task = mFinishedTasks.front(); - if (task) - { - task->Invoke(mLuaThread); - this->LuaxRelease(state, task); - mFinishedMutex.Lock(); - mFinishedTasks.pop(); - mFinishedMutex.Unlock(); - } - } - } - - void Thread::Sleep(uint ms) - { - ASSERT(mImpl); - if (mImpl) - { - mImpl->Sleep(ms); - } - } - - void Thread::SetSleepTime(uint ms) - { - mSleepTime = ms; - } - - } -} \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/thread.h b/source/libs/asura-lib-utils/threading/thread.h deleted file mode 100644 index 0e75770..0000000 --- a/source/libs/asura-lib-utils/threading/thread.h +++ /dev/null @@ -1,221 +0,0 @@ -#ifndef __ASURA_THREAD_H__ -#define __ASURA_THREAD_H__ - -#include -#include - -#include - -#include "task.h" -#include "mutex.h" -#include "semaphore.h" - -namespace AsuraEngine -{ - namespace Threading - { - - class ThreadImpl; - - /// - /// 线程的几种不同的实现: - /// 1: Deferred(延迟模式),线程上的任务完成后,需要手动在主线程调用Post方法, - /// 在主线程调回调函数,将发布从异步改为同步操作,解决主lua_State冲突的问题。 - /// 2: Immediate(立即模式),每一个线程维护一个lua_newthread创建出来的lua_State。 - /// 回调函数在不同的lua_State中调用,避免不同的线程访问同一个lua_State。 - /// - enum ThreadType - { - THREAD_TYPE_DEFERRED, - THREAD_TYPE_IMMEDIATE, - }; - - enum ThreadState - { - THREAD_STATE_IDLE, ///< 闲置,还未创建内核对象 - THREAD_STATE_RUNNING, ///< 正在运行循环 - THREAD_STATE_PAUSED, ///< 在循环中暂停 - THREAD_STATE_STOPPED, ///< 退出循环 - }; - - /// - /// 线程主体,每个线程维护一个task queue。 - /// - class Thread ASURA_FINAL - : public AEScripting::Portable - { - public: - - LUAX_DECL_FACTORY(Thread); - - Thread(lua_State* luaThread, ThreadType type = THREAD_TYPE_DEFERRED, uint sleepTime = 1, const std::string& name = ""); - ~Thread(); - - bool AddTask(Task* task); - /// - /// 获得等待处理的任务数 - /// - uint GetTaskCount(); - - void Idle(); - - /// - /// 创建内核对象,并运行。如果是daemon,会等待手动stop。否则会在某时刻队列完成后自动stop。 - /// - bool Start(bool daemon = true, uint32 stacksize = 0); - - /// - /// 非同步线程控制,不是实时的。可能需要在主线程里使用Is函数确认到达指定状态。 - /// - void Pause(); - void Resume(); - void Stop(); - - /// - /// 同步线程控制,会等返回来信号后继续向下执行。会造成主线程等待。 - /// - void PauseSync(); - void ResumeSync(); - void StopSync(); - - /// - /// 父线程等待本线程结束后才继续执行。 - /// - void Join(); - - ThreadState GetState(); - - /// - /// 逻辑层面的线程状态: - /// 1: Idle(空闲),线程创建后的默认状态,可以随时加任务并且Start。 - /// 2: Running(运行),内核对象呗创建,已经处于内核调度中,并处理具体Task。 - /// 3: Paused(暂停),依然存在于内核中,但是跳过了对任务的处理,逻辑上暂停。 - /// 4: Stopped(停止),依然存在于内核中,但是已经无法继续处理任务。 - /// - bool IsIdle(); - bool IsRunning(); - bool IsPaused(); - bool IsStopped(); - - bool IsCurrent(); - - /// - /// 执行任务队列。 - /// - void Process(); - - const std::string& GetName(); - - /// - /// 回调。 - /// - void Post(); - - /// - /// 休眠函数 - /// - void Sleep(uint ms); - - /// - /// 设置休眠时间 - /// - void SetSleepTime(uint ms); - - private: - - //----------------------------------------------------------------------------// - - LUAX_DECL_ENUM(ThreadType); - LUAX_DECL_ENUM(ThreadState); - - LUAX_DECL_METHOD(_New); - LUAX_DECL_METHOD(_AddTask); - LUAX_DECL_METHOD(_Start); - LUAX_DECL_METHOD(_Idle); - LUAX_DECL_METHOD(_Pause); - LUAX_DECL_METHOD(_Resume); - LUAX_DECL_METHOD(_Stop); - LUAX_DECL_METHOD(_Join); - LUAX_DECL_METHOD(_IsRunning); - LUAX_DECL_METHOD(_IsPaused); - LUAX_DECL_METHOD(_IsStopped); - LUAX_DECL_METHOD(_IsCurrent); - LUAX_DECL_METHOD(_Sleep); - LUAX_DECL_METHOD(_Post); - LUAX_DECL_METHOD(_GetName); - LUAX_DECL_METHOD(_GetType); - LUAX_DECL_METHOD(_GetState); - LUAX_DECL_METHOD(_SetSleepTime); - - //----------------------------------------------------------------------------// - - /// - /// 此次运行是否是守护模式。 - /// - bool mIsDaemon; - - lua_State* mLuaThread; - - ThreadImpl* mImpl; - std::string mName; - ThreadType mType; - uint mSleepTime; - - ThreadState mState; - Mutex mStateMutex; - - /// - /// 同步控制相关的信号量 - /// - Semaphore mSemPause; - Semaphore mSemResume; - Semaphore mSemStop; - - /// - /// 待处理的任务队列。 - /// - std::queue mTaskQueue; - Mutex mTaskQueueMutex; - - /// - /// 延迟模式使用 - /// - std::queue mFinishedTasks; - Mutex mFinishedMutex; - - /// - /// 立即模式使用,回调使用的lua线程 - /// - lua_State* mCallbackThread; - Luax::LuaxMemberRef mCallbackThreadRef; - - }; - - /// - /// 线程的具体实现,对用户是透明的,一共准备了四种策略: - /// 1: win32 - /// 2: posix - /// 3: SDL - /// 4: std::thread - /// - ASURA_ABSTRACT class ThreadImpl - { - public: - ThreadImpl() {}; - virtual ~ThreadImpl() {}; - - virtual bool Start(Thread* thread, uint32 stacksize = 0) = 0; - virtual void Join() = 0; - virtual void Kill() = 0; - - virtual void Sleep(uint ms) = 0; - - virtual bool IsRunning() = 0; - virtual bool IsCurrent() = 0; - - }; - - } -} - -#endif \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/thread_impl_posix.cpp b/source/libs/asura-lib-utils/threading/thread_impl_posix.cpp deleted file mode 100644 index d2ad7af..0000000 --- a/source/libs/asura-lib-utils/threading/thread_impl_posix.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "thread_impl_posix.h" - -namespace AsuraEngine -{ - namespace Threading - { - - } -} \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/thread_impl_posix.h b/source/libs/asura-lib-utils/threading/thread_impl_posix.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/libs/asura-lib-utils/threading/thread_impl_sdl.cpp b/source/libs/asura-lib-utils/threading/thread_impl_sdl.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/source/libs/asura-lib-utils/threading/thread_impl_sdl.h b/source/libs/asura-lib-utils/threading/thread_impl_sdl.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/libs/asura-lib-utils/threading/thread_impl_std.cpp b/source/libs/asura-lib-utils/threading/thread_impl_std.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/source/libs/asura-lib-utils/threading/thread_impl_std.h b/source/libs/asura-lib-utils/threading/thread_impl_std.h deleted file mode 100644 index 0e7d3da..0000000 --- a/source/libs/asura-lib-utils/threading/thread_impl_std.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef __ASURA_THREAD_STD_H__ -#define __ASURA_THREAD_STD_H__ - -#include "../utils_config.h" - -#if ASURA_THREAD_STD - -#include - -#include "thread.h" - -namespace AsuraEngine -{ - namespace Threading - { - - /// - /// Thread的std::thread实现。 - /// - class ThreadImplSTD : public ThreadImpl - { - public: - - ThreadImplSTD(); - ~ThreadImplSTD(); - - bool Start(Thread* thread, uint32 stacksize) override; - void Join() override; - void Kill() override; - - bool IsRunning() override; - bool IsCurrent() override; - - private: - - }; - - } -} - -#endif // #if ASURA_THREAD_STD - -#endif // __ASURA_THREAD_STD_H__ \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp b/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp deleted file mode 100644 index 6871c2d..0000000 --- a/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "thread_impl_win32.h" -#include "thread.h" - -#include - -namespace AsuraEngine -{ - namespace Threading - { - - static DWORD WINAPI _thread_win32_runner(LPVOID param) - { - Thread* thread = (Thread*)param; - thread->Process(); - return 0; - } - - ThreadImplWin32::ThreadImplWin32() - { - } - - ThreadImplWin32::~ThreadImplWin32() - { - if (!mHandle) return; - ::CloseHandle(mHandle); - mHandle = 0; - } - - bool ThreadImplWin32::Start(Thread* thread, uint32 stacksize/*=0*/) - { - assert(!IsRunning()); - mHandle = ::CreateThread( - NULL - , stacksize - , _thread_win32_runner - , thread - , 0 /*创建后立即进行调度*/ - , NULL); - - return mHandle; - } - - void ThreadImplWin32::Join() - { - // 父线程等待此线程返回 - ::WaitForSingleObject(mHandle, INFINITE); - } - - void ThreadImplWin32::Kill() - { - ::TerminateThread(mHandle, FALSE); - } - - void ThreadImplWin32::Sleep(uint ms) - { - ::Sleep(ms); - } - - bool ThreadImplWin32::IsRunning() - { - if (mHandle) { - DWORD exitCode = 0; - // https://blog.csdn.net/yuanmeng567/article/details/19485719 - ::GetExitCodeThread(mHandle, &exitCode); - return exitCode == STILL_ACTIVE; - } - return false; - } - - bool ThreadImplWin32::IsCurrent() - { - return mHandle == ::GetCurrentThread(); - } - - } -} \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/thread_impl_win32.h b/source/libs/asura-lib-utils/threading/thread_impl_win32.h deleted file mode 100644 index a22aeef..0000000 --- a/source/libs/asura-lib-utils/threading/thread_impl_win32.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef __ASURA_THREAD_WIN32_H__ -#define __ASURA_THREAD_WIN32_H__ - -#include "../utils_config.h" - -#if ASURA_THREAD_WIN32 - -#include - -#include "thread.h" - -namespace AsuraEngine -{ - namespace Threading - { - - /// - /// Thread的win32实现。 - /// - class ThreadImplWin32 : public ThreadImpl - { - public: - - ThreadImplWin32(); - ~ThreadImplWin32(); - - bool Start(Thread* thread, uint32 stacksize) override; - void Join() override; - void Kill() override; - - void Sleep(uint ms) override; - - bool IsRunning() override; - bool IsCurrent() override; - - private: - - HANDLE mHandle; - - }; - - } -} - -#endif // #if ASURA_THREAD_WIN32 - -#endif // __ASURA_THREAD_WIN32_H__ \ No newline at end of file diff --git a/source/libs/asura-lib-utils/threading/thread_task.cpp b/source/libs/asura-lib-utils/threading/thread_task.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/source/libs/asura-lib-utils/threading/thread_task.h b/source/libs/asura-lib-utils/threading/thread_task.h deleted file mode 100644 index 1ea0a1a..0000000 --- a/source/libs/asura-lib-utils/threading/thread_task.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef __ASURA_THRAD_TASK_H__ -#define __ASURA_THRAD_TASK_H__ - -#include -#include - -namespace AsuraEngine -{ - namespace Threading - { - - /// - /// 希望放在另一个线程处理的任务,继承Task并重写Execute方法。 - /// - ASURA_ABSTRACT class ThreadTask - : virtual public AEScripting::NativeAccessor - { - public: - - ThreadTask(); - virtual ~ThreadTask(); - - /// - /// 执行任务,完成后返回true,调用回调函数。 - /// - virtual bool Execute() = 0; - - /// - /// 调用回调。 - /// - virtual void Invoke() = 0; - - protected: - - Luax::LuaxMemberRef mCallback; - - }; - - } -} - -namespace AEThreading = AsuraEngine::Threading; - -#endif \ No newline at end of file -- cgit v1.1-26-g67d0