Skip to content

Instantly share code, notes, and snippets.

@gavinb
Created July 14, 2013 22:54
Show Gist options
  • Save gavinb/5996436 to your computer and use it in GitHub Desktop.
Save gavinb/5996436 to your computer and use it in GitHub Desktop.
Rust Networking test code which triggers an odd linker error relating to `rt::sched::__extensions__`.
// Cribbed from Rust libstd test code
// Causes a linker error on OS X
use std::cell::Cell;
use std::task::Task;
use std::rt::io::net::tcp;
use std::rt::io::net::ip;
use std::rt::local::Local;
use std::rt::io::{Reader, Writer, Listener};
/// Creates a new scheduler in a new thread and runs a task in it,
/// then waits for the scheduler to exit. Failure of the task
/// will abort the process.
fn run_in_newsched_task(f: ~fn()) {
use std::rt::sched::*;
use std::rt::task::Task;
use std::unstable::run_in_bare_thread;
use std::rt::uv::uvio::UvEventLoop;
let f = Cell::new(f);
do run_in_bare_thread {
let mut sched = ~UvEventLoop::new_scheduler();
let task = ~Coroutine::with_task(&mut sched.stack_pool,
~Task::without_unwinding(),
f.take());
sched.enqueue_task(task);
sched.run();
}
}
/// Create a new task and run it right now. Aborts on failure
fn spawntask_immediately(f: ~fn()) {
use std::rt::sched::*;
use std::rt::task::Task;
let mut sched = Local::take::<Scheduler>();
let task = ~Coroutine::with_task(&mut sched.stack_pool,
~Task::without_unwinding(),
f);
do sched.switch_running_tasks_and_then(task) |task| {
let task = Cell::new(task);
do Local::borrow::<Scheduler> |sched| {
sched.enqueue_task(task.take());
}
}
}
fn main() {
do run_in_newsched_task {
let addr = ip::Ipv4(127, 0, 0, 1, 5080);
do spawntask_immediately {
let mut listener = tcp::TcpListener::bind(addr);
let mut stream = listener.accept();
let mut buf = [0];
stream.read(buf);
assert!(buf[0] == 99);
}
do spawntask_immediately {
let mut stream = tcp::TcpStream::connect(addr);
stream.write([99]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment