From 2f27188b52d4815496efc254cf53c6d089571d79 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Thu, 17 Nov 2016 19:59:47 +0200 Subject: [PATCH] Forgot to decrease the number of occupied slots when removing tasks. And also to reset it when clearing them. Removed the used tasks counter completely because it's useless and has a high risk of producing nasty bugs. --- source/Tasks.cpp | 18 ++++++------------ source/Tasks.hpp | 44 ++++++++++++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/source/Tasks.cpp b/source/Tasks.cpp index 30dc7fe7..813e3a98 100644 --- a/source/Tasks.cpp +++ b/source/Tasks.cpp @@ -18,7 +18,6 @@ namespace SqMod { SQMODE_DECL_TYPENAME(Typename, _SC("SqTask")) // ------------------------------------------------------------------------------------------------ -Uint32 Tasks::s_Used = 0; Tasks::Time Tasks::s_Last = 0; Tasks::Time Tasks::s_Prev = 0; Tasks::Interval Tasks::s_Intervals[SQMOD_MAX_TASKS]; @@ -169,6 +168,8 @@ void Tasks::Register(HSQUIRRELVM vm) .FmtFunc(_SC("SetTag"), &Task::SetTag) .Func(_SC("Terminate"), &Task::Terminate) .Func(_SC("GetArgument"), &Task::GetArgument) + // Static functions + .StaticFunc(_SC("Used"), &Tasks::GetUsed) ); } @@ -216,8 +217,10 @@ SQInteger Tasks::FindUnused() // ------------------------------------------------------------------------------------------------ SQInteger Tasks::Create(Int32 id, Int32 type, HSQUIRRELVM vm) { + // Locate the identifier of a free slot + const SQInteger slot = FindUnused(); // See if we have where to store this task - if (s_Used >= SQMOD_MAX_TASKS) + if (slot < 0) { return sq_throwerror(vm, "Reached the maximum number of tasks"); } @@ -284,14 +287,6 @@ SQInteger Tasks::Create(Int32 id, Int32 type, HSQUIRRELVM vm) } } - // Obtain the identifier of a free slot - const SQInteger slot = FindUnused(); - // Validate the slot, although it shouldn't be necessary - if (slot < 0) - { - return sq_throwerror(vm, "Unable to acquire a task slot"); - } - // At this point we can grab a reference to our slot Task & task = s_Tasks[slot]; // Were there any arguments specified? @@ -319,12 +314,11 @@ SQInteger Tasks::Create(Int32 id, Int32 type, HSQUIRRELVM vm) ++task.mArgc; } } + // Alright, at this point we can initialize the slot task.Init(func, inst, intrv, itr, id, type); // Now initialize the interval s_Intervals[slot] = intrv; - // Increase the number of used slots - ++s_Used; // Push the tag instance on the stack sq_pushobject(vm, task.mSelf); // Specify that this function returns a value diff --git a/source/Tasks.hpp b/source/Tasks.hpp index cc190772..523e2801 100644 --- a/source/Tasks.hpp +++ b/source/Tasks.hpp @@ -78,8 +78,7 @@ private: */ ~Task() { - Release(); - Clear(); + Terminate(); } /* ---------------------------------------------------------------------------------------- @@ -105,6 +104,16 @@ private: */ void Init(HSQOBJECT & inst, HSQOBJECT & func, Interval intrv, Iterator itr, Int32 id, Int32 type); + /* ---------------------------------------------------------------------------------------- + * Release managed script resources. + */ + void Release(); + + /* ---------------------------------------------------------------------------------------- + * Execute the managed task. + */ + Interval Execute(); + /* ---------------------------------------------------------------------------------------- * Clear the arguments. */ @@ -120,12 +129,7 @@ private: } /* ---------------------------------------------------------------------------------------- - * Release managed script resources. - */ - void Release(); - - /* ---------------------------------------------------------------------------------------- - * Terminate the task + * Terminate the task. */ void Terminate() { @@ -133,11 +137,6 @@ private: Clear(); } - /* ---------------------------------------------------------------------------------------- - * Execute the managed task. - */ - Interval Execute(); - /* ---------------------------------------------------------------------------------------- * Retrieve the associated user tag. */ @@ -267,7 +266,6 @@ private: }; // -------------------------------------------------------------------------------------------- - static Uint32 s_Used; // The number of occupied slots. static Time s_Last; // Last time point. static Time s_Prev; // Previous time point. static Interval s_Intervals[SQMOD_MAX_TASKS]; // List of intervals to be processed. @@ -364,6 +362,24 @@ protected: public: + /* -------------------------------------------------------------------------------------------- + * Retrieve the number of used tasks slots. + */ + static SQInteger GetUsed() + { + SQInteger n = 0; + // Iterate task list + for (const auto & t : s_Tasks) + { + if (VALID_ENTITY(t.mEntity)) + { + ++n; + } + } + // Return the final count + return n; + } + /* -------------------------------------------------------------------------------------------- * Cleanup all tasks associated with the specified entity. */