Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15 andreas 1
/*
2
 * Copyright (C) 2021 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "ttimer.h"
20
#include "terror.h"
21
 
22
TTimer::TTimer()
23
{
24
    DECL_TRACER("TTimer::TTimer()");
25
}
26
 
27
TTimer::~TTimer()
28
{
29
    DECL_TRACER("TTimer::~TTimer()");
30
}
31
 
32
void TTimer::run()
33
{
34
    DECL_TRACER("TTimer::run()");
35
 
36
    if (mThread.joinable() || mMsec == std::chrono::milliseconds(0))
37
        return;
38
 
39
    try
40
    {
41
        mStopped = false;
42
        mOnce = false;
43
        mThread = std::thread([=] { this->_run(); });
44
        mThread.detach();
45
    }
46
    catch (std::exception& e)
47
    {
48
        MSG_ERROR("Error starting the TTimer thread: " << e.what());
49
    }
50
}
51
 
52
void TTimer::run_once()
53
{
54
    DECL_TRACER("TTimer::run_once()");
55
 
56
    if (mThread.joinable() || mMsec == std::chrono::milliseconds(0))
57
        return;
58
 
59
    try
60
    {
61
        mStopped = false;
62
        mOnce = true;
63
        mThread = std::thread([=] { this->_run(); });
64
        mThread.detach();
65
    }
66
    catch (std::exception& e)
67
    {
68
        MSG_ERROR("Error starting the TTimer thread: " << e.what());
69
    }
70
}
71
 
72
void TTimer::run_once(std::chrono::milliseconds ms)
73
{
74
    DECL_TRACER("TTimer::run_once(std::chrono::milliseconds ms)");
75
 
76
    if (mThread.joinable() || ms == std::chrono::milliseconds(0))
77
        return;
78
 
79
    try
80
    {
81
        mStopped = false;
82
        mOnce = true;
83
        mMsec = ms;
84
        mThread = std::thread([=] { this->_run(); });
85
        mThread.detach();
86
    }
87
    catch (std::exception& e)
88
    {
89
        MSG_ERROR("Error starting the TTimer thread: " << e.what());
90
    }
91
}
92
 
93
void TTimer::_run()
94
{
95
    mRunning = true;
96
 
21 andreas 97
    while (!mStopped && !prg_stopped)
15 andreas 98
    {
99
        std::this_thread::sleep_for(mMsec);
100
 
101
        if (mStopped)
102
            break;
103
 
104
        if (_callback)
105
            _callback(mCounter);
106
 
107
        mCounter++;
108
 
109
        if (mOnce)
110
            break;
111
    }
112
 
113
    mRunning = false;
114
}