1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-04-25 13:47:12 +02:00

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.
This commit is contained in:
Sandu Liviu Catalin 2016-11-17 19:59:47 +02:00
parent 23d9caeac3
commit 2f27188b52
2 changed files with 36 additions and 26 deletions

View File

@ -18,7 +18,6 @@ namespace SqMod {
SQMODE_DECL_TYPENAME(Typename, _SC("SqTask")) SQMODE_DECL_TYPENAME(Typename, _SC("SqTask"))
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Uint32 Tasks::s_Used = 0;
Tasks::Time Tasks::s_Last = 0; Tasks::Time Tasks::s_Last = 0;
Tasks::Time Tasks::s_Prev = 0; Tasks::Time Tasks::s_Prev = 0;
Tasks::Interval Tasks::s_Intervals[SQMOD_MAX_TASKS]; Tasks::Interval Tasks::s_Intervals[SQMOD_MAX_TASKS];
@ -169,6 +168,8 @@ void Tasks::Register(HSQUIRRELVM vm)
.FmtFunc(_SC("SetTag"), &Task::SetTag) .FmtFunc(_SC("SetTag"), &Task::SetTag)
.Func(_SC("Terminate"), &Task::Terminate) .Func(_SC("Terminate"), &Task::Terminate)
.Func(_SC("GetArgument"), &Task::GetArgument) .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) 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 // 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"); 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 // At this point we can grab a reference to our slot
Task & task = s_Tasks[slot]; Task & task = s_Tasks[slot];
// Were there any arguments specified? // Were there any arguments specified?
@ -319,12 +314,11 @@ SQInteger Tasks::Create(Int32 id, Int32 type, HSQUIRRELVM vm)
++task.mArgc; ++task.mArgc;
} }
} }
// Alright, at this point we can initialize the slot // Alright, at this point we can initialize the slot
task.Init(func, inst, intrv, itr, id, type); task.Init(func, inst, intrv, itr, id, type);
// Now initialize the interval // Now initialize the interval
s_Intervals[slot] = intrv; s_Intervals[slot] = intrv;
// Increase the number of used slots
++s_Used;
// Push the tag instance on the stack // Push the tag instance on the stack
sq_pushobject(vm, task.mSelf); sq_pushobject(vm, task.mSelf);
// Specify that this function returns a value // Specify that this function returns a value

View File

@ -78,8 +78,7 @@ private:
*/ */
~Task() ~Task()
{ {
Release(); Terminate();
Clear();
} }
/* ---------------------------------------------------------------------------------------- /* ----------------------------------------------------------------------------------------
@ -105,6 +104,16 @@ private:
*/ */
void Init(HSQOBJECT & inst, HSQOBJECT & func, Interval intrv, Iterator itr, Int32 id, Int32 type); 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. * Clear the arguments.
*/ */
@ -120,12 +129,7 @@ private:
} }
/* ---------------------------------------------------------------------------------------- /* ----------------------------------------------------------------------------------------
* Release managed script resources. * Terminate the task.
*/
void Release();
/* ----------------------------------------------------------------------------------------
* Terminate the task
*/ */
void Terminate() void Terminate()
{ {
@ -133,11 +137,6 @@ private:
Clear(); Clear();
} }
/* ----------------------------------------------------------------------------------------
* Execute the managed task.
*/
Interval Execute();
/* ---------------------------------------------------------------------------------------- /* ----------------------------------------------------------------------------------------
* Retrieve the associated user tag. * 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_Last; // Last time point.
static Time s_Prev; // Previous time point. static Time s_Prev; // Previous time point.
static Interval s_Intervals[SQMOD_MAX_TASKS]; // List of intervals to be processed. static Interval s_Intervals[SQMOD_MAX_TASKS]; // List of intervals to be processed.
@ -364,6 +362,24 @@ protected:
public: 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. * Cleanup all tasks associated with the specified entity.
*/ */