diff options
author | Philip Withnall <pwithnall@endlessos.org> | 2023-03-23 00:52:02 +0000 |
---|---|---|
committer | Philip Withnall <pwithnall@endlessos.org> | 2023-03-23 14:11:36 +0000 |
commit | 56c013cb6e8a9d5a6e42fc9846ecc24f289834e1 (patch) | |
tree | 3ae71a71c6e87962dd6c0e4d54d85a761058d1c3 | |
parent | 8b0cf41d88ada0b9b8034d7596b318012635065d (diff) | |
download | glib-56c013cb6e8a9d5a6e42fc9846ecc24f289834e1.tar.gz |
inotify: Use IN_NONBLOCK to avoid a fcntl() syscall where possible
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 <pwithnall@endlessos.org>
-rw-r--r-- | gio/inotify/inotify-kernel.c | 15 |
1 files 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); } |