Skip to content

Commit

Permalink
Merge pull request #90 from EmperorYP7/synced-enforcer
Browse files Browse the repository at this point in the history
feat: Added async ticker
  • Loading branch information
hsluoyz authored Apr 1, 2021
2 parents d7254f2 + 0875c60 commit 6baafaa
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
2 changes: 2 additions & 0 deletions casbin/casbin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<ClCompile Include="effect\default_effector.cpp" />
<ClCompile Include="enforcer.cpp" />
<ClCompile Include="enforcer_cached.cpp" />
<ClCompile Include="util\ticker.cpp" />
<ClCompile Include="exception\casbin_adapter_exception.cpp" />
<ClCompile Include="exception\casbin_enforcer_exception.cpp" />
<ClCompile Include="exception\casbin_rbac_exception.cpp" />
Expand Down Expand Up @@ -288,6 +289,7 @@
<ClInclude Include="util\built_in_functions.h" />
<ClInclude Include="util\pch.h" />
<ClInclude Include="util\util.h" />
<ClInclude Include="util\ticker.h" />
</ItemGroup>
<ItemGroup>
<None Include=".clang-format" />
Expand Down
6 changes: 6 additions & 0 deletions casbin/casbin.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@
<ClCompile Include="util\array_equals.cpp">
<Filter>Source Files\util</Filter>
</ClCompile>
<ClCompile Include="util\ticker.cpp">
<Filter>Source Files\util</Filter>
</ClCompile>
<ClCompile Include="util\array_remove_duplicates.cpp">
<Filter>Source Files\util</Filter>
</ClCompile>
Expand Down Expand Up @@ -473,6 +476,9 @@
<ClInclude Include="enforcer_synced.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="util\ticker.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include=".clang-format" />
Expand Down
54 changes: 54 additions & 0 deletions casbin/util/ticker.cpp
Original file line number Diff line number Diff line change
@@ -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<void()> onTick, chrono::duration<int64_t, nano> 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<mutex> lock(_tickIntervalMutex);
_futures2.push_back(async(launch::async, _onTick));
this_thread::sleep_for( _tickInterval );
}
}
}

#endif // TICKER_CPP
56 changes: 56 additions & 0 deletions casbin/util/ticker.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <functional>
#include <chrono>
#include <vector>
#include <thread>
#include <future>
#include <condition_variable>
#include <iostream>
#include <mutex>

using namespace std;

class Ticker {
public:
typedef chrono::duration<int64_t, nano> tick_interval_t;
typedef function<void()> on_tick_t;
typedef vector<future<void>> future_vec;

Ticker(function<void()> onTick, chrono::duration<int64_t, nano> 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

0 comments on commit 6baafaa

Please sign in to comment.