cactusdynamics / cactus-rt

A C++ framework for programming real-time applications
Mozilla Public License 2.0
185 stars 24 forks source link

Signal handling #115

Closed lu-maca closed 3 months ago

lu-maca commented 3 months ago

Hi all,

let's say I have two threads, one RT and the other non RT and the latter shall wait for an event from the first. Are you planning to introduce a generic "signal handling" module in the framework? At the moment, as far as I can see, only some termination signals can be handled.

Thanks :) Luca

shuhaowu commented 3 months ago

Hi! It seems like there are several concepts involved here:

POSIX signal handling in cactus-rt is designed to allow your application to listen to a POSIX signal. The primary intended usage for this is for the application to gracefully shutdown upon receiving of a termination signal from outside the process. The signal_handling_example shows how this can be done via either the wait on semaphore method or the busy wait method: https://github.com/cactusdynamics/cactus-rt/blob/0ee4132/examples/signal_handling_example/main.cc#L47-L55.

Waking a non-RT thread from a RT thread is not easily solvable by cactus-rt. It seems like this is what you want? There are in principle two solutions to this problem:

  1. Have your real-time thread set a condition to be true and have the non-realtime thread spin wait (either busily or with a sleep).
    • This method works, but has the drawback of CPU usage, live locks, or latency.
  2. Use condition variables or semaphores to wake up other threads blocked on those condition variables or semaphores.

We have internally also discussed the second problem of waking a non-RT thread from a RT thread (and vice versa, or even RT-to-RT signaling). Our thought process on it is as follows:

  1. The lack of support of priority inheritance on condition variables and semaphores in glibc paints a grim picture, as we cannot use standard library functions to implement such a framework. There's librtpi which implements a real-time-safe condition variable. However, it is licensed under LGPL, which makes it more difficult to use for closed-source applications. Since it is not owned by us, this would be hard to relicense for closed-source applications (like we can do for cactus-rt).
  2. There are also methods to avoid livelock and CPU usage associated with spin wait, but this is also hard to implement and not suitable for all purposes.

These imply it will be difficult for us to create a generally-applicable interface for this in cactus-rt, which is supposed to generally support 1000Hz hard-real-time applications without too many gotchas. It's likely better, at this time, for applications to pick one of the above solutions and understand the tradeoff being made. In the future where more low-level libraries are available, we may revisit this stance.

lu-maca commented 3 months ago

Thanks for the exhaustive reply. Waking a thread from another is exactly what I would like.

Basically my problem is the following: I have two threads, the first RT that sends a pulse per second and the other that shall "receive" the PPS and after 75ms do some job. Then, this second thread shall do this job every 125ms until the PPS is sent again by the first thread; when this happens the second shall "realign" with it, wait again 75ms and do the job as previously.

My first idea was to raise a SIGUSR1/USR2 signal from the first thread and to catch it in the second thread.

shuhaowu commented 3 months ago

That seems like an application-specific concerns and I don't think there's anything the framework can do. As a note, Linux signal handlers are meant not for generic code and has lots of restrictions. See signal-safety(7).

lu-maca commented 3 months ago

That seems like an application-specific concerns and I don't think there's anything the framework can do. As a note, Linux signal handlers are meant not for generic code and has lots of restrictions. See signal-safety(7).

Having a thread that wakes others is indeed a typical problem of real time systems. I'm trying to understand what are the limitations of using linux as a RTOS, this one is a big one.

Thanks for the infos.

Luca