Skip to content

Commit

Permalink
unix_fd_add: Port to safe-io
Browse files Browse the repository at this point in the history
  • Loading branch information
A6GibKm committed Jul 2, 2023
1 parent ef635d2 commit a40998e
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions glib/src/source.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(unix)]
use std::os::unix::io::RawFd;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd};
use std::{cell::RefCell, mem::transmute, num::NonZeroU32, time::Duration};

use ffi::{self, gboolean, gpointer};
#[cfg(all(not(unix), docsrs))]
use libc::c_int as RawFd;

#[cfg(any(unix, docsrs))]
use crate::IOCondition;
Expand Down Expand Up @@ -175,33 +173,35 @@ fn into_raw_child_watch_local<F: FnMut(Pid, i32) + 'static>(func: F) -> gpointer
#[cfg(any(unix, docsrs))]
#[cfg_attr(docsrs, doc(cfg(unix)))]
unsafe extern "C" fn trampoline_unix_fd<
F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static,
F: FnMut(BorrowedFd, IOCondition) -> Continue + Send + 'static,
>(
fd: i32,
raw_fd: i32,
condition: ffi::GIOCondition,
func: gpointer,
) -> gboolean {
let func: &RefCell<F> = &*(func as *const RefCell<F>);
let fd = BorrowedFd::borrow_raw(raw_fd);
(*func.borrow_mut())(fd, from_glib(condition)).into_glib()
}

#[cfg(any(unix, docsrs))]
#[cfg_attr(docsrs, doc(cfg(unix)))]
unsafe extern "C" fn trampoline_unix_fd_local<
F: FnMut(RawFd, IOCondition) -> Continue + 'static,
F: FnMut(BorrowedFd, IOCondition) -> Continue + 'static,
>(
fd: i32,
raw_fd: i32,
condition: ffi::GIOCondition,
func: gpointer,
) -> gboolean {
let func: &ThreadGuard<RefCell<F>> = &*(func as *const ThreadGuard<RefCell<F>>);
let fd = BorrowedFd::borrow_raw(raw_fd);
(*func.get_ref().borrow_mut())(fd, from_glib(condition)).into_glib()
}

#[cfg(any(unix, docsrs))]
#[cfg_attr(docsrs, doc(cfg(unix)))]
unsafe extern "C" fn destroy_closure_unix_fd<
F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static,
F: FnMut(BorrowedFd, IOCondition) -> Continue + Send + 'static,
>(
ptr: gpointer,
) {
Expand All @@ -211,7 +211,7 @@ unsafe extern "C" fn destroy_closure_unix_fd<
#[cfg(any(unix, docsrs))]
#[cfg_attr(docsrs, doc(cfg(unix)))]
unsafe extern "C" fn destroy_closure_unix_fd_local<
F: FnMut(RawFd, IOCondition) -> Continue + 'static,
F: FnMut(BorrowedFd, IOCondition) -> Continue + 'static,
>(
ptr: gpointer,
) {
Expand All @@ -220,7 +220,7 @@ unsafe extern "C" fn destroy_closure_unix_fd_local<

#[cfg(any(unix, docsrs))]
#[cfg_attr(docsrs, doc(cfg(unix)))]
fn into_raw_unix_fd<F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static>(
fn into_raw_unix_fd<F: FnMut(BorrowedFd, IOCondition) -> Continue + Send + 'static>(
func: F,
) -> gpointer {
let func: Box<RefCell<F>> = Box::new(RefCell::new(func));
Expand All @@ -229,7 +229,9 @@ fn into_raw_unix_fd<F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static>(

#[cfg(any(unix, docsrs))]
#[cfg_attr(docsrs, doc(cfg(unix)))]
fn into_raw_unix_fd_local<F: FnMut(RawFd, IOCondition) -> Continue + 'static>(func: F) -> gpointer {
fn into_raw_unix_fd_local<D: AsFd, F: FnMut(D, IOCondition) -> Continue + 'static>(
func: F,
) -> gpointer {
let func: Box<ThreadGuard<RefCell<F>>> = Box::new(ThreadGuard::new(RefCell::new(func)));
Box::into_raw(func) as gpointer
}
Expand Down Expand Up @@ -771,14 +773,15 @@ where
/// The default main loop almost always is the main loop of the main thread.
/// Thus, the closure is called on the main thread.
#[doc(alias = "g_unix_fd_add_full")]
pub fn unix_fd_add<F>(fd: RawFd, condition: IOCondition, func: F) -> SourceId
pub fn unix_fd_add<D, F>(fd: D, condition: IOCondition, func: F) -> SourceId
where
F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static,
D: AsFd,
F: FnMut(BorrowedFd, IOCondition) -> Continue + Send + 'static,
{
unsafe {
from_glib(ffi::g_unix_fd_add_full(
ffi::G_PRIORITY_DEFAULT,
fd,
fd.as_fd().as_raw_fd(),
condition.into_glib(),
Some(trampoline_unix_fd::<F>),
into_raw_unix_fd(func),
Expand All @@ -805,9 +808,10 @@ where
/// This function panics if called from a different thread than the one that
/// owns the main context.
#[doc(alias = "g_unix_fd_add_full")]
pub fn unix_fd_add_local<F>(fd: RawFd, condition: IOCondition, func: F) -> SourceId
pub fn unix_fd_add_local<D, F>(fd: D, condition: IOCondition, func: F) -> SourceId
where
F: FnMut(RawFd, IOCondition) -> Continue + 'static,
D: AsFd,
F: FnMut(BorrowedFd, IOCondition) -> Continue + 'static,
{
unsafe {
let context = MainContext::default();
Expand All @@ -816,7 +820,7 @@ where
.expect("default main context already acquired by another thread");
from_glib(ffi::g_unix_fd_add_full(
ffi::G_PRIORITY_DEFAULT,
fd,
fd.as_fd().as_raw_fd(),
condition.into_glib(),
Some(trampoline_unix_fd_local::<F>),
into_raw_unix_fd_local(func),
Expand Down Expand Up @@ -1052,18 +1056,19 @@ where
/// `func` will be called repeatedly while the file descriptor matches the given IO condition
/// until it returns `Continue(false)`.
#[doc(alias = "g_unix_fd_source_new")]
pub fn unix_fd_source_new<F>(
fd: RawFd,
pub fn unix_fd_source_new<D, F>(
fd: D,
condition: IOCondition,
name: Option<&str>,
priority: Priority,
func: F,
) -> Source
where
F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static,
D: AsFd,
F: FnMut(BorrowedFd, IOCondition) -> Continue + Send + 'static,
{
unsafe {
let source = ffi::g_unix_fd_source_new(fd, condition.into_glib());
let source = ffi::g_unix_fd_source_new(fd.as_fd().as_raw_fd(), condition.into_glib());
ffi::g_source_set_callback(
source,
Some(transmute::<
Expand Down

0 comments on commit a40998e

Please sign in to comment.