-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ros_control/Executor #151
Merged
Merged
ros_control/Executor #151
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c925ab8
Initial commit for Executor
gilwoolee b102958
CMakeLists cleanup
gilwoolee f912a93
Add documentation and tests; fix style
jslee02 f722010
Address Mike's comments
jslee02 0ec48ea
Merge branch 'master' into ros_Executor
jslee02 55000a4
Merge branch 'master' into ros_Executor
gilwoolee d6c937d
Update docstring of executor classes per Mike's comment
jslee02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef AIKIDO_UTIL_EXECUTORMULTIPLEXER_HPP_ | ||
#define AIKIDO_UTIL_EXECUTORMULTIPLEXER_HPP_ | ||
|
||
#include <functional> | ||
#include <mutex> | ||
#include <vector> | ||
|
||
namespace aikido { | ||
namespace util { | ||
|
||
/// Combine multiple executors (i.e. no argument callbacks) into one executor. | ||
/// | ||
/// This helper class allows one ExecutorThread to call multiple executors by | ||
/// sequentially calling the callbacks added to this class. | ||
/// | ||
/// \sa ExecutorThread | ||
class ExecutorMultiplexer final | ||
{ | ||
public: | ||
/// Default constructor. | ||
ExecutorMultiplexer() = default; | ||
|
||
/// Default destructor. | ||
~ExecutorMultiplexer() = default; | ||
|
||
/// Adds a callback. The added callbacks will be called by operator(). | ||
/// | ||
/// The order of callback calling is implementation detail that is subject to | ||
/// change. | ||
/// | ||
/// \param[in] callback Any callable object that doesn't return and take any | ||
/// parameters. | ||
void addCallback(std::function<void ()> callback); | ||
|
||
/// Removes all the added callbacks. | ||
void removeAllCallbacks(); | ||
|
||
/// Returns true if no callback is added. Otherwise, returns false. | ||
bool isEmpty() const; | ||
|
||
/// Returns the number of added callbacks. | ||
std::size_t getNumCallbacks() const; | ||
|
||
/// Executes all the added callbacked in order of they added. | ||
void operator ()(); | ||
|
||
private: | ||
/// Mutex for the list of callbacks. The array of callbacks will be locked | ||
/// during it's modified and the callbacks are called. | ||
std::mutex mMutex; | ||
|
||
/// Array of callbacks. | ||
std::vector<std::function<void ()>> mCallbacks; | ||
}; | ||
|
||
} // namespace util | ||
} // namespace aikido | ||
|
||
#endif // ifndef AIKIDO_UTIL_EXECUTORMULTIPLEXER_HPP_ |
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,71 @@ | ||
#ifndef AIKIDO_UTIL_EXECUTORTHREAD_HPP_ | ||
#define AIKIDO_UTIL_EXECUTORTHREAD_HPP_ | ||
|
||
#include <atomic> | ||
#include <chrono> | ||
#include <functional> | ||
#include <thread> | ||
|
||
namespace aikido { | ||
namespace util { | ||
|
||
/// ExecutorThread is a wrapper of std::thread that calls a callback | ||
/// periodically. | ||
/// | ||
/// If you want to let ExecutorThread calls multiple callbacks then consider | ||
/// using ExecutorMultiplexer. | ||
/// | ||
/// \code | ||
/// ExecutorThread exec( | ||
/// []() { std::cout << "running...\n"; }, std::chrono::milliseconds(10)); | ||
/// | ||
/// // thread is running | ||
/// | ||
/// // The destructor of ExecutorThread stops the thread. | ||
/// \endcode | ||
/// | ||
/// \sa ExecutorMultiplexer | ||
class ExecutorThread final | ||
{ | ||
public: | ||
/// Constructs from callback and period. The thread begins execution | ||
/// immediately upon construction. | ||
/// \param[in] callback Callback to be repeatedly executed by the thread. | ||
/// \param[in] period The period of calling the callback. | ||
template <typename Duration> | ||
ExecutorThread(std::function<void ()> callback, const Duration& period); | ||
|
||
/// Default destructor. The thread stops as ExecutorThread is destructed. | ||
~ExecutorThread(); | ||
|
||
/// Returns true if the thread is running. | ||
bool isRunning() const; | ||
|
||
/// Stops the thread. It is safe to call this function even when the thread | ||
/// already stopped. | ||
void stop(); | ||
|
||
private: | ||
/// The loop function that will be executed by the thread. | ||
void spin(); | ||
|
||
private: | ||
/// Callback to be periodically executed by the thread. | ||
std::function<void ()> mCallback; | ||
|
||
/// The callback is called in this period. | ||
std::chrono::milliseconds mPeriod; | ||
|
||
/// Flag whether the thread is running. | ||
std::atomic<bool> mIsRunning; | ||
|
||
/// Thread. | ||
std::thread mThread; | ||
}; | ||
|
||
} // namespace util | ||
} // namespace aikido | ||
|
||
#include <aikido/util/detail/ExecutorThread-impl.hpp> | ||
|
||
#endif // ifndef AIKIDO_UTIL_EXECUTORTHREAD_HPP_ |
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,19 @@ | ||
#include <aikido/util/ExecutorThread.hpp> | ||
|
||
namespace aikido { | ||
namespace util { | ||
|
||
//============================================================================== | ||
template <typename Duration> | ||
ExecutorThread::ExecutorThread( | ||
std::function<void ()> callback, const Duration& period) | ||
: mCallback{std::move(callback)}, | ||
mPeriod{std::chrono::duration_cast<std::chrono::milliseconds>(period)}, | ||
mIsRunning{true}, | ||
mThread{std::thread{&ExecutorThread::spin, this}} | ||
{ | ||
// Do nothing | ||
} | ||
|
||
} // namespace util | ||
} // namespace aikido |
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,46 @@ | ||
#include <aikido/util/ExecutorMultiplexer.hpp> | ||
|
||
#include <dart/dart.hpp> | ||
|
||
namespace aikido { | ||
namespace util { | ||
|
||
//============================================================================= | ||
void ExecutorMultiplexer::addCallback(std::function<void ()> callback) | ||
{ | ||
std::lock_guard<std::mutex> lock{mMutex}; | ||
DART_UNUSED(lock); | ||
|
||
mCallbacks.emplace_back(std::move(callback)); | ||
} | ||
|
||
//============================================================================= | ||
void ExecutorMultiplexer::removeAllCallbacks() | ||
{ | ||
mCallbacks.clear(); | ||
} | ||
|
||
//============================================================================= | ||
bool ExecutorMultiplexer::isEmpty() const | ||
{ | ||
return mCallbacks.empty(); | ||
} | ||
|
||
//============================================================================= | ||
std::size_t ExecutorMultiplexer::getNumCallbacks() const | ||
{ | ||
return mCallbacks.size(); | ||
} | ||
|
||
//============================================================================= | ||
void ExecutorMultiplexer::operator()() | ||
{ | ||
std::lock_guard<std::mutex> lock{mMutex}; | ||
DART_UNUSED(lock); | ||
|
||
for (const auto& callback : mCallbacks) | ||
callback(); | ||
} | ||
|
||
} // namespace util | ||
} // namespace aikido |
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,56 @@ | ||
#include <iostream> | ||
#include <aikido/util/ExecutorThread.hpp> | ||
|
||
namespace aikido { | ||
namespace util { | ||
|
||
//============================================================================= | ||
ExecutorThread::~ExecutorThread() | ||
{ | ||
stop(); | ||
} | ||
|
||
//============================================================================= | ||
bool ExecutorThread::isRunning() const | ||
{ | ||
return mIsRunning.load(); | ||
} | ||
|
||
//============================================================================= | ||
void ExecutorThread::stop() | ||
{ | ||
mIsRunning.store(false); | ||
|
||
if (mThread.joinable()) | ||
mThread.join(); | ||
} | ||
|
||
//============================================================================= | ||
void ExecutorThread::spin() | ||
{ | ||
auto currentTime = std::chrono::steady_clock::now(); | ||
|
||
while (mIsRunning.load()) | ||
{ | ||
try | ||
{ | ||
mCallback(); | ||
} | ||
catch (const std::exception& e) | ||
{ | ||
std::cerr << "Exception thrown by callback: " << e.what() << std::endl; | ||
// TODO: We should find another way to handle this error, so we don't | ||
// print directly to std::cerr. Unfortunately, we don't have a better | ||
// solution yet since Aikido doesn't use any particular logging framework. | ||
mIsRunning.store(false); | ||
|
||
break; | ||
} | ||
|
||
currentTime += mPeriod; | ||
std::this_thread::sleep_until(currentTime); | ||
} | ||
} | ||
|
||
} // namespace util | ||
} // namespace aikido |
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,57 @@ | ||
#include <aikido/util/ExecutorMultiplexer.hpp> | ||
#include <aikido/util/ExecutorThread.hpp> | ||
#include <gtest/gtest.h> | ||
|
||
using namespace aikido::util; | ||
|
||
static int numCalled = 0; | ||
|
||
void foo() { numCalled++; } | ||
|
||
//============================================================================== | ||
TEST(ExecutorMultiplexer, Execute) | ||
{ | ||
ExecutorMultiplexer exec; | ||
EXPECT_TRUE(exec.isEmpty()); | ||
EXPECT_TRUE(exec.getNumCallbacks() == 0u); | ||
|
||
exec.addCallback(foo); | ||
exec.addCallback([]() {}); | ||
EXPECT_TRUE(!exec.isEmpty()); | ||
EXPECT_TRUE(exec.getNumCallbacks() == 2u); | ||
|
||
EXPECT_TRUE(numCalled == 0); | ||
exec(); | ||
EXPECT_TRUE(numCalled == 1); | ||
|
||
exec.removeAllCallbacks(); | ||
EXPECT_TRUE(exec.isEmpty()); | ||
|
||
EXPECT_TRUE(numCalled == 1); | ||
exec(); | ||
EXPECT_TRUE(numCalled == 1); | ||
} | ||
|
||
//============================================================================== | ||
TEST(ExecutorThread, Execute) | ||
{ | ||
ExecutorThread exec([]() {}, std::chrono::nanoseconds(1)); | ||
|
||
EXPECT_TRUE(exec.isRunning()); | ||
exec.stop(); | ||
EXPECT_TRUE(!exec.isRunning()); | ||
} | ||
|
||
//============================================================================== | ||
TEST(ExecutorThread, ExceptionThrownByCallback) | ||
{ | ||
ExecutorThread exec( | ||
[]() { throw std::exception(); }, std::chrono::milliseconds(1)); | ||
|
||
// Assumed that the 3 second sleep is essentially a timeout for the test. If | ||
// this is not the case, either increase the sleep time or remove this test | ||
// (unless there is a better test for this). | ||
std::this_thread::sleep_for(std::chrono::seconds(3)); | ||
|
||
EXPECT_TRUE(!exec.isRunning()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: We should find another way to handle this error, so we don't print directly to
std::cerr
. Unfortunately, I don't have an immediately better solution since Aikido doesn't use any particular logging framework.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I think we'll have to keep it as a TODO for now. #155