From 56c013cb6e8a9d5a6e42fc9846ecc24f289834e1 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 23 Mar 2023 00:52:02 +0000 Subject: [PATCH] inotify: Use IN_NONBLOCK to avoid a fcntl() syscall where possible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `inotify_init1()` API has supported this flag for a long time (possibly since it was first introduced, although I haven’t bothered doing the archaeology). This saves a syscall when first connecting to inotify. Signed-off-by: Philip Withnall --- gio/inotify/inotify-kernel.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/gio/inotify/inotify-kernel.c b/gio/inotify/inotify-kernel.c index 92d61fc31..7733d398e 100644 --- a/gio/inotify/inotify-kernel.c +++ b/gio/inotify/inotify-kernel.c @@ -377,6 +377,7 @@ ik_source_new (gboolean (* callback) (ik_event_t *event)) }; InotifyKernelSource *iks; GSource *source; + gboolean should_set_nonblock = FALSE; source = g_source_new (&source_funcs, sizeof (InotifyKernelSource)); iks = (InotifyKernelSource *) source; @@ -384,17 +385,23 @@ ik_source_new (gboolean (* callback) (ik_event_t *event)) g_source_set_static_name (source, "inotify kernel source"); iks->unmatched_moves = g_hash_table_new (NULL, NULL); - iks->fd = inotify_init1 (IN_CLOEXEC); + iks->fd = inotify_init1 (IN_CLOEXEC | IN_NONBLOCK); if (iks->fd < 0) - iks->fd = inotify_init (); + { + should_set_nonblock = TRUE; + iks->fd = inotify_init (); + } if (iks->fd >= 0) { GError *error = NULL; - g_unix_set_fd_nonblocking (iks->fd, TRUE, &error); - g_assert_no_error (error); + if (should_set_nonblock) + { + g_unix_set_fd_nonblocking (iks->fd, TRUE, &error); + g_assert_no_error (error); + } iks->fd_tag = g_source_add_unix_fd (source, iks->fd, G_IO_IN); }