Struct rutie::Thread [−][src]
pub struct Thread { /* fields omitted */ }
Expand description
Thread
Implementations
Creates a new green thread.
The returning value of the closure will be available as #value
of the thread
Examples
use rutie::{Fixnum, Thread, VM};
Thread::new(|| {
let computation_result = 1 + 2;
Fixnum::new(computation_result)
});
Ruby:
Thread.new do
computation_result = 1 + 2
computation_result
end
Tells scheduler to switch to other threads while current thread is waiting for a readable event on the given file descriptor.
Examples
use std::os::unix::io::AsRawFd;
use std::os::unix::net::UnixStream;
use rutie::{Thread, VM};
let (unix_socket, _) = UnixStream::pair().unwrap();
Thread::wait_fd(unix_socket.as_raw_fd());
pub fn call_without_gvl<F, R, G>(func: F, unblock_func: Option<G>) -> R where
F: FnMut() -> R,
G: FnMut(),
pub fn call_without_gvl<F, R, G>(func: F, unblock_func: Option<G>) -> R where
F: FnMut() -> R,
G: FnMut(),
Release GVL for current thread.
Warning! Due to MRI limitations, interaction with Ruby objects is not allowed while GVL is released, it may cause unexpected behaviour. Read more at Ruby documentation
You should extract all the information from Ruby world before invoking
thread_call_without_gvl
.
GVL will be re-acquired when the closure is finished.
Examples
#[macro_use] extern crate rutie;
use rutie::{Class, Fixnum, Object, Thread};
class!(Calculator);
methods!(
Calculator,
rtself,
fn heavy_computation() -> Fixnum {
let computation = || { 2 * 2 };
let unblocking_function = || {};
// release GVL for current thread until `computation` is completed
let result = Thread::call_without_gvl(
computation,
Some(unblocking_function)
);
// GVL is re-acquired, we can interact with Ruby-world
Fixnum::new(result)
}
);
fn main() {
Class::new("Calculator", None).define(|klass| {
klass.def("heavy_computation", heavy_computation);
});
}
pub fn call_without_gvl2<F, R, G>(func: F, unblock_func: Option<G>) -> R where
F: FnMut() -> R,
G: FnMut(),
Trait Implementations
Returns a singleton class of current object. Read more
Gets an immutable reference to the Rust structure which is wrapped into a Ruby object. Read more
Gets a mutable reference to the Rust structure which is wrapped into a Ruby object.
Defines an instance method for the given class or object. Read more
Defines a private instance method for the given class or object. Read more
Defines a class method for given class or singleton method for object. Read more
An alias for define_method
(similar to Ruby syntax def some_method
).
An alias for define_private_method
(similar to Ruby syntax private def some_method
).
An alias for define_singleton_method
(similar to Ruby def self.some_method
).
Calls a given method on an object similarly to Ruby Object#send
method Read more
Checks whether the object responds to given method Read more
fn protect_send(
&self,
method: &str,
arguments: &[AnyObject]
) -> Result<AnyObject, AnyException>
fn protect_send(
&self,
method: &str,
arguments: &[AnyObject]
) -> Result<AnyObject, AnyException>
protect_send
returns Result<AnyObject, AnyObject> Read more
fn protect_public_send(
&self,
method: &str,
arguments: &[AnyObject]
) -> Result<AnyObject, AnyException>
fn protect_public_send(
&self,
method: &str,
arguments: &[AnyObject]
) -> Result<AnyObject, AnyException>
protect_public_send
returns Result<AnyObject, AnyObject> Read more
Converts struct to AnyObject
Read more
Gets an instance variable of object Read more
Sets an instance variable for object Read more
Unsafely casts current object to the specified Ruby type Read more
Safely casts current object to the specified Ruby type Read more