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()");
93 andreas 30
 
31
    if (mRunning)
32
    {
33
        mStopped = true;
34
 
35
        while (mRunning)
36
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
37
    }
15 andreas 38
}
39
 
40
void TTimer::run()
41
{
42
    DECL_TRACER("TTimer::run()");
43
 
93 andreas 44
    if (mRunning || mThread.joinable() || mMsec == std::chrono::milliseconds(0))
15 andreas 45
        return;
46
 
47
    try
48
    {
49
        mStopped = false;
50
        mOnce = false;
51
        mThread = std::thread([=] { this->_run(); });
52
        mThread.detach();
53
    }
54
    catch (std::exception& e)
55
    {
56
        MSG_ERROR("Error starting the TTimer thread: " << e.what());
57
    }
58
}
59
 
60
void TTimer::run_once()
61
{
62
    DECL_TRACER("TTimer::run_once()");
63
 
93 andreas 64
    if (mRunning || mThread.joinable() || mMsec == std::chrono::milliseconds(0))
15 andreas 65
        return;
66
 
67
    try
68
    {
69
        mStopped = false;
70
        mOnce = true;
71
        mThread = std::thread([=] { this->_run(); });
72
        mThread.detach();
73
    }
74
    catch (std::exception& e)
75
    {
76
        MSG_ERROR("Error starting the TTimer thread: " << e.what());
77
    }
78
}
79
 
80
void TTimer::run_once(std::chrono::milliseconds ms)
81
{
82
    DECL_TRACER("TTimer::run_once(std::chrono::milliseconds ms)");
83
 
93 andreas 84
    if (mRunning || mThread.joinable() || ms == std::chrono::milliseconds(0))
15 andreas 85
        return;
86
 
87
    try
88
    {
89
        mStopped = false;
90
        mOnce = true;
91
        mMsec = ms;
92
        mThread = std::thread([=] { this->_run(); });
93
        mThread.detach();
94
    }
95
    catch (std::exception& e)
96
    {
97
        MSG_ERROR("Error starting the TTimer thread: " << e.what());
98
    }
99
}
100
 
101
void TTimer::_run()
102
{
93 andreas 103
    DECL_TRACER("TTimer::_run()");
104
 
105
    if (mRunning)
106
        return;
107
 
15 andreas 108
    mRunning = true;
109
 
21 andreas 110
    while (!mStopped && !prg_stopped)
15 andreas 111
    {
112
        std::this_thread::sleep_for(mMsec);
113
 
114
        if (mStopped)
115
            break;
116
 
117
        if (_callback)
118
            _callback(mCounter);
119
 
120
        mCounter++;
121
 
122
        if (mOnce)
123
            break;
124
    }
125
 
126
    mRunning = false;
127
}