summaryrefslogtreecommitdiff
path: root/source/modules/asura-utils/Threads/binding/_thread.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-08-02 20:51:00 +0800
committerchai <chaifix@163.com>2019-08-02 20:51:00 +0800
commitbad78945ceba425f6a80e3b8dca2414d592970eb (patch)
tree8bf7540766349c534bf9e5746b24fd7507ba034e /source/modules/asura-utils/Threads/binding/_thread.cpp
parent99b90496765df21c5f377f42b9ed073ccb34c1fd (diff)
*修改文件名格式
Diffstat (limited to 'source/modules/asura-utils/Threads/binding/_thread.cpp')
-rw-r--r--source/modules/asura-utils/Threads/binding/_thread.cpp208
1 files changed, 208 insertions, 0 deletions
diff --git a/source/modules/asura-utils/Threads/binding/_thread.cpp b/source/modules/asura-utils/Threads/binding/_thread.cpp
new file mode 100644
index 0000000..aaa9e8d
--- /dev/null
+++ b/source/modules/asura-utils/Threads/binding/_thread.cpp
@@ -0,0 +1,208 @@
+#include "../Thread.h"
+
+using namespace std;
+
+namespace_begin(AsuraEngine)
+namespace_begin(Threads)
+
+ 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 },
+ { "Dispatch", _Dispatch },
+ { "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",
+ { "IDLE", 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<int>(1, THREAD_TYPE_DEFERRED);
+ uint sleepTime = state.GetValue<uint>(2,1);
+ cc8* name = state.GetValue<cc8*>(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<Task>(2);
+ self->AddTask(task);
+ self->LuaxRetain<Task>(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:Dispatch()
+ LUAX_IMPL_METHOD(Thread, _Dispatch)
+ {
+ LUAX_PREPARE(L, Thread);
+ self->Dispatch();
+ 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->m_Type);
+ return 1;
+ }
+
+ // thread:GetState()
+ LUAX_IMPL_METHOD(Thread, _GetState)
+ {
+ LUAX_PREPARE(L, Thread);
+ state.Push(self->m_State);
+ return 1;
+ }
+
+ // thread:SetSleepTime(sleepTime)
+ LUAX_IMPL_METHOD(Thread, _SetSleepTime)
+ {
+ LUAX_PREPARE(L, Thread);
+ uint time = state.CheckValue<uint>(2);
+ self->SetSleepTime(time);
+ return 0;
+ }
+
+ }
+}