From f4c338c63f3456a8eccd56c35e233843687d55be Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 29 Mar 2019 00:43:25 +0800 Subject: *thread --- .../asura-lib-utils/io/binding/_data_buffer.cpp | 18 ++++---- source/libs/asura-lib-utils/io/binding/_file.cpp | 12 ++--- .../asura-lib-utils/io/binding/_file_system.cpp | 22 ++++----- .../libs/asura-lib-utils/io/binding/_io_task.cpp | 18 ++++++-- source/libs/asura-lib-utils/io/data_buffer.cpp | 10 +++++ source/libs/asura-lib-utils/io/data_buffer.h | 3 ++ source/libs/asura-lib-utils/io/file.cpp | 10 ++++- source/libs/asura-lib-utils/io/io_task.cpp | 25 ++++++++--- source/libs/asura-lib-utils/io/io_task.h | 26 +++++++---- source/libs/asura-lib-utils/scripting/lua_env.h | 28 ++++++++++-- .../asura-lib-utils/threading/binding/_thread.cpp | 21 +++++++-- source/libs/asura-lib-utils/threading/mutex.h | 16 +++---- source/libs/asura-lib-utils/threading/task.cpp | 12 +++++ source/libs/asura-lib-utils/threading/task.h | 11 ++--- source/libs/asura-lib-utils/threading/thread.cpp | 14 +++--- source/libs/asura-lib-utils/threading/thread.h | 52 ++++++++++++++++++---- .../threading/thread_impl_posix.cpp | 9 ++++ .../threading/thread_impl_win32.cpp | 8 +++- .../libs/asura-lib-utils/threading/thread_task.cpp | 0 .../libs/asura-lib-utils/threading/thread_task.h | 44 ++++++++++++++++++ source/libs/asura-lib-utils/type.h | 8 ++-- source/libs/asura-lib-utils/utils_module.cpp | 1 + source/libs/asura-lib-utils/utils_module.h | 1 + 23 files changed, 285 insertions(+), 84 deletions(-) create mode 100644 source/libs/asura-lib-utils/threading/thread_task.cpp create mode 100644 source/libs/asura-lib-utils/threading/thread_task.h (limited to 'source/libs/asura-lib-utils') diff --git a/source/libs/asura-lib-utils/io/binding/_data_buffer.cpp b/source/libs/asura-lib-utils/io/binding/_data_buffer.cpp index c42888c..6725bc4 100644 --- a/source/libs/asura-lib-utils/io/binding/_data_buffer.cpp +++ b/source/libs/asura-lib-utils/io/binding/_data_buffer.cpp @@ -10,12 +10,12 @@ namespace AsuraEngine LUAX_REGISTRY(DataBuffer) { LUAX_REGISTER_METHODS(state, - { "New", _New }, - { "GetBuffer", _GetData }, - { "GetSize", _GetSize }, - { "Refactor", _Refactor }, - { "Load", _Load }, - { "Clear", _Clear } + { "New", _New }, + { "GetData", _GetData }, + { "GetSize", _GetSize }, + { "Refactor", _Refactor }, + { "Load", _Load }, + { "Clear", _Clear } ); } @@ -49,14 +49,14 @@ namespace AsuraEngine } } - // lsting, len = databuffer:GetBuffer() + // lsting, len = databuffer:GetData() LUAX_IMPL_METHOD(DataBuffer, _GetData) { LUAX_SETUP(L, "U"); DataBuffer* self = state.GetUserdata(1); lua_pushlstring(L, self->GetData(), self->GetSize()); - return 2; + return 1; } // length = databuffer:GetSize() @@ -74,7 +74,7 @@ namespace AsuraEngine { LUAX_PREPARE(L, DataBuffer); - size_t size = state.CheckParam(2); + size_t size = state.CheckValue(2); self->Refactor(size); return 0; } diff --git a/source/libs/asura-lib-utils/io/binding/_file.cpp b/source/libs/asura-lib-utils/io/binding/_file.cpp index 0670379..c44bc90 100644 --- a/source/libs/asura-lib-utils/io/binding/_file.cpp +++ b/source/libs/asura-lib-utils/io/binding/_file.cpp @@ -49,7 +49,7 @@ namespace AsuraEngine { LUAX_STATE(L); - cc8* name = state.CheckParam(1); + cc8* name = state.CheckValue(1); File* file = new File(name); file->PushLuaxUserdata(state); return 1; @@ -60,7 +60,7 @@ namespace AsuraEngine { LUAX_PREPARE(L, File); - File::FileMode mode = (File::FileMode)state.CheckParam(2); + File::FileMode mode = (File::FileMode)state.CheckValue(2); state.Push(self->Open(mode)); return 1; } @@ -115,7 +115,7 @@ namespace AsuraEngine DataBuffer* db = state.CheckUserdata(2); if (!db) return state.ErrorType(2, "DataBuffer"); - int len = state.CheckParam(3); + int len = state.CheckValue(3); int size = self->Read(db, len); state.Push(size); return 1; @@ -164,7 +164,7 @@ namespace AsuraEngine { LUAX_PREPARE(L, File); - int pos = state.CheckParam(2); + int pos = state.CheckValue(2); state.Push(self->Seek(pos)); return 1; } @@ -174,8 +174,8 @@ namespace AsuraEngine { LUAX_PREPARE(L, File); - BufferMode mode = (BufferMode)state.CheckParam(2); - int size = state.CheckParam(3); + BufferMode mode = (BufferMode)state.CheckValue(2); + int size = state.CheckValue(3); state.Push(self->SetBuffer(mode, size)); return 1; } diff --git a/source/libs/asura-lib-utils/io/binding/_file_system.cpp b/source/libs/asura-lib-utils/io/binding/_file_system.cpp index 7132456..3843451 100644 --- a/source/libs/asura-lib-utils/io/binding/_file_system.cpp +++ b/source/libs/asura-lib-utils/io/binding/_file_system.cpp @@ -46,7 +46,7 @@ namespace AsuraEngine { PREPARE(L); - const char* arg0 = state.CheckParam(1); + const char* arg0 = state.CheckValue(1); fs->Init(arg0); return 0; } @@ -111,7 +111,7 @@ namespace AsuraEngine { PREPARE(L); - cc8* path = state.CheckParam(1); + cc8* path = state.CheckValue(1); std::string mp; if (fs->GetMountPoint(path, ASURA_OUT mp)) state.Push(mp); @@ -126,7 +126,7 @@ namespace AsuraEngine { PREPARE(L); - cc8* dir = state.CheckParam(1); + cc8* dir = state.CheckValue(1); fs->SetWriteDirectory(dir); return 0; } @@ -146,7 +146,7 @@ namespace AsuraEngine { PREPARE(L); - cc8* name = state.CheckParam(1); + cc8* name = state.CheckValue(1); File* file = fs->NewFile(name); if (file) file->PushLuaxUserdata(state); @@ -160,7 +160,7 @@ namespace AsuraEngine { PREPARE(L); - cc8* path = state.CheckParam(1); + cc8* path = state.CheckValue(1); state.Push(fs->NewDirectory(path)); return 1; } @@ -170,7 +170,7 @@ namespace AsuraEngine { PREPARE(L); - cc8* path = state.CheckParam(1); + cc8* path = state.CheckValue(1); DataBuffer* db = state.CheckUserdata(2); state.Push(fs->Write(path, db)); return 1; @@ -181,7 +181,7 @@ namespace AsuraEngine { PREPARE(L); - cc8* path = state.CheckParam(1); + cc8* path = state.CheckValue(1); DataBuffer* db = state.CheckUserdata(2); state.Push(fs->Append(path, db)); return 1; @@ -192,7 +192,7 @@ namespace AsuraEngine { PREPARE(L); - cc8* path = state.CheckParam(1); + cc8* path = state.CheckValue(1); state.Push(fs->Remove(path)); return 1; } @@ -202,7 +202,7 @@ namespace AsuraEngine { PREPARE(L); - cc8* path = state.CheckParam(1); + cc8* path = state.CheckValue(1); FileData* fd = fs->Read(path); if (fd) { @@ -223,7 +223,7 @@ namespace AsuraEngine { PREPARE(L); - cc8* path = state.CheckParam(1); + cc8* path = state.CheckValue(1); FileInfo info; if (fs->GetFileInfo(path, &info)) { @@ -244,7 +244,7 @@ namespace AsuraEngine { PREPARE(L); - cc8* path = state.CheckParam(1); + cc8* path = state.CheckValue(1); std::vector items; if(fs->GetDirectoryItems(path, ASURA_OUT items)) { diff --git a/source/libs/asura-lib-utils/io/binding/_io_task.cpp b/source/libs/asura-lib-utils/io/binding/_io_task.cpp index c03ff2a..b3c5988 100644 --- a/source/libs/asura-lib-utils/io/binding/_io_task.cpp +++ b/source/libs/asura-lib-utils/io/binding/_io_task.cpp @@ -16,19 +16,29 @@ namespace AsuraEngine LUAX_POSTPROCESS(IOTask) { + LUAX_REGISTER_ENUM(state, "EIOTaskType", + { "READ", IOTASK_TYPE_READ }, + { "WRITE", IOTASK_TYPE_WRITE }, + { "APPEND", IOTASK_TYPE_APPEND } + ); } - // task = IOTask.New(path, dst) + // task = IOTask.New(path, buffer, type, callback) LUAX_IMPL_METHOD(IOTask, _New) { LUAX_STATE(L); - cc8* path = state.CheckParam(1); + cc8* path = state.CheckValue(1); DataBuffer* db = state.CheckUserdata(2); - IOTask* task = new IOTask(path, db); + IOTaskType type = (IOTaskType)state.CheckValue(3); + bool cbk = state.GetTop() >= 4 && state.IsType(4, LUA_TFUNCTION); + + IOTask* task = new IOTask(path, db, type); + task->SetLuaxMemberRef(state, task->mBufferRef, 2); + if(cbk) + task->SetLuaxMemberRef(state, task->mCallback, 4); task->PushLuaxUserdata(state); - task->SetLuaxMemberRef(state, task->mDstRef, 2); return 1; } diff --git a/source/libs/asura-lib-utils/io/data_buffer.cpp b/source/libs/asura-lib-utils/io/data_buffer.cpp index 000869d..5049b38 100644 --- a/source/libs/asura-lib-utils/io/data_buffer.cpp +++ b/source/libs/asura-lib-utils/io/data_buffer.cpp @@ -88,5 +88,15 @@ namespace AsuraEngine return mSize; } + void DataBuffer::Lock() + { + mMutex.Lock(); + } + + void DataBuffer::Unlock() + { + mMutex.Unlock(); + } + } } \ No newline at end of file diff --git a/source/libs/asura-lib-utils/io/data_buffer.h b/source/libs/asura-lib-utils/io/data_buffer.h index 9dfe541..445bdf4 100644 --- a/source/libs/asura-lib-utils/io/data_buffer.h +++ b/source/libs/asura-lib-utils/io/data_buffer.h @@ -34,6 +34,9 @@ namespace AsuraEngine size_t Load(const void* bytes, std::size_t size); void Move(void* bytes, std::size_t size); void Clear(); + + void Lock(); + void Unlock(); private: diff --git a/source/libs/asura-lib-utils/io/file.cpp b/source/libs/asura-lib-utils/io/file.cpp index 976203d..0ff8c90 100644 --- a/source/libs/asura-lib-utils/io/file.cpp +++ b/source/libs/asura-lib-utils/io/file.cpp @@ -134,7 +134,10 @@ namespace AsuraEngine if (length < 0) throw Exception("Invalid read size."); - return PHYSFS_readBytes(mFileHandle, dst->GetData(), length); + dst->Lock(); + size_t size = PHYSFS_readBytes(mFileHandle, dst->GetData(), length); + dst->Unlock(); + return size; } size_t File::ReadAll(ASURA_OUT DataBuffer* dst) @@ -149,7 +152,10 @@ namespace AsuraEngine if (dst->GetSize() < length) throw Exception("Data buffer is too small compares to file length."); - return PHYSFS_readBytes(mFileHandle, dst->GetData(), length); + dst->Lock(); + size_t size = PHYSFS_readBytes(mFileHandle, dst->GetData(), length); + dst->Unlock(); + return size; } #ifdef ASURA_WINDOWS diff --git a/source/libs/asura-lib-utils/io/io_task.cpp b/source/libs/asura-lib-utils/io/io_task.cpp index 1a50e56..7203175 100644 --- a/source/libs/asura-lib-utils/io/io_task.cpp +++ b/source/libs/asura-lib-utils/io/io_task.cpp @@ -1,5 +1,8 @@ +#include "file_system.h" #include "io_task.h" +#include + using namespace AEScripting; using namespace Luax; @@ -8,9 +11,9 @@ namespace AsuraEngine namespace IO { - IOTask::IOTask(const std::string& path, DataBuffer* buffer) + IOTask::IOTask(const std::string& path, DataBuffer* buffer, IOTaskType type) : mPath(path) - , mDst(buffer) + , mBuffer(buffer) { } @@ -20,17 +23,29 @@ namespace AsuraEngine bool IOTask::Execute() { + File file(mPath); + if (mType == IOTASK_TYPE_WRITE) + { + + } + // 从path读取内容保存在mBuffer中 + else if (mType == IOTASK_TYPE_READ) + { + file.Open(File::FILE_MODE_READ); + file.ReadAll(mBuffer); + file.Close(); + } return true; } - void IOTask::Invoke(lua_State* thread) + void IOTask::Invoke() { if (mCallback) { - LuaxScopedState state(thread); + LuaxScopedState state(LuaEnv::Get()->GetMainState()); if (PushLuaxMemberRef(state, mCallback)) { - PushLuaxMemberRef(state, mDstRef); + PushLuaxMemberRef(state, mBufferRef); state.Call(1, 0); } } diff --git a/source/libs/asura-lib-utils/io/io_task.h b/source/libs/asura-lib-utils/io/io_task.h index a79b2a8..5f454ee 100644 --- a/source/libs/asura-lib-utils/io/io_task.h +++ b/source/libs/asura-lib-utils/io/io_task.h @@ -13,32 +13,42 @@ namespace AsuraEngine namespace IO { + enum IOTaskType + { + IOTASK_TYPE_READ, + IOTASK_TYPE_WRITE, + IOTASK_TYPE_APPEND, + }; + /// /// 读取文件任务。 /// class IOTask ASURA_FINAL - : public AEScripting::Portable - , public AEThreading::Task + : public AEThreading::Task + , public AEScripting::Portable { public: LUAX_DECL_FACTORY(IOTask); - IOTask(const std::string& path, DataBuffer* buffer); + IOTask(const std::string& path, DataBuffer* buffer, IOTaskType type); ~IOTask(); bool Execute() override ; - void Invoke(lua_State* thread) override; + void Invoke() override; private: - std::string mPath; - - DataBuffer* mDst; - Luax::LuaxMemberRef mDstRef; + LUAX_DECL_ENUM(IOTaskType); LUAX_DECL_METHOD(_New); + std::string mPath; + IOTaskType mType; + + ASURA_REF DataBuffer* mBuffer; + Luax::LuaxMemberRef mBufferRef; + }; } diff --git a/source/libs/asura-lib-utils/scripting/lua_env.h b/source/libs/asura-lib-utils/scripting/lua_env.h index a467479..3bef3df 100644 --- a/source/libs/asura-lib-utils/scripting/lua_env.h +++ b/source/libs/asura-lib-utils/scripting/lua_env.h @@ -16,21 +16,41 @@ namespace AsuraEngine { /// - /// 程序通过这个接口访问主lua state。 + /// 程序通过这个接口访问主lua state。Asura程序的思想是,进程主线程维护一个lua虚拟机,应用的主要逻辑跑在此虚拟机的主lua + /// 线程里(或者叫主栈里)。 /// class LuaEnv ASURA_FINAL : public Singleton { public: - LuaEnv() : mMainState(0){}; + LuaEnv() : mMainState(0) {}; ~LuaEnv() {}; /// /// 设置主虚拟机的主执行栈。 /// - inline void Init(lua_State* L) { ASSERT(!mMainState); mMainState = L; }; + inline void Init() + { + ASSERT(!mMainState); + // 创建主线程 + mMainState = Luax::LuaxRuntime::Get().Open(); + ASSERT(mMainState); + }; - inline lua_State* GetMainState() { return mMainState; }; + inline lua_State* GetMainState() + { + return mMainState; + }; + + inline Luax::LuaxState& GetMainLuaxState() + { + return Luax::LuaxRuntime::Get()[mMainState].state; + }; + + inline void Exit() + { + Luax::LuaxRuntime::Get().Close(mMainState); + } private: diff --git a/source/libs/asura-lib-utils/threading/binding/_thread.cpp b/source/libs/asura-lib-utils/threading/binding/_thread.cpp index 00252a9..9403486 100644 --- a/source/libs/asura-lib-utils/threading/binding/_thread.cpp +++ b/source/libs/asura-lib-utils/threading/binding/_thread.cpp @@ -18,7 +18,8 @@ namespace AsuraEngine { "AddTask", _AddTask }, { "IsRunning", _IsRunning }, { "IsCurrent", _IsCurrent }, - { "GetName", _GetName } + { "GetName", _GetName }, + { "Sleep", _Sleep } ); } @@ -81,7 +82,7 @@ namespace AsuraEngine LUAX_PREPARE(L, Thread); state.Push(self->IsRunning()); - return 0; + return 1; } // thread:IsCurrent() @@ -90,7 +91,7 @@ namespace AsuraEngine LUAX_PREPARE(L, Thread); state.Push(self->IsCurrent()); - return 0; + return 1; } // thread:GetName() @@ -98,6 +99,20 @@ namespace AsuraEngine { LUAX_PREPARE(L, Thread); + state.Push(self->GetName()); + return 1; + } + + // Thread.Sleep(milliseconds) + LUAX_IMPL_METHOD(Thread, _Sleep) + { + LUAX_STATE(L); + int ms = state.CheckValue(1); +#if ASURA_THREAD_WIN32 + ::Sleep(ms); +#elif ASURA_THREAD_STD + +#endif return 0; } diff --git a/source/libs/asura-lib-utils/threading/mutex.h b/source/libs/asura-lib-utils/threading/mutex.h index 51fe63e..7e7d877 100644 --- a/source/libs/asura-lib-utils/threading/mutex.h +++ b/source/libs/asura-lib-utils/threading/mutex.h @@ -32,15 +32,15 @@ namespace AsuraEngine }; - class Lock + class _mutex_locker { public: - Lock(Mutex& mutex) + _mutex_locker(Mutex& mutex) : m(mutex) { m.Lock(); }; - ~Lock() + ~_mutex_locker() { m.Unlock(); } @@ -50,11 +50,11 @@ namespace AsuraEngine }; // 将所在的栈从此位置开始到退栈处作为临界区锁定。 -#define lock(mutex) Lock _asura_scoped_lock_0x0_(mutex) -#define lock2(mutex) Lock _asura_scoped_lock_0x1_(mutex) -#define lock3(mutex) Lock _asura_scoped_lock_0x2_(mutex) -#define lock4(mutex) Lock _asura_scoped_lock_0x3_(mutex) -#define lock5(mutex) Lock _asura_scoped_lock_0x4_(mutex) +#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 { diff --git a/source/libs/asura-lib-utils/threading/task.cpp b/source/libs/asura-lib-utils/threading/task.cpp index e69de29..2e84ed4 100644 --- a/source/libs/asura-lib-utils/threading/task.cpp +++ b/source/libs/asura-lib-utils/threading/task.cpp @@ -0,0 +1,12 @@ +#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 index 9f78860..9c0b12c 100644 --- a/source/libs/asura-lib-utils/threading/task.h +++ b/source/libs/asura-lib-utils/threading/task.h @@ -13,12 +13,12 @@ namespace AsuraEngine /// 希望放在另一个线程处理的任务,继承Task并重写Execute方法。 /// ASURA_ABSTRACT class Task - : virtual public AEScripting::NativeAccessor + : public virtual AEScripting::NativeAccessor { public: - Task(); - virtual ~Task(); + Task() {}; + virtual ~Task() {}; /// /// 执行任务,完成后返回true,调用回调函数。 @@ -28,10 +28,11 @@ namespace AsuraEngine /// /// 调用回调。 /// - virtual void Invoke(lua_State* thread) = 0; + virtual void Invoke() = 0; - protected: + protected: + // 取回调函数 Luax::LuaxMemberRef mCallback; }; diff --git a/source/libs/asura-lib-utils/threading/thread.cpp b/source/libs/asura-lib-utils/threading/thread.cpp index 51738de..9c71ace 100644 --- a/source/libs/asura-lib-utils/threading/thread.cpp +++ b/source/libs/asura-lib-utils/threading/thread.cpp @@ -13,9 +13,6 @@ namespace AsuraEngine Thread::Thread(Luax::LuaxState& father, const std::string& name) : mName(name) { - mState = lua_newthread(father); - SetLuaxMemberRef(father, mStateRef, -1); - lua_pop(father, 1); // mState } Thread::~Thread() @@ -79,13 +76,18 @@ namespace AsuraEngine return mName; } - void Thread::Execute() + void Thread::Process() { + LUAX_STATE(AEScripting::LuaEnv::Get()->GetMainState()); while (!mTaskQueue.empty()) { Task* task = mTaskQueue.front(); - if (task->Execute()) - task->Invoke(mState); + if (task && task->Execute()) + { + // unsafe + task->Invoke(); + this->LuaxRelease(state, task); + } mMutex.Lock(); mTaskQueue.pop(); diff --git a/source/libs/asura-lib-utils/threading/thread.h b/source/libs/asura-lib-utils/threading/thread.h index 1bd5f15..3fa079a 100644 --- a/source/libs/asura-lib-utils/threading/thread.h +++ b/source/libs/asura-lib-utils/threading/thread.h @@ -16,6 +16,28 @@ namespace AsuraEngine class ThreadImpl; + /// + /// 线程的几种不同的实现: + /// 1: Deferred(延迟模式),线程上的任务完成后,需要手动在主线程调用Post方法, + /// 在主线程调回调函数,将发布从异步改为同步操作,解决主lua_State冲突的问题。 + /// 2: Immediate(立即模式),每一个线程维护一个lua_newthread创建出来的lua_State。 + /// 回调函数在不同的lua_State中调用,避免不同的线程访问同一个lua_State。 + /// 3: Daemon(守护模式),线程会一直运行在后台。 + /// + enum ThreadType + { + THREAD_TYPE_DEFERRED, + THREAD_TYPE_IMMEDIATE, + THREAD_TYPE_DAEMON + }; + + enum ThreadState + { + THREAD_STATE_RUNNING, + THREAD_STATE_DEAD, + THREAD_STATE_SUSPEND, + }; + /// /// 线程主体,每个线程维护一个task queue。 /// @@ -26,7 +48,7 @@ namespace AsuraEngine LUAX_DECL_FACTORY(Thread); - Thread(Luax::LuaxState& father, const std::string& name = ""); + Thread(ThreadType type, Luax::LuaxState& luaThread, const std::string& name = ""); ~Thread(); bool AddTask(Task* task); @@ -34,8 +56,10 @@ namespace AsuraEngine void Start(uint32 stacksize = 0); /// - /// 强制终止线程。注意要避免在new和delete之间执行TerminateThread,否则程序将不能在继续使用new了。 + /// 强制终止线程。注意要避免在new和delete之间执行TerminateThread,否则程序将不能在继 + /// 续使用new了。 /// https://blog.csdn.net/anye3000/article/details/7470674 + /// 注意:尽量不要使用这个接口,应该让线程自己运行到终点,而不是手动关闭它。 /// void Kill(); @@ -51,12 +75,21 @@ namespace AsuraEngine /// /// 执行任务队列。 /// - void Execute(); + void Process(); const std::string& GetName(); + /// + /// 回调。 + /// + void Post(); + private: + //----------------------------------------------------------------------------// + + LUAX_DECL_ENUM(ThreadType); + LUAX_DECL_METHOD(_New); LUAX_DECL_METHOD(_Start); LUAX_DECL_METHOD(_Join); @@ -65,6 +98,10 @@ namespace AsuraEngine LUAX_DECL_METHOD(_IsRunning); LUAX_DECL_METHOD(_IsCurrent); LUAX_DECL_METHOD(_GetName); + LUAX_DECL_METHOD(_Sleep); + LUAX_DECL_METHOD(_Post); + + //----------------------------------------------------------------------------// ThreadImpl* mImpl; std::string mName; @@ -75,11 +112,10 @@ namespace AsuraEngine std::queue mTaskQueue; Mutex mMutex; - /// - /// 此线程的lua执行栈,为了避免和主执行栈冲突 - /// - lua_State* mState; - Luax::LuaxMemberRef mStateRef; + lua_State* mLuaThread; + + std::queue mFinishedTasks; + Mutex mFinishedMutex; }; diff --git a/source/libs/asura-lib-utils/threading/thread_impl_posix.cpp b/source/libs/asura-lib-utils/threading/thread_impl_posix.cpp index e69de29..d2ad7af 100644 --- a/source/libs/asura-lib-utils/threading/thread_impl_posix.cpp +++ b/source/libs/asura-lib-utils/threading/thread_impl_posix.cpp @@ -0,0 +1,9 @@ +#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_win32.cpp b/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp index fd1b066..ad859b6 100644 --- a/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp +++ b/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp @@ -1,6 +1,8 @@ #include "thread_impl_win32.h" #include "thread.h" +#include + namespace AsuraEngine { namespace Threading @@ -9,7 +11,11 @@ namespace AsuraEngine static DWORD WINAPI _thread_win32_runner(LPVOID param) { Thread* thread = (Thread*)param; - thread->Execute(); + while (thread->IsRunning()) + { + thread->Process(); + ::Sleep(100); + } return 0; } diff --git a/source/libs/asura-lib-utils/threading/thread_task.cpp b/source/libs/asura-lib-utils/threading/thread_task.cpp new file mode 100644 index 0000000..e69de29 diff --git a/source/libs/asura-lib-utils/threading/thread_task.h b/source/libs/asura-lib-utils/threading/thread_task.h new file mode 100644 index 0000000..1ea0a1a --- /dev/null +++ b/source/libs/asura-lib-utils/threading/thread_task.h @@ -0,0 +1,44 @@ +#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 diff --git a/source/libs/asura-lib-utils/type.h b/source/libs/asura-lib-utils/type.h index aa00108..1ed2d42 100644 --- a/source/libs/asura-lib-utils/type.h +++ b/source/libs/asura-lib-utils/type.h @@ -7,7 +7,7 @@ namespace AsuraEngine { - //---------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// typedef int8_t int8; typedef uint8_t uint8; @@ -27,7 +27,7 @@ namespace AsuraEngine typedef const char cc8; - //---------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// #ifndef ASSERT #ifdef NDEBUG @@ -42,7 +42,7 @@ namespace AsuraEngine #endif #endif - //---------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// #ifdef _WIN32 #define ASURA_FINAL final @@ -76,7 +76,7 @@ namespace AsuraEngine /// #define ASURA_MOVE - //---------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------// #define ASURA_SDL_HOST 1 diff --git a/source/libs/asura-lib-utils/utils_module.cpp b/source/libs/asura-lib-utils/utils_module.cpp index a0539ed..61780e6 100644 --- a/source/libs/asura-lib-utils/utils_module.cpp +++ b/source/libs/asura-lib-utils/utils_module.cpp @@ -13,6 +13,7 @@ namespace AsuraEngine LUAX_REGISTER_FACTORY(state, DataBuffer); LUAX_REGISTER_FACTORY(state, FileData); LUAX_REGISTER_FACTORY(state, File); + LUAX_REGISTER_FACTORY(state, IOTask); // Threading LUAX_REGISTER_FACTORY(state, Thread); } diff --git a/source/libs/asura-lib-utils/utils_module.h b/source/libs/asura-lib-utils/utils_module.h index ae875b9..e802730 100644 --- a/source/libs/asura-lib-utils/utils_module.h +++ b/source/libs/asura-lib-utils/utils_module.h @@ -5,6 +5,7 @@ #include "io/data_buffer.h" #include "io/file_data.h" #include "io/file.h" +#include "io/io_task.h" #include "threading/thread.h" -- cgit v1.1-26-g67d0