dpc / mioco.pre-0.9

Scalable, coroutine-based, asynchronous IO handling library for Rust programming language. (aka MIO COroutines).
Mozilla Public License 2.0
457 stars 30 forks source link

Evaluate using coroutine::Context instead of coroutine::Coroutine. Maybe there are some gains by not using global scheduling etc. #6

Closed dpc closed 9 years ago

dpc commented 9 years ago

Mioco requires only a context switch from and from each handler, and does not require full flexible model of coroutine with keeping track of runnable coroutines etc.

Right now mioco uses coroutine primitives, potentially that are shared with other users of coroutine. If any handler was to use call Coroutine::yield() or other control mechanisms on it's own, that could lead to rather unexpected results within mio.

That's another reason, to plug into lower-level coroutine::Context interface.

zonyitoo commented 9 years ago

I am planning on refactor the coroutine-rs library to work just like coroutine in Boost. That would provide more flexible APIs for you to define what user could do in your callback function.

dpc commented 9 years ago

I don't know how boost one works, but what we need in essence is really a context state for each handler ability to switch to it and and get back. Alternatively an exposed scheduler API. So I can have one scheduler for each mioco server instance, and potentially user being able to use coroutines withing handlers, but without interfering with a mioco server scheduler state.

zonyitoo commented 9 years ago

Maybe you can put the Scheduler into a thread local structure.

a context state for each handler ability to switch to it and and get back

I think this is in the feature clonable-handler.

dpc commented 9 years ago

I thought clonable handler just gives me an ability to clone Closure handler. Which I already use.

I want to have more Schedulers on the same thread.

zonyitoo commented 9 years ago

I don't know what exactly you want. Maybe I should look deep into your source code.

dpc commented 9 years ago

I don't think source will tell much. It's just, I would like to it to be possible to have a scheduler state that cannot be interfered from outside.

In mioco we have server, that is scheduling coroutines (handlers). It depends on coroutine_handle.resume() jumping into handler, and coroutine::block() in the handler to get back to server. If handlers code will start using coroutines on it's own, then there might be more runnable coroutines and then it all falls appart: eg. block() will not get back to the server code, but instead resume some other coroutine that did yield() before or something similar.

zonyitoo commented 9 years ago

Ok, I see. So you want to have a scheduler. Maybe you could redefine the spawn function to restrict those new spawned coroutines to be managed inside the scheduler.

On the other hand, you could also reverse your architecture, which means that a server just one of the coroutines that running on the scheduler. I think you may be interested in this repo: https://github.com/zonyitoo/simplesched

dpc commented 9 years ago

If I'm to work on this, I think I'm just going to get a context switch functionality.

zonyitoo commented 9 years ago

So, maybe I should expose the context API?

dpc commented 9 years ago

That would be awesome. I've been looking into https://github.com/rustcc/coroutine-rs/blob/master/src/context.rs on how to plug mioco to it, but that's it.

zonyitoo commented 9 years ago

Please try to use this crate: https://github.com/zonyitoo/context-rs Happy hacking :)

dpc commented 9 years ago

Awesome! Thanks!

dpc commented 9 years ago

context-rs was exactly what I needed!

zonyitoo commented 9 years ago

You may try the feature-fnbox branch.

dpc commented 9 years ago

@zonyitoo : What's the difference? What does it give me? :) . I see that "Send" from initfn is gone.

You want me to try to see if it works OK?

zonyitoo commented 9 years ago

This version uses the latest feature FnBox in the libstd. So I can get rid of the Thunk and Send trait.

Please try whether it works Ok, I want to merge it into the master branch.

dpc commented 9 years ago

Any reason you use internal libc? I thought we're supposed to use crates.io version, no?

zonyitoo commented 9 years ago

Just for historical reason, let me make some updates.

dpc commented 9 years ago

Ready. Branch context-feature-fnbox. It works here. I like the fact that I don't have to fake Send now. But the additional feature-s will prevent us from working on anything but nightly for a while.

dpc commented 9 years ago

I can merge it as soon as you make it available on crates.io so it builds for everyone.

zonyitoo commented 9 years ago

I already merge it to the master branch, but since the simd crate is not ready for pushing to crates.io, I have to wait ...

dpc commented 9 years ago

No hurry. :)