Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: moving managed pointers across tasks #3836

Closed
erickt opened this issue Oct 22, 2012 · 2 comments
Closed

RFC: moving managed pointers across tasks #3836

erickt opened this issue Oct 22, 2012 · 2 comments
Labels
A-concurrency Area: Concurrency related issues. A-typesystem Area: The type system

Comments

@erickt
Copy link
Contributor

erickt commented Oct 22, 2012

@graydon and I were talking on IRC about using conditions for serializations error handling today. He convinced me to try out using subtasks to parse and catch failures, but unfortunately doing this means that we can't use serialization error handling on types with managed pointers.

This problem would go away if we could somehow move managed pointers from task A to task B. Even though it's generally unsafe to send a managed pointer across tasks, it's theoretically safe if it's the last thing a task does before it dies.

Here's an obviously wrong, only-works-for-ints example function:

fn try_managed<T>(f: fn~() -> @T) -> Result<@T, ()> {
    let result: Result<*libc::c_void, ()> = do task::try {
        unsafe { 
            let v = f();
            cast::forget(v);
            cast::reinterpret_cast::<@T, *libc::c_void>(&v)
        }
    };

    match move result {
        Ok(move v) => {
            unsafe {
                let v = cast::reinterpret_cast::<*libc::c_void, @T>(&v);
                cast::bump_box_refcount(v);
                Ok(move v)
            }
        }
        Err(move e) => Err(e),
    }
}

fn main() {
    let r = do try_managed {
        @5
    };

    match r {
        Err(_) => io::println("err"),
        Ok(x) => io::println(fmt!("%?", *x)),
    }
}

It segfaults for more complicated data structures, like ~strs.

@nikomatsakis
Copy link
Contributor

@pcwalton has been talking about adding something which would permit @ to be sent across tasks by serializing and deserializing them. Essentially, cloning them, just as Erlang does. This would be a simpler addition (no language changes at all) though it'd likely not be as efficient.

Keep in mind that while @ pointers will not be aliased if sent as part of the "last thing the task does", however we define that, it may still require some kind of cleverness in the garbage collector and so forth, depending on how we ultimately implement @ pointers.

So I guess I'm...unsure about this idea, but not completely opposed.

@catamorphism
Copy link
Contributor

Going to go ahead and close this since it's 3 months old and there wasn't much interest. Reopen if you feel strongly about it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-concurrency Area: Concurrency related issues. A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

3 participants