SD_EVENT_SOURCE_UNREF(3)
π
SD_EVENT_SOURCE_UNREF(3) β the sd_event_source_unref page, bestie edition
okay so this is the SD_EVENT_SOURCE_UNREF(3) manual page (yep, from the sd_event_source_unref family, she's the main character of this file) and she's here to teach you about reference counters on event sources. ngl it's lowkey iconic how much these tiny functions do. let's go π
β¨ NAME β¨
sd_event_source_unref, sd_event_source_unrefp, sd_event_source_ref, sd_event_source_disable_unref, sd_event_source_disable_unrefp β increase or decrease event source reference counters, that's the whole vibe π
πΈ SYNOPSIS πΈ
#include <systemd/sd-event.h>
sd_event_source* sd_event_source_unref(sd_event_source *source);
void sd_event_source_unrefp(sd_event_source **source);
sd_event_source* sd_event_source_ref(sd_event_source *source);
sd_event_source* sd_event_source_disable_unref(sd_event_source *source);
void sd_event_source_disable_unrefp(sd_event_source **source);
π¦ DESCRIPTION π¦
sd_event_source_unref()β use this to decrement by one the internal reference counter of the event source object you passed assource. The counter starts out at one when the event source gets created, via calls likesd_event_add_io(3)orsd_event_add_time(3). And the second the counter hits zero? the object gets detached from the event loop object and destroyed. bye bestie πsd_event_source_unrefp()β basicallysd_event_source_unref()'s sibling, except she takes a pointer to a pointer to ansd_event_sourceobject. This one is so useful with GCC's and LLVM's Clean-up Variable Attribute[1]. Note: this function is defined as an inline function. π§sd_event_source_ref()β the opposite direction, queen: increase by one the internal reference counter of the event source object you handed her assource.- so here's a fun little quirk:
sd_event_source_unref(),sd_bus_creds_unrefp()andsd_bus_creds_ref()execute no operation if the event source object you passed isNULL. no drama, no crash, just vibes π
β οΈ real talk though: event source objects stay alive and may be dispatched as long as they have a reference counter greater than zero. so if you want to drop a reference of an event source and make sure the associated event source handler function is not called anymore, the move is to combine a call ofsd_event_source_unref()with a prior call tosd_event_source_set_enabled()withSD_EVENT_OFFβ or just callsd_event_source_disable_unref(), see below. no cap, do this.
sd_event_source_disable_unref()β she's the combo meal π: a call tosd_event_source_set_enabled()withSD_EVENT_OFFplussd_event_source_unref(). This ensures the source is disabled before the local reference to it is lost. And good news β thesourceparameter is allowed to beNULL.sd_event_source_disable_unrefp()β similar tosd_event_source_unrefp(), but in addition it disables the source first. Same deal as before: useful with GCC's and LLVM's Clean-up Variable Attribute[1], and note that this function is defined as an inline function. π«Ά
π₯ RETURN VALUE π₯
sd_event_source_unref() and sd_event_source_disable_unref() always return NULL. and sd_event_source_ref() always returns the event source object you passed in. she gives back what you gave her, periodt.
π NOTES π
Functions described here are available as a shared library, which can be compiled against and linked to with the libsystemd pkg-config(1) file. β¨
All functions listed here are thread-agnostic and only a single thread may operate on a given object at any given time. Different threads may access the same object at different times. Multiple independent objects may be used from different threads in parallel. (yes, it's giving "share the toy but one at a time" π)
The code described here uses getenv(3), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not call setenv(3) from a parallel thread. It is recommended to only do calls to setenv() from an early phase of the program when no other threads have been started. π§
π HISTORY π
sd_event_source_unref(),sd_event_source_unrefp(), andsd_event_source_ref()were added in version 229.sd_event_source_disable_unref()andsd_event_source_disable_unrefp()were added in version 243.
glow-up timeline, honestly. π
β SEE ALSO β
sd-event(3), sd_event_add_io(3), sd_event_add_time(3), sd_event_add_signal(3), sd_event_add_child(3), sd_event_add_inotify(3), sd_event_add_defer(3), sd_event_source_set_enabled(3) β go read all of them, iykyk π¦
π NOTES (the footnote one)
- Clean-up Variable Attribute β https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
and that's the page!! this one shipped with systemd 261.3, so if you're quoting her in a group chat, cite her properly π«Ά mwah, go unref something responsibly π
π¦ where this page comes from
- ubuntu stonking: libsystemd-dev 261.3-0ubuntu3 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms