summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/3rd-party/Lua51/loadlib.c2
-rw-r--r--source/3rd-party/Luax/luax_class.cpp6
-rw-r--r--source/3rd-party/Luax/luax_class.hpp6
-rw-r--r--source/3rd-party/Luax/luax_class.inl44
-rw-r--r--source/3rd-party/Luax/luax_config.h6
-rw-r--r--source/3rd-party/Luax/luax_globalstate.h7
-rw-r--r--source/3rd-party/Luax/luax_internal.h12
-rw-r--r--source/3rd-party/Luax/luax_ref.cpp6
-rw-r--r--source/3rd-party/Luax/luax_state.cpp16
-rw-r--r--source/3rd-party/Luax/luax_state.h8
-rw-r--r--source/3rd-party/Luax/luax_vm.cpp32
-rw-r--r--source/3rd-party/Luax/luax_vm.h34
-rw-r--r--source/3rd-party/plotscript/plot.c5
-rw-r--r--source/3rd-party/plotscript/test/first_meeting.plot9
-rw-r--r--source/Asura.Editor/controls/binding/_button.cpp (renamed from source/Asura.Editor/widgets/binding/_button.cpp)0
-rw-r--r--source/Asura.Editor/controls/button.cpp (renamed from source/Asura.Editor/widgets/button.cpp)0
-rw-r--r--source/Asura.Editor/controls/button.h (renamed from source/Asura.Editor/widgets/button.h)0
-rw-r--r--source/Asura.Editor/controls/checkbox.cpp (renamed from source/Asura.Editor/widgets/checkbox.cpp)0
-rw-r--r--source/Asura.Editor/controls/checkbox.h (renamed from source/Asura.Editor/widgets/checkbox.h)0
-rw-r--r--source/Asura.Editor/controls/hslider.cpp (renamed from source/Asura.Editor/widgets/hslider.cpp)0
-rw-r--r--source/Asura.Editor/controls/hslider.h (renamed from source/Asura.Editor/widgets/hslider.h)0
-rw-r--r--source/Asura.Editor/controls/hvslider.cpp (renamed from source/Asura.Editor/widgets/hvslider.cpp)0
-rw-r--r--source/Asura.Editor/controls/hvslider.h (renamed from source/Asura.Editor/widgets/hvslider.h)0
-rw-r--r--source/Asura.Editor/controls/label.cpp (renamed from source/Asura.Editor/widgets/label.cpp)0
-rw-r--r--source/Asura.Editor/controls/label.h (renamed from source/Asura.Editor/widgets/label.h)0
-rw-r--r--source/Asura.Editor/controls/panel.cpp (renamed from source/Asura.Editor/widgets/panel.cpp)0
-rw-r--r--source/Asura.Editor/controls/panel.h (renamed from source/Asura.Editor/widgets/panel.h)0
-rw-r--r--source/Asura.Editor/controls/progress.cpp (renamed from source/Asura.Editor/widgets/progress.cpp)0
-rw-r--r--source/Asura.Editor/controls/progress.h (renamed from source/Asura.Editor/widgets/progress.h)0
-rw-r--r--source/Asura.Editor/controls/radio_button.cpp (renamed from source/Asura.Editor/widgets/radio_button.cpp)0
-rw-r--r--source/Asura.Editor/controls/radio_button.h (renamed from source/Asura.Editor/widgets/radio_button.h)0
-rw-r--r--source/Asura.Editor/controls/textbox.cpp (renamed from source/Asura.Editor/widgets/textbox.cpp)0
-rw-r--r--source/Asura.Editor/controls/textbox.h (renamed from source/Asura.Editor/widgets/textbox.h)0
-rw-r--r--source/Asura.Editor/controls/vslider.cpp (renamed from source/Asura.Editor/widgets/vslider.cpp)0
-rw-r--r--source/Asura.Editor/controls/vslider.h (renamed from source/Asura.Editor/widgets/vslider.h)0
-rw-r--r--source/Asura.Editor/controls/widget.h (renamed from source/Asura.Editor/widgets/widget.h)0
-rw-r--r--source/libs/asura-lib-utils/io/binding/_data_buffer.cpp1
-rw-r--r--source/libs/asura-lib-utils/io/io_batch_task.cpp0
-rw-r--r--source/libs/asura-lib-utils/io/io_batch_task.h31
-rw-r--r--source/libs/asura-lib-utils/io/io_task.cpp4
-rw-r--r--source/libs/asura-lib-utils/io/io_task.h2
-rw-r--r--source/libs/asura-lib-utils/scripting/lua_env.h14
-rw-r--r--source/libs/asura-lib-utils/threading/binding/_thread.cpp154
-rw-r--r--source/libs/asura-lib-utils/threading/semaphore.cpp88
-rw-r--r--source/libs/asura-lib-utils/threading/semaphore.h70
-rw-r--r--source/libs/asura-lib-utils/threading/task.h4
-rw-r--r--source/libs/asura-lib-utils/threading/thread.cpp217
-rw-r--r--source/libs/asura-lib-utils/threading/thread.h108
-rw-r--r--source/libs/asura-lib-utils/threading/thread_impl_win32.cpp11
-rw-r--r--source/libs/asura-lib-utils/threading/thread_impl_win32.h3
-rw-r--r--source/tests/05-physfs/main.cpp32
51 files changed, 769 insertions, 163 deletions
diff --git a/source/3rd-party/Lua51/loadlib.c b/source/3rd-party/Lua51/loadlib.c
index 9455c53..6158c53 100644
--- a/source/3rd-party/Lua51/loadlib.c
+++ b/source/3rd-party/Lua51/loadlib.c
@@ -654,7 +654,7 @@ LUALIB_API int luaopen_package (lua_State *L) {
lua_setfield(L, -2, "config");
/* set field `loaded' */
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
- lua_setfield(L, -2, "loaded"); // LUA_REGISTRYINDEX["_LOADED"]Ϊpackage.loaded\LUA_ENVIRONINDEX["loaded"]
+ lua_setfield(L, -2, "loaded");
/* set field `preload' */
lua_newtable(L);
lua_setfield(L, -2, "preload");
diff --git a/source/3rd-party/Luax/luax_class.cpp b/source/3rd-party/Luax/luax_class.cpp
index 960fd47..bb1609d 100644
--- a/source/3rd-party/Luax/luax_class.cpp
+++ b/source/3rd-party/Luax/luax_class.cpp
@@ -46,13 +46,13 @@ namespace Luax
lua_setfield(L, -2, "__index");
lua_pushstring(L, type);
- lua_pushcclosure(L, l___tostring, 1);
+ lua_pushcclosure(L, _Tostring, 1);
lua_setfield(L, -2, "__tostring");
return 1;
}
- int LuaxPlainClass::l___tostring(lua_State* L)
+ int LuaxPlainClass::_Tostring(lua_State* L)
{
// upvalues:
// 1: class name
@@ -184,7 +184,7 @@ namespace Luax
lua_setfield(L, -2, "__index");
lua_pushstring(L, type);
- lua_pushcclosure(L, l___tostring, 1);
+ lua_pushcclosure(L, _Tostring, 1);
lua_setfield(L, -2, "__tostring");
// classmetatableΪbaseClass
diff --git a/source/3rd-party/Luax/luax_class.hpp b/source/3rd-party/Luax/luax_class.hpp
index 99ac381..257e1ca 100644
--- a/source/3rd-party/Luax/luax_class.hpp
+++ b/source/3rd-party/Luax/luax_class.hpp
@@ -180,12 +180,12 @@ namespace Luax
public:
//
- LUAX_DECL_METHOD( l___tostring );
+ LUAX_DECL_METHOD( _Tostring );
LUAX_DECL_METHOD( l_GetClass );
LUAX_DECL_METHOD( l_GetClassName );
//
- LUAX_DECL_METHOD( l___gc );
+ LUAX_DECL_METHOD( _GC );
#if LUAX_ENABLE_NATIVE_EXTEND
LUAX_DECL_METHOD( l_ExtendFactory );
#endif
@@ -213,7 +213,7 @@ namespace Luax
///
static int registry(lua_State* L);
- LUAX_DECL_METHOD( l___tostring );
+ LUAX_DECL_METHOD( _Tostring );
LUAX_DECL_METHOD( l_Extend );
LUAX_DECL_METHOD( l_New );
LUAX_DECL_METHOD( l_TypeOf );
diff --git a/source/3rd-party/Luax/luax_class.inl b/source/3rd-party/Luax/luax_class.inl
index d1c8c4b..45e6552 100644
--- a/source/3rd-party/Luax/luax_class.inl
+++ b/source/3rd-party/Luax/luax_class.inl
@@ -116,7 +116,12 @@ namespace Luax
u32 count = state.GetValue<u32>(-1, 0); // get the count (or 0)
lua_pop(state, 1); // pop the old count
- if (count == 0) return; // nothing to do
+ // no such reference
+ if (count == 0)
+ {
+ state.Pop(2); // userdata, reftable
+ return; // nothing to do
+ }
if (count > 1) {
lua_pushnumber(state, count - 1); // push the new count
@@ -125,7 +130,12 @@ namespace Luax
lua_pushnil(state); // maybe cause gc
}
lua_settable(state, -3); // save it in the table
+
+ state.Pop(1); // reftable
+ return;
}
+ state.Pop(2); // nil, reftable
+ return;
}
}
@@ -194,9 +204,10 @@ namespace Luax
///
/// userdataԴref tablemember tableclass table
- /// ref table kvǿtableuserdataüͨuserdataΪkeyΪvalueԼԱ
+ /// ref table kvǿtableuserdataüͨuserdataΪkey
+ /// ΪvalueԼԱ
/// member table luaʵijԱ
- /// class table б͵ʵеĺ
+ /// class table б͵ʵеĺ
///
template<typename T>
void LuaxNativeClass<T>::BindToLua(LuaxState& state)
@@ -205,8 +216,13 @@ namespace Luax
assert(!T::IsLuaxClassSingleton());
assert(!mUserdata);
- // userdataջעַҪתΪT*ֱthisܻᵼ¶ؼ̳еɥʧ̬
- state.PushPtrUserdata(static_cast<T*>(this));
+ ///
+ /// userdataջעַҪתΪT*ֱthisܻᵼ¶ؼ̳еɥʧ̬
+ /// ֱӴthisȥڶؼ̳£òһͷ麯ġҪthis
+ /// תΪĵ͵ַõһ麯ͨһʵֶ̬
+ ///
+ T* p = static_cast<T*>(this);
+ state.PushPtrUserdata(p);
lua_newtable(state); // ref table޷luaʣC
lua_newtable(state); // member tableluaдĶԱ
@@ -222,10 +238,13 @@ namespace Luax
int memberTable = top - 1;
int refTable = top - 2;
- // ref table ע __tostring
- lua_pushcfunction(state, l___tostring);
+ // ref table ע __tostring __gc
+ lua_pushcfunction(state, _Tostring);
lua_setfield(state, refTable, "__tostring");
+ lua_pushcfunction(state, _GC);
+ lua_setfield(state, refTable, "__gc");
+
// ref table __index __newindex Ϊ member table
lua_pushvalue(state, memberTable);
lua_setfield(state, refTable, "__index");
@@ -233,9 +252,6 @@ namespace Luax
lua_pushvalue(state, memberTable);
lua_setfield(state, refTable, "__newindex");
- lua_pushcfunction(state, l___gc);
- lua_setfield(state, refTable, "__gc");
-
// Ԫ
lua_setmetatable(state, -2); // class is meta of member
lua_setmetatable(state, -2); // member is meta of ref
@@ -321,8 +337,12 @@ namespace Luax
/// ͷŹʵ
///
template<typename T>
- int LuaxNativeClass<T>::l___gc(lua_State* L)
+ int LuaxNativeClass<T>::_GC(lua_State* L)
{
+#if LUAX_PROFILER
+ std::cout << "Luax: GC<" << T::GetLuaxClassName() << ">\n";
+#endif
+
LUAX_SETUP(L, "U");
T* self = state.GetUserdata<T>(1);
delete self;
@@ -334,7 +354,7 @@ namespace Luax
/// ַ
///
template<typename T>
- int LuaxNativeClass<T>::l___tostring(lua_State* L)
+ int LuaxNativeClass<T>::_Tostring(lua_State* L)
{
// params:
// 1: userdata
diff --git a/source/3rd-party/Luax/luax_config.h b/source/3rd-party/Luax/luax_config.h
index c251c6f..2a8ed3e 100644
--- a/source/3rd-party/Luax/luax_config.h
+++ b/source/3rd-party/Luax/luax_config.h
@@ -55,7 +55,11 @@ namespace Luax
#define LUAX_ENABLE_PLAIN_CLASS 0
#define LUAX_ENABLE_PLAIN_ENUM 0
-#define LUAX_PROFILER 0
+#define LUAX_PROFILER 1
+
+#if LUAX_PROFILER
+#include <iostream>
+#endif
}
diff --git a/source/3rd-party/Luax/luax_globalstate.h b/source/3rd-party/Luax/luax_globalstate.h
new file mode 100644
index 0000000..91be51f
--- /dev/null
+++ b/source/3rd-party/Luax/luax_globalstate.h
@@ -0,0 +1,7 @@
+#ifndef __LUAX_GLOBAL_STATE_H__
+#define __LUAX_GLOBAL_STATE_H__
+
+// luaglobal_State
+typedef struct global_State global_State;
+
+#endif \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_internal.h b/source/3rd-party/Luax/luax_internal.h
new file mode 100644
index 0000000..5904008
--- /dev/null
+++ b/source/3rd-party/Luax/luax_internal.h
@@ -0,0 +1,12 @@
+#ifndef __LUAX_INTERNAL_H__
+#define __LUAX_INTERNAL_H__
+
+///
+/// luaԴʹ
+///
+extern "C"
+{
+#include "lua51/lstate.h"
+}
+
+#endif \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_ref.cpp b/source/3rd-party/Luax/luax_ref.cpp
index 78596e3..d4be775 100644
--- a/source/3rd-party/Luax/luax_ref.cpp
+++ b/source/3rd-party/Luax/luax_ref.cpp
@@ -23,7 +23,7 @@ namespace Luax
{
assert(mRefID != LUA_NOREF);
- LuaxVM* vm = LuaxVM::TryGetVM(state);
+ LuaxVM* vm = state.GetVM();
if (!vm) return false;
if (mMode == STRONG_REF)
{
@@ -37,13 +37,15 @@ namespace Luax
}
else
{
+ state.PushNil();
return false;
}
+ return true;
}
void LuaxRef::SetRef(LuaxState& state, int idx)
{
- LuaxVM* vm = LuaxVM::TryGetVM(state);
+ LuaxVM* vm = state.GetVM();
if (!vm) return;
if (mMode == STRONG_REF)
{
diff --git a/source/3rd-party/Luax/luax_state.cpp b/source/3rd-party/Luax/luax_state.cpp
index b4d989a..3eae1df 100644
--- a/source/3rd-party/Luax/luax_state.cpp
+++ b/source/3rd-party/Luax/luax_state.cpp
@@ -1,8 +1,8 @@
-//#include "luax_class.h"
#include "luax_enum.h"
#include "luax_state.h"
#include "luax_vm.h"
#include "luax_class.hpp"
+#include "luax_internal.h"
namespace Luax
{
@@ -28,6 +28,16 @@ namespace Luax
luaL_openlibs(mState);
}
+ global_State* LuaxState::GetGlobalState()
+ {
+ return G(mState);
+ }
+
+ LuaxVM* LuaxState::GetVM()
+ {
+ return LuaxVM::TryGetVM(G(mState));
+ }
+
void LuaxState::PushGlobalNamespace()
{
int top = GetTop();
@@ -642,8 +652,8 @@ namespace Luax
return value;
}
- void LuaxState::PushPtrUserdata(void* ptr) {
-
+ void LuaxState::PushPtrUserdata(void* ptr)
+ {
void** handle = (void**)lua_newuserdata(this->mState, sizeof(void*));
assert(handle);
(*handle) = ptr;
diff --git a/source/3rd-party/Luax/luax_state.h b/source/3rd-party/Luax/luax_state.h
index dc3b79f..7c7d813 100644
--- a/source/3rd-party/Luax/luax_state.h
+++ b/source/3rd-party/Luax/luax_state.h
@@ -5,6 +5,7 @@
#include "luax_config.h"
#include "luax_reftable.h"
+#include "luax_globalstate.h"
namespace Luax
{
@@ -41,6 +42,10 @@ namespace Luax
///
inline lua_State* GetHandle() { return mState; };
+ global_State* GetGlobalState();
+
+ LuaxVM* GetVM();
+
//------------------------------------------------------------------------------//
void OpenLibs();
@@ -206,10 +211,7 @@ namespace Luax
void* operator &();
void* operator new(size_t size);
- //------------------------------------------------------------------------------//
-
lua_State* const mState;
-
};
//--------------------------------------------------------------------------------//
diff --git a/source/3rd-party/Luax/luax_vm.cpp b/source/3rd-party/Luax/luax_vm.cpp
index 549e20b..5e9d1f5 100644
--- a/source/3rd-party/Luax/luax_vm.cpp
+++ b/source/3rd-party/Luax/luax_vm.cpp
@@ -1,32 +1,39 @@
+#include "luax_internal.h"
#include "luax_vm.h"
namespace Luax
{
- LuaxVM::ThreadMap threadMap; // ̲ͨ߳Ϊ˷
+ LuaxVM::VMap LuaxVM::VMs; // ̲ͨ߳Ϊ˷
- LuaxVM* LuaxVM::TryGetVM(lua_State* L)
+ LuaxVM* LuaxVM::TryGetVM(global_State* gState)
{
- auto it = threadMap.find(L);
- if (it != threadMap.end())
+ auto it = VMs.find(gState);
+ if (it != VMs.end())
return it->second;
else
return nullptr;
}
+ LuaxVM* LuaxVM::TryGetVM(lua_State* state)
+ {
+ return TryGetVM(G(state));
+ }
+
LuaxVM::LuaxVM()
: mStrongRefTable()
, mWeakRefTable()
{
mMainThread = luaL_newstate();
assert(mMainThread);
- mThreads.insert(mMainThread);
+ mGlobalState = G(mMainThread);
- threadMap.insert(std::pair<lua_State*, LuaxVM*>(mMainThread, this));
+ VMs.insert(std::pair<global_State*, LuaxVM*>(mGlobalState, this));
}
LuaxVM::~LuaxVM()
{
+ VMs.erase(mGlobalState);
lua_close(mMainThread);
}
@@ -34,7 +41,7 @@ namespace Luax
void LuaxVM::Setup()
{
LUAX_STATE(mMainThread);
- // ȫñ
+
mStrongRefTable.Init(state, "_LUAX_STRONGREF_TABLE");
mWeakRefTable.Init(state, "_LUAX_WEAKREF_TABLE", "v");
}
@@ -43,7 +50,6 @@ namespace Luax
{
lua_State* thread = lua_newthread(mMainThread);
assert(thread);
- mThreads.insert(thread);
return thread;
}
@@ -62,14 +68,4 @@ namespace Luax
return mWeakRefTable;
}
- bool LuaxVM::HasThread(lua_State* L)
- {
- return mThreads.find(L) != mThreads.end();
- }
-
- int LuaxVM::GetThreadCount()
- {
- return mThreads.size();
- }
-
} \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_vm.h b/source/3rd-party/Luax/luax_vm.h
index 9039a04..1bac71e 100644
--- a/source/3rd-party/Luax/luax_vm.h
+++ b/source/3rd-party/Luax/luax_vm.h
@@ -7,6 +7,7 @@
#include "luax_ref.h"
#include "luax_config.h"
#include "luax_state.h"
+#include "luax_globalstate.h"
namespace Luax
{
@@ -17,38 +18,41 @@ namespace Luax
class LuaxVM
{
public:
+
+ ///
+ /// global_Stateõ
+ ///
+ static LuaxVM* TryGetVM(global_State* gState);
+ static LuaxVM* TryGetVM(lua_State* state);
+
LuaxVM();
~LuaxVM();
+ ///
+ /// ҪֶSetupʼһЩ״̬
+ ///
void Setup();
- lua_State* CreateThread();
lua_State* GetMainThread();
-
- int GetThreadCount();
+ lua_State* CreateThread();
LuaxRefTable& GetStrongRefTable();
LuaxRefTable& GetWeakRefTable();
- bool HasThread(lua_State* L);
-
- static LuaxVM* TryGetVM(lua_State* L);
-
- typedef std::map<lua_State*, LuaxVM*> ThreadMap;
-
private:
- static ThreadMap threadMap; // ̲ͨ߳Ϊ˷
+ typedef std::map<global_State*, LuaxVM*> VMap;
- LuaxRefTable mStrongRefTable; // _LUAX_STRONGREF_TABLE
- LuaxRefTable mWeakRefTable; // _LUAX_WEAKREF_TABLE
+ static VMap VMs; // ͨglobal_StateΪ˷
- lua_State* mMainThread; // ߳
+ LuaxRefTable mStrongRefTable; // _LUAX_STRONGREF_TABLE
+ LuaxRefTable mWeakRefTable; // _LUAX_WEAKREF_TABLE
- std::unordered_set<lua_State*> mThreads; // ߳
+ global_State* mGlobalState; // global_Stateɵǰ̹߳
+ lua_State* mMainThread; // ߳
#if LUAX_PROFILER
- size_t mObjectCount; // ͳڴдʵ
+ size_t mObjectCount; // ͳڴдʵ
#endif
};
diff --git a/source/3rd-party/plotscript/plot.c b/source/3rd-party/plotscript/plot.c
new file mode 100644
index 0000000..6028671
--- /dev/null
+++ b/source/3rd-party/plotscript/plot.c
@@ -0,0 +1,5 @@
+/*
+ * ڽű̷š
+*/
+
+
diff --git a/source/3rd-party/plotscript/test/first_meeting.plot b/source/3rd-party/plotscript/test/first_meeting.plot
new file mode 100644
index 0000000..5cf69d6
--- /dev/null
+++ b/source/3rd-party/plotscript/test/first_meeting.plot
@@ -0,0 +1,9 @@
+
+#ʱ
+
+@():
+
+@camera:
+ move_to(1, 2, 4s) & move_to()
+
+ \ No newline at end of file
diff --git a/source/Asura.Editor/widgets/binding/_button.cpp b/source/Asura.Editor/controls/binding/_button.cpp
index fbae64f..fbae64f 100644
--- a/source/Asura.Editor/widgets/binding/_button.cpp
+++ b/source/Asura.Editor/controls/binding/_button.cpp
diff --git a/source/Asura.Editor/widgets/button.cpp b/source/Asura.Editor/controls/button.cpp
index 97de284..97de284 100644
--- a/source/Asura.Editor/widgets/button.cpp
+++ b/source/Asura.Editor/controls/button.cpp
diff --git a/source/Asura.Editor/widgets/button.h b/source/Asura.Editor/controls/button.h
index 14cd041..14cd041 100644
--- a/source/Asura.Editor/widgets/button.h
+++ b/source/Asura.Editor/controls/button.h
diff --git a/source/Asura.Editor/widgets/checkbox.cpp b/source/Asura.Editor/controls/checkbox.cpp
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/checkbox.cpp
+++ b/source/Asura.Editor/controls/checkbox.cpp
diff --git a/source/Asura.Editor/widgets/checkbox.h b/source/Asura.Editor/controls/checkbox.h
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/checkbox.h
+++ b/source/Asura.Editor/controls/checkbox.h
diff --git a/source/Asura.Editor/widgets/hslider.cpp b/source/Asura.Editor/controls/hslider.cpp
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/hslider.cpp
+++ b/source/Asura.Editor/controls/hslider.cpp
diff --git a/source/Asura.Editor/widgets/hslider.h b/source/Asura.Editor/controls/hslider.h
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/hslider.h
+++ b/source/Asura.Editor/controls/hslider.h
diff --git a/source/Asura.Editor/widgets/hvslider.cpp b/source/Asura.Editor/controls/hvslider.cpp
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/hvslider.cpp
+++ b/source/Asura.Editor/controls/hvslider.cpp
diff --git a/source/Asura.Editor/widgets/hvslider.h b/source/Asura.Editor/controls/hvslider.h
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/hvslider.h
+++ b/source/Asura.Editor/controls/hvslider.h
diff --git a/source/Asura.Editor/widgets/label.cpp b/source/Asura.Editor/controls/label.cpp
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/label.cpp
+++ b/source/Asura.Editor/controls/label.cpp
diff --git a/source/Asura.Editor/widgets/label.h b/source/Asura.Editor/controls/label.h
index 1d053f8..1d053f8 100644
--- a/source/Asura.Editor/widgets/label.h
+++ b/source/Asura.Editor/controls/label.h
diff --git a/source/Asura.Editor/widgets/panel.cpp b/source/Asura.Editor/controls/panel.cpp
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/panel.cpp
+++ b/source/Asura.Editor/controls/panel.cpp
diff --git a/source/Asura.Editor/widgets/panel.h b/source/Asura.Editor/controls/panel.h
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/panel.h
+++ b/source/Asura.Editor/controls/panel.h
diff --git a/source/Asura.Editor/widgets/progress.cpp b/source/Asura.Editor/controls/progress.cpp
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/progress.cpp
+++ b/source/Asura.Editor/controls/progress.cpp
diff --git a/source/Asura.Editor/widgets/progress.h b/source/Asura.Editor/controls/progress.h
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/progress.h
+++ b/source/Asura.Editor/controls/progress.h
diff --git a/source/Asura.Editor/widgets/radio_button.cpp b/source/Asura.Editor/controls/radio_button.cpp
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/radio_button.cpp
+++ b/source/Asura.Editor/controls/radio_button.cpp
diff --git a/source/Asura.Editor/widgets/radio_button.h b/source/Asura.Editor/controls/radio_button.h
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/radio_button.h
+++ b/source/Asura.Editor/controls/radio_button.h
diff --git a/source/Asura.Editor/widgets/textbox.cpp b/source/Asura.Editor/controls/textbox.cpp
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/textbox.cpp
+++ b/source/Asura.Editor/controls/textbox.cpp
diff --git a/source/Asura.Editor/widgets/textbox.h b/source/Asura.Editor/controls/textbox.h
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/textbox.h
+++ b/source/Asura.Editor/controls/textbox.h
diff --git a/source/Asura.Editor/widgets/vslider.cpp b/source/Asura.Editor/controls/vslider.cpp
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/vslider.cpp
+++ b/source/Asura.Editor/controls/vslider.cpp
diff --git a/source/Asura.Editor/widgets/vslider.h b/source/Asura.Editor/controls/vslider.h
index e69de29..e69de29 100644
--- a/source/Asura.Editor/widgets/vslider.h
+++ b/source/Asura.Editor/controls/vslider.h
diff --git a/source/Asura.Editor/widgets/widget.h b/source/Asura.Editor/controls/widget.h
index 9ffd1fb..9ffd1fb 100644
--- a/source/Asura.Editor/widgets/widget.h
+++ b/source/Asura.Editor/controls/widget.h
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 6725bc4..cd73b31 100644
--- a/source/libs/asura-lib-utils/io/binding/_data_buffer.cpp
+++ b/source/libs/asura-lib-utils/io/binding/_data_buffer.cpp
@@ -42,6 +42,7 @@ namespace AsuraEngine
size_t size = lua_tonumber(L, 1);
DataBuffer* buffer = new DataBuffer(size);
buffer->PushLuaxUserdata(state);
+ return 1;
}
else
{
diff --git a/source/libs/asura-lib-utils/io/io_batch_task.cpp b/source/libs/asura-lib-utils/io/io_batch_task.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/source/libs/asura-lib-utils/io/io_batch_task.cpp
diff --git a/source/libs/asura-lib-utils/io/io_batch_task.h b/source/libs/asura-lib-utils/io/io_batch_task.h
new file mode 100644
index 0000000..c0be921
--- /dev/null
+++ b/source/libs/asura-lib-utils/io/io_batch_task.h
@@ -0,0 +1,31 @@
+#ifndef __ASURA_IO_BATCH_TASK_H__
+#define __ASURA_IO_BATCH_TASK_H__
+
+#include "io_task.h"
+
+namespace AsuraEngine
+{
+ namespace IO
+ {
+
+ ///
+ /// дһύһtableδ󷵻ؽ
+ ///
+ class IOBatchTask ASURA_FINAL : public AEThreading::Task
+ {
+ public:
+
+ private:
+
+ ///
+ /// ÿһĽṹ£
+ /// { path = "", }
+ ///
+ Luax::LuaxMemberRef mTasks;
+
+ };
+
+ }
+}
+
+#endif \ No newline at end of file
diff --git a/source/libs/asura-lib-utils/io/io_task.cpp b/source/libs/asura-lib-utils/io/io_task.cpp
index 38e21ad..361b9c5 100644
--- a/source/libs/asura-lib-utils/io/io_task.cpp
+++ b/source/libs/asura-lib-utils/io/io_task.cpp
@@ -38,11 +38,11 @@ namespace AsuraEngine
return true;
}
- void IOTask::Invoke()
+ void IOTask::Invoke(lua_State* invokeThreaad)
{
if (mCallback)
{
- LuaxScopedState state(LuaEnv::Get()->GetMainThread());
+ LuaxScopedState state(invokeThreaad);
if (PushLuaxMemberRef(state, mCallback))
{
PushLuaxMemberRef(state, mBufferRef);
diff --git a/source/libs/asura-lib-utils/io/io_task.h b/source/libs/asura-lib-utils/io/io_task.h
index 5f454ee..8f04142 100644
--- a/source/libs/asura-lib-utils/io/io_task.h
+++ b/source/libs/asura-lib-utils/io/io_task.h
@@ -35,7 +35,7 @@ namespace AsuraEngine
~IOTask();
bool Execute() override ;
- void Invoke() override;
+ void Invoke(lua_State* invokeThreaad) override;
private:
diff --git a/source/libs/asura-lib-utils/scripting/lua_env.h b/source/libs/asura-lib-utils/scripting/lua_env.h
index bae6c4c..e2fc4fc 100644
--- a/source/libs/asura-lib-utils/scripting/lua_env.h
+++ b/source/libs/asura-lib-utils/scripting/lua_env.h
@@ -16,8 +16,8 @@ namespace AsuraEngine
{
///
- /// ͨӿڷlua stateAsura˼ǣ߳άһluaӦõҪ߼ڴlua
- /// ߳߽ջ
+ /// ͨӿڷlua stateAsura˼ǣ߳άһluaӦ
+ /// Ҫ߼ڴlua߳߽ջ
///
class LuaEnv ASURA_FINAL : public Singleton<LuaEnv>
{
@@ -51,14 +51,14 @@ namespace AsuraEngine
private:
///
- /// ̱߳һluaһAsuraԶֻһ󲿷ִ붼УֻһΡ
- /// ߳\ִջӦglobal_Stateģ
+ /// ̱߳һluaһAsuraԶֻһ󲿷ִ붼
+ /// УֻһΡ߳\ִջӦglobal_Stateģ
///
/// struct lua_State *mainthread;
///
- /// ʹlua_newstate()ᴴһһglobal_Stateṹһ߳lua_Stateء
- /// global_State̹߳˵һ߳lua_newstate()⣬߳lua_newthread()
- ///
+ /// ʹlua_newstate()ᴴһһglobal_Stateṹһ
+ /// ߳lua_Stateءglobal_State̹߳˵һ
+ /// ߳lua_newstate()⣬߳lua_newthread()
///
Luax::LuaxVM* mVM;
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;
}
diff --git a/source/libs/asura-lib-utils/threading/semaphore.cpp b/source/libs/asura-lib-utils/threading/semaphore.cpp
new file mode 100644
index 0000000..d59ec78
--- /dev/null
+++ b/source/libs/asura-lib-utils/threading/semaphore.cpp
@@ -0,0 +1,88 @@
+#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
new file mode 100644
index 0000000..80773d8
--- /dev/null
+++ b/source/libs/asura-lib-utils/threading/semaphore.h
@@ -0,0 +1,70 @@
+#ifndef __ASURA_SEMAPHORE_H__
+#define __ASURA_SEMAPHORE_H__
+
+#include "../utils_config.h"
+
+#if ASURA_THREAD_WIN32
+#include <windows.h>
+#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.h b/source/libs/asura-lib-utils/threading/task.h
index 9c0b12c..fb7aa5f 100644
--- a/source/libs/asura-lib-utils/threading/task.h
+++ b/source/libs/asura-lib-utils/threading/task.h
@@ -26,9 +26,9 @@ namespace AsuraEngine
virtual bool Execute() = 0;
///
- /// ûص
+ /// ûصinvoke threadص
///
- virtual void Invoke() = 0;
+ virtual void Invoke(lua_State* invokeThreaad) = 0;
protected:
diff --git a/source/libs/asura-lib-utils/threading/thread.cpp b/source/libs/asura-lib-utils/threading/thread.cpp
index 628eee4..0f4f5da 100644
--- a/source/libs/asura-lib-utils/threading/thread.cpp
+++ b/source/libs/asura-lib-utils/threading/thread.cpp
@@ -10,14 +10,50 @@ namespace AsuraEngine
namespace Threading
{
- Thread::Thread(ThreadType type, Luax::LuaxState& luaThread, const std::string& name/* = ""*/)
+ 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()
{
- delete mImpl;
+ 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)\
@@ -31,20 +67,72 @@ namespace AsuraEngine
} \
}
- bool Thread::AddTask(Task* task)
+ bool Thread::Start(bool isDaemon /*= true*/, uint32 stacksize /*= 0*/)
{
- lock(mMutex);
- mTaskQueue.push(task);
- return true;
- }
+ if (mState != THREAD_STATE_IDLE)
+ return false;
+
+ // Ѿһ֮ǰģر
+ if (mImpl)
+ {
+ delete mImpl;
+ mImpl = nullptr;
+ }
- void Thread::Start(uint32 stacksize)
- {
#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()
@@ -53,21 +141,40 @@ namespace AsuraEngine
mImpl->Join();
}
- void Thread::Kill()
+ ThreadState Thread::GetState()
{
- ASSERT(mImpl);
- mImpl->Kill();
+ ThreadState state;
+ mStateMutex.Lock();
+ state = mState;
+ mStateMutex.Unlock();
+ return state;
}
bool Thread::IsRunning()
{
ASSERT(mImpl);
- return mImpl->IsRunning();
+
+ 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();
}
@@ -78,22 +185,88 @@ namespace AsuraEngine
void Thread::Process()
{
- LUAX_STATE(AEScripting::LuaEnv::Get()->GetMainThread());
- while (!mTaskQueue.empty())
+ 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<Task>(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 = mTaskQueue.front();
- if (task && task->Execute())
+ Task* task = mFinishedTasks.front();
+ if (task)
{
- // unsafe
- task->Invoke();
+ task->Invoke(mLuaThread);
this->LuaxRelease<Task>(state, task);
+ mFinishedMutex.Lock();
+ mFinishedTasks.pop();
+ mFinishedMutex.Unlock();
}
+ }
+ }
- mMutex.Lock();
- mTaskQueue.pop();
- mMutex.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
index 3928963..0e75770 100644
--- a/source/libs/asura-lib-utils/threading/thread.h
+++ b/source/libs/asura-lib-utils/threading/thread.h
@@ -8,6 +8,7 @@
#include "task.h"
#include "mutex.h"
+#include "semaphore.h"
namespace AsuraEngine
{
@@ -22,20 +23,19 @@ namespace AsuraEngine
/// ̵߳ص첽Ϊͬlua_Stateͻ⡣
/// 2: Immediateģʽÿһ߳άһlua_newthreadlua_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,
+ THREAD_STATE_IDLE, ///< ãδں˶
+ THREAD_STATE_RUNNING, ///< ѭ
+ THREAD_STATE_PAUSED, ///< ѭͣ
+ THREAD_STATE_STOPPED, ///< ˳ѭ
};
///
@@ -48,27 +48,54 @@ namespace AsuraEngine
LUAX_DECL_FACTORY(Thread);
- Thread(ThreadType type, Luax::LuaxState& luaThread, const std::string& name = "");
+ 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();
- void Start(uint32 stacksize = 0);
+ ///
+ /// ں˶󣬲Сdaemonȴֶstopijʱ̶ɺԶstop
+ ///
+ bool Start(bool daemon = true, uint32 stacksize = 0);
///
- /// ǿֹ̡߳עҪnewdeleteִ֮TerminateThread򽫲ڼ
- /// ʹnewˡ
- /// https://blog.csdn.net/anye3000/article/details/7470674
- /// ע⣺ҪʹӿڣӦ߳Լеյ㣬ֶر
+ /// ͬ߳̿ƣʵʱġҪ߳ʹIsȷϵָ״̬
///
- void Kill();
+ 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();
@@ -84,41 +111,84 @@ namespace AsuraEngine
///
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(_Kill);
- LUAX_DECL_METHOD(_AddTask);
LUAX_DECL_METHOD(_IsRunning);
+ LUAX_DECL_METHOD(_IsPaused);
+ LUAX_DECL_METHOD(_IsStopped);
LUAX_DECL_METHOD(_IsCurrent);
- LUAX_DECL_METHOD(_GetName);
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<Task*> mTaskQueue;
- Mutex mMutex;
-
- lua_State* mLuaThread;
+ Mutex mTaskQueueMutex;
+ ///
+ /// ӳģʽʹ
+ ///
std::queue<Task*> mFinishedTasks;
Mutex mFinishedMutex;
+ ///
+ /// ģʽʹãصʹõlua߳
+ ///
+ lua_State* mCallbackThread;
+ Luax::LuaxMemberRef mCallbackThreadRef;
+
};
///
@@ -138,6 +208,8 @@ namespace AsuraEngine
virtual void Join() = 0;
virtual void Kill() = 0;
+ virtual void Sleep(uint ms) = 0;
+
virtual bool IsRunning() = 0;
virtual bool IsCurrent() = 0;
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 ad859b6..6871c2d 100644
--- a/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp
+++ b/source/libs/asura-lib-utils/threading/thread_impl_win32.cpp
@@ -11,11 +11,7 @@ namespace AsuraEngine
static DWORD WINAPI _thread_win32_runner(LPVOID param)
{
Thread* thread = (Thread*)param;
- while (thread->IsRunning())
- {
- thread->Process();
- ::Sleep(100);
- }
+ thread->Process();
return 0;
}
@@ -55,6 +51,11 @@ namespace AsuraEngine
::TerminateThread(mHandle, FALSE);
}
+ void ThreadImplWin32::Sleep(uint ms)
+ {
+ ::Sleep(ms);
+ }
+
bool ThreadImplWin32::IsRunning()
{
if (mHandle) {
diff --git a/source/libs/asura-lib-utils/threading/thread_impl_win32.h b/source/libs/asura-lib-utils/threading/thread_impl_win32.h
index 361152f..a22aeef 100644
--- a/source/libs/asura-lib-utils/threading/thread_impl_win32.h
+++ b/source/libs/asura-lib-utils/threading/thread_impl_win32.h
@@ -26,9 +26,10 @@ namespace AsuraEngine
bool Start(Thread* thread, uint32 stacksize) override;
void Join() override;
-
void Kill() override;
+ void Sleep(uint ms) override;
+
bool IsRunning() override;
bool IsCurrent() override;
diff --git a/source/tests/05-physfs/main.cpp b/source/tests/05-physfs/main.cpp
index b547f1f..4400c44 100644
--- a/source/tests/05-physfs/main.cpp
+++ b/source/tests/05-physfs/main.cpp
@@ -25,35 +25,43 @@ function main()
print(file:GetFileName())
print(file:GetExtension())
print(file:GetName())
-
dst = DataBuffer.New(138567)
- thread = Thread.New("IO thread")
+ thread = Thread.New(EThreadType.DEFFERRED)
local content = ""
local cont = "ok"
+ if true then
+ local buff=DataBuffer.New(10)
+ print(buff:GetSize())
+ buff = nil
+ end
local task = IOTask.New("root/physfs2.txt", dst, EIOTaskType.READ, function(db)
function _r()
print("test..............")
local c = db:GetData()
- print(c)
+ print(#c)
end
function err(msg)
print(msg)
end
xpcall(_r, err)
+ thread:Stop()
end)
thread:AddTask(task)
- thread:Start()
-
- print("-------------end-------------")
-
- print(thread:GetName())
+ task = nil
+ thread:Start()
- while(true) do
+ while true do
thread:Post()
- Thread.Sleep(100)
+ Thread.Sleep(100)
+ local a = io.read()
+ if a == "x" then
+ thread:Stop()
+ end
+ if thread:IsStopped() then
+ break
+ end
end
- io.read()
end
function err(msg)
@@ -70,7 +78,7 @@ int main(int argc, char* args[])
// ÿһasura򱣴һlua stateһһlua߳
AEScripting::LuaEnv::Get()->Init();
- Luax::LuaxState& state = LuaEnv::Get()->GetMainLuaxState();
+ LUAX_STATE(LuaEnv::Get()->GetMainThread());
state.OpenLibs();
state.PushGlobalNamespace();