Skip to content

Commit

Permalink
Implemented StableApiDefinition::thread_sleep for TruffleRuby. (#457)
Browse files Browse the repository at this point in the history
* Implemented StableApiDefinition::thread_sleep.

* Shorter sleep in thread_sleep test.
  • Loading branch information
goyox86 authored Dec 11, 2024
1 parent 40146c2 commit b932fe6
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 2 deletions.
8 changes: 8 additions & 0 deletions crates/rb-sys-tests/src/stable_api_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,11 @@ parity_test!(
},
expected: true
);

parity_test!(
name: test_rb_thread_sleep,
func: thread_sleep,
data_factory: {
std::time::Duration::from_millis(100)
}
);
4 changes: 4 additions & 0 deletions crates/rb-sys/src/stable_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::VALUE;
use std::{
os::raw::{c_char, c_long},
ptr::NonNull,
time::Duration,
};

pub trait StableApiDefinition {
Expand Down Expand Up @@ -171,6 +172,9 @@ pub trait StableApiDefinition {
/// access to underlying flags of the RString. The caller must ensure that
/// the `VALUE` is a valid pointer to an RString.
unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool;

/// Blocks the current thread until the given duration has passed.
fn thread_sleep(&self, duration: Duration);
}

#[cfg(stable_api_enable_compiled_mod)]
Expand Down
6 changes: 6 additions & 0 deletions crates/rb-sys/src/stable_api/compiled.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,9 @@ impl_rstring_interned_p(VALUE obj) {

return !(FL_TEST(obj, RSTRING_FSTR) == 0);
}

void
impl_thread_sleep(struct timeval time) {
rb_thread_wait_for(time);
}

20 changes: 19 additions & 1 deletion crates/rb-sys/src/stable_api/compiled.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::StableApiDefinition;
use crate::{ruby_value_type, VALUE};
use crate::{ruby_value_type, timeval, VALUE};
use std::{
os::raw::{c_char, c_long},
ptr::NonNull,
time::Duration,
};

#[allow(dead_code)]
Expand Down Expand Up @@ -75,6 +76,9 @@ extern "C" {

#[link_name = "impl_rstring_interned_p"]
fn impl_rstring_interned_p(obj: VALUE) -> bool;

#[link_name = "impl_thread_sleep"]
fn impl_thread_sleep(interval: timeval);
}

pub struct Definition;
Expand Down Expand Up @@ -192,7 +196,21 @@ impl StableApiDefinition for Definition {
impl_integer_type_p(obj)
}

#[inline]
unsafe fn rstring_interned_p(&self, obj: VALUE) -> bool {
impl_rstring_interned_p(obj)
}

#[inline]
fn thread_sleep(&self, duration: Duration) {
let seconds = duration.as_secs() as _;
let microseconds = duration.subsec_micros() as _;

let time = crate::timeval {
tv_sec: seconds,
tv_usec: microseconds,
};

unsafe { impl_thread_sleep(time) }
}
}
16 changes: 15 additions & 1 deletion crates/rb-sys/src/stable_api/ruby_2_6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
use std::{
os::raw::{c_char, c_long},
ptr::NonNull,
time::Duration,
};

#[cfg(not(ruby_eq_2_6))]
Expand Down Expand Up @@ -103,7 +104,7 @@ impl StableApiDefinition for Definition {
if self.special_const_p(obj) {
true
} else {
let rbasic = obj as *const crate::Rbasic;
let rbasic = obj as *const crate::RBasic;
((*rbasic).flags & crate::ruby_fl_type::RUBY_FL_FREEZE as VALUE) != 0
}
}
Expand Down Expand Up @@ -262,4 +263,17 @@ impl StableApiDefinition for Definition {

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}

#[inline]
fn thread_sleep(&self, duration: Duration) {
let seconds = duration.as_secs() as _;
let microseconds = duration.subsec_micros() as _;

let time = crate::timeval {
tv_sec: seconds,
tv_usec: microseconds,
};

unsafe { crate::rb_thread_wait_for(time) }
}
}
14 changes: 14 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_2_7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use std::{
os::raw::{c_char, c_long},
ptr::NonNull,
time::Duration,
};

