Skip to content

Commit

Permalink
[feat] mutex + fiber + thread + io
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-jpq committed Jul 19, 2022
1 parent cc852bf commit 17f7b7e
Show file tree
Hide file tree
Showing 10 changed files with 1,766 additions and 54 deletions.
6 changes: 1 addition & 5 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,7 @@ impl TryConvert for Proc {
///
/// This effectivly makes the closure's lifetime managed by Ruby. It will be
/// dropped when the returned `Value` is garbage collected.
fn wrap_closure<F, R>(func: F) -> (*mut F, Value)
where
F: FnMut(&[Value], Option<Proc>) -> R,
R: BlockReturn,
{
pub(crate) fn wrap_closure<F>(func: F) -> (*mut F, Value) {
struct Closure();
impl DataTypeFunctions for Closure {}
let data_type = memoize!(DataType: {
Expand Down
53 changes: 7 additions & 46 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,54 +679,27 @@
//!
// * `rb_fatal`:
// * `rb_fdopen`:
// * `rb_fd_clr`:
// * `rb_fd_copy`:
// * `rb_fd_dup`:
// * `rb_fd_fix_cloexec`:
// * `rb_fd_init`:
// * `rb_fd_isset`:
// * `rb_fd_max`:
// * `rb_fd_ptr`:
// * `rb_fd_resize`:
// * `rb_fd_select`:
// * `rb_fd_set`:
// * `rb_fd_term`:
// * `rb_fd_zero`:
// * `rb_feature_provided`:
// * `rb_fiber_alive_p`:
// * `rb_fiber_current`:
// * `rb_fiber_new`:
// * `rb_fiber_raise`:
// * `rb_fiber_resume`:
// * `rb_fiber_resume_kw`:
// * `rb_fiber_scheduler_address_resolve`:
// * `rb_fiber_scheduler_block`:
// * `rb_fiber_scheduler_close`:
// * `rb_fiber_transfer_kw`:
// * `rb_fiber_yield_kw`:
// * `rb_fiber_scheduler_current`:
// * `rb_fiber_scheduler_current_for_thread`:
// * `rb_fiber_scheduler_get`:
// * `rb_fiber_scheduler_io_close`:
// * `rb_fiber_scheduler_io_pread`:
// * `rb_fiber_scheduler_io_pwrite`:
// * `rb_fiber_scheduler_io_read`:
// * `rb_fiber_scheduler_io_read_memory`:
// * `rb_fiber_scheduler_io_result`:
// * `rb_fiber_scheduler_io_result_apply`:
// * `rb_fiber_scheduler_io_wait`:
// * `rb_fiber_scheduler_io_wait_readable`:
// * `rb_fiber_scheduler_io_wait_writable`:
// * `rb_fiber_scheduler_io_write`:
// * `rb_fiber_scheduler_io_write_memory`:
// * `rb_fiber_scheduler_kernel_sleep`:
// * `rb_fiber_scheduler_kernel_sleepv`:
// * `rb_fiber_scheduler_make_timeout`:
// * `rb_fiber_scheduler_process_wait`:
// * `rb_fiber_scheduler_set`:
// * `rb_fiber_scheduler_unblock`:
// * `rb_fiber_transfer`:
// * `rb_fiber_transfer_kw`:
// * `rb_fiber_yield`:
// * `rb_fiber_yield_kw`:
//! * `rb_filesystem_encindex`: [`encoding::Index::filesystem`].
//! * `rb_filesystem_encoding`:
//! [`RbEncoding::filesystem`](encoding::RbEncoding::filesystem).
Expand Down Expand Up @@ -796,7 +769,6 @@
// * `rb_funcall_passing_block`:
// * `rb_funcall_passing_block_kw`:
//! * `rb_funcall_with_block`: [`Value::funcall_with_block`].
// * `rb_funcall_with_block_kw`:
// * `rb_f_abort`:
// * `rb_f_exec`:
// * `rb_f_exit`:
Expand Down Expand Up @@ -855,7 +827,6 @@
//! # `rb_h`
//!
// * `rb_Hash`:
// * `rb_hash`:
//! * `rb_hash_aref`: [`RHash::aref`].
//! * `rb_hash_aset`: [`RHash::aset`].
// * `rb_hash_bulk_insert`:
Expand Down Expand Up @@ -917,7 +888,6 @@
//! ## `rb_io`
//!
// * `rb_io_addstr`:
// * `rb_io_ascii8bit_binmode`:
// * `rb_io_binmode`:
// * `rb_io_bufwrite`:
// * `rb_io_check_byte_readable`:
Expand All @@ -927,13 +897,9 @@
// * `rb_io_check_io`:
// * `rb_io_check_readable`:
// * `rb_io_check_writable`:
// * `rb_io_close`:
// * `rb_io_descriptor`:
// * `rb_io_eof`:
// * `rb_io_extract_encoding_option`:
// * `rb_io_extract_modeenc`:
// * `rb_io_fdopen`:
// * `rb_io_flush`:
// * `rb_io_fptr_finalize`:
// * `rb_io_getbyte`:
// * `rb_io_gets`:
Expand Down Expand Up @@ -1087,13 +1053,8 @@
// * `rb_mod_sys_fail`:
// * `rb_mod_sys_fail_str`:
// * `rb_must_asciicompat`:
// * `rb_mutex_lock`:
// * `rb_mutex_locked_p`:
// * `rb_mutex_new`:
// * `rb_mutex_sleep`:
// * `rb_mutex_synchronize`:
// * `rb_mutex_trylock`:
// * `rb_mutex_unlock`:
//!
//! ## `rb_n`
//!
Expand Down Expand Up @@ -1183,9 +1144,7 @@
// * `rb_obj_instance_eval`:
// * `rb_obj_instance_exec`:
// * `rb_obj_instance_variables`:
// * `rb_obj_is_fiber`:
// * `rb_obj_is_instance_of`:
//! * `rb_obj_is_kind_of`: [`Value::is_kind_of`].
// * `rb_obj_is_method`:
//! * `rb_obj_is_proc`: [`Proc::from_value`](block::Proc::from_value).
// * `rb_obj_method`:
Expand All @@ -1211,7 +1170,6 @@
// * `rb_path2class`:
// * `rb_path_check`:
// * `rb_path_to_class`:
// * `rb_pipe`:
// * `RB_POSFIXABLE`:
// * `rb_postponed_job_register`:
// * `rb_postponed_job_register_one`:
Expand Down Expand Up @@ -1530,7 +1488,6 @@
// * `rb_thread_current`:
// * `rb_thread_fd_close`:
// * `rb_thread_fd_select`:
// * `rb_thread_fd_writable`:
// * `rb_thread_interrupted`:
// * `rb_thread_kill`:
// * `rb_thread_local_aref`:
Expand All @@ -1544,7 +1501,6 @@
// * `rb_thread_sleep_deadly`:
// * `rb_thread_sleep_forever`:
// * `rb_thread_stop`:
// * `rb_thread_wait_fd`:
// * `rb_thread_wait_for`:
// * `rb_thread_wakeup`:
// * `rb_thread_wakeup_alive`:
Expand Down Expand Up @@ -1821,15 +1777,20 @@ mod object;
mod r_array;
mod r_bignum;
mod r_complex;
#[cfg(ruby_gt_3_0)]
pub mod r_fiber;
mod r_file;
mod r_float;
pub mod r_hash;
pub mod r_io;
mod r_match;
pub mod r_mutex;
mod r_object;
mod r_rational;
mod r_regexp;
pub mod r_string;
pub mod r_struct;
pub mod r_thread;
pub mod r_typed_data;
mod range;
#[cfg(feature = "rb-sys-interop")]
Expand Down
10 changes: 9 additions & 1 deletion src/object.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{ffi::CString, mem::transmute, ops::Deref};

use crate::ruby_sys::{
rb_define_singleton_method, rb_extend_object, rb_ivar_get, rb_ivar_set, rb_singleton_class,
rb_define_singleton_method, rb_extend_object, rb_hash, rb_ivar_get, rb_ivar_set,
rb_singleton_class,
};

use crate::{
Expand All @@ -16,6 +17,13 @@ use crate::{

/// Functions available all non-immediate values.
pub trait Object: Deref<Target = Value> + Copy {
/// Call `obj.hash`
fn hash(self) -> Result<i64, Error> {
debug_assert_value!(self);
let hash = protect(|| Value::new(unsafe { rb_hash(self.as_rb_value()) }))?;
hash.try_convert()
}

/// Define a singleton method in `self`'s scope.
///
/// Singleton methods defined on a class are Ruby's method for implementing
Expand Down
Loading

0 comments on commit 17f7b7e

Please sign in to comment.