Abstract_ThreadClass.cpp 2.68 KB
Newer Older
1
2
3
4
5
/**
 *  @author Sylvain Colomer
 *  @date 18/04/19.
 */

6
7
8
9
10
11
12

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

13
14
15
16
17
#include <sys/time.h>
#include "../include/Abstract_ThreadClass.h"

//%%%%%%%%%%%%%%%%%%%%%%%%% begin/end phase function %%%%%%%%%%%%%%%%%%%%%%%%%%%%

18
19
Abstract_ThreadClass::Abstract_ThreadClass(int task_period, int task_deadline)
{
20
21
    task_period=task_period;
    task_deadline=task_deadline;
22
    cout << "task_period = " << task_period <<endl;
23
24
25
}


26
27
Abstract_ThreadClass::~Abstract_ThreadClass()
{
28
29
30
31
32
33
34
35
36
37

    currentState = LifeCoreState::QUIT;

    if(runFlag){
        stop();
    }
}

//%%%%%%%%%%%%%%%%%%%%%%%%% run function %%%%%%%%%%%%%%%%%%%%%%%%%%%%

38
39
void Abstract_ThreadClass::run()
{
40
41
42
43
44
45
46
47

    long long currentThreadDelay;

    gettimeofday(&begin, 0);
    gettimeofday(&front_checkpoint, 0);

    currentState = LifeCoreState::RUN;

48
49
    while(isRunFlag())
    {
50
        usleep(task_period);
51
52
        
        cout<<"abstract class thread : run() "<<endl;
53
54
55
56

        gettimeofday(&end_checkpoint, 0);
        currentThreadDelay=(end_checkpoint.tv_sec-front_checkpoint.tv_sec) * 1000000L + (end_checkpoint.tv_usec-front_checkpoint.tv_usec);

57
58
        if (currentThreadDelay > task_period )
        {
59
60
            gettimeofday(&front_checkpoint, 0);

61
62
            if (currentThreadDelay > task_period + task_deadline)
            {
63
64
                currentState = LifeCoreState::DEADLINE_EXCEEDED;
            }
65
66
            else 
            
67
                currentState = LifeCoreState::RUN;
68
           //}
69
70
71
72
73
74
        }
    }
}

//%%%%%%%%%%%%%%%%%%%%%%%%% control function %%%%%%%%%%%%%%%%%%%%%%%%%%%%

75
76
void Abstract_ThreadClass::init()
{
77
78
79
80
    currentState = LifeCoreState::INIT;
    currentState = LifeCoreState::READY;
}

81
82
void Abstract_ThreadClass::start()
{
83
84
85
86
87
88
    setRunFlag(true);
    currentState = LifeCoreState::RUN;
    principalThread= std::thread(&Abstract_ThreadClass::run, this);
}


89
90
void Abstract_ThreadClass::stop()
{
91
92
93
94
95
    currentState = LifeCoreState::STOP;
    setRunFlag(false);
    principalThread.join();
}

96
97
void Abstract_ThreadClass::lazyStop()
{
98
99
100
101
    currentState = LifeCoreState::STOP;
    setRunFlag(false);
}

102
103
void Abstract_ThreadClass::play()
{
104
105
106
107
    currentState = LifeCoreState::RUN;
    setRunFlag(true);
}

108
109
void Abstract_ThreadClass::pause()
{
110
111
112

}

113
114
void Abstract_ThreadClass::join()
{
115
116
117
118
119
    principalThread.join();
}

//%%%%%%%%%%%%%%%%%%%%%%%%% getters and setters %%%%%%%%%%%%%%%%%%%%%%%%%%%%

120
121
bool Abstract_ThreadClass::isRunFlag() const 
{
122
123
124
125
    return runFlag;
}


126
127
void Abstract_ThreadClass::setRunFlag(bool runFlag) 
{
128
129
130
131
132
133
    runFlag_mutex.lock();
    runFlag = runFlag;
    runFlag_mutex.unlock();

}

134
135
LifeCoreState Abstract_ThreadClass::getCurrentState() const 
{
136
137
138
139
    return currentState;
}