#[cfg(not(ruby_eq_2_7))]
Expand Down Expand Up @@ -262,4 +263,17 @@ impl StableApiDefinition for Definition {

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}

#[inline]
fn thread_sleep(&self, duration: Duration) {
let seconds = duration.as_secs() as _;
let microseconds = duration.subsec_micros() as _;

let time = crate::timeval {
tv_sec: seconds,
tv_usec: microseconds,
};

unsafe { crate::rb_thread_wait_for(time) }
}
}
14 changes: 14 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_3_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use std::{
os::raw::{c_char, c_long},
ptr::NonNull,
time::Duration,
};

#[cfg(not(ruby_eq_3_0))]
Expand Down Expand Up @@ -270,4 +271,17 @@ impl StableApiDefinition for Definition {

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}

#[inline]
fn thread_sleep(&self, duration: Duration) {
let seconds = duration.as_secs() as _;
let microseconds = duration.subsec_micros() as _;

let time = crate::timeval {
tv_sec: seconds,
tv_usec: microseconds,
};

unsafe { crate::rb_thread_wait_for(time) }
}
}
14 changes: 14 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_3_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use std::{
os::raw::{c_char, c_long},
ptr::NonNull,
time::Duration,
};

#[cfg(not(ruby_eq_3_1))]
Expand Down Expand Up @@ -263,4 +264,17 @@ impl StableApiDefinition for Definition {

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}

#[inline]
fn thread_sleep(&self, duration: Duration) {
let seconds = duration.as_secs() as _;
let microseconds = duration.subsec_micros() as _;

let time = crate::timeval {
tv_sec: seconds,
tv_usec: microseconds,
};

unsafe { crate::rb_thread_wait_for(time) }
}
}
14 changes: 14 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_3_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use std::{
os::raw::{c_char, c_long},
ptr::NonNull,
time::Duration,
};

#[cfg(not(ruby_eq_3_2))]
Expand Down Expand Up @@ -261,4 +262,17 @@ impl StableApiDefinition for Definition {

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}

#[inline]
fn thread_sleep(&self, duration: Duration) {
let seconds = duration.as_secs() as _;
let microseconds = duration.subsec_micros() as _;

let time = crate::timeval {
tv_sec: seconds,
tv_usec: microseconds,
};

unsafe { crate::rb_thread_wait_for(time) }
}
}
14 changes: 14 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_3_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use std::{
os::raw::{c_char, c_long},
ptr::NonNull,
time::Duration,
};

#[cfg(not(ruby_eq_3_3))]
Expand Down Expand Up @@ -254,4 +255,17 @@ impl StableApiDefinition for Definition {

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}

#[inline]
fn thread_sleep(&self, duration: Duration) {
let seconds = duration.as_secs() as _;
let microseconds = duration.subsec_micros() as _;

let time = crate::timeval {
tv_sec: seconds,
tv_usec: microseconds,
};

unsafe { crate::rb_thread_wait_for(time) }
}
}
14 changes: 14 additions & 0 deletions crates/rb-sys/src/stable_api/ruby_3_4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
use std::{
os::raw::{c_char, c_long},
ptr::NonNull,
time::Duration,
};

#[cfg(not(ruby_eq_3_4))]
Expand Down Expand Up @@ -254,4 +255,17 @@ impl StableApiDefinition for Definition {

(flags & crate::ruby_rstring_flags::RSTRING_FSTR as VALUE) != 0
}

#[inline]
fn thread_sleep(&self, duration: Duration) {
let seconds = duration.as_secs() as _;
let microseconds = duration.subsec_micros() as _;

let time = crate::timeval {
tv_sec: seconds,
tv_usec: microseconds,
};

unsafe { crate::rb_thread_wait_for(time) }
}
}

0 comments on commit b932fe6

Please sign in to comment.