[systemd-devel] EXT: sdbus_event loop state mark as volatile?

Simon McVittie smcv at collabora.com
Fri Sep 6 15:04:33 UTC 2019


On Fri, 06 Sep 2019 at 06:57:22 +0000, Ray, Ian (GE Healthcare) wrote:
> If thread-safety is a design goal (and I don’t believe that it is [1])
> then atomic or thread-safe primitives should be used.
> 
> [1] https://lists.freedesktop.org/archives/systemd-devel/2017-March/038519.html

[1] is about sd-bus, not sd-event, and doesn't say anything about whether
sd-event is designed to be thread-safe or not.

However, I think you're correct to say that struct sd_event is also only
designed to be used from the single thread that "owns" it.

If you need a thread-safe event loop, then you need something like
GLib's GMainContext, with mutexes to protect its data structures against
concurrent access, and a well-defined mechanism for one main-context to
"post" events to other main-contexts (which might be running concurrently
in a different thread). Many other event loops are available; GMainContext
happens to be the one I'm most familiar with, and I know that it is
designed to be thread-safe.

The price that things like GMainContext pay for being thread-safe is
that they are more complex and less efficient than sd-event: in general,
all operations on a thread-aware event loop have to pay the complexity
and performance cost of being thread-aware, even if the current program
only has one thread.

    smcv


More information about the systemd-devel mailing list