Skip to content

Commit

Permalink
Add timers' set_calback methods that use SchedulingGroup
Browse files Browse the repository at this point in the history
This commit adds the method `set_callback_under_group` to
all 3 timer specializations.
  • Loading branch information
patjed41 committed Mar 24, 2023
1 parent 3c822a4 commit 595b15b
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
61 changes: 61 additions & 0 deletions seastar/src/clocks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::cxx_async_futures::VoidFuture;
use crate::SchedulingGroup;
use core::cmp::Ordering;
use cxx::UniquePtr;
use std::fmt;
Expand Down Expand Up @@ -37,6 +38,13 @@ mod ffi {
fn manual_sleep(nanos: i64) -> VoidFuture;
}

#[namespace = "seastar_ffi::scheduling"]
unsafe extern "C++" {
include!("seastar/src/scheduling.hh");

type scheduling_group = crate::scheduling_group;
}

#[namespace = "seastar_ffi::timer::steady_clock"]
unsafe extern "C++" {
include!("seastar/src/timer.hh");
Expand All @@ -52,6 +60,14 @@ mod ffi {
dropper: unsafe fn(*mut u8),
);

unsafe fn sct_set_callback_under_group(
timer: Pin<&mut steady_clock_timer>,
callback: *mut u8,
caller: unsafe fn(*mut u8),
dropper: unsafe fn(*mut u8),
sg: &scheduling_group,
);

fn sct_arm_at(timer: Pin<&mut steady_clock_timer>, at: i64);
fn sct_arm_at_periodic(timer: Pin<&mut steady_clock_timer>, at: i64, period: i64);

Expand Down Expand Up @@ -80,6 +96,14 @@ mod ffi {
dropper: unsafe fn(*mut u8),
);

unsafe fn lct_set_callback_under_group(
timer: Pin<&mut lowres_clock_timer>,
callback: *mut u8,
caller: unsafe fn(*mut u8),
dropper: unsafe fn(*mut u8),
sg: &scheduling_group,
);

fn lct_arm_at(timer: Pin<&mut lowres_clock_timer>, at: i64);
fn lct_arm_at_periodic(timer: Pin<&mut lowres_clock_timer>, at: i64, period: i64);

Expand Down Expand Up @@ -108,6 +132,14 @@ mod ffi {
dropper: unsafe fn(*mut u8),
);

unsafe fn mct_set_callback_under_group(
timer: Pin<&mut manual_clock_timer>,
callback: *mut u8,
caller: unsafe fn(*mut u8),
dropper: unsafe fn(*mut u8),
sg: &scheduling_group,
);

fn mct_arm_at(timer: Pin<&mut manual_clock_timer>, at: i64);
fn mct_arm_at_periodic(timer: Pin<&mut manual_clock_timer>, at: i64, period: i64);

Expand Down Expand Up @@ -574,6 +606,14 @@ mod clock_implementation {
dropper: fn(*mut u8),
);

fn set_callback_under_group(
cpp_timer: &mut CppTimer,
callback: *mut u8,
caller: fn(*mut u8),
dropper: fn(*mut u8),
sg: &SchedulingGroup,
);

fn arm_at(cpp_timer: &mut CppTimer, at: i64);

fn arm_at_periodic(cpp_timer: &mut CppTimer, at: i64, period: i64);
Expand Down Expand Up @@ -620,6 +660,27 @@ macro_rules! timer_impl {
};
}

fn set_callback_under_group(
cpp_timer: &mut CppTimer,
callback: *mut u8,
caller: fn(*mut u8),
dropper: fn(*mut u8),
sg: &SchedulingGroup,
) {
match cpp_timer {
CppTimer::$cpp_timer_variant(timer) => unsafe {
[<$ffi_pref _set_callback_under_group>](
timer.pin_mut(),
callback,
caller,
dropper,
&sg.inner,
);
},
_ => panic!(),
};
}

