Skip to content

Commit 5e47fa2

Browse files
authored
Merge pull request #1614 from ogios/main
glib: `unix_fd_add_full` and `unix_fd_add_local_full`
2 parents 42e781a + 5091577 commit 5e47fa2

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

glib/src/source.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,39 @@ where
888888
}
889889
}
890890

891+
#[cfg(unix)]
892+
#[cfg_attr(docsrs, doc(cfg(unix)))]
893+
// rustdoc-stripper-ignore-next
894+
/// Adds a closure to be called by the main loop the returned `Source` is attached to whenever a
895+
/// UNIX file descriptor reaches the given IO condition.
896+
///
897+
/// `func` will be called repeatedly with `priority` while the file descriptor matches the given IO condition
898+
/// until it returns `ControlFlow::Break`.
899+
///
900+
/// The default main loop almost always is the main loop of the main thread.
901+
/// Thus, the closure is called on the main thread.
902+
#[doc(alias = "g_unix_fd_add_full")]
903+
pub fn unix_fd_add_full<F>(
904+
fd: RawFd,
905+
priority: Priority,
906+
condition: IOCondition,
907+
func: F,
908+
) -> SourceId
909+
where
910+
F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static,
911+
{
912+
unsafe {
913+
from_glib(ffi::g_unix_fd_add_full(
914+
priority.into_glib(),
915+
fd,
916+
condition.into_glib(),
917+
Some(trampoline_unix_fd::<F>),
918+
into_raw_unix_fd(func),
919+
Some(destroy_closure_unix_fd::<F>),
920+
))
921+
}
922+
}
923+
891924
#[cfg(unix)]
892925
#[cfg_attr(docsrs, doc(cfg(unix)))]
893926
// rustdoc-stripper-ignore-next
@@ -926,6 +959,49 @@ where
926959
}
927960
}
928961

962+
#[cfg(unix)]
963+
#[cfg_attr(docsrs, doc(cfg(unix)))]
964+
// rustdoc-stripper-ignore-next
965+
/// Adds a closure to be called by the main loop the returned `Source` is attached to whenever a
966+
/// UNIX file descriptor reaches the given IO condition.
967+
///
968+
/// `func` will be called repeatedly with `priority` while the file descriptor matches the given IO condition
969+
/// until it returns `ControlFlow::Break`.
970+
///
971+
/// The default main loop almost always is the main loop of the main thread.
972+
/// Thus, the closure is called on the main thread.
973+
///
974+
/// Different to `unix_fd_add()`, this does not require `func` to be
975+
/// `Send` but can only be called from the thread that owns the main context.
976+
///
977+
/// This function panics if called from a different thread than the one that
978+
/// owns the main context.
979+
#[doc(alias = "g_unix_fd_add_full")]
980+
pub fn unix_fd_add_local_full<F>(
981+
fd: RawFd,
982+
priority: Priority,
983+
condition: IOCondition,
984+
func: F,
985+
) -> SourceId
986+
where
987+
F: FnMut(RawFd, IOCondition) -> ControlFlow + 'static,
988+
{
989+
unsafe {
990+
let context = MainContext::default();
991+
let _acquire = context
992+
.acquire()
993+
.expect("default main context already acquired by another thread");
994+
from_glib(ffi::g_unix_fd_add_full(
995+
priority.into_glib(),
996+
fd,
997+
condition.into_glib(),
998+
Some(trampoline_unix_fd_local::<F>),
999+
into_raw_unix_fd_local(func),
1000+
Some(destroy_closure_unix_fd_local::<F>),
1001+
))
1002+
}
1003+
}
1004+
9291005
// rustdoc-stripper-ignore-next
9301006
/// The priority of sources
9311007
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]

0 commit comments

Comments
 (0)