summaryrefslogtreecommitdiff
path: root/source/libs/asura-lib-utils/threading/binding/_thread.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/libs/asura-lib-utils/threading/binding/_thread.cpp')
-rw-r--r--source/libs/asura-lib-utils/threading/binding/_thread.cpp154
1 files changed, 122 insertions, 32 deletions
diff --git a/source/libs/asura-lib-utils/threading/binding/_thread.cpp b/source/libs/asura-lib-utils/threading/binding/_thread.cpp
index 9403486..a5aff03 100644
--- a/source/libs/asura-lib-utils/threading/binding/_thread.cpp
+++ b/source/libs/asura-lib-utils/threading/binding/_thread.cpp
@@ -1,5 +1,4 @@
#include "../thread.h"
-#include "../task.h"
using namespace std;
@@ -12,67 +11,114 @@ namespace AsuraEngine
{
LUAX_REGISTER_METHODS(state,
{ "New", _New },
+ { "AddTask", _AddTask },
{ "Start", _Start },
+ { "Idle", _Idle },
+ { "Pause", _Pause },
+ { "Resume", _Resume },
+ { "Stop", _Stop },
{ "Join", _Join },
- { "Kill", _Kill },
- { "AddTask", _AddTask },
{ "IsRunning", _IsRunning },
+ { "IsPaused", _IsPaused },
+ { "IsStopped", _IsStopped },
{ "IsCurrent", _IsCurrent },
+ { "Sleep", _Sleep },
+ { "Post", _Post },
{ "GetName", _GetName },
- { "Sleep", _Sleep }
+ { "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(name)
+ // thread = Thread.New(thread_type, sleepTime, name)
LUAX_IMPL_METHOD(Thread, _New)
{
LUAX_STATE(L);
- cc8* name = state.GetValue<cc8*>(1, "");
+ 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, name);
+ Thread* thread = new Thread(state, type, sleepTime, name);
thread->PushLuaxUserdata(state);
return 1;
}
- // thread:Start()
+ // 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);
- self->Start();
+
+ 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:Join()
- LUAX_IMPL_METHOD(Thread, _Join)
+ // thread:Pause()
+ LUAX_IMPL_METHOD(Thread, _Pause)
{
LUAX_PREPARE(L, Thread);
- self->Join();
+ self->Pause();
return 0;
}
- // thread:Kill()
- LUAX_IMPL_METHOD(Thread, _Kill)
+ // thread:Resume()
+ LUAX_IMPL_METHOD(Thread, _Resume)
{
LUAX_PREPARE(L, Thread);
- self->Kill();
+ self->Resume();
return 0;
}
- // successed = thread:AddTask(task)
- LUAX_IMPL_METHOD(Thread, _AddTask)
+ // thread:Stop()
+ LUAX_IMPL_METHOD(Thread, _Stop)
{
LUAX_PREPARE(L, Thread);
+ self->Stop();
+ return 0;
+ }
- Task* task = state.GetUserdata<Task>(2);
- self->AddTask(task);
- self->LuaxRetain<Task>(state, task);
+ // thread:Join()
+ LUAX_IMPL_METHOD(Thread, _Join)
+ {
+ LUAX_PREPARE(L, Thread);
+ self->Join();
return 0;
}
@@ -80,39 +126,83 @@ namespace AsuraEngine
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.Sleep(milliseconds)
- LUAX_IMPL_METHOD(Thread, _Sleep)
+ // thread:GetType()
+ LUAX_IMPL_METHOD(Thread, _GetType)
{
- LUAX_STATE(L);
- int ms = state.CheckValue<int>(1);
-#if ASURA_THREAD_WIN32
- ::Sleep(ms);
-#elif ASURA_THREAD_STD
+ LUAX_PREPARE(L, Thread);
+ state.Push(self->mType);
+ return 1;
+ }
-#endif
+ // 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<uint>(2);
+ self->SetSleepTime(time);
return 0;
}