Robotics StackExchange | Archived questions

guard condition race condition

Hello world, I read about guard conditions and how they can be used to interrupt calls to rcl_wait, but I encountered a problem.

Since the guard condition is missing functions for resetting it I assume no state is being stored and interrupting would only work if the guard condition is being waited on while triggering it.

I want to use one to implement an executor for ROS2 Humble which can be "interrupted" which would cancel a spin if currently running and prevent new spins from being started until another action has completed. I tought I could implement that with a guard condition and a boolean flag in the following way:

interrupt:
    closed = true
    guard_condition.trigger()
    <lock wait set while performing action>
    closed = false

spin:
    if (closed):
        return
    <lock wait set while doing stuff or return if the lock could not be aquired>

Memory fences aside, this code has a race condition if setting the flag and triggering the guard condition happens between the check and the call to rcl_wait which could cause the close operation to wait until the spin completes naturally.

To prevent this I would need to release a lock after entering the wait, which would be similar to a condition variable and is not possible with rcl_wait.

Is there a way to prevent this race condition or do I need to find another way?

Asked by Deric on 2023-01-11 06:57:44 UTC

Comments

Answers