-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avoid using boost::rational in busy loop
- Loading branch information
Showing
6 changed files
with
168 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2018-2023 Project Tsurugi. | ||
* | ||
* 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. | ||
*/ | ||
#pragma once | ||
|
||
#include <atomic> | ||
|
||
#include <takatori/util/assertion.h> | ||
|
||
#include <tateyama/utils/cache_align.h> | ||
|
||
namespace tateyama::task_scheduler::impl { | ||
|
||
/** | ||
* @brief util class to get the notification periodically while clients count up | ||
*/ | ||
class cache_align periodic_notifier { | ||
public: | ||
|
||
/** | ||
* @brief construct default instance | ||
*/ | ||
periodic_notifier() = default; | ||
|
||
/** | ||
* @brief construct instance | ||
* @param notifications the numerator of notification ratio | ||
* @param period the denominator of the notification ratio | ||
* @details client gets the notifications in accordance with the given notification ratio. | ||
* The number of notifications that equals to `notifications` will be made by the time `period` count of | ||
* `count_up` calls finish. | ||
*/ | ||
periodic_notifier( | ||
std::size_t notifications, | ||
std::size_t period | ||
) noexcept : | ||
notifications_(notifications), | ||
period_(period) | ||
{ | ||
BOOST_ASSERT(notifications != 0); //NOLINT | ||
BOOST_ASSERT(period != 0); //NOLINT | ||
BOOST_ASSERT(notifications <= period); //NOLINT | ||
} | ||
|
||
bool count_up() { | ||
current_ += notifications_; | ||
if(current_ >= period_) { | ||
current_ -= period_; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void reset() { | ||
current_ = 0; | ||
} | ||
|
||
private: | ||
std::size_t notifications_{1}; | ||
std::size_t period_{1}; | ||
std::size_t current_{}; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2018-2023 Project Tsurugi. | ||
* | ||
* 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 <tateyama/task_scheduler/impl/periodic_notifier.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace tateyama::task_scheduler::impl { | ||
|
||
using namespace std::literals::string_literals; | ||
using namespace std::chrono_literals; | ||
|
||
class periodic_notifier_test : public ::testing::Test { | ||
|
||
}; | ||
|
||
using namespace std::string_view_literals; | ||
|
||
TEST_F(periodic_notifier_test, default_instance) { | ||
periodic_notifier n{}; | ||
EXPECT_TRUE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
} | ||
|
||
TEST_F(periodic_notifier_test, simple) { | ||
periodic_notifier n{1, 2}; | ||
EXPECT_FALSE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
EXPECT_FALSE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
EXPECT_FALSE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
} | ||
|
||
TEST_F(periodic_notifier_test, ratio_2_3) { | ||
periodic_notifier n{2, 3}; | ||
EXPECT_FALSE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
EXPECT_FALSE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
} | ||
|
||
TEST_F(periodic_notifier_test, ratio_1_3) { | ||
periodic_notifier n{1, 3}; | ||
EXPECT_FALSE(n.count_up()); | ||
EXPECT_FALSE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
EXPECT_FALSE(n.count_up()); | ||
EXPECT_FALSE(n.count_up()); | ||
EXPECT_TRUE(n.count_up()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters