-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShutdownCommand.cpp
More file actions
114 lines (83 loc) · 3.13 KB
/
ShutdownCommand.cpp
File metadata and controls
114 lines (83 loc) · 3.13 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// Created by Sierra Kilo on 06-Aug-18.
//
#include "CommandExec.h"
#include <iostream>
#ifdef __WIN32
#include <synchapi.h>
#else
#include <unistd.h>
#endif
using namespace std;
ShutdownCommand::ShutdownCommand() {
// std::atomic_init(&terminate_timer, false);
// use unique lock for the asynchronous operation of changing the
// value for the terminate_timer_flag
mu_terminate_timer_flag = new std::mutex();
std::unique_lock<mutex> locker1(*mu_terminate_timer_flag);
terminate_timer_flag = false;
locker1.unlock();
}
void ShutdownCommand::startShutdownTimerThread(TimeObject time_data) {
this->timerThread = std::thread(&ShutdownCommand::startShutdownTimer, this, time_data);
this->timerThread.detach();
}
bool ShutdownCommand::getTerminateTimerFlagValue() {
return terminate_timer_flag;
}
void ShutdownCommand::cancelShutdownTimer() {
std::unique_lock<mutex> locker1(*mu_terminate_timer_flag);
terminate_timer_flag = true;
locker1.unlock();
}
void ShutdownCommand::startShutdownTimer(TimeObject time_data) {
std::unique_lock<mutex> locker1(*mu_terminate_timer_flag);
terminate_timer_flag = false;
locker1.unlock();
long long int t_target = time_data.get_msecs();
auto t_start = std::chrono::high_resolution_clock::now();
std::chrono::milliseconds delay(100);
while(true) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
auto t_now = std::chrono::high_resolution_clock::now();
std::chrono::milliseconds elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(t_now - t_start);
long long int elapsed_ms = elapsed.count();
if (terminate_timer_flag) {
cout << "\n\n" << "&&&&&&&&&&&&&&&&&&&&&& AKIROSI TIMER" << endl;
break;
} else if (elapsed_ms >= t_target) {
cout << "\n\n" << "&&&&&&&&&&&&&&&&&&&&&& TELOS TIMER" << endl;
executeShutdown();
break;
}
}
}
void ShutdownCommand::executeShutdown(){
#ifdef _WIN32
// -s is used for shutdown, -f is used to force shutdown,
// preventing the computer from getting stuck from background applications.
//system("shutdown -s -f");
cout << "\n\n---------------------Diag: Shutdown would be executed here!" << endl << endl;
#else
// usleep takes sleep time in us (1 millionth of a second)
// usleep(time_data.get_msecs() * 1000); // usleep takes sleep time in us (1 millionth of a second)
#endif
}
ShutdownCommand::ShutdownCommand(ShutdownCommand &&obj) : timerThread(std::move(obj.timerThread)){
std::cout << "Move Constructor is called" << std::endl;
}
ShutdownCommand &ShutdownCommand::operator=(ShutdownCommand &&obj) {
std::cout << "Move Assignment is called" << std::endl;
if (timerThread.joinable()) {
timerThread.join();
}
timerThread = std::move(obj.timerThread);
return *this;
}
ShutdownCommand::~ShutdownCommand() {
// cout << "==========> BEFORE this->timerThread.join()!" << endl;
// if (this->timerThread.joinable()) {
// this->timerThread.join();
// }
// cout << "==========> ShutdownCommand DESTRUCTOR!" << endl;
}