mco

mco is a high-performance library for programming stackful coroutines with which you can easily develop and maintain massive concurrent programs. It can be thought as the Rust version of the popular [Goroutine][go].

way mco?

Initial code frok from May and we add Many improvements(Inspired by Golang, parking_lot and crossbeam) and more…

mco crates

mco Powerful standard library

Crates based on mco implementation

Features

Usage

mco = "0.1"

A naive echo server implemented with mco:

#[macro_use]
extern crate mco;

use mco::net::TcpListener;
use std::io::{Read, Write};

fn main() {
    let listener = TcpListener::bind("127.0.0.1:8000").unwrap();
    while let Ok((mut stream, _)) = listener.accept() {
        go!(move || {
            let mut buf = vec![0; 1024 * 16]; // alloc in heap!
            while let Ok(n) = stream.read(&mut buf) {
                if n == 0 {
                    break;
                }
                stream.write_all(&buf[0..n]).unwrap();
            }
        });
    }
}

More examples

The I/O heavy bound examples

Caveat

There is a detailed [document][caveat] that describes mco’s main restrictions. In general, there are four things you should follow when writing programs that use coroutines:

It’s considered unsafe with the following pattern:

set_tls();
// Or another coroutine API that would cause scheduling:
yield_now(); 
use_tls();

but it’s safe if your code is not sensitive about the previous state of TLS. Or there is no coroutines scheduling between set TLS and use TLS.

Note:

The first three rules are common when using cooperative asynchronous libraries in Rust. Even using a futures-based system also have these limitations. So what you should really focus on is a coroutine stack size, make sure it’s big enough for your applications.

How to tune a stack size

mco::config().set_stack_size(6*1024*1024);

zxj347284221

联系方式(添加好友请备注’mco’) 微信群:先加微信,然后拉进群

zxj347284221