Skip to content

Commit 6baafaa

Browse files
authored
Merge pull request #90 from EmperorYP7/synced-enforcer
feat: Added async ticker
2 parents d7254f2 + 0875c60 commit 6baafaa

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

casbin/casbin.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
<ClCompile Include="effect\default_effector.cpp" />
159159
<ClCompile Include="enforcer.cpp" />
160160
<ClCompile Include="enforcer_cached.cpp" />
161+
<ClCompile Include="util\ticker.cpp" />
161162
<ClCompile Include="exception\casbin_adapter_exception.cpp" />
162163
<ClCompile Include="exception\casbin_enforcer_exception.cpp" />
163164
<ClCompile Include="exception\casbin_rbac_exception.cpp" />
@@ -288,6 +289,7 @@
288289
<ClInclude Include="util\built_in_functions.h" />
289290
<ClInclude Include="util\pch.h" />
290291
<ClInclude Include="util\util.h" />
292+
<ClInclude Include="util\ticker.h" />
291293
</ItemGroup>
292294
<ItemGroup>
293295
<None Include=".clang-format" />

casbin/casbin.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@
171171
<ClCompile Include="util\array_equals.cpp">
172172
<Filter>Source Files\util</Filter>
173173
</ClCompile>
174+
<ClCompile Include="util\ticker.cpp">
175+
<Filter>Source Files\util</Filter>
176+
</ClCompile>
174177
<ClCompile Include="util\array_remove_duplicates.cpp">
175178
<Filter>Source Files\util</Filter>
176179
</ClCompile>
@@ -473,6 +476,9 @@
473476
<ClInclude Include="enforcer_synced.h">
474477
<Filter>Header Files</Filter>
475478
</ClInclude>
479+
<ClInclude Include="util\ticker.h">
480+
<Filter>Header Files</Filter>
481+
</ClInclude>
476482
</ItemGroup>
477483
<ItemGroup>
478484
<None Include=".clang-format" />

casbin/util/ticker.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2020 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "pch.h"
18+
19+
#ifndef TICKER_CPP
20+
#define TICKER_CPP
21+
22+
#include "./ticker.h"
23+
24+
Ticker::Ticker(function<void()> onTick, chrono::duration<int64_t, nano> tickInterval)
25+
: _onTick (onTick)
26+
, _tickInterval (tickInterval)
27+
, _running (false) {}
28+
29+
Ticker::~Ticker () {
30+
stop();
31+
}
32+
33+
void Ticker::start() {
34+
if (_running) return;
35+
_running = true;
36+
_futures1.push_back(async(launch::async, &Ticker::timer_loop, this));
37+
}
38+
39+
void Ticker::stop() {
40+
_running = false;
41+
}
42+
43+
void Ticker::timer_loop()
44+
{
45+
while (_running) {
46+
{
47+
lock_guard<mutex> lock(_tickIntervalMutex);
48+
_futures2.push_back(async(launch::async, _onTick));
49+
this_thread::sleep_for( _tickInterval );
50+
}
51+
}
52+
}
53+
54+
#endif // TICKER_CPP

casbin/util/ticker.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef TICKER_H
18+
#define TICKER_H
19+
20+
#include <cstdint>
21+
#include <functional>
22+
#include <chrono>
23+
#include <vector>
24+
#include <thread>
25+
#include <future>
26+
#include <condition_variable>
27+
#include <iostream>
28+
#include <mutex>
29+
30+
using namespace std;
31+
32+
class Ticker {
33+
public:
34+
typedef chrono::duration<int64_t, nano> tick_interval_t;
35+
typedef function<void()> on_tick_t;
36+
typedef vector<future<void>> future_vec;
37+
38+
Ticker(function<void()> onTick, chrono::duration<int64_t, nano> tickInterval);
39+
40+
~Ticker();
41+
42+
void start();
43+
44+
void stop();
45+
46+
private:
47+
void timer_loop();
48+
on_tick_t _onTick;
49+
tick_interval_t _tickInterval;
50+
atomic_bool _running;
51+
mutex _tickIntervalMutex;
52+
future_vec _futures1;
53+
future_vec _futures2;
54+
};
55+
56+
#endif // TICKER_H

0 commit comments

Comments
 (0)