-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In the current code, there is a race condition in rmw_wait. This happens because of the following sequence: 1. Without taking a lock, we check if any of the entities in the wait_set are ready, and if not attach the condition_variable to it. 2. Then we take the lock, and go to sleep on the condition_variable. However, doing step 1 takes time, and we check in a particular order: guard_conditions, events, subscriptions, services, clients. The race happens because a subscription may come in after we've checked subscriptions in the above list (while we are checking services and clients). In that case, we'll unnecessarily go to sleep on the condition_variable, even though something is ready. This increases the latency. To solve the issue, we still do all of the checking and attaching unlocked. However, we update the "notify" method of each entity so that it takes the condition_variable lock, and in addition to kicking the condition_variable it sets a boolean in the wait_set structure to "true". Then, in rmw_wait(), after we have finished attaching, we take the lock, and set a predicate on the condition_variable so that it will quit if wait_set_data->triggered is true. Thus, if one of the entities became ready after we checked, we'll notice it and not go to sleep. This also allows us to deal with "spurious" wakeups, where the condition_variable was woken up even though nothing in this wait_set became ready. Signed-off-by: Chris Lalancette <[email protected]>
- Loading branch information
1 parent
61f2a70
commit f9ee9ed
Showing
8 changed files
with
111 additions
and
61 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
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
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,37 @@ | ||
// Copyright 2023 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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 DETAIL__RMW_WAIT_SET_DATA_HPP_ | ||
#define DETAIL__RMW_WAIT_SET_DATA_HPP_ | ||
|
||
#include <condition_variable> | ||
#include <mutex> | ||
|
||
#include "rmw/rmw.h" | ||
|
||
namespace rmw_zenoh_cpp | ||
{ | ||
|
||
struct rmw_wait_set_data_t | ||
{ | ||
std::condition_variable condition_variable; | ||
std::mutex condition_mutex; | ||
|
||
bool triggered{false}; | ||
|
||
rmw_context_t * context; | ||
}; | ||
} // namespace rmw_zenoh_cpp | ||
|
||
#endif // DETAIL__RMW_WAIT_SET_DATA_HPP_ |
Oops, something went wrong.