Skip to content

Commit 76b8041

Browse files
committed
---
yaml --- r: 41689 b: refs/heads/master c: 1b1700f h: refs/heads/master i: 41687: ad2e1b2 v: v3
1 parent dd05fa7 commit 76b8041

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 4c441e95d69909265e69cffd3439f890914e02b3
2+
refs/heads/master: 1b1700f44b88225bc3557e1ca497644b9852e268
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2f46b763da2c098913884f101b6d71d69af41b49
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650

trunk/src/libcore/private.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ extern mod rustrt {
3737
unsafe fn rust_destroy_little_lock(lock: rust_little_lock);
3838
unsafe fn rust_lock_little_lock(lock: rust_little_lock);
3939
unsafe fn rust_unlock_little_lock(lock: rust_little_lock);
40+
41+
unsafe fn rust_raw_thread_start(f: &fn()) -> *raw_thread;
42+
unsafe fn rust_raw_thread_join_delete(thread: *raw_thread);
4043
}
4144

4245
#[abi = "rust-intrinsic"]
@@ -46,6 +49,37 @@ extern mod rusti {
4649
fn atomic_xsub(dst: &mut int, src: int) -> int;
4750
}
4851

52+
#[allow(non_camel_case_types)] // runtime type
53+
type raw_thread = libc::c_void;
54+
55+
/**
56+
Start a new thread outside of the current runtime context and wait for it to terminate.
57+
58+
The executing thread has no access to a task pointer and will be using a normal large stack.
59+
*/
60+
pub unsafe fn run_in_bare_thread(f: ~fn()) {
61+
let (port, chan) = pipes::stream();
62+
// XXX Unfortunate that this creates an extra scheduler but it's necessary
63+
// since rust_raw_thread_join_delete is blocking
64+
do task::spawn_sched(task::SingleThreaded) unsafe {
65+
let closure: &fn() = || {
66+
f()
67+
};
68+
let thread = rustrt::rust_raw_thread_start(closure);
69+
rustrt::rust_raw_thread_join_delete(thread);
70+
chan.send(());
71+
}
72+
port.recv();
73+
}
74+
75+
#[test]
76+
fn test_run_in_bare_thread() unsafe {
77+
let i = 100;
78+
do run_in_bare_thread {
79+
assert i == 100;
80+
}
81+
}
82+
4983
#[allow(non_camel_case_types)] // runtime type
5084
type rust_port_id = uint;
5185

trunk/src/rt/rust_builtin.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "rust_util.h"
1616
#include "rust_scheduler.h"
1717
#include "sync/timer.h"
18+
#include "sync/rust_thread.h"
1819
#include "rust_abi.h"
1920
#include "rust_port.h"
2021

@@ -972,6 +973,36 @@ rust_log_str(uint32_t level, const char *str, size_t size) {
972973
task->sched_loop->get_log().log(task, level, "%.*s", (int)size, str);
973974
}
974975

976+
extern "C" CDECL void record_sp_limit(void *limit);
977+
978+
class raw_thread: public rust_thread {
979+
public:
980+
fn_env_pair *fn;
981+
982+
raw_thread(fn_env_pair *fn) : fn(fn) { }
983+
984+
virtual void run() {
985+
record_sp_limit(0);
986+
fn->f(NULL, fn->env, NULL);
987+
}
988+
};
989+
990+
extern "C" raw_thread*
991+
rust_raw_thread_start(fn_env_pair *fn) {
992+
assert(fn);
993+
raw_thread *thread = new raw_thread(fn);
994+
thread->start();
995+
return thread;
996+
}
997+
998+
extern "C" void
999+
rust_raw_thread_join_delete(raw_thread *thread) {
1000+
assert(thread);
1001+
thread->join();
1002+
delete thread;
1003+
}
1004+
1005+
9751006
//
9761007
// Local Variables:
9771008
// mode: C++

trunk/src/rt/rustrt.def.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,5 @@ linenoiseHistoryAdd
208208
linenoiseHistorySetMaxLen
209209
linenoiseHistorySave
210210
linenoiseHistoryLoad
211+
rust_raw_thread_start
212+
rust_raw_thread_join_delete

0 commit comments

Comments
 (0)