Skip to content
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

optimize timeout judgement according to different condition #187

Merged
merged 3 commits into from
Feb 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions rmw_fastrtps_cpp/src/rmw_wait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,22 @@ rmw_wait(
// otherwise the decision to wait might be incorrect
std::unique_lock<std::mutex> lock(*conditionMutex);

// First check variables.
// If wait_timeout is null, wait indefinitely (so we have to wait)
// If wait_timeout is not null and either of its fields are nonzero, we have to wait
bool hasToWait = (wait_timeout && (wait_timeout->sec > 0 || wait_timeout->nsec > 0)) ||
!wait_timeout;
hasToWait &= !check_wait_set_for_data(subscriptions, guard_conditions, services, clients);
bool hasData = check_wait_set_for_data(subscriptions, guard_conditions, services, clients);
auto predicate = [subscriptions, guard_conditions, services, clients]() {
return check_wait_set_for_data(subscriptions, guard_conditions, services, clients);
};

bool timeout = false;

if (hasToWait) {
if (!hasData) {
if (!wait_timeout) {
conditionVariable->wait(lock);
} else {
auto predicate = [subscriptions, guard_conditions, services, clients]() {
return check_wait_set_for_data(subscriptions, guard_conditions, services, clients);
};
conditionVariable->wait(lock, predicate);
} else if (wait_timeout->sec > 0 || wait_timeout->nsec > 0) {
auto n = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::seconds(wait_timeout->sec));
n += std::chrono::nanoseconds(wait_timeout->nsec);
timeout = !conditionVariable->wait_for(lock, n, predicate);
} else {
timeout = true;
}
}

Expand All @@ -173,14 +169,6 @@ rmw_wait(
// after we check, it will be caught on the next call to this function).
lock.unlock();

// Even if this was a non-blocking wait, signal a timeout if there's no data.
// This makes the return behavior consistent with rcl expectations for zero timeout value.
// Do this before detaching the listeners because the data gets cleared for guard conditions.
bool hasData = check_wait_set_for_data(subscriptions, guard_conditions, services, clients);
if (!hasData && wait_timeout && wait_timeout->sec == 0 && wait_timeout->nsec == 0) {
timeout = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this is what causes the tests to fail. If the timeout is reached (even if the timeout is 0) RMW_RET_TIMEOUT should be returned (or at least that's what the test expects)

}

if (subscriptions) {
for (size_t i = 0; i < subscriptions->subscriber_count; ++i) {
void * data = subscriptions->subscribers[i];
Expand Down