From 0875c60291aa7cf84c7d42fcf039e13172bd65c5 Mon Sep 17 00:00:00 2001 From: "Yash Pandey (YP)" Date: Tue, 30 Mar 2021 21:57:41 +0530 Subject: [PATCH] feat: added async ticker Signed-off-by: Yash Pandey (YP) --- casbin/casbin.vcxproj | 2 ++ casbin/casbin.vcxproj.filters | 6 ++++ casbin/util/ticker.cpp | 54 +++++++++++++++++++++++++++++++++ casbin/util/ticker.h | 56 +++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 casbin/util/ticker.cpp create mode 100644 casbin/util/ticker.h diff --git a/casbin/casbin.vcxproj b/casbin/casbin.vcxproj index 829cd0c0..fd369464 100644 --- a/casbin/casbin.vcxproj +++ b/casbin/casbin.vcxproj @@ -158,6 +158,7 @@ + @@ -288,6 +289,7 @@ + diff --git a/casbin/casbin.vcxproj.filters b/casbin/casbin.vcxproj.filters index b1435e0e..6539ef1e 100644 --- a/casbin/casbin.vcxproj.filters +++ b/casbin/casbin.vcxproj.filters @@ -171,6 +171,9 @@ Source Files\util + + Source Files\util + Source Files\util @@ -473,6 +476,9 @@ Header Files + + Header Files + diff --git a/casbin/util/ticker.cpp b/casbin/util/ticker.cpp new file mode 100644 index 00000000..fe0a713d --- /dev/null +++ b/casbin/util/ticker.cpp @@ -0,0 +1,54 @@ +/* +* Copyright 2020 The casbin Authors. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "pch.h" + +#ifndef TICKER_CPP +#define TICKER_CPP + +#include "./ticker.h" + +Ticker::Ticker(function onTick, chrono::duration tickInterval) + : _onTick (onTick) + , _tickInterval (tickInterval) + , _running (false) {} + +Ticker::~Ticker () { + stop(); +} + +void Ticker::start() { + if (_running) return; + _running = true; + _futures1.push_back(async(launch::async, &Ticker::timer_loop, this)); +} + +void Ticker::stop() { + _running = false; +} + +void Ticker::timer_loop() +{ + while (_running) { + { + lock_guard lock(_tickIntervalMutex); + _futures2.push_back(async(launch::async, _onTick)); + this_thread::sleep_for( _tickInterval ); + } + } +} + +#endif // TICKER_CPP diff --git a/casbin/util/ticker.h b/casbin/util/ticker.h new file mode 100644 index 00000000..b587a4dc --- /dev/null +++ b/casbin/util/ticker.h @@ -0,0 +1,56 @@ +/* + * Copyright 2020 The casbin Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TICKER_H +#define TICKER_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +class Ticker { +public: + typedef chrono::duration tick_interval_t; + typedef function on_tick_t; + typedef vector> future_vec; + + Ticker(function onTick, chrono::duration tickInterval); + + ~Ticker(); + + void start(); + + void stop(); + +private: + void timer_loop(); + on_tick_t _onTick; + tick_interval_t _tickInterval; + atomic_bool _running; + mutex _tickIntervalMutex; + future_vec _futures1; + future_vec _futures2; +}; + +#endif // TICKER_H