summaryrefslogtreecommitdiff
path: root/source/modules/asura-utils
diff options
context:
space:
mode:
Diffstat (limited to 'source/modules/asura-utils')
-rw-r--r--source/modules/asura-utils/classes.h9
-rw-r--r--source/modules/asura-utils/exceptions/exception.h24
-rw-r--r--source/modules/asura-utils/io/file.cpp4
-rw-r--r--source/modules/asura-utils/scripting/portable.hpp28
-rw-r--r--source/modules/asura-utils/threading/binding/_coroutine.cpp6
-rw-r--r--source/modules/asura-utils/threading/binding/_thread.cpp6
-rw-r--r--source/modules/asura-utils/threading/conditional.cpp136
-rw-r--r--source/modules/asura-utils/threading/conditional.h48
-rw-r--r--source/modules/asura-utils/threading/coroutine.cpp17
-rw-r--r--source/modules/asura-utils/threading/coroutine.h44
-rw-r--r--source/modules/asura-utils/threading/mutex.cpp154
-rw-r--r--source/modules/asura-utils/threading/mutex.h143
-rw-r--r--source/modules/asura-utils/threading/semaphore.cpp154
-rw-r--r--source/modules/asura-utils/threading/semaphore.h86
-rw-r--r--source/modules/asura-utils/threading/task.cpp10
-rw-r--r--source/modules/asura-utils/threading/task.h51
-rw-r--r--source/modules/asura-utils/threading/thread.cpp456
-rw-r--r--source/modules/asura-utils/threading/thread.h414
-rw-r--r--source/modules/asura-utils/threading/thread_impl_posix.cpp10
-rw-r--r--source/modules/asura-utils/threading/thread_impl_posix.h2
-rw-r--r--source/modules/asura-utils/threading/thread_impl_std.h40
-rw-r--r--source/modules/asura-utils/threading/thread_impl_win32.cpp116
-rw-r--r--source/modules/asura-utils/threading/thread_impl_win32.h45
-rw-r--r--source/modules/asura-utils/threading/threadable.h24
-rw-r--r--source/modules/asura-utils/utils_module.h2
25 files changed, 1003 insertions, 1026 deletions
diff --git a/source/modules/asura-utils/classes.h b/source/modules/asura-utils/classes.h
new file mode 100644
index 0000000..8c89b6a
--- /dev/null
+++ b/source/modules/asura-utils/classes.h
@@ -0,0 +1,9 @@
+#ifndef __ASURAENGINE_CLASSES_H__
+#define __ASURAENGINE_CLASSES_H__
+
+#define GET_SET(TYPE,PROP_NAME,VAR_NAME) void Set##PROP_NAME (TYPE val) { VAR_NAME = val; } const TYPE Get##PROP_NAME () const {return (const TYPE)VAR_NAME; }
+
+#define namespace_begin(NAMESPACE) namespace NAMESPACE {
+#define namespace_end }
+
+#endif
diff --git a/source/modules/asura-utils/exceptions/exception.h b/source/modules/asura-utils/exceptions/exception.h
index 9873a38..4acbc1e 100644
--- a/source/modules/asura-utils/exceptions/exception.h
+++ b/source/modules/asura-utils/exceptions/exception.h
@@ -7,23 +7,23 @@
namespace AsuraEngine
{
- class Exception : public std::exception
- {
- public:
+class Exception : public std::exception
+{
+public:
- Exception(const char *fmt, ...);
- virtual ~Exception() throw();
+ Exception(const char *fmt, ...);
+ virtual ~Exception() throw();
- inline virtual const char *what() const throw()
- {
- return message.c_str();
- }
+ inline virtual const char *what() const throw()
+ {
+ return message.c_str();
+ }
- private:
+private:
- std::string message;
+ std::string message;
- }; // Exception
+}; // Exception
}
diff --git a/source/modules/asura-utils/io/file.cpp b/source/modules/asura-utils/io/file.cpp
index 2061fb1..a2f7403 100644
--- a/source/modules/asura-utils/io/file.cpp
+++ b/source/modules/asura-utils/io/file.cpp
@@ -80,8 +80,8 @@ namespace AsuraEngine
throw Exception("Could not open file %s (%s)", m_FileName.c_str(), err);
}
- m_FileHandle = handle;
- m_Mode = mode;
+ m_FileHandle = handle;
+ m_Mode = mode;
if (m_FileHandle && !SetBuffer(m_BufferMode,m_BufferSize))
{
diff --git a/source/modules/asura-utils/scripting/portable.hpp b/source/modules/asura-utils/scripting/portable.hpp
index 2716da4..d691455 100644
--- a/source/modules/asura-utils/scripting/portable.hpp
+++ b/source/modules/asura-utils/scripting/portable.hpp
@@ -12,22 +12,18 @@ extern "C" {
namespace AsuraEngine
{
- namespace Scripting
- {
-
- ///
- /// ҪΪ࣬userdatamember ref̳д࣬ע̳С
- ///
- using Object = Luax::LuaxObject;
-
- ///
- /// ҪעluanativeҪ̳дģ塣BASEָ࣬ĬLuaxObjectָ
- /// LuaxObjectࡢ
- ///
- template<typename TYPE, typename BASE = Luax::LuaxObject>
- using Portable = Luax::LuaxNativeClass<TYPE, BASE>;
-
- }
+namespace Scripting
+{
+
+
+/// ҪΪ࣬userdatamember ref̳д࣬ע̳С
+using Object = Luax::LuaxObject;
+
+/// ҪעluanativeҪ̳дģ塣BASEָ࣬ĬLuaxObjectָLuaxObjectࡢ
+template<typename TYPE, typename BASE = Luax::LuaxObject>
+using Portable = Luax::LuaxNativeClass<TYPE, BASE>;
+
+}
}
namespace AEScripting = AsuraEngine::Scripting;
diff --git a/source/modules/asura-utils/threading/binding/_coroutine.cpp b/source/modules/asura-utils/threading/binding/_coroutine.cpp
index 7f74cca..a710623 100644
--- a/source/modules/asura-utils/threading/binding/_coroutine.cpp
+++ b/source/modules/asura-utils/threading/binding/_coroutine.cpp
@@ -2,10 +2,8 @@
using namespace std;
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
LUAX_REGISTRY(Coroutine)
{
diff --git a/source/modules/asura-utils/threading/binding/_thread.cpp b/source/modules/asura-utils/threading/binding/_thread.cpp
index ae43242..b835453 100644
--- a/source/modules/asura-utils/threading/binding/_thread.cpp
+++ b/source/modules/asura-utils/threading/binding/_thread.cpp
@@ -2,10 +2,8 @@
using namespace std;
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
LUAX_REGISTRY(Thread)
{
diff --git a/source/modules/asura-utils/threading/conditional.cpp b/source/modules/asura-utils/threading/conditional.cpp
index e49bfde..c4d32d9 100644
--- a/source/modules/asura-utils/threading/conditional.cpp
+++ b/source/modules/asura-utils/threading/conditional.cpp
@@ -1,86 +1,84 @@
#include "conditional.h"
-namespace AsuraEngine
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
+
+Conditional::Conditional()
+ : m_Waiting(0)
+ , m_Signals(0)
{
- namespace Threading
- {
+}
- Conditional::Conditional()
- : m_Waiting(0)
- , m_Signals(0)
- {
- }
+Conditional::~Conditional()
+{
+}
- Conditional::~Conditional()
- {
- }
+void Conditional::Signal()
+{
+ m_Mutex.Lock();
+ if (m_Waiting > m_Signals)
+ {
+ ++m_Signals;
+ signal(m_WaitSem);
+ m_Mutex.Unlock();
+ wait(m_DoneSem);
+ }
+ else
+ {
+ m_Mutex.Unlock();
+ }
+}
- void Conditional::Signal()
- {
- m_Mutex.Lock();
- if (m_Waiting > m_Signals)
- {
- ++m_Signals;
- signal(m_WaitSem);
- m_Mutex.Unlock();
- wait(m_DoneSem);
- }
- else
- {
- m_Mutex.Unlock();
- }
+void Conditional::Broadcast()
+{
+ m_Mutex.Lock();
+ if (m_Waiting> m_Signals) {
+ int i, num_waiting;
+
+ num_waiting = (m_Waiting - m_Signals);
+ m_Signals = m_Waiting;
+ for (i = 0; i < num_waiting; ++i) {
+ signal(m_WaitSem);
}
-
- void Conditional::Broadcast()
- {
- m_Mutex.Lock();
- if (m_Waiting> m_Signals) {
- int i, num_waiting;
-
- num_waiting = (m_Waiting - m_Signals);
- m_Signals = m_Waiting;
- for (i = 0; i < num_waiting; ++i) {
- signal(m_WaitSem);
- }
- m_Mutex.Unlock();
- for (i = 0; i < num_waiting; ++i) {
- wait(m_DoneSem);
- }
- }
- else {
- m_Mutex.Unlock();
- }
-
+ m_Mutex.Unlock();
+ for (i = 0; i < num_waiting; ++i) {
+ wait(m_DoneSem);
}
+ }
+ else {
+ m_Mutex.Unlock();
+ }
- bool Conditional::Wait(Mutex* mutex, int timeout /*= ASURA_MUTEX_MAXWAIT*/)
- {
- bool retval;
-
- m_Mutex.Lock();
- ++m_Waiting;
- m_Mutex.Unlock();
-
- mutex->Unlock();
+}
- retval = wait(m_WaitSem, timeout);
+bool Conditional::Wait(Mutex* mutex, int timeout /*= ASURA_MUTEX_MAXWAIT*/)
+{
+ bool retval;
- m_Mutex.Lock();
- if (m_Signals > 0) {
- if (!retval) {
- wait(m_WaitSem);
- }
- signal(m_DoneSem);
+ m_Mutex.Lock();
+ ++m_Waiting;
+ m_Mutex.Unlock();
- --m_Signals;
- }
- --m_Waiting;
- m_Mutex.Unlock();
+ mutex->Unlock();
- m_Mutex.Lock();
+ retval = wait(m_WaitSem, timeout);
- return retval;
+ m_Mutex.Lock();
+ if (m_Signals > 0) {
+ if (!retval) {
+ wait(m_WaitSem);
}
+ signal(m_DoneSem);
+ --m_Signals;
}
-} \ No newline at end of file
+ --m_Waiting;
+ m_Mutex.Unlock();
+
+ m_Mutex.Lock();
+
+ return retval;
+}
+
+namespace_end
+namespace_end \ No newline at end of file
diff --git a/source/modules/asura-utils/threading/conditional.h b/source/modules/asura-utils/threading/conditional.h
index 3aee392..ff832ac 100644
--- a/source/modules/asura-utils/threading/conditional.h
+++ b/source/modules/asura-utils/threading/conditional.h
@@ -1,41 +1,41 @@
#ifndef __ASURA_CONDITIONAL_H__
#define __ASURA_CONDITIONAL_H__
+#include <asura-utils/classes.h>
+
#include "mutex.h"
#include "semaphore.h"
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
- ///
- ///
- ///
- class Conditional
- {
- public:
+///
+///
+///
+class Conditional
+{
+public:
- Conditional();
- ~Conditional();
+ Conditional();
+ ~Conditional();
- void Signal();
- void Broadcast();
- bool Wait(Mutex* mutex, int timeout = ASURA_MUTEX_MAXWAIT);
+ void Signal();
+ void Broadcast();
+ bool Wait(Mutex* mutex, int timeout = ASURA_MUTEX_MAXWAIT);
- private:
+private:
- Mutex m_Mutex;
+ Mutex m_Mutex;
- Semaphore m_WaitSem;
- Semaphore m_DoneSem;
+ Semaphore m_WaitSem;
+ Semaphore m_DoneSem;
- int m_Waiting;
- int m_Signals;
+ int m_Waiting;
+ int m_Signals;
- };
+};
- }
-}
+namespace_end
+namespace_end
#endif \ No newline at end of file
diff --git a/source/modules/asura-utils/threading/coroutine.cpp b/source/modules/asura-utils/threading/coroutine.cpp
index 9f65c5f..552a415 100644
--- a/source/modules/asura-utils/threading/coroutine.cpp
+++ b/source/modules/asura-utils/threading/coroutine.cpp
@@ -1,16 +1,15 @@
#include "coroutine.h"
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
+
/*
- Coroutine::Coroutine()
- {
+Coroutine::Coroutine()
+{
- }
+}
*/
- }
-}
+namespace_end
+namespace_end
diff --git a/source/modules/asura-utils/threading/coroutine.h b/source/modules/asura-utils/threading/coroutine.h
index bd0f922..830dcd2 100644
--- a/source/modules/asura-utils/threading/coroutine.h
+++ b/source/modules/asura-utils/threading/coroutine.h
@@ -1,39 +1,39 @@
#ifndef __ASURA_COROUTINE_H__
#define __ASURA_COROUTINE_H__
+#include <asura-utils/classes.h>
+
#include "../scripting/portable.hpp"
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
- ///
- /// luaЭ̣һЩ߼
- ///
- class Coroutine ASURA_FINAL
- : public AEScripting::Portable<Coroutine>
- {
- public:
+///
+/// luaЭ̣һЩ߼
+///
+class Coroutine ASURA_FINAL
+ : public AEScripting::Portable<Coroutine>
+{
+public:
- LUAX_DECL_FACTORY(Coroutine);
+ LUAX_DECL_FACTORY(Coroutine);
- private:
+private:
- ///
- /// ǰЭ̵state
- ///
- lua_State* m_ThreadState;
+ ///
+ /// ǰЭ̵state
+ ///
+ lua_State* m_ThreadState;
- LUAX_DECL_METHOD(_New);
- LUAX_DECL_METHOD(_Run);
+ LUAX_DECL_METHOD(_New);
+ LUAX_DECL_METHOD(_Run);
- };
+};
- }
-}
+namespace_end
+namespace_end
namespace AEThreading = AsuraEngine::Threading;
diff --git a/source/modules/asura-utils/threading/mutex.cpp b/source/modules/asura-utils/threading/mutex.cpp
index a36af1b..ff0b3fa 100644
--- a/source/modules/asura-utils/threading/mutex.cpp
+++ b/source/modules/asura-utils/threading/mutex.cpp
@@ -2,106 +2,104 @@
#include "mutex.h"
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
#define try_create_mutex(impl)\
- if (!m_Impl) \
- { \
- try \
- { \
- m_Impl = new impl(); \
- } \
- catch (Exception& e) \
- { \
- m_Impl = nullptr; \
- } \
- }
-
- Mutex::Mutex()
- : m_Impl(nullptr)
- {
+if (!m_Impl) \
+{ \
+try \
+{ \
+ m_Impl = new impl(); \
+} \
+catch (Exception& e) \
+{ \
+ m_Impl = nullptr; \
+} \
+}
+
+Mutex::Mutex()
+ : m_Impl(nullptr)
+{
#if ASURA_MUTEX_WIN32_CRITICLE_SECTION
- try_create_mutex(MutexImplWin32_CS);
+ try_create_mutex(MutexImplWin32_CS);
#endif
#if ASURA_MUTEX_WIN32_KERNAL_MUTEX
- try_create_mutex(MutexImplWin32_KM);
+ try_create_mutex(MutexImplWin32_KM);
#endif
- ASSERT(m_Impl);
- }
+ ASSERT(m_Impl);
+}
- Mutex::~Mutex()
- {
- if(m_Impl)
- delete m_Impl;
- }
+Mutex::~Mutex()
+{
+ if(m_Impl)
+ delete m_Impl;
+}
- void Mutex::Lock()
- {
- ASSERT(m_Impl);
+void Mutex::Lock()
+{
+ ASSERT(m_Impl);
- m_Impl->Lock();
- }
+ m_Impl->Lock();
+}
- void Mutex::Unlock()
- {
- ASSERT(m_Impl);
+void Mutex::Unlock()
+{
+ ASSERT(m_Impl);
- m_Impl->Unlock();
- }
+ m_Impl->Unlock();
+}
#if ASURA_MUTEX_WIN32_CRITICLE_SECTION
- MutexImplWin32_CS::MutexImplWin32_CS()
- {
- ::InitializeCriticalSection(&m_Mutex);
- }
+MutexImplWin32_CS::MutexImplWin32_CS()
+{
+ ::InitializeCriticalSection(&m_Mutex);
+}
- MutexImplWin32_CS::~MutexImplWin32_CS()
- {
- ::DeleteCriticalSection(&m_Mutex);
- }
+MutexImplWin32_CS::~MutexImplWin32_CS()
+{
+ ::DeleteCriticalSection(&m_Mutex);
+}
- void MutexImplWin32_CS::Lock()
- {
- ::EnterCriticalSection(&m_Mutex);
- }
+void MutexImplWin32_CS::Lock()
+{
+ ::EnterCriticalSection(&m_Mutex);
+}
- void MutexImplWin32_CS::Unlock()
- {
- ::LeaveCriticalSection(&m_Mutex);
- }
+void MutexImplWin32_CS::Unlock()
+{
+ ::LeaveCriticalSection(&m_Mutex);
+}
#endif // ASURA_MUTEX_WIN32_CRITICLE_SECTION
#if ASURA_MUTEX_WIN32_KERNAL_MUTEX
- MutexImplWin32_KM::MutexImplWin32_KM()
- {
- m_Handle = ::CreateMutex(NULL, FALSE, NULL);
- if (!m_Handle)
- throw Exception("Cant use win32 mutex.");
- }
-
- MutexImplWin32_KM::~MutexImplWin32_KM()
- {
- ::CloseHandle(m_Handle);
- m_Handle = NULL;
- }
-
- void MutexImplWin32_KM::Lock()
- {
- ::WaitForSingleObject(m_Handle, ASURA_MUTEX_MAXWAIT);
- }
-
- void MutexImplWin32_KM::Unlock()
- {
- ::ReleaseMutex(m_Handle);
- }
+MutexImplWin32_KM::MutexImplWin32_KM()
+{
+ m_Handle = ::CreateMutex(NULL, FALSE, NULL);
+ if (!m_Handle)
+ throw Exception("Cant use win32 mutex.");
+}
+
+MutexImplWin32_KM::~MutexImplWin32_KM()
+{
+ ::CloseHandle(m_Handle);
+ m_Handle = NULL;
+}
+
+void MutexImplWin32_KM::Lock()
+{
+ ::WaitForSingleObject(m_Handle, ASURA_MUTEX_MAXWAIT);
+}
+
+void MutexImplWin32_KM::Unlock()
+{
+ ::ReleaseMutex(m_Handle);
+}
#endif // ASURA_MUTEX_WIN32_KERNAL_MUTEX
- }
-} \ No newline at end of file
+namespace_end
+namespace_end \ No newline at end of file
diff --git a/source/modules/asura-utils/threading/mutex.h b/source/modules/asura-utils/threading/mutex.h
index 9e9d2c2..623a3db 100644
--- a/source/modules/asura-utils/threading/mutex.h
+++ b/source/modules/asura-utils/threading/mutex.h
@@ -2,127 +2,126 @@
#define __ASURA_MUTEX_H__
#include <asura-utils/type.h>
+#include <asura-utils/classes.h>
#include "../utils_config.h"
#if ASURA_THREAD_WIN32
- #include <windows.h>
+#include <windows.h>
#endif
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
#define ASURA_MUTEX_MAXWAIT (~(uint32)0)
- class MutexImpl;
+class MutexImpl;
- class Mutex
- {
- public:
+class Mutex
+{
+public:
- Mutex();
- ~Mutex();
+ Mutex();
+ ~Mutex();
- void Lock();
- void Unlock();
+ void Lock();
+ void Unlock();
- private:
+private:
- // ֹ
- Mutex(const Mutex&);
- Mutex& operator=(const Mutex&);
+ // ֹ
+ Mutex(const Mutex&);
+ Mutex& operator=(const Mutex&);
- MutexImpl* m_Impl;
+ MutexImpl* m_Impl;
- };
+};
- class _mutex_locker
- {
- public:
- _mutex_locker(Mutex& mutex)
- : m(mutex)
- {
- m.Lock();
- };
- ~_mutex_locker()
- {
- m.Unlock();
- }
- operator bool() { return false; };
- private:
- void* operator new(size_t);
- Mutex& m;
- };
+class _mutex_locker
+{
+public:
+ _mutex_locker(Mutex& mutex)
+ : m(mutex)
+ {
+ m.Lock();
+ };
+ ~_mutex_locker()
+ {
+ m.Unlock();
+ }
+ operator bool() { return false; };
+private:
+ void* operator new(size_t);
+ Mutex& m;
+};
#define lock(m) \
- if(_mutex_locker _asura_mutex_locker = m){} else
+if(_mutex_locker _asura_mutex_locker = m){} else
- ASURA_ABSTRACT class MutexImpl
- {
- public:
+ASURA_ABSTRACT class MutexImpl
+{
+public:
- MutexImpl() {};
- virtual ~MutexImpl() {};
+ MutexImpl() {};
+ virtual ~MutexImpl() {};
- virtual void Lock() = 0;
- virtual void Unlock() = 0;
+ 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:
+//https://blog.csdn.net/l799623787/article/details/18259949
+class MutexImplWin32_CS ASURA_FINAL : public MutexImpl
+{
+public:
- MutexImplWin32_CS();
- ~MutexImplWin32_CS();
+ MutexImplWin32_CS();
+ ~MutexImplWin32_CS();
- void Lock() override;
- void Unlock() override;
+ void Lock() override;
+ void Unlock() override;
- private:
+private:
- //HANDLE m_Handle;
- CRITICAL_SECTION m_Mutex;
+ //HANDLE m_Handle;
+ CRITICAL_SECTION m_Mutex;
- };
+};
#endif // ASURA_MUTEX_WIN32_CRITICLE_SECTION
#if ASURA_MUTEX_WIN32_KERNAL_MUTEX
- class MutexImplWin32_KM ASURA_FINAL : public MutexImpl
- {
- public:
+class MutexImplWin32_KM ASURA_FINAL : public MutexImpl
+{
+public:
- MutexImplWin32_KM();
- ~MutexImplWin32_KM();
+ MutexImplWin32_KM();
+ ~MutexImplWin32_KM();
- void Lock() override;
- void Unlock() override;
+ void Lock() override;
+ void Unlock() override;
- private:
+private:
- HANDLE m_Handle;
+ HANDLE m_Handle;
- };
+};
#endif // ASURA_MUTEX_WIN32_KERNAL_MUTEX
#if ASURA_THREAD_STD
- class MutexImplSTD ASURA_FINAL : public MutexImpl
- {
- };
+class MutexImplSTD ASURA_FINAL : public MutexImpl
+{
+};
#endif // ASURA_THREAD_STD
- }
-}
+namespace_end
+namespace_end
namespace AEThreading = AsuraEngine::Threading;
diff --git a/source/modules/asura-utils/threading/semaphore.cpp b/source/modules/asura-utils/threading/semaphore.cpp
index 7e5ccf5..f9ffb35 100644
--- a/source/modules/asura-utils/threading/semaphore.cpp
+++ b/source/modules/asura-utils/threading/semaphore.cpp
@@ -4,98 +4,96 @@
#include "mutex.h"
#include "semaphore.h"
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
#define try_create_semaphore(impl) \
- if (!m_Impl) \
- { \
- try \
- { \
- m_Impl = new impl(init_count); \
- } \
- catch (Exception& e) \
- { \
- m_Impl = nullptr; \
- } \
- }
+if (!m_Impl) \
+{ \
+ try \
+ { \
+ m_Impl = new impl(init_count); \
+ } \
+ catch (Exception& e) \
+ { \
+ m_Impl = nullptr; \
+ } \
+}
- Semaphore::Semaphore(unsigned int init_count)
- : m_Impl(nullptr)
- {
+Semaphore::Semaphore(unsigned int init_count)
+ : m_Impl(nullptr)
+{
#ifdef ASURA_THREAD_WIN32
- try_create_semaphore(SemaphoreWin32);
+ try_create_semaphore(SemaphoreWin32);
#endif
- //ASSERT(m_Impl);
- }
+ //ASSERT(m_Impl);
+}
- Semaphore::~Semaphore()
- {
- if (m_Impl) delete m_Impl;
- }
+Semaphore::~Semaphore()
+{
+ if (m_Impl) delete m_Impl;
+}
- void Semaphore::Signal()
- {
- ASSERT(m_Impl);
- m_Impl->Signal();
- }
+void Semaphore::Signal()
+{
+ ASSERT(m_Impl);
+ m_Impl->Signal();
+}
- bool Semaphore::Wait(int timeout /*= ASURA_MUTEX_MAXWAIT*/)
- {
- ASSERT(m_Impl);
- return m_Impl->Wait(timeout);
- }
+bool Semaphore::Wait(int timeout /*= ASURA_MUTEX_MAXWAIT*/)
+{
+ ASSERT(m_Impl);
+ return m_Impl->Wait(timeout);
+}
#if ASURA_THREAD_WIN32
- SemaphoreWin32::SemaphoreWin32(unsigned int init_value)
- : SemaphoreImpl(init_value)
- {
- // UINT_MAX get error.
- m_Sem = CreateSemaphore(NULL, init_value, INT_MAX, NULL);
- if (!m_Sem)
- {
- int errorCode = GetLastError();
- throw Exception("Cant use win32 semaphore. Error code: %d.", errorCode);
- }
- }
+SemaphoreWin32::SemaphoreWin32(unsigned int init_value)
+ : SemaphoreImpl(init_value)
+{
+ // UINT_MAX get error.
+ m_Sem = CreateSemaphore(NULL, init_value, INT_MAX, NULL);
+ if (!m_Sem)
+ {
+ int errorCode = GetLastError();
+ throw Exception("Cant use win32 semaphore. Error code: %d.", errorCode);
+ }
+}
- SemaphoreWin32::~SemaphoreWin32()
- {
- CloseHandle(m_Sem);
- }
+SemaphoreWin32::~SemaphoreWin32()
+{
+ CloseHandle(m_Sem);
+}
- void SemaphoreWin32::Signal()
- {
- InterlockedIncrement(&m_Count);
- if (ReleaseSemaphore(m_Sem, 1, NULL) == FALSE)
- InterlockedDecrement(&m_Count);
- }
+void SemaphoreWin32::Signal()
+{
+ InterlockedIncrement(&m_Count);
+ if (ReleaseSemaphore(m_Sem, 1, NULL) == FALSE)
+ InterlockedDecrement(&m_Count);
+}
- bool SemaphoreWin32::Wait(int timeout)
- {
- int result;
- result = WaitForSingleObject(m_Sem, timeout);
- if (result == WAIT_OBJECT_0)
- {
- InterlockedDecrement(&m_Count);
- return true;
- }
- else if(result == WAIT_TIMEOUT)
- {
- // ʱ
- return false;
- }
- else
- {
- // δ֪
- throw Exception("WaitForSingleObject() failed");
- }
- }
+bool SemaphoreWin32::Wait(int timeout)
+{
+ int result;
+ result = WaitForSingleObject(m_Sem, timeout);
+ if (result == WAIT_OBJECT_0)
+ {
+ InterlockedDecrement(&m_Count);
+ return true;
+ }
+ else if(result == WAIT_TIMEOUT)
+ {
+ // ʱ
+ return false;
+ }
+ else
+ {
+ // δ֪
+ throw Exception("WaitForSingleObject() failed");
+ }
+}
#endif // ASURA_THREAD_WIN32
- }
-} \ No newline at end of file
+namespace_end
+namespace_end \ No newline at end of file
diff --git a/source/modules/asura-utils/threading/semaphore.h b/source/modules/asura-utils/threading/semaphore.h
index c75ae8a..ae7b10b 100644
--- a/source/modules/asura-utils/threading/semaphore.h
+++ b/source/modules/asura-utils/threading/semaphore.h
@@ -7,64 +7,62 @@
#include <windows.h>
#endif
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
- class SemaphoreImpl;
+class SemaphoreImpl;
- ///
- /// ź
- ///
- class Semaphore
- {
- public:
+///
+/// ź
+///
+class Semaphore
+{
+public:
- Semaphore(unsigned int init_count = 1);
- ~Semaphore();
+ Semaphore(unsigned int init_count = 1);
+ ~Semaphore();
- void Signal();
- bool Wait(int timeout = ASURA_MUTEX_MAXWAIT);
+ void Signal();
+ bool Wait(int timeout = ASURA_MUTEX_MAXWAIT);
- private:
- SemaphoreImpl* m_Impl;
- };
+private:
+ SemaphoreImpl* m_Impl;
+};
- class SemaphoreImpl
- {
- public:
- SemaphoreImpl(unsigned int init_value)
- : m_Count(init_value)
- {
- };
- virtual ~SemaphoreImpl() {};
- virtual void Signal() = 0;
- virtual bool Wait(int timeout) = 0;
- inline int Current() { return m_Count; }
- protected:
- unsigned int m_Count;
- };
+class SemaphoreImpl
+{
+public:
+ SemaphoreImpl(unsigned int init_value)
+ : m_Count(init_value)
+ {
+ };
+ virtual ~SemaphoreImpl() {};
+ virtual void Signal() = 0;
+ virtual bool Wait(int timeout) = 0;
+ inline int Current() { return m_Count; }
+protected:
+ unsigned int m_Count;
+};
#define wait(sem, ...) sem.Wait(__VA_ARGS__)
#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 m_Sem;
- };
+class SemaphoreWin32 : public SemaphoreImpl
+{
+public:
+ SemaphoreWin32(unsigned int init_value);
+ ~SemaphoreWin32();
+ void Signal() override;
+ bool Wait(int timeout) override;
+private:
+ HANDLE m_Sem;
+};
#endif // ASURA_THREAD_WIN32
- }
-}
+namespace_end
+namespace_end
#endif \ No newline at end of file
diff --git a/source/modules/asura-utils/threading/task.cpp b/source/modules/asura-utils/threading/task.cpp
index decb74c..ea3f68e 100644
--- a/source/modules/asura-utils/threading/task.cpp
+++ b/source/modules/asura-utils/threading/task.cpp
@@ -3,10 +3,8 @@
using namespace AEScripting;
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
- }
-}
+namespace_end
+namespace_end
diff --git a/source/modules/asura-utils/threading/task.h b/source/modules/asura-utils/threading/task.h
index 6461fff..b959012 100644
--- a/source/modules/asura-utils/threading/task.h
+++ b/source/modules/asura-utils/threading/task.h
@@ -3,41 +3,40 @@
#include <asura-utils/type.h>
#include <asura-utils/scripting/portable.hpp>
+#include <asura-utils/classes.h>
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
- ///
- /// ϣһ̴߳񣬼̳TaskдExecute
- ///
- ASURA_ABSTRACT class Task : public AEScripting::Object
- {
- public:
+///
+/// ϣһ̴߳񣬼̳TaskдExecute
+///
+ASURA_ABSTRACT class Task : public AEScripting::Object
+{
+public:
- Task() {};
- virtual ~Task() {};
+ Task() {};
+ virtual ~Task() {};
- ///
- /// ִɺ󷵻trueûص
- ///
- virtual bool Execute() = 0;
+ ///
+ /// ִɺ󷵻trueûص
+ ///
+ virtual bool Execute() = 0;
- ///
- /// ûصinvoke threadص
- ///
- virtual void Invoke(lua_State* invokeThreaad) = 0;
+ ///
+ /// ûصinvoke threadص
+ ///
+ virtual void Invoke(lua_State* invokeThreaad) = 0;
- protected:
+protected:
- // ȡص
- Luax::LuaxMemberRef m_Callback;
+ // ȡص
+ Luax::LuaxMemberRef m_Callback;
- };
+};
- }
-}
+namespace_end
+namespace_end
namespace AEThreading = AsuraEngine::Threading;
diff --git a/source/modules/asura-utils/threading/thread.cpp b/source/modules/asura-utils/threading/thread.cpp
index 48f287f..cb71d32 100644
--- a/source/modules/asura-utils/threading/thread.cpp
+++ b/source/modules/asura-utils/threading/thread.cpp
@@ -5,285 +5,283 @@
#include "thread_impl_sdl.h"
#include "thread_impl_std.h"
-namespace AsuraEngine
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
+
+Thread::Thread(lua_State* luaThread, ThreadType type /*= THREAD_TYPE_DEFERRED*/, uint sleepTime /*= 0*/, const std::string& name /*= ""*/)
+ : m_Name(name)
+ , m_State(THREAD_STATE_IDLE)
+ , m_Type(type)
+ , m_LuaThread(luaThread)
+ , m_CallbackThread(nullptr)
+ , m_SleepTime(sleepTime)
{
- namespace Threading
+ LUAX_STATE(luaThread);
+ if (type == THREAD_TYPE_IMMEDIATE)
{
+ Luax::LuaxVM* vm = state.GetVM();
+ ASSERT(vm);
+ m_CallbackThread = vm->CreateThread();
+ ASSERT(m_CallbackThread);
+ SetLuaxMemberRef(state, m_CallbackThreadRef, -1);
+ state.Pop(); // callback thread
+ }
+}
- Thread::Thread(lua_State* luaThread, ThreadType type /*= THREAD_TYPE_DEFERRED*/, uint sleepTime /*= 0*/, const std::string& name /*= ""*/)
- : m_Name(name)
- , m_State(THREAD_STATE_IDLE)
- , m_Type(type)
- , m_LuaThread(luaThread)
- , m_CallbackThread(nullptr)
- , m_SleepTime(sleepTime)
- {
- LUAX_STATE(luaThread);
- if (type == THREAD_TYPE_IMMEDIATE)
- {
- Luax::LuaxVM* vm = state.GetVM();
- ASSERT(vm);
- m_CallbackThread = vm->CreateThread();
- ASSERT(m_CallbackThread);
- SetLuaxMemberRef(state, m_CallbackThreadRef, -1);
- state.Pop(); // callback thread
- }
- }
-
- Thread::~Thread()
- {
- if (m_Impl)
- {
- delete m_Impl;
- m_Impl = nullptr;
- }
- }
+Thread::~Thread()
+{
+ if (m_Impl)
+ {
+ delete m_Impl;
+ m_Impl = nullptr;
+ }
+}
- bool Thread::AddTask(Task* task)
- {
- lock(m_TaskQueueMutex)
- {
- task->Retain();
- m_TaskQueue.push(task);
- }
- return true;
- }
+bool Thread::AddTask(Task* task)
+{
+ lock(m_TaskQueueMutex)
+ {
+ task->Retain();
+ m_TaskQueue.push(task);
+ }
+ return true;
+}
- uint Thread::GetTaskCount()
- {
- return m_TaskQueue.size();
- }
+uint Thread::GetTaskCount()
+{
+ return m_TaskQueue.size();
+}
- void Thread::Idle()
- {
- m_State = THREAD_STATE_IDLE;
- }
+void Thread::Idle()
+{
+ m_State = THREAD_STATE_IDLE;
+}
#define try_start_thread(impl)\
- if (!m_Impl) \
- { \
- m_Impl = new impl(); \
- if (!m_Impl->Start(this, stacksize)) \
- { \
- delete m_Impl; \
- m_Impl = nullptr; \
- } \
- }
-
- bool Thread::Start(bool isDaemon /*= true*/, uint32 stacksize /*= 0*/)
- {
- if (m_State != THREAD_STATE_IDLE)
- return false;
+if (!m_Impl) \
+{ \
+m_Impl = new impl(); \
+if (!m_Impl->Start(this, stacksize)) \
+{ \
+ delete m_Impl; \
+ m_Impl = nullptr; \
+} \
+}
+
+bool Thread::Start(bool isDaemon /*= true*/, uint32 stacksize /*= 0*/)
+{
+ if (m_State != THREAD_STATE_IDLE)
+ return false;
- // Ѿһ֮ǰģر
- if (m_Impl)
- {
- delete m_Impl;
- m_Impl = nullptr;
- }
+ // Ѿһ֮ǰģر
+ if (m_Impl)
+ {
+ delete m_Impl;
+ m_Impl = nullptr;
+ }
#if ASURA_THREAD_WIN32
- try_start_thread(ThreadImplWin32);
+ try_start_thread(ThreadImplWin32);
#endif
- if (!m_Impl)
- return false;
+ if (!m_Impl)
+ return false;
- m_IsDaemon = isDaemon;
- m_StateMutex.Lock();
- m_State = THREAD_STATE_RUNNING;
- m_StateMutex.Unlock();
- }
+ m_IsDaemon = isDaemon;
+ m_StateMutex.Lock();
+ m_State = THREAD_STATE_RUNNING;
+ m_StateMutex.Unlock();
+}
- void Thread::Pause()
- {
- ASSERT(m_Impl);
+void Thread::Pause()
+{
+ ASSERT(m_Impl);
- lock(m_StateMutex)
- {
- m_State = THREAD_STATE_PAUSED;
- }
- }
+ lock(m_StateMutex)
+ {
+ m_State = THREAD_STATE_PAUSED;
+ }
+}
- void Thread::Resume()
- {
- ASSERT(m_Impl);
+void Thread::Resume()
+{
+ ASSERT(m_Impl);
- lock(m_StateMutex)
- {
- if (m_State == THREAD_STATE_PAUSED)
- m_State = THREAD_STATE_RUNNING;
- }
- }
+ lock(m_StateMutex)
+ {
+ if (m_State == THREAD_STATE_PAUSED)
+ m_State = THREAD_STATE_RUNNING;
+ }
+}
- void Thread::Stop()
- {
- ASSERT(m_Impl);
+void Thread::Stop()
+{
+ ASSERT(m_Impl);
- lock(m_StateMutex)
- {
- m_State = THREAD_STATE_STOPPED;
- }
- }
+ lock(m_StateMutex)
+ {
+ m_State = THREAD_STATE_STOPPED;
+ }
+}
- void Thread::PauseSync()
- {
- Pause();
- wait(m_SemPause);
- }
+void Thread::PauseSync()
+{
+ Pause();
+ wait(m_SemPause);
+}
- void Thread::ResumeSync()
- {
- Resume();
- wait(m_SemResume);
- }
+void Thread::ResumeSync()
+{
+ Resume();
+ wait(m_SemResume);
+}
- void Thread::StopSync()
- {
- Stop();
- wait(m_SemStop);
- }
+void Thread::StopSync()
+{
+ Stop();
+ wait(m_SemStop);
+}
- void Thread::Join()
- {
- ASSERT(m_Impl);
- m_Impl->Join();
- }
+void Thread::Join()
+{
+ ASSERT(m_Impl);
+ m_Impl->Join();
+}
- ThreadState Thread::GetState()
- {
- ThreadState state;
- lock(m_StateMutex)
- {
- state = m_State;
- }
- return state;
- }
+ThreadState Thread::GetState()
+{
+ ThreadState state;
+ lock(m_StateMutex)
+ {
+ state = m_State;
+ }
+ return state;
+}
- bool Thread::IsRunning()
- {
- ASSERT(m_Impl);
+bool Thread::IsRunning()
+{
+ ASSERT(m_Impl);
- return GetState() == THREAD_STATE_RUNNING;
- }
+ return GetState() == THREAD_STATE_RUNNING;
+}
- bool Thread::IsPaused()
- {
- ASSERT(m_Impl);
+bool Thread::IsPaused()
+{
+ ASSERT(m_Impl);
- return GetState() == THREAD_STATE_PAUSED;
- }
+ return GetState() == THREAD_STATE_PAUSED;
+}
- bool Thread::IsStopped()
- {
- ASSERT(m_Impl);
+bool Thread::IsStopped()
+{
+ ASSERT(m_Impl);
- return GetState() == THREAD_STATE_STOPPED;
- }
+ return GetState() == THREAD_STATE_STOPPED;
+}
- bool Thread::IsCurrent()
- {
- ASSERT(m_Impl);
+bool Thread::IsCurrent()
+{
+ ASSERT(m_Impl);
- return m_Impl->IsCurrent();
- }
+ return m_Impl->IsCurrent();
+}
- const std::string& Thread::GetName()
- {
- return m_Name;
- }
+const std::string& Thread::GetName()
+{
+ return m_Name;
+}
- int Thread::Process()
- {
- LUAX_STATE(m_LuaThread);
+int Thread::Process()
+{
+ LUAX_STATE(m_LuaThread);
- do{
- if (IsRunning())
+ do{
+ if (IsRunning())
+ {
+ while (!m_TaskQueue.empty())
+ {
+ Task* task = m_TaskQueue.front();
+ if (task && task->Execute())
{
- while (!m_TaskQueue.empty())
+ if (m_Type == THREAD_TYPE_DEFERRED)
+ {
+ m_FinishedMutex.Lock();
+ task->Retain();
+ m_FinishedTasks.push(task);
+ m_FinishedMutex.Unlock();
+ }
+ else if (m_Type == THREAD_TYPE_IMMEDIATE)
{
- Task* task = m_TaskQueue.front();
- if (task && task->Execute())
- {
- if (m_Type == THREAD_TYPE_DEFERRED)
- {
- m_FinishedMutex.Lock();
- task->Retain();
- m_FinishedTasks.push(task);
- m_FinishedMutex.Unlock();
- }
- else if (m_Type == THREAD_TYPE_IMMEDIATE)
- {
- // unsafe
- task->Invoke(m_CallbackThread);
- this->LuaxRelease<Task>(state, task);
- }
- m_TaskQueueMutex.Lock();
- m_TaskQueue.pop();
- task->Release();
- m_TaskQueueMutex.Unlock();
- }
+ // unsafe
+ task->Invoke(m_CallbackThread);
+ this->LuaxRelease<Task>(state, task);
}
+ m_TaskQueueMutex.Lock();
+ m_TaskQueue.pop();
+ task->Release();
+ m_TaskQueueMutex.Unlock();
}
+ }
+ }
- // ˳ѭ
- if (IsStopped())
- break;
-
- // CPUʹ
- Sleep(m_SleepTime);
-
- } while (m_IsDaemon);
+ // ˳ѭ
+ if (IsStopped())
+ break;
- // ػ̣߳еstop״̬
- if (!m_IsDaemon)
- Stop();
+ // CPUʹ
+ Sleep(m_SleepTime);
- signal(m_SemStop);
+ } while (m_IsDaemon);
- // ״̬ΪIdle
- Idle();
+ // ػ̣߳еstop״̬
+ if (!m_IsDaemon)
+ Stop();
- return 0;
- }
+ signal(m_SemStop);
- ///
- /// ӳģʽص
- ///
- void Thread::Dispatch()
- {
- if (m_Type != THREAD_TYPE_DEFERRED)
- return;
+ // ״̬ΪIdle
+ Idle();
- LUAX_STATE(m_LuaThread);
- while (!m_FinishedTasks.empty())
- {
- Task* task = m_FinishedTasks.front();
- if (task)
- {
- task->Invoke(m_LuaThread);
- this->LuaxRelease<Task>(state, task);
- m_FinishedMutex.Lock();
- m_FinishedTasks.pop();
- task->Release();
- m_FinishedMutex.Unlock();
- }
- }
- }
+ return 0;
+}
- void Thread::Sleep(uint ms)
- {
- ASSERT(m_Impl);
- if (m_Impl)
- {
- m_Impl->Sleep(ms);
- }
- }
+///
+/// ӳģʽص
+///
+void Thread::Dispatch()
+{
+ if (m_Type != THREAD_TYPE_DEFERRED)
+ return;
- void Thread::SetSleepTime(uint ms)
+ LUAX_STATE(m_LuaThread);
+ while (!m_FinishedTasks.empty())
+ {
+ Task* task = m_FinishedTasks.front();
+ if (task)
{
- m_SleepTime = ms;
+ task->Invoke(m_LuaThread);
+ this->LuaxRelease<Task>(state, task);
+ m_FinishedMutex.Lock();
+ m_FinishedTasks.pop();
+ task->Release();
+ m_FinishedMutex.Unlock();
}
+ }
+}
+void Thread::Sleep(uint ms)
+{
+ ASSERT(m_Impl);
+ if (m_Impl)
+ {
+ m_Impl->Sleep(ms);
}
-} \ No newline at end of file
+}
+
+void Thread::SetSleepTime(uint ms)
+{
+ m_SleepTime = ms;
+}
+
+namespace_end
+namespace_end \ No newline at end of file
diff --git a/source/modules/asura-utils/threading/thread.h b/source/modules/asura-utils/threading/thread.h
index 1de4e33..0235b6a 100644
--- a/source/modules/asura-utils/threading/thread.h
+++ b/source/modules/asura-utils/threading/thread.h
@@ -11,214 +11,212 @@
#include "semaphore.h"
#include "threadable.h"
-namespace AsuraEngine
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
+
+class ThreadImpl;
+
+///
+/// ̵߳ļֲͬʵ֣
+/// 1: Deferredӳģʽ߳ϵɺҪ̵ֶ߳Dispatch
+/// ̵߳ص첽Ϊͬlua_Stateͻ⡣
+/// 2: Immediateģʽÿһ߳άһlua_newthreadlua_State
+/// صڲͬlua_Stateеãⲻ̷ͬ߳ͬһlua_State
+///
+enum ThreadType
{
- namespace Threading
- {
-
- class ThreadImpl;
-
- ///
- /// ̵߳ļֲͬʵ֣
- /// 1: Deferredӳģʽ߳ϵɺҪ̵ֶ߳Dispatch
- /// ̵߳ص첽Ϊͬlua_Stateͻ⡣
- /// 2: Immediateģʽÿһ߳άһlua_newthreadlua_State
- /// صڲͬlua_Stateеãⲻ̷ͬ߳ͬһlua_State
- ///
- enum ThreadType
- {
- THREAD_TYPE_DEFERRED,
- THREAD_TYPE_IMMEDIATE, // unsafe
- };
-
- enum ThreadState
- {
- THREAD_STATE_IDLE, ///< ãδں˶
- THREAD_STATE_RUNNING, ///< ѭ
- THREAD_STATE_PAUSED, ///< ѭͣ
- THREAD_STATE_STOPPED, ///< ˳ѭ
- };
-
- ///
- /// ߳壬ÿ߳άһtask queue
- ///
- class Thread ASURA_FINAL
- : public AEScripting::Portable<Thread>
- , public Threadable
- {
- 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ȴֶstopijʱ̶ɺԶ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();
-
- ///
- /// ִС
- ///
- int Process() override;
-
- const std::string& GetName();
-
- ///
- /// ص
- ///
- void Dispatch();
-
- ///
- /// ߺ
- ///
- 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(_Dispatch);
- LUAX_DECL_METHOD(_GetName);
- LUAX_DECL_METHOD(_GetType);
- LUAX_DECL_METHOD(_GetState);
- LUAX_DECL_METHOD(_SetSleepTime);
-
- //----------------------------------------------------------------------------//
-
- ThreadImpl* m_Impl;
-
- lua_State* m_LuaThread;
-
- ///
- /// ˴Ƿػģʽ
- ///
- bool m_IsDaemon;
-
- std::string m_Name;
- ThreadType m_Type;
- uint m_SleepTime;
-
- ThreadState m_State;
- Mutex m_StateMutex;
-
- ///
- /// ͬصź
- ///
- Semaphore m_SemPause;
- Semaphore m_SemResume;
- Semaphore m_SemStop;
-
- ///
- /// С
- ///
- std::queue<Task*> m_TaskQueue;
- Mutex m_TaskQueueMutex;
-
- ///
- /// ӳģʽʹ
- ///
- std::queue<Task*> m_FinishedTasks;
- Mutex m_FinishedMutex;
-
- ///
- /// ģʽʹãصʹõlua߳
- ///
- lua_State* m_CallbackThread;
- Luax::LuaxMemberRef m_CallbackThreadRef;
-
- };
-
- ///
- /// ̵߳ľʵ֣û͸ģһ׼ֲԣ
- /// 1: win32
- /// 2: posix
- /// 3: SDL
- /// 4: std::thread
- ///
- ASURA_ABSTRACT class ThreadImpl
- {
- public:
- ThreadImpl() {};
- virtual ~ThreadImpl() {};
-
- virtual bool Start(Threadable* 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;
-
- };
-
- }
-}
+ THREAD_TYPE_DEFERRED,
+ THREAD_TYPE_IMMEDIATE, // unsafe
+};
+
+enum ThreadState
+{
+ THREAD_STATE_IDLE, ///< ãδں˶
+ THREAD_STATE_RUNNING, ///< ѭ
+ THREAD_STATE_PAUSED, ///< ѭͣ
+ THREAD_STATE_STOPPED, ///< ˳ѭ
+};
+
+///
+/// ߳壬ÿ߳άһtask queue
+///
+class Thread ASURA_FINAL
+ : public AEScripting::Portable<Thread>
+ , public Threadable
+{
+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ȴֶstopijʱ̶ɺԶ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();
+
+ ///
+ /// ִС
+ ///
+ int Process() override;
+
+ const std::string& GetName();
+
+ ///
+ /// ص
+ ///
+ void Dispatch();
+
+ ///
+ /// ߺ
+ ///
+ 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(_Dispatch);
+ LUAX_DECL_METHOD(_GetName);
+ LUAX_DECL_METHOD(_GetType);
+ LUAX_DECL_METHOD(_GetState);
+ LUAX_DECL_METHOD(_SetSleepTime);
+
+ //----------------------------------------------------------------------------//
+
+ ThreadImpl* m_Impl;
+
+ lua_State* m_LuaThread;
+
+ ///
+ /// ˴Ƿػģʽ
+ ///
+ bool m_IsDaemon;
+
+ std::string m_Name;
+ ThreadType m_Type;
+ uint m_SleepTime;
+
+ ThreadState m_State;
+ Mutex m_StateMutex;
+
+ ///
+ /// ͬصź
+ ///
+ Semaphore m_SemPause;
+ Semaphore m_SemResume;
+ Semaphore m_SemStop;
+
+ ///
+ /// С
+ ///
+ std::queue<Task*> m_TaskQueue;
+ Mutex m_TaskQueueMutex;
+
+ ///
+ /// ӳģʽʹ
+ ///
+ std::queue<Task*> m_FinishedTasks;
+ Mutex m_FinishedMutex;
+
+ ///
+ /// ģʽʹãصʹõlua߳
+ ///
+ lua_State* m_CallbackThread;
+ Luax::LuaxMemberRef m_CallbackThreadRef;
+
+};
+
+///
+/// ̵߳ľʵ֣û͸ģһ׼ֲԣ
+/// 1: win32
+/// 2: posix
+/// 3: SDL
+/// 4: std::thread
+///
+ASURA_ABSTRACT class ThreadImpl
+{
+public:
+ ThreadImpl() {};
+ virtual ~ThreadImpl() {};
+
+ virtual bool Start(Threadable* 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;
+
+};
+
+namespace_end
+namespace_end
#endif \ No newline at end of file
diff --git a/source/modules/asura-utils/threading/thread_impl_posix.cpp b/source/modules/asura-utils/threading/thread_impl_posix.cpp
index d689353..d59bd8f 100644
--- a/source/modules/asura-utils/threading/thread_impl_posix.cpp
+++ b/source/modules/asura-utils/threading/thread_impl_posix.cpp
@@ -1,11 +1,9 @@
#include "thread_impl_posix.h"
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
- }
-} \ No newline at end of file
+namespace_end
+namespace_end \ No newline at end of file
diff --git a/source/modules/asura-utils/threading/thread_impl_posix.h b/source/modules/asura-utils/threading/thread_impl_posix.h
index e69de29..a65a2ca 100644
--- a/source/modules/asura-utils/threading/thread_impl_posix.h
+++ b/source/modules/asura-utils/threading/thread_impl_posix.h
@@ -0,0 +1,2 @@
+#include <asura-utils/classes.h>
+
diff --git a/source/modules/asura-utils/threading/thread_impl_std.h b/source/modules/asura-utils/threading/thread_impl_std.h
index 4cc5dfe..a2623ee 100644
--- a/source/modules/asura-utils/threading/thread_impl_std.h
+++ b/source/modules/asura-utils/threading/thread_impl_std.h
@@ -9,34 +9,32 @@
#include "thread.h"
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
- ///
- /// Threadstd::threadʵ֡
- ///
- class ThreadImplSTD : public ThreadImpl
- {
- public:
+///
+/// Threadstd::threadʵ֡
+///
+class ThreadImplSTD : public ThreadImpl
+{
+public:
- ThreadImplSTD();
- ~ThreadImplSTD();
+ ThreadImplSTD();
+ ~ThreadImplSTD();
- bool Start(Threadable* thread, uint32 stacksize) override;
- void Join() override;
- void Kill() override;
+ bool Start(Threadable* thread, uint32 stacksize) override;
+ void Join() override;
+ void Kill() override;
- bool IsRunning() override;
- bool IsCurrent() override;
+ bool IsRunning() override;
+ bool IsCurrent() override;
- private:
+private:
- };
+};
- }
-}
+namespace_end
+namespace_end
#endif // #if ASURA_THREAD_STD
diff --git a/source/modules/asura-utils/threading/thread_impl_win32.cpp b/source/modules/asura-utils/threading/thread_impl_win32.cpp
index 61b1c13..c876be9 100644
--- a/source/modules/asura-utils/threading/thread_impl_win32.cpp
+++ b/source/modules/asura-utils/threading/thread_impl_win32.cpp
@@ -5,75 +5,73 @@
#if ASURA_THREAD_WIN32
-namespace AsuraEngine
-{
- namespace Threading
- {
-
- static DWORD WINAPI _thread_win32_runner(LPVOID param)
- {
- Threadable* thread = (Threadable*)param;
- return thread->Process(); // β
- }
-
- ThreadImplWin32::ThreadImplWin32()
- {
- }
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
- ThreadImplWin32::~ThreadImplWin32()
- {
- if (!m_Handle) return;
- ::CloseHandle(m_Handle);
- m_Handle = 0;
- }
+static DWORD WINAPI _thread_win32_runner(LPVOID param)
+{
+ Threadable* thread = (Threadable*)param;
+ return thread->Process(); // β
+}
- bool ThreadImplWin32::Start(Threadable* thread, uint32 stacksize/*=0*/)
- {
- assert(!IsRunning());
- m_Handle = ::CreateThread(
- NULL
- , stacksize
- , _thread_win32_runner
- , thread
- , 0 /*е*/
- , NULL);
+ThreadImplWin32::ThreadImplWin32()
+{
+}
- return m_Handle;
- }
+ThreadImplWin32::~ThreadImplWin32()
+{
+ if (!m_Handle) return;
+ ::CloseHandle(m_Handle);
+ m_Handle = 0;
+}
- void ThreadImplWin32::Join()
- {
- // ̵߳ȴ̷߳
- ::WaitForSingleObject(m_Handle, INFINITE);
- }
+bool ThreadImplWin32::Start(Threadable* thread, uint32 stacksize/*=0*/)
+{
+ assert(!IsRunning());
+ m_Handle = ::CreateThread(
+ NULL
+ , stacksize
+ , _thread_win32_runner
+ , thread
+ , 0 /*е*/
+ , NULL);
- void ThreadImplWin32::Kill()
- {
- ::TerminateThread(m_Handle, FALSE);
- }
+ return m_Handle;
+}
- void ThreadImplWin32::Sleep(uint ms)
- {
- ::Sleep(ms);
- }
+void ThreadImplWin32::Join()
+{
+ // ̵߳ȴ̷߳
+ ::WaitForSingleObject(m_Handle, INFINITE);
+}
- bool ThreadImplWin32::IsRunning()
- {
- if (m_Handle) {
- DWORD exitCode = 0;
- // https://blog.csdn.net/yuanmeng567/article/details/19485719
- ::GetExitCodeThread(m_Handle, &exitCode);
- return exitCode == STILL_ACTIVE;
- }
- return false;
- }
+void ThreadImplWin32::Kill()
+{
+ ::TerminateThread(m_Handle, FALSE);
+}
- bool ThreadImplWin32::IsCurrent()
- {
- return m_Handle == ::GetCurrentThread();
- }
+void ThreadImplWin32::Sleep(uint ms)
+{
+ ::Sleep(ms);
+}
+bool ThreadImplWin32::IsRunning()
+{
+ if (m_Handle) {
+ DWORD exitCode = 0;
+ // https://blog.csdn.net/yuanmeng567/article/details/19485719
+ ::GetExitCodeThread(m_Handle, &exitCode);
+ return exitCode == STILL_ACTIVE;
}
+ return false;
}
+bool ThreadImplWin32::IsCurrent()
+{
+ return m_Handle == ::GetCurrentThread();
+}
+
+namespace_end
+namespace_end
+
#endif // ASURA_THREAD_WIN32 \ No newline at end of file
diff --git a/source/modules/asura-utils/threading/thread_impl_win32.h b/source/modules/asura-utils/threading/thread_impl_win32.h
index d1b7ae5..846670b 100644
--- a/source/modules/asura-utils/threading/thread_impl_win32.h
+++ b/source/modules/asura-utils/threading/thread_impl_win32.h
@@ -9,39 +9,36 @@
#include "thread.h"
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
- ///
- /// Threadwin32ʵ֡
- ///
- class ThreadImplWin32 : public ThreadImpl
- {
- public:
+///
+/// Threadwin32ʵ֡
+///
+class ThreadImplWin32 : public ThreadImpl
+{
+public:
- ThreadImplWin32();
- ~ThreadImplWin32();
+ ThreadImplWin32();
+ ~ThreadImplWin32();
- bool Start(Threadable* thread, uint32 stacksize) override;
- void Join() override;
- void Kill() override;
+ bool Start(Threadable* thread, uint32 stacksize) override;
+ void Join() override;
+ void Kill() override;
- void Sleep(uint ms) override;
+ void Sleep(uint ms) override;
- bool IsRunning() override;
- bool IsCurrent() override;
+ bool IsRunning() override;
+ bool IsCurrent() override;
- private:
+private:
- HANDLE m_Handle;
+ HANDLE m_Handle;
- };
+};
- }
-}
+namespace_end
+namespace_end
#endif // #if ASURA_THREAD_WIN32
-
#endif // __ASURA_THREAD_WIN32_H__ \ No newline at end of file
diff --git a/source/modules/asura-utils/threading/threadable.h b/source/modules/asura-utils/threading/threadable.h
index 08f807d..66973c5 100644
--- a/source/modules/asura-utils/threading/threadable.h
+++ b/source/modules/asura-utils/threading/threadable.h
@@ -3,23 +3,21 @@
#include "../type.h"
-namespace AsuraEngine
-{
- namespace Threading
- {
+namespace_begin(AsuraEngine)
+namespace_begin(Threading)
- ASURA_ABSTRACT class Threadable
- {
- public:
+ASURA_ABSTRACT class Threadable
+{
+public:
- Threadable() {};
- virtual ~Threadable() {};
+ Threadable() {};
+ virtual ~Threadable() {};
- virtual int Process() = 0;
+ virtual int Process() = 0;
- };
+};
- }
-}
+namespace_end
+namespace_end
#endif \ No newline at end of file
diff --git a/source/modules/asura-utils/utils_module.h b/source/modules/asura-utils/utils_module.h
index e802730..479b052 100644
--- a/source/modules/asura-utils/utils_module.h
+++ b/source/modules/asura-utils/utils_module.h
@@ -11,6 +11,8 @@
#include "module.h"
+#include "classes.h"
+
namespace AsuraEngine
{