From e8bbb5d293c6944755788f4f25f4c75749150e84 Mon Sep 17 00:00:00 2001 From: Ilia Udalov Date: Sat, 18 Feb 2017 14:06:30 +0300 Subject: [PATCH] Fix codestyle --- src/cpp/garbage-collector.cpp | 19 ++-- src/cpp/garbage-collector.h | 15 ++- src/cpp/lua-module.cpp | 20 ++-- src/cpp/shared-table.cpp | 70 ++++++------ src/cpp/shared-table.h | 4 +- src/cpp/spin-mutex.h | 6 +- src/cpp/stored-object.cpp | 58 ++++------ src/cpp/stored-object.h | 10 +- src/cpp/threading.cpp | 186 ++++++++++++++------------------ src/cpp/threading.h | 12 +-- src/cpp/utils.h | 19 ++-- tests/cpp/garbage-collector.cpp | 26 ++--- tests/cpp/shared-table.cpp | 22 ++-- tests/cpp/test-main.cpp | 2 +- tests/cpp/test-utils.h | 6 +- 15 files changed, 209 insertions(+), 266 deletions(-) diff --git a/src/cpp/garbage-collector.cpp b/src/cpp/garbage-collector.cpp index 14b0ea5..67981b0 100644 --- a/src/cpp/garbage-collector.cpp +++ b/src/cpp/garbage-collector.cpp @@ -8,9 +8,9 @@ namespace effil { GarbageCollector::GarbageCollector() - : state_(GCState::Idle), - lastCleanup_(0), - step_(200) {} + : state_(GCState::Idle) + , lastCleanup_(0) + , step_(200) {} GCObject* GarbageCollector::get(GCObjectHandle handle) { std::lock_guard g(lock_); @@ -32,30 +32,30 @@ bool GarbageCollector::has(GCObjectHandle handle) const { void GarbageCollector::cleanup() { std::lock_guard g(lock_); - if (state_ == GCState::Stopped) return; + if (state_ == GCState::Stopped) + return; assert(state_ != GCState::Running); state_ = GCState::Running; std::vector grey; std::map> black; - for(const auto& handleAndObject : objects_) + for (const auto& handleAndObject : objects_) if (handleAndObject.second->instances() > 1) grey.push_back(handleAndObject.first); - while(!grey.empty()) { + while (!grey.empty()) { GCObjectHandle handle = grey.back(); grey.pop_back(); auto object = objects_[handle]; black[handle] = object; - for(GCObjectHandle refHandle : object->refers()) + for (GCObjectHandle refHandle : object->refers()) if (black.find(refHandle) == black.end()) grey.push_back(refHandle); } - DEBUG << "Removing " << (objects_.size() - black.size()) - << " out of " << objects_.size() << std::endl; + DEBUG << "Removing " << (objects_.size() - black.size()) << " out of " << objects_.size() << std::endl; // Sweep phase objects_ = std::move(black); @@ -80,7 +80,6 @@ void GarbageCollector::resume() { state_ = GCState::Idle; } - GarbageCollector& getGC() { static GarbageCollector pool; return pool; diff --git a/src/cpp/garbage-collector.h b/src/cpp/garbage-collector.h index 9fb78e7..5fcb04a 100644 --- a/src/cpp/garbage-collector.h +++ b/src/cpp/garbage-collector.h @@ -18,7 +18,8 @@ static const GCObjectHandle GCNull = nullptr; // Child has to care about storing data and concurrent access. class GCObject { public: - GCObject() noexcept : refs_(new std::set) {} + GCObject() noexcept + : refs_(new std::set) {} GCObject(const GCObject& init) = default; GCObject(GCObject&& init) = default; virtual ~GCObject() = default; @@ -31,11 +32,7 @@ protected: std::shared_ptr> refs_; }; -enum class GCState { - Idle, - Running, - Stopped -}; +enum class GCState { Idle, Running, Stopped }; class GarbageCollector { public: @@ -43,9 +40,10 @@ public: ~GarbageCollector() = default; // This method is used to create all managed objects. - template + template ObjectType create(Args&&... args) { - if (lastCleanup_.fetch_add(1) == step_) cleanup(); + if (lastCleanup_.fetch_add(1) == step_) + cleanup(); auto object = std::make_shared(std::forward(args)...); std::lock_guard g(lock_); @@ -75,7 +73,6 @@ private: GarbageCollector(const GarbageCollector&) = delete; }; - GarbageCollector& getGC(); } // effil \ No newline at end of file diff --git a/src/cpp/lua-module.cpp b/src/cpp/lua-module.cpp index 15a08d7..2ba9b01 100644 --- a/src/cpp/lua-module.cpp +++ b/src/cpp/lua-module.cpp @@ -12,25 +12,21 @@ sol::object createThreadFactory(sol::this_state lua, const sol::function& func) return sol::make_object(lua, std::make_unique(func)); } -sol::object createShare(sol::this_state lua) { - return sol::make_object(lua, getGC().create()); -} +sol::object createShare(sol::this_state lua) { return sol::make_object(lua, getGC().create()); } } // namespace -extern "C" int luaopen_libeffil(lua_State *L) { +extern "C" int luaopen_libeffil(lua_State* L) { sol::state_view lua(L); effil::LuaThread::getUserType(lua); SharedTable::getUserType(lua); ThreadFactory::getUserType(lua); - sol::table public_api = lua.create_table_with( - "thread", createThreadFactory, // - "thread_id", threadId, // - "sleep", sleep, // - "yield", yield, // - "share", createShare // - ); + sol::table public_api = lua.create_table_with("thread", createThreadFactory, // + "thread_id", threadId, // + "sleep", sleep, // + "yield", yield, // + "share", createShare // + ); sol::stack::push(lua, public_api); return 1; } - diff --git a/src/cpp/shared-table.cpp b/src/cpp/shared-table.cpp index dc997dc..f39950d 100644 --- a/src/cpp/shared-table.cpp +++ b/src/cpp/shared-table.cpp @@ -8,24 +8,21 @@ namespace effil { SharedTable::SharedTable() - : GCObject(), - data_(std::make_shared()) { -} + : GCObject() + , data_(std::make_shared()) {} SharedTable::SharedTable(const SharedTable& init) - : GCObject(init), - data_(init.data_) { -} + : GCObject(init) + , data_(init.data_) {} -sol::object SharedTable::getUserType(sol::state_view &lua) { - static sol::usertype type( - "new", sol::no_constructor, // - sol::meta_function::new_index, &SharedTable::luaSet,// - sol::meta_function::index, &SharedTable::luaGet,// - sol::meta_function::length, &SharedTable::length,// - "__pairs", &SharedTable::pairs, // - "__ipairs", &SharedTable::ipairs // - ); +sol::object SharedTable::getUserType(sol::state_view& lua) { + static sol::usertype type("new", sol::no_constructor, // + sol::meta_function::new_index, &SharedTable::luaSet, // + sol::meta_function::index, &SharedTable::luaGet, // + sol::meta_function::length, &SharedTable::length, // + "__pairs", &SharedTable::pairs, // + "__ipairs", &SharedTable::ipairs // + ); sol::stack::push(lua, type); return sol::stack::pop(lua); } @@ -33,8 +30,10 @@ sol::object SharedTable::getUserType(sol::state_view &lua) { void SharedTable::set(StoredObject&& key, StoredObject&& value) { std::lock_guard g(data_->lock); - if (key->gcHandle()) refs_->insert(key->gcHandle()); - if (value->gcHandle()) refs_->insert(value->gcHandle()); + if (key->gcHandle()) + refs_->insert(key->gcHandle()); + if (value->gcHandle()) + refs_->insert(value->gcHandle()); data_->entries[std::move(key)] = std::move(value); } @@ -59,8 +58,10 @@ void SharedTable::luaSet(const sol::stack_object& luaKey, const sol::stack_objec // in this case object is not obligatory to own data auto it = data_->entries.find(key); if (it != data_->entries.end()) { - if (it->first->gcHandle()) refs_->erase(it->first->gcHandle()); - if (it->second->gcHandle()) refs_->erase(it->second->gcHandle()); + if (it->first->gcHandle()) + refs_->erase(it->first->gcHandle()); + if (it->second->gcHandle()) + refs_->erase(it->second->gcHandle()); data_->entries.erase(it); } @@ -85,14 +86,12 @@ size_t SharedTable::length() const { size_t len = 0u; sol::optional value; auto iter = data_->entries.find(createStoredObject(static_cast(1))); - if (iter != data_->entries.end()) - { - do - { + if (iter != data_->entries.end()) { + do { ++len; ++iter; - } - while ((iter != data_->entries.end()) && (value = storedObjectToDouble(iter->first)) && (static_cast(value.value()) == len + 1)); + } while ((iter != data_->entries.end()) && (value = storedObjectToDouble(iter->first)) && + (static_cast(value.value()) == len + 1)); } return len; } @@ -104,8 +103,7 @@ SharedTable::PairsIterator SharedTable::getNext(const sol::object& key, sol::thi auto upper = data_->entries.upper_bound(obj); if (upper != data_->entries.end()) return std::tuple(upper->first->unpack(lua), upper->second->unpack(lua)); - } - else { + } else { if (!data_->entries.empty()) { const auto& begin = data_->entries.begin(); return std::tuple(begin->first->unpack(lua), begin->second->unpack(lua)); @@ -115,14 +113,16 @@ SharedTable::PairsIterator SharedTable::getNext(const sol::object& key, sol::thi } SharedTable::PairsIterator SharedTable::pairs(sol::this_state lua) const { - auto next = [](sol::this_state lua, SharedTable table, sol::stack_object key){ return table.getNext(key, lua); }; + auto next = [](sol::this_state lua, SharedTable table, sol::stack_object key) { return table.getNext(key, lua); }; return std::tuple( - sol::make_object(lua, std::function(next)).as(), - sol::make_object(lua, *this) - ); + sol::make_object( + lua, std::function(next)) + .as(), + sol::make_object(lua, *this)); } -std::tuple ipairsNext(sol::this_state lua, SharedTable table, sol::optional key) { +std::tuple ipairsNext(sol::this_state lua, SharedTable table, + sol::optional key) { size_t index = key ? key.value() + 1 : 1; auto objKey = createStoredObject(static_cast(index)); sol::object value = table.get(objKey, lua); @@ -132,10 +132,8 @@ std::tuple ipairsNext(sol::this_state lua, SharedTable } std::tuple SharedTable::ipairs(sol::this_state lua) const { - return std::tuple( - sol::make_object(lua, ipairsNext).as(), - sol::make_object(lua, *this) - ); + return std::tuple(sol::make_object(lua, ipairsNext).as(), + sol::make_object(lua, *this)); } } // effil diff --git a/src/cpp/shared-table.h b/src/cpp/shared-table.h index 5f4458e..ac1a0fe 100644 --- a/src/cpp/shared-table.h +++ b/src/cpp/shared-table.h @@ -22,7 +22,7 @@ public: SharedTable(const SharedTable& init); virtual ~SharedTable() = default; - static sol::object getUserType(sol::state_view &lua); + static sol::object getUserType(sol::state_view& lua); void set(StoredObject&&, StoredObject&&); sol::object get(const StoredObject& key, const sol::this_state& state) const; PairsIterator getNext(const sol::object& key, sol::this_state lua); @@ -36,7 +36,6 @@ public: PairsIterator ipairs(sol::this_state) const; private: - struct SharedData { SpinMutex lock; DataEntries entries; @@ -46,4 +45,3 @@ private: }; } // effil - diff --git a/src/cpp/spin-mutex.h b/src/cpp/spin-mutex.h index 0f5d2a4..36cf0fb 100644 --- a/src/cpp/spin-mutex.h +++ b/src/cpp/spin-mutex.h @@ -8,14 +8,12 @@ namespace effil { class SpinMutex { public: void lock() noexcept { - while(lock_.test_and_set(std::memory_order_acquire)) { + while (lock_.test_and_set(std::memory_order_acquire)) { std::this_thread::yield(); } } - void unlock() noexcept { - lock_.clear(std::memory_order_release); - } + void unlock() noexcept { lock_.clear(std::memory_order_release); } private: std::atomic_flag lock_ = ATOMIC_FLAG_INIT; diff --git a/src/cpp/stored-object.cpp b/src/cpp/stored-object.cpp index 207f76d..c7ed13d 100644 --- a/src/cpp/stored-object.cpp +++ b/src/cpp/stored-object.cpp @@ -13,7 +13,7 @@ namespace effil { namespace { -template +template class PrimitiveHolder : public BaseHolder { public: PrimitiveHolder(const sol::stack_object& luaObject) noexcept @@ -29,9 +29,7 @@ public: return data_ < static_cast*>(other)->data_; } - sol::object unpack(sol::this_state state) const final { - return sol::make_object(state, data_); - } + sol::object unpack(sol::this_state state) const final { return sol::make_object(state, data_); } StoredType getData() { return data_; } @@ -41,11 +39,12 @@ private: class FunctionHolder : public BaseHolder { public: - template + template FunctionHolder(SolObject luaObject) noexcept { sol::state_view lua(luaObject.lua_state()); sol::function dumper = lua["string"]["dump"]; - if (!dumper.valid()) throw Exception() << "Invalid string.dump()"; + if (!dumper.valid()) + throw Exception() << "Invalid string.dump()"; function_ = dumper(luaObject); } @@ -69,7 +68,7 @@ private: class TableHolder : public BaseHolder { public: - template + template TableHolder(const SolType& luaObject) { assert(luaObject.template is()); handle_ = luaObject.template as().handle(); @@ -79,7 +78,7 @@ public: TableHolder(GCObjectHandle handle) : handle_(handle) {} - bool rawCompare(const BaseHolder *other) const final { + bool rawCompare(const BaseHolder* other) const final { return handle_ < static_cast(other)->handle_; } @@ -87,7 +86,7 @@ public: return sol::make_object(state, *static_cast(getGC().get(handle_))); } - GCObjectHandle gcHandle() const override { return handle_; } + GCObjectHandle gcHandle() const override { return handle_; } private: GCObjectHandle handle_; @@ -103,7 +102,7 @@ void dumpTable(SharedTable* target, sol::table luaTable, SolTableToShared& visit StoredObject makeStoredObject(sol::object luaObject, SolTableToShared& visited) { if (luaObject.get_type() == sol::type::table) { sol::table luaTable = luaObject; - auto comparator = [&luaTable](const std::pair& element){ + auto comparator = [&luaTable](const std::pair& element) { return element.first == luaTable; }; auto st = std::find_if(visited.begin(), visited.end(), comparator); @@ -122,14 +121,14 @@ StoredObject makeStoredObject(sol::object luaObject, SolTableToShared& visited) } void dumpTable(SharedTable* target, sol::table luaTable, SolTableToShared& visited) { - for(auto& row : luaTable) { + for (auto& row : luaTable) { target->set(makeStoredObject(row.first, visited), makeStoredObject(row.second, visited)); } } -template +template StoredObject fromSolObject(const SolObject& luaObject) { - switch(luaObject.get_type()) { + switch (luaObject.get_type()) { case sol::type::nil: break; case sol::type::boolean: @@ -142,8 +141,7 @@ StoredObject fromSolObject(const SolObject& luaObject) { return std::make_unique(luaObject); case sol::type::function: return std::make_unique(luaObject); - case sol::type::table: - { + case sol::type::table: { sol::table luaTable = luaObject; // Tables pool is used to store tables. // Right now not defiantly clear how ownership between states works. @@ -164,31 +162,21 @@ StoredObject fromSolObject(const SolObject& luaObject) { } // namespace -StoredObject createStoredObject(bool value) { - return std::make_unique>(value); -} +StoredObject createStoredObject(bool value) { return std::make_unique>(value); } -StoredObject createStoredObject(double value) { - return std::make_unique>(value); -} +StoredObject createStoredObject(double value) { return std::make_unique>(value); } StoredObject createStoredObject(const std::string& value) { return std::make_unique>(value); } -StoredObject createStoredObject(const sol::object &object) { - return fromSolObject(object); -} +StoredObject createStoredObject(const sol::object& object) { return fromSolObject(object); } -StoredObject createStoredObject(const sol::stack_object &object) { - return fromSolObject(object); -} +StoredObject createStoredObject(const sol::stack_object& object) { return fromSolObject(object); } -StoredObject createStoredObject(GCObjectHandle handle) { - return std::make_unique(handle); -} +StoredObject createStoredObject(GCObjectHandle handle) { return std::make_unique(handle); } -template +template sol::optional getPrimitiveHolderData(const StoredObject& sobj) { auto ptr = dynamic_cast*>(sobj.get()); if (ptr) @@ -196,13 +184,9 @@ sol::optional getPrimitiveHolderData(const StoredObject& sobj) { return sol::optional(); } -sol::optional storedObjectToBool(const StoredObject& sobj) { - return getPrimitiveHolderData(sobj); -} +sol::optional storedObjectToBool(const StoredObject& sobj) { return getPrimitiveHolderData(sobj); } -sol::optional storedObjectToDouble(const StoredObject& sobj) { - return getPrimitiveHolderData(sobj); -} +sol::optional storedObjectToDouble(const StoredObject& sobj) { return getPrimitiveHolderData(sobj); } sol::optional storedObjectToString(const StoredObject& sobj) { return getPrimitiveHolderData(sobj); diff --git a/src/cpp/stored-object.h b/src/cpp/stored-object.h index 1c9a132..cc852f3 100644 --- a/src/cpp/stored-object.h +++ b/src/cpp/stored-object.h @@ -13,7 +13,7 @@ public: bool compare(const BaseHolder* other) const { if (typeid(*this) == typeid(*other)) - return rawCompare(other); + return rawCompare(other); return typeid(*this).before(typeid(*other)); } @@ -30,17 +30,15 @@ private: typedef std::unique_ptr StoredObject; struct StoredObjectLess { - bool operator()(const StoredObject& lhs, const StoredObject& rhs) const { - return lhs->compare(rhs.get()); - } + bool operator()(const StoredObject& lhs, const StoredObject& rhs) const { return lhs->compare(rhs.get()); } }; StoredObject createStoredObject(bool); StoredObject createStoredObject(double); StoredObject createStoredObject(const std::string&); StoredObject createStoredObject(GCObjectHandle); -StoredObject createStoredObject(const sol::object &); -StoredObject createStoredObject(const sol::stack_object &); +StoredObject createStoredObject(const sol::object&); +StoredObject createStoredObject(const sol::stack_object&); sol::optional storedObjectToBool(const StoredObject&); sol::optional storedObjectToDouble(const StoredObject&); diff --git a/src/cpp/threading.cpp b/src/cpp/threading.cpp index c9b4e63..7033287 100644 --- a/src/cpp/threading.cpp +++ b/src/cpp/threading.cpp @@ -7,20 +7,15 @@ namespace effil { class LuaHookStopException : public std::exception {}; -std::string threadId() -{ +std::string threadId() { std::stringstream ss; ss << std::this_thread::get_id(); return ss.str(); } -void yield() -{ - std::this_thread::yield(); -} +void yield() { std::this_thread::yield(); } -void sleep(int64_t time, sol::optional period) -{ +void sleep(int64_t time, sol::optional period) { std::string metric = period ? period.value() : "s"; if (metric == "ms") std::this_thread::sleep_for(std::chrono::milliseconds(time)); @@ -36,14 +31,15 @@ thread_local LuaThread::ThreadData* LuaThread::pThreadLocalData = NULL; // class LuaThread -LuaThread::LuaThread(std::shared_ptr threadData, const std::string& function, const sol::variadic_args& args) { +LuaThread::LuaThread(std::shared_ptr threadData, const std::string& function, + const sol::variadic_args& args) { pThreadData_ = threadData; assert(pThreadData_); pThreadData_->command = ThreadCommand::Nothing; pThreadData_->status = ThreadStatus::Running; std::vector arguments; - for(const auto& iter: args) { + for (const auto& iter : args) { StoredObject store = createStoredObject(iter.get()); arguments.push_back(store->unpack(sol::this_state{pThreadData_->luaState})); } @@ -53,57 +49,52 @@ LuaThread::LuaThread(std::shared_ptr threadData, const std::string& pThread_->detach(); } -void LuaThread::luaHook(lua_State*, lua_Debug*) -{ - if (pThreadLocalData) - { - switch (pThreadLocalData->command) - { - case ThreadCommand::Pause: - { - pThreadLocalData->status = ThreadStatus::Paused; - ThreadCommand cmd = pThreadLocalData->command; - while (cmd == ThreadCommand::Pause) { - std::this_thread::yield(); - cmd = pThreadLocalData->command; +void LuaThread::luaHook(lua_State*, lua_Debug*) { + if (pThreadLocalData) { + switch (pThreadLocalData->command) { + case ThreadCommand::Pause: { + pThreadLocalData->status = ThreadStatus::Paused; + ThreadCommand cmd = pThreadLocalData->command; + while (cmd == ThreadCommand::Pause) { + std::this_thread::yield(); + cmd = pThreadLocalData->command; + } + assert(cmd != ThreadCommand::Nothing); + if (cmd == ThreadCommand::Resume) { + pThreadLocalData->status = ThreadStatus::Running; + break; // Just go out of the function + } else { /* HOOK_STOP - do nothing and go to the next case */ + } } - assert(cmd != ThreadCommand::Nothing); - if (cmd == ThreadCommand::Resume) - { - pThreadLocalData->status = ThreadStatus::Running; - break; // Just go out of the function - } - else { /* HOOK_STOP - do nothing and go to the next case */} - } - case ThreadCommand::Cancel: - throw LuaHookStopException(); - default: - case ThreadCommand::Nothing: - break; + case ThreadCommand::Cancel: + throw LuaHookStopException(); + default: + case ThreadCommand::Nothing: + break; } } } -void LuaThread::work(std::shared_ptr threadData, const std::string strFunction, std::vector&& arguments) { +void LuaThread::work(std::shared_ptr threadData, const std::string strFunction, + std::vector&& arguments) { try { pThreadLocalData = threadData.get(); assert(threadData); const sol::object& stringLoader = threadData->luaState["loadstring"]; - REQUIRE(stringLoader.valid() && stringLoader.get_type() == sol::type::function) << "Invalid loadstring function"; + REQUIRE(stringLoader.valid() && stringLoader.get_type() == sol::type::function) + << "Invalid loadstring function"; sol::function userFuncObj = static_cast(stringLoader)(strFunction); sol::function_result results = userFuncObj(sol::as_args(arguments)); (void)results; // TODO: try to avoid use of useless sol::function_result here sol::variadic_args args(threadData->luaState, -lua_gettop(threadData->luaState)); - for(const auto& iter: args) { + for (const auto& iter : args) { StoredObject store = createStoredObject(iter.get()); threadData->results.emplace_back(std::move(store)); } threadData->status = ThreadStatus::Completed; - } - catch (const LuaHookStopException&) { + } catch (const LuaHookStopException&) { threadData->status = ThreadStatus::Canceled; - } - catch (const sol::error& err) { + } catch (const sol::error& err) { threadData->status = ThreadStatus::Failed; sol::stack::push(threadData->luaState, err.what()); StoredObject store = createStoredObject(sol::stack::pop(threadData->luaState)); @@ -111,23 +102,13 @@ void LuaThread::work(std::shared_ptr threadData, const std::string s } } -void LuaThread::cancel() -{ - pThreadData_->command = ThreadCommand::Cancel; -} +void LuaThread::cancel() { pThreadData_->command = ThreadCommand::Cancel; } -void LuaThread::pause() -{ - pThreadData_->command = ThreadCommand::Pause; -} +void LuaThread::pause() { pThreadData_->command = ThreadCommand::Pause; } -void LuaThread::resume() -{ - pThreadData_->command = ThreadCommand::Resume; -} +void LuaThread::resume() { pThreadData_->command = ThreadCommand::Resume; } -std::tuple LuaThread::wait(sol::this_state state) const -{ +std::tuple LuaThread::wait(sol::this_state state) const { ThreadStatus stat = pThreadData_->status; while (stat == ThreadStatus::Running) { @@ -135,52 +116,49 @@ std::tuple LuaThread::wait(sol::this_state state) const stat = pThreadData_->status; } sol::table returns = sol::state_view(state).create_table(); - if (stat == ThreadStatus::Completed) - { - for (const StoredObject& obj: pThreadData_->results) - { + if (stat == ThreadStatus::Completed) { + for (const StoredObject& obj : pThreadData_->results) { returns.add(obj->unpack(state)); } } return std::make_tuple(sol::make_object(state, threadStatusToString(stat)), std::move(returns)); } -std::string LuaThread::threadStatusToString(ThreadStatus stat) const -{ - switch(stat) - { - case ThreadStatus::Running: return "running"; - case ThreadStatus::Paused: return "paused"; - case ThreadStatus::Canceled: return "canceled"; - case ThreadStatus::Completed: return "completed"; - case ThreadStatus::Failed: return "failed"; +std::string LuaThread::threadStatusToString(ThreadStatus stat) const { + switch (stat) { + case ThreadStatus::Running: + return "running"; + case ThreadStatus::Paused: + return "paused"; + case ThreadStatus::Canceled: + return "canceled"; + case ThreadStatus::Completed: + return "completed"; + case ThreadStatus::Failed: + return "failed"; } assert(false); return "unknown"; } -std::string LuaThread::status() const -{ - return threadStatusToString(pThreadData_->status); -} +std::string LuaThread::status() const { return threadStatusToString(pThreadData_->status); } -sol::object LuaThread::getUserType(sol::state_view &lua) -{ - static sol::usertype type( - "new", sol::no_constructor, // - "cancel", &LuaThread::cancel, // - "pause", &LuaThread::pause, // - "resume", &LuaThread::resume, // - "status", &LuaThread::status, // - "wait", &LuaThread::wait - ); +sol::object LuaThread::getUserType(sol::state_view& lua) { + static sol::usertype type("new", sol::no_constructor, // + "cancel", &LuaThread::cancel, // + "pause", &LuaThread::pause, // + "resume", &LuaThread::resume, // + "status", &LuaThread::status, // + "wait", &LuaThread::wait); sol::stack::push(lua, type); return sol::stack::pop(lua); } // class ThreadFactory -ThreadFactory::ThreadFactory(const sol::function& func) : stepwise_(false), step_(100U) { +ThreadFactory::ThreadFactory(const sol::function& func) + : stepwise_(false) + , step_(100U) { sol::state_view lua(func.lua_state()); const sol::object& dumper = lua["string"]["dump"]; REQUIRE(dumper.valid() && dumper.get_type() == sol::type::function) << "Unable to get string.dump()"; @@ -194,10 +172,8 @@ ThreadFactory::ThreadFactory(const sol::function& func) : stepwise_(false), step std::unique_ptr ThreadFactory::runThread(const sol::variadic_args& args) { std::shared_ptr threadData = std::make_shared(); assert(threadData.get()); - threadData->luaState.open_libraries( - sol::lib::base, sol::lib::string, - sol::lib::package, sol::lib::io, sol::lib::os - ); + threadData->luaState.open_libraries(sol::lib::base, sol::lib::string, sol::lib::package, sol::lib::io, + sol::lib::os); if (stepwise_) lua_sethook(threadData->luaState, LuaThread::luaHook, LUA_MASKCOUNT, step_); @@ -213,48 +189,42 @@ std::unique_ptr ThreadFactory::runThread(const sol::variadic_args& ar return std::make_unique(threadData, strFunction_, args); } -bool ThreadFactory::stepwise(const sol::optional& value) -{ - bool ret = stepwise_ ; +bool ThreadFactory::stepwise(const sol::optional& value) { + bool ret = stepwise_; if (value) stepwise_ = value.value(); return ret; } -unsigned int ThreadFactory::step(const sol::optional& value) -{ +unsigned int ThreadFactory::step(const sol::optional& value) { bool ret = step_; if (value) step_ = value.value(); return ret; } -std::string ThreadFactory::packagePath(const sol::optional& value) -{ +std::string ThreadFactory::packagePath(const sol::optional& value) { std::string& ret = packagePath_; if (value) packagePath_ = value.value(); return ret; } -std::string ThreadFactory::packageCPath(const sol::optional& value) -{ +std::string ThreadFactory::packageCPath(const sol::optional& value) { std::string& ret = packageCPath_; if (value) packageCPath_ = value.value(); return ret; } -sol::object ThreadFactory::getUserType(sol::state_view &lua) -{ - static sol::usertype type( - "new", sol::no_constructor, // - sol::meta_function::call, &ThreadFactory::runThread,// - "stepwise", &ThreadFactory::stepwise, // - "step", &ThreadFactory::step, // - "package_path", &ThreadFactory::packagePath, // - "package_cpath", &ThreadFactory::packageCPath // - ); +sol::object ThreadFactory::getUserType(sol::state_view& lua) { + static sol::usertype type("new", sol::no_constructor, // + sol::meta_function::call, &ThreadFactory::runThread, // + "stepwise", &ThreadFactory::stepwise, // + "step", &ThreadFactory::step, // + "package_path", &ThreadFactory::packagePath, // + "package_cpath", &ThreadFactory::packageCPath // + ); sol::stack::push(lua, type); return sol::stack::pop(lua); } diff --git a/src/cpp/threading.h b/src/cpp/threading.h index 7395a29..5bae0bd 100644 --- a/src/cpp/threading.h +++ b/src/cpp/threading.h @@ -23,15 +23,14 @@ public: Failed, }; - enum class ThreadCommand - { + enum class ThreadCommand { Nothing = 1, Cancel, Pause, Resume, }; - struct ThreadData{ + struct ThreadData { sol::state luaState; std::atomic status; std::atomic command; @@ -39,7 +38,7 @@ public: }; LuaThread(std::shared_ptr threadData, const std::string& function, const sol::variadic_args& args); - static sol::object getUserType(sol::state_view &lua); + static sol::object getUserType(sol::state_view& lua); static void luaHook(lua_State*, lua_Debug*); /* Public lua methods*/ @@ -54,7 +53,8 @@ private: LuaThread& operator=(const LuaThread&) = delete; std::string threadStatusToString(ThreadStatus stat) const; - static void work(std::shared_ptr threadData, const std::string strFunction, std::vector&& arguments); + static void work(std::shared_ptr threadData, const std::string strFunction, + std::vector&& arguments); std::shared_ptr pThreadData_; std::shared_ptr pThread_; @@ -65,7 +65,7 @@ private: class ThreadFactory { public: ThreadFactory(const sol::function& func); - static sol::object getUserType(sol::state_view &lua); + static sol::object getUserType(sol::state_view& lua); /* Public lua methods*/ std::unique_ptr runThread(const sol::variadic_args& args); diff --git a/src/cpp/utils.h b/src/cpp/utils.h index 94ead35..5b04724 100644 --- a/src/cpp/utils.h +++ b/src/cpp/utils.h @@ -9,9 +9,10 @@ namespace effil { class Exception : public sol::error { public: - Exception() noexcept : sol::error("") {} + Exception() noexcept + : sol::error("") {} - template + template Exception& operator<<(const T& value) { std::stringstream ss; ss << value; @@ -19,9 +20,7 @@ public: return *this; } - virtual const char* what() const noexcept override { - return message_.c_str(); - } + virtual const char* what() const noexcept override { return message_.c_str(); } private: std::string message_; @@ -29,10 +28,14 @@ private: } // effil -#define REQUIRE(cond) if (!cond) throw effil::Exception() +#define REQUIRE(cond) \ + if (!cond) \ + throw effil::Exception() #ifdef NDEBUG -# define DEBUG if (false) std::cout +#define DEBUG \ + if (false) \ + std::cout #else -# define DEBUG std::cout +#define DEBUG std::cout #endif diff --git a/tests/cpp/garbage-collector.cpp b/tests/cpp/garbage-collector.cpp index d8c52be..f81ca04 100644 --- a/tests/cpp/garbage-collector.cpp +++ b/tests/cpp/garbage-collector.cpp @@ -29,8 +29,10 @@ TEST(gc, collect) { ASSERT_EQ(getGC().size(), (size_t)0); { - GCObject o1 = getGC().create();; - GCObject o2 = getGC().create();; + GCObject o1 = getGC().create(); + ; + GCObject o2 = getGC().create(); + ; } EXPECT_EQ(getGC().size(), (size_t)2); getGC().cleanup(); @@ -42,7 +44,6 @@ namespace { struct Dummy : public GCObject { void add(GCObjectHandle ref) { refs_->insert(ref); } }; - } TEST(gc, withRefs) { @@ -52,14 +53,14 @@ TEST(gc, withRefs) { { Dummy orphan = getGC().create(); - for(size_t i = 0; i < 3; i++) { + for (size_t i = 0; i < 3; i++) { Dummy child = getGC().create(); root.add(child.handle()); } } - EXPECT_EQ(getGC().size(), (size_t) 5); + EXPECT_EQ(getGC().size(), (size_t)5); getGC().cleanup(); - EXPECT_EQ(getGC().size(), (size_t) 4); + EXPECT_EQ(getGC().size(), (size_t)4); } getGC().cleanup(); EXPECT_EQ(getGC().size(), (size_t)0); @@ -69,13 +70,14 @@ TEST(gc, autoCleanup) { std::vector threads; size_t objectsPerThread = 1000; - for(size_t i = 0; i < 5; i++) - threads.emplace_back([=]{ - for(size_t i = 0; i < objectsPerThread; i++) - getGC().create(); + for (size_t i = 0; i < 5; i++) + threads.emplace_back([=] { + for (size_t i = 0; i < objectsPerThread; i++) + getGC().create(); }); - for(auto& thread : threads) thread.join(); + for (auto& thread : threads) + thread.join(); EXPECT_LT(getGC().size(), getGC().step()); } @@ -92,7 +94,7 @@ end )"); EXPECT_EQ(getGC().size(), (size_t)1001); -lua.script(R"( + lua.script(R"( for i=1,1000 do st[i] = nil end diff --git a/tests/cpp/shared-table.cpp b/tests/cpp/shared-table.cpp index eb9cb5a..dd1bb0d 100644 --- a/tests/cpp/shared-table.cpp +++ b/tests/cpp/shared-table.cpp @@ -80,16 +80,17 @@ TEST(sharedTable, multipleThreads) { std::vector threads; - threads.emplace_back([=](){ + threads.emplace_back([=]() { sol::state lua; - bootstrapState(lua);; + bootstrapState(lua); + ; lua["st"] = st; lua.script(R"( while not st.ready do end st.fst = true)"); }); - threads.emplace_back([=](){ + threads.emplace_back([=]() { sol::state lua; bootstrapState(lua); lua["st"] = st; @@ -98,7 +99,7 @@ while not st.ready do end st.snd = true)"); }); - threads.emplace_back([=](){ + threads.emplace_back([=]() { sol::state lua; bootstrapState(lua); lua["st"] = st; @@ -112,7 +113,9 @@ st.thr = true)"); lua["st"] = st; lua.script("st.ready = true"); - for(auto& thread : threads) { thread.join(); } + for (auto& thread : threads) { + thread.join(); + } EXPECT_EQ(lua["st"]["fst"], true); EXPECT_EQ(lua["st"]["snd"], true); @@ -235,7 +238,7 @@ TEST(sharedTable, stressWithThreads) { const size_t threadCount = 10; std::vector threads; - for(size_t i = 0; i < threadCount; i++) { + for (size_t i = 0; i < threadCount; i++) { threads.emplace_back([=] { sol::state lua; bootstrapState(lua); @@ -243,20 +246,21 @@ TEST(sharedTable, stressWithThreads) { std::stringstream ss; ss << "st[" << i << "] = 1" << std::endl; ss << "for i = 1, 100000 do" << std::endl; - ss << " st[" << i << "] = " << "st[" << i << "] + 1" << std::endl; + ss << " st[" << i << "] = " + << "st[" << i << "] + 1" << std::endl; ss << "end" << std::endl; lua.script(ss.str()); }); } - for(auto& thread : threads) { + for (auto& thread : threads) { thread.join(); } sol::state lua; bootstrapState(lua); lua["st"] = st; - for(size_t i = 0; i < threadCount; i++) { + for (size_t i = 0; i < threadCount; i++) { EXPECT_TRUE(lua["st"][i] == 100'001) << (double)lua["st"][i] << std::endl; } } diff --git a/tests/cpp/test-main.cpp b/tests/cpp/test-main.cpp index b42b721..8401258 100644 --- a/tests/cpp/test-main.cpp +++ b/tests/cpp/test-main.cpp @@ -1,6 +1,6 @@ #include "gtest/gtest.h" -int main(int argc, char **argv) { +int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } \ No newline at end of file diff --git a/tests/cpp/test-utils.h b/tests/cpp/test-utils.h index 637611b..65c2cde 100644 --- a/tests/cpp/test-utils.h +++ b/tests/cpp/test-utils.h @@ -6,11 +6,7 @@ namespace effil { inline void bootstrapState(sol::state& lua) { - lua.open_libraries( - sol::lib::base, - sol::lib::string, - sol::lib::table - ); + lua.open_libraries(sol::lib::base, sol::lib::string, sol::lib::table); SharedTable::getUserType(lua); }