Add class Timer.
This commit is contained in:
parent
bbf0d11a51
commit
9a6a5a94b5
13 changed files with 271 additions and 73 deletions
|
@ -11,6 +11,7 @@ set(HEADERS_BASE
|
|||
src/base/kernel/interfaces/ISignalListener.h
|
||||
src/base/kernel/interfaces/IStrategy.h
|
||||
src/base/kernel/interfaces/IStrategyListener.h
|
||||
src/base/kernel/interfaces/ITimerListener.h
|
||||
src/base/kernel/interfaces/IWatcherListener.h
|
||||
src/base/kernel/Process.h
|
||||
src/base/kernel/Signals.h
|
||||
|
@ -30,6 +31,7 @@ set(HEADERS_BASE
|
|||
src/base/tools/Chrono.h
|
||||
src/base/tools/Handle.h
|
||||
src/base/tools/String.h
|
||||
src/base/tools/Timer.h
|
||||
)
|
||||
|
||||
set(SOURCES_BASE
|
||||
|
@ -50,6 +52,7 @@ set(SOURCES_BASE
|
|||
src/base/tools/Arguments.cpp
|
||||
src/base/tools/Buffer.cpp
|
||||
src/base/tools/String.cpp
|
||||
src/base/tools/Timer.cpp
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -29,37 +29,31 @@
|
|||
#include "base/kernel/interfaces/IWatcherListener.h"
|
||||
#include "base/io/Watcher.h"
|
||||
#include "base/tools/Handle.h"
|
||||
#include "base/tools/Timer.h"
|
||||
|
||||
|
||||
xmrig::Watcher::Watcher(const String &path, IWatcherListener *listener) :
|
||||
m_listener(listener),
|
||||
m_path(path)
|
||||
{
|
||||
m_timer = new Timer(this);
|
||||
|
||||
m_fsEvent = new uv_fs_event_t;
|
||||
m_fsEvent->data = this;
|
||||
uv_fs_event_init(uv_default_loop(), m_fsEvent);
|
||||
|
||||
m_timer = new uv_timer_t;
|
||||
uv_timer_init(uv_default_loop(), m_timer);
|
||||
|
||||
m_fsEvent->data = m_timer->data = this;
|
||||
|
||||
start();
|
||||
}
|
||||
|
||||
|
||||
xmrig::Watcher::~Watcher()
|
||||
{
|
||||
Handle::close(m_timer);
|
||||
delete m_timer;
|
||||
|
||||
Handle::close(m_fsEvent);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Watcher::onTimer(uv_timer_t *handle)
|
||||
{
|
||||
static_cast<Watcher *>(handle->data)->reload();
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Watcher::onFsEvent(uv_fs_event_t *handle, const char *filename, int, int)
|
||||
{
|
||||
if (!filename) {
|
||||
|
@ -72,8 +66,8 @@ void xmrig::Watcher::onFsEvent(uv_fs_event_t *handle, const char *filename, int,
|
|||
|
||||
void xmrig::Watcher::queueUpdate()
|
||||
{
|
||||
uv_timer_stop(m_timer);
|
||||
uv_timer_start(m_timer, xmrig::Watcher::onTimer, kDelay, 0);
|
||||
m_timer->stop();
|
||||
m_timer->start(kDelay, 0);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -26,30 +26,33 @@
|
|||
#define XMRIG_WATCHER_H
|
||||
|
||||
|
||||
#include "base/kernel/interfaces/ITimerListener.h"
|
||||
#include "base/tools/String.h"
|
||||
|
||||
|
||||
typedef struct uv_fs_event_s uv_fs_event_t;
|
||||
typedef struct uv_timer_s uv_timer_t;
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class IWatcherListener;
|
||||
class Timer;
|
||||
|
||||
|
||||
class Watcher
|
||||
class Watcher : public ITimerListener
|
||||
{
|
||||
public:
|
||||
Watcher(const String &path, IWatcherListener *listener);
|
||||
~Watcher();
|
||||
~Watcher() override;
|
||||
|
||||
protected:
|
||||
inline void onTimer(const Timer *) override { reload(); }
|
||||
|
||||
private:
|
||||
constexpr static int kDelay = 500;
|
||||
|
||||
static void onFsEvent(uv_fs_event_t *handle, const char *filename, int events, int status);
|
||||
static void onTimer(uv_timer_t *handle);
|
||||
|
||||
void queueUpdate();
|
||||
void reload();
|
||||
|
@ -57,11 +60,12 @@ private:
|
|||
|
||||
IWatcherListener *m_listener;
|
||||
String m_path;
|
||||
Timer *m_timer;
|
||||
uv_fs_event_t *m_fsEvent;
|
||||
uv_timer_t *m_timer;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_WATCHER_H */
|
||||
|
|
47
src/base/kernel/interfaces/ITimerListener.h
Normal file
47
src/base/kernel/interfaces/ITimerListener.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_ITIMERLISTENER_H
|
||||
#define XMRIG_ITIMERLISTENER_H
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class Timer;
|
||||
|
||||
|
||||
class ITimerListener
|
||||
{
|
||||
public:
|
||||
virtual ~ITimerListener() = default;
|
||||
|
||||
virtual void onTimer(const Timer *timer) = 0;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif // XMRIG_ITIMERLISTENER_H
|
|
@ -5,6 +5,7 @@
|
|||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
|
91
src/base/tools/Timer.cpp
Normal file
91
src/base/tools/Timer.cpp
Normal file
|
@ -0,0 +1,91 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "base/kernel/interfaces/ITimerListener.h"
|
||||
#include "base/tools/Handle.h"
|
||||
#include "base/tools/Timer.h"
|
||||
|
||||
|
||||
xmrig::Timer::Timer(ITimerListener *listener) :
|
||||
m_listener(listener),
|
||||
m_timer(nullptr)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
|
||||
xmrig::Timer::Timer(ITimerListener *listener, uint64_t timeout, uint64_t repeat) :
|
||||
m_listener(listener),
|
||||
m_timer(nullptr)
|
||||
{
|
||||
init();
|
||||
start(timeout, repeat);
|
||||
}
|
||||
|
||||
|
||||
xmrig::Timer::~Timer()
|
||||
{
|
||||
Handle::close(m_timer);
|
||||
}
|
||||
|
||||
|
||||
uint64_t xmrig::Timer::repeat() const
|
||||
{
|
||||
return uv_timer_get_repeat(m_timer);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Timer::setRepeat(uint64_t repeat)
|
||||
{
|
||||
uv_timer_set_repeat(m_timer, repeat);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Timer::start(uint64_t timeout, uint64_t repeat)
|
||||
{
|
||||
uv_timer_start(m_timer, onTimer, timeout, repeat);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Timer::stop()
|
||||
{
|
||||
uv_timer_stop(m_timer);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Timer::init()
|
||||
{
|
||||
m_timer = new uv_timer_t;
|
||||
m_timer->data = this;
|
||||
uv_timer_init(uv_default_loop(), m_timer);
|
||||
}
|
||||
|
||||
|
||||
void xmrig::Timer::onTimer(uv_timer_t *handle)
|
||||
{
|
||||
const Timer *timer = static_cast<Timer *>(handle->data);
|
||||
|
||||
timer->m_listener->onTimer(timer);
|
||||
}
|
66
src/base/tools/Timer.h
Normal file
66
src/base/tools/Timer.h
Normal file
|
@ -0,0 +1,66 @@
|
|||
/* XMRig
|
||||
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef XMRIG_TIMER_H
|
||||
#define XMRIG_TIMER_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
typedef struct uv_timer_s uv_timer_t;
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
|
||||
class ITimerListener;
|
||||
|
||||
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
Timer(ITimerListener *listener);
|
||||
Timer(ITimerListener *listener, uint64_t timeout, uint64_t repeat);
|
||||
~Timer();
|
||||
|
||||
uint64_t repeat() const;
|
||||
void setRepeat(uint64_t repeat);
|
||||
void start(uint64_t timeout, uint64_t repeat);
|
||||
void stop();
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
||||
static void onTimer(uv_timer_t *handle);
|
||||
|
||||
ITimerListener *m_listener;
|
||||
uv_timer_t *m_timer;
|
||||
};
|
||||
|
||||
|
||||
} /* namespace xmrig */
|
||||
|
||||
|
||||
#endif /* XMRIG_TIMER_H */
|
Loading…
Add table
Add a link
Reference in a new issue