This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.h
58 lines (50 loc) · 1.53 KB
/
tasks.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once
#include <thread>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <map>
#include <chrono>
enum TaskStates { TASK_ENABLE, TASK_DISABLE, TASK_RUN_ONCE, TASK_REMOVE };
struct Task
{
int id;
void* var;
unsigned long period;
unsigned short stat;
std::condition_variable cv;
void(*func)(int, void*);
Task(void(*task_func)(int, void*), unsigned long task_period,
unsigned short task_stat, void* task_var = nullptr, int task_id = -1)
{
func = task_func;
id = task_id;
var = task_var;
stat = task_stat;
period = task_period;
}
};
extern std::map<int, Task*> map_tasks;
extern std::mutex mx_task;
void deamonTask(int task_id);
/**
Sets task with given id (if it exists) to given state.
If task state was TASK_DISABLE, and new state is != TASK_DISABLE, it will notify_one() task
@note: this function will attempt to lock mutex mx_task
*/
void setTaskState(int id, unsigned short state);
void setTaskPeriod(int id, unsigned long period);
void setTaskVar(int id, void* var);
/**
Returns id for given task_func, if no such task exists returns -1.
@note: this function will attempt to lock mutex mx_task
*/
int getTaskId(void(*task_func)(int, void*));
unsigned long getTaskPeriod(int id);
unsigned short getTaskState(int id);
void* getTaskVar(int id);
/**
Creates new task if it doesn't already exist.
*/
int createTask(void(*task_func)(int, void*), unsigned long task_period, unsigned short task_stat, void* task_var);
void destroyTask(int id);