fn arm_at(cpp_timer: &mut CppTimer, at: i64) {
match cpp_timer {
CppTimer::$cpp_timer_variant(timer) => {
Expand Down
1 change: 1 addition & 0 deletions seastar/src/scheduling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod ffi {
}
}

pub(crate) use ffi::scheduling_group;
use ffi::*;

/// Identifies function calls that are accounted as a group.
Expand Down
30 changes: 30 additions & 0 deletions seastar/src/timer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ void sct_set_callback(
timer.set_callback(callback_object(callback, caller, dropper));
}

void sct_set_callback_under_group(
steady_clock_timer& timer,
uint8_t* callback,
rust::Fn<void(uint8_t*)> caller,
rust::Fn<void(uint8_t*)> dropper,
const scheduling_group& sg
) {
timer.set_callback(sg, callback_object(callback, caller, dropper));
}

void sct_arm_at(steady_clock_timer& timer, int64_t at) {
timer.arm(to_sc_time_point(at));
}
Expand Down Expand Up @@ -124,6 +134,16 @@ void lct_set_callback(
timer.set_callback(callback_object(callback, caller, dropper));
}

void lct_set_callback_under_group(
lowres_clock_timer& timer,
uint8_t* callback,
rust::Fn<void(uint8_t*)> caller,
rust::Fn<void(uint8_t*)> dropper,
const scheduling_group& sg
) {
timer.set_callback(sg, callback_object(callback, caller, dropper));
}

void lct_arm_at(lowres_clock_timer& timer, int64_t at) {
timer.arm(to_lc_time_point(at));
}
Expand Down Expand Up @@ -173,6 +193,16 @@ void mct_set_callback(
timer.set_callback(callback_object(callback, caller, dropper));
}

void mct_set_callback_under_group(
manual_clock_timer& timer,
uint8_t* callback,
rust::Fn<void(uint8_t*)> caller,
rust::Fn<void(uint8_t*)> dropper,
const scheduling_group& sg
) {
timer.set_callback(sg, callback_object(callback, caller, dropper));
}

void mct_arm_at(manual_clock_timer& timer, int64_t at) {
timer.arm(to_mc_time_point(at));
}
Expand Down
26 changes: 26 additions & 0 deletions seastar/src/timer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace seastar_ffi {
namespace timer {

using scheduling_group = seastar::scheduling_group;

namespace steady_clock {

using steady_clock_timer = seastar::timer<seastar::steady_clock_type>;
Expand All @@ -20,6 +22,14 @@ void sct_set_callback(
rust::Fn<void(uint8_t*)> dropper
);

void sct_set_callback_under_group(
steady_clock_timer& timer,
uint8_t* callback,
rust::Fn<void(uint8_t*)> caller,
rust::Fn<void(uint8_t*)> dropper,
const scheduling_group& sg
);

void sct_arm_at(steady_clock_timer& timer, int64_t at);

void sct_arm_at_periodic(steady_clock_timer& timer, int64_t at, int64_t period);
Expand Down Expand Up @@ -49,6 +59,14 @@ void lct_set_callback(
rust::Fn<void(uint8_t*)> dropper
);

void lct_set_callback_under_group(
lowres_clock_timer& timer,
uint8_t* callback,
rust::Fn<void(uint8_t*)> caller,
rust::Fn<void(uint8_t*)> dropper,
const scheduling_group& sg
);

void lct_arm_at(lowres_clock_timer& timer, int64_t at);

void lct_arm_at_periodic(lowres_clock_timer& timer, int64_t ay, int64_t period);
Expand Down Expand Up @@ -78,6 +96,14 @@ void mct_set_callback(
rust::Fn<void(uint8_t*)> dropper
);

void mct_set_callback_under_group(
manual_clock_timer& timer,
uint8_t* callback,
rust::Fn<void(uint8_t*)> caller,
rust::Fn<void(uint8_t*)> dropper,
const scheduling_group& sg
);

void mct_arm_at(manual_clock_timer& timer, int64_t at);

void mct_arm_at_periodic(manual_clock_timer& timer, int64_t at, int64_t period);
Expand Down
18 changes: 18 additions & 0 deletions seastar/src/timer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::assert_runtime_is_running;
use crate::ffi_utils::{get_dropper, get_fn_mut_void_caller};
use crate::CppTimer;
use crate::SchedulingGroup;
use crate::{Clock, Duration, Instant};
use std::marker::PhantomData;

Expand Down Expand Up @@ -94,6 +95,23 @@ impl<ClockType: Clock> Timer<ClockType> {
ClockType::set_callback(&mut self.inner, boxed_callback, caller, dropper);
}

/// Sets the callback function to be called under specified group when
/// the timer expires.
///
/// # Arguments
/// * `callback` - The callback to be executed when the timer expires.
/// * `sg` - The scheduling group under which the callback will be executed.
pub fn set_callback_under_group<Func: FnMut() + 'static>(
&mut self,
callback: Func,
sg: &SchedulingGroup,
) {
let caller = get_fn_mut_void_caller(&callback);
let dropper = get_dropper(&callback);
let boxed_callback = Box::into_raw(Box::new(callback)) as *mut u8;
ClockType::set_callback_under_group(&mut self.inner, boxed_callback, caller, dropper, &sg);
}

/// Sets the timer expiration time.
///
/// It is illegal to arm a timer that has already been armed (and not disarmed by
Expand Down

0 comments on commit 595b15b

Please sign in to comment.