Struct rutie::Thread[][src]

pub struct Thread { /* fields omitted */ }

Thread

Implementations

impl Thread[src]

pub fn new<F, R>(func: F) -> Self where
    F: FnMut() -> R,
    R: Object
[src]

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

pub fn wait_fd(fd: RawFd)[src]

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(), 
[src]

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(), 
[src]

pub fn call_with_gvl<F, R>(func: F) -> R where
    F: FnMut() -> R, 
[src]

Trait Implementations

impl Debug for Thread[src]

impl From<Value> for Thread[src]

impl Into<AnyObject> for Thread[src]

impl Into<Value> for Thread[src]

impl Object for Thread[src]

impl PartialEq<Thread> for Thread[src]

impl VerifiedObject for Thread[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.