Skip to content

Instantly share code, notes, and snippets.

@daCyuubi
Created July 16, 2018 06:05
Show Gist options
  • Save daCyuubi/e7d3638911a07ae5b3a2e6a2a6456e6c to your computer and use it in GitHub Desktop.
Save daCyuubi/e7d3638911a07ae5b3a2e6a2a6456e6c to your computer and use it in GitHub Desktop.
shitty threading clone
#include "taskmgr.h"
void taskmgr_UpdateCurrentTime(int Time)
{
//set taskmgr current time
taskmgr_Time = Time;
}
int taskmgr_RegisterTask(int ExecuteAt, void* Function)
{
//init tasks count
if (taskmgr_TasksCount == -1)
{
taskmgr_TasksCount++;
}
//setup task
taskmgr_Tasks[taskmgr_TasksCount].taskmgr_ExecuteAt = ExecuteAt;
taskmgr_Tasks[taskmgr_TasksCount].taskmgr_Function = &Function;
//increcement tasks count
taskmgr_TasksCount++;
//return task id
return taskmgr_TasksCount - 1;
}
void taskmgr_ExecuteAll()
{
//no tasks to execute
if (taskmgr_TasksCount < 0)
{
return TASKMGR_NO_TASKS;
}
//loop through struct array
for (int Index = 0; Index < taskmgr_TasksCount; Index++)
{
//check if execution time for task equals taskmgr current time
if (taskmgr_Tasks[Index].ExecuteAt == taskmgr_Time)
{
//execute void function
((void(*)()) taskmgr_Tasks[Index].taskmgr_Function)();
}
}
}
int taskmgr_Execute(int Index)
{
//no tasks to execute
if (taskmgr_TasksCount < 0)
{
return TASKMGR_NO_TASKS;
}
//index is invalid, either higher than tasks count or lower than tasks count
if (Index > taskmgr_TasksCount)
{
return TASKMGR_INVALID_INDEX;
}
else if (Index < taskmgr_TasksCount)
{
return TASKMGR_INVALID_INDEX;
}
//execute void function
((void(*)()) taskmgr_Tasks[Index].taskmgr_Function)();
}
//err codes
#define TASKMGR_NO_TASKS 0
#define TASKMGR_INVALID_INDEX 1
struct taskmgr_Task
{
int taskmgr_ExecuteAt;
void* taskmgr_Function;
};
int taskmgr_Time;
int taskmgr_TasksCount = -1;
struct taskmgr_Task taskmgr_Tasks[];
void taskmgr_UpdateCurrentTime(int Time);
int taskmgr_RegisterTask(int ExecuteAt, void* Function);
void taskmgr_ExecuteAll();
int taskmgr_Execute(int Index);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment