Skip to content

Commit 8b09e01

Browse files
committed
Add redox system
1 parent 0743694 commit 8b09e01

37 files changed

+4313
-5
lines changed

src/libstd/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,13 @@ extern crate rustc_unicode;
316316
extern crate libc;
317317

318318
// We always need an unwinder currently for backtraces
319-
extern crate unwind;
319+
//REDOX TODO extern crate unwind;
320320

321321
#[cfg(stage0)]
322322
extern crate alloc_system;
323323

324324
// compiler-rt intrinsics
325-
extern crate compiler_builtins;
325+
//REDOX TODO extern crate compiler_builtins;
326326

327327
// Make std testable by not duplicating lang items and other globals. See #2912
328328
#[cfg(test)] extern crate std as realstd;
@@ -456,6 +456,8 @@ mod memchr;
456456
#[macro_use]
457457
#[path = "sys/common/mod.rs"] mod sys_common;
458458

459+
#[cfg(redox)]
460+
#[path = "sys/redox/mod.rs"] mod sys;
459461
#[cfg(unix)]
460462
#[path = "sys/unix/mod.rs"] mod sys;
461463
#[cfg(windows)]

src/libstd/os/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#![stable(feature = "os", since = "1.0.0")]
1414
#![allow(missing_docs, bad_style)]
1515

16+
#[cfg(redox)]
17+
#[stable(feature = "rust1", since = "1.0.0")]
18+
pub use sys::ext as unix;
1619
#[cfg(unix)]
1720
#[stable(feature = "rust1", since = "1.0.0")]
1821
pub use sys::ext as unix;

src/libstd/sys/common/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub mod condvar;
3232
pub mod io;
3333
pub mod memchr;
3434
pub mod mutex;
35-
pub mod net;
3635
pub mod poison;
3736
pub mod remutex;
3837
pub mod rwlock;
@@ -42,6 +41,12 @@ pub mod thread_local;
4241
pub mod util;
4342
pub mod wtf8;
4443

44+
#[cfg(redox)]
45+
pub use sys::net;
46+
47+
#[cfg(not(redox))]
48+
pub mod net;
49+
4550
#[cfg(any(not(cargobuild), feature = "backtrace"))]
4651
#[cfg(any(all(unix, not(any(target_os = "macos", target_os = "ios", target_os = "emscripten"))),
4752
all(windows, target_env = "gnu")))]

src/libstd/sys/common/util.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ pub fn dumb_print(args: fmt::Arguments) {
3333
let _ = Stderr::new().map(|mut stderr| stderr.write_fmt(args));
3434
}
3535

36+
// On Redox, use an illegal instruction
37+
#[cfg(redox)]
38+
unsafe fn abort_internal() -> ! {
39+
::intrinsics::abort()
40+
}
41+
3642
// On Unix-like platforms, libc::abort will unregister signal handlers
3743
// including the SIGABRT handler, preventing the abort from being blocked, and
3844
// fclose streams, with the side effect of flushing them so libc bufferred

src/libstd/sys/redox/args.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Global initialization and retreival of command line arguments.
12+
//!
13+
//! On some platforms these are stored during runtime startup,
14+
//! and on some they are retrieved from the system on demand.
15+
16+
#![allow(dead_code)] // runtime init functions not used during testing
17+
18+
use ffi::OsString;
19+
use marker::PhantomData;
20+
use vec;
21+
22+
/// One-time global initialization.
23+
pub unsafe fn init(argc: isize, argv: *const *const u8) { imp::init(argc, argv) }
24+
25+
/// One-time global cleanup.
26+
pub unsafe fn cleanup() { imp::cleanup() }
27+
28+
/// Returns the command line arguments
29+
pub fn args() -> Args {
30+
imp::args()
31+
}
32+
33+
pub struct Args {
34+
iter: vec::IntoIter<OsString>,
35+
_dont_send_or_sync_me: PhantomData<*mut ()>,
36+
}
37+
38+
impl Iterator for Args {
39+
type Item = OsString;
40+
fn next(&mut self) -> Option<OsString> { self.iter.next() }
41+
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
42+
}
43+
44+
impl ExactSizeIterator for Args {
45+
fn len(&self) -> usize { self.iter.len() }
46+
}
47+
48+
impl DoubleEndedIterator for Args {
49+
fn next_back(&mut self) -> Option<OsString> { self.iter.next_back() }
50+
}
51+
52+
mod imp {
53+
use os::unix::prelude::*;
54+
use mem;
55+
use ffi::{CStr, OsString};
56+
use marker::PhantomData;
57+
use libc;
58+
use super::Args;
59+
60+
use sys_common::mutex::Mutex;
61+
62+
static mut GLOBAL_ARGS_PTR: usize = 0;
63+
static LOCK: Mutex = Mutex::new();
64+
65+
pub unsafe fn init(argc: isize, argv: *const *const u8) {
66+
let args = (0..argc).map(|i| {
67+
CStr::from_ptr(*argv.offset(i) as *const libc::c_char).to_bytes().to_vec()
68+
}).collect();
69+
70+
LOCK.lock();
71+
let ptr = get_global_ptr();
72+
assert!((*ptr).is_none());
73+
(*ptr) = Some(box args);
74+
LOCK.unlock();
75+
}
76+
77+
pub unsafe fn cleanup() {
78+
LOCK.lock();
79+
*get_global_ptr() = None;
80+
LOCK.unlock();
81+
}
82+
83+
pub fn args() -> Args {
84+
let bytes = clone().unwrap_or(Vec::new());
85+
let v: Vec<OsString> = bytes.into_iter().map(|v| {
86+
OsStringExt::from_vec(v)
87+
}).collect();
88+
Args { iter: v.into_iter(), _dont_send_or_sync_me: PhantomData }
89+
}
90+
91+
fn clone() -> Option<Vec<Vec<u8>>> {
92+
unsafe {
93+
LOCK.lock();
94+
let ptr = get_global_ptr();
95+
let ret = (*ptr).as_ref().map(|s| (**s).clone());
96+
LOCK.unlock();
97+
return ret
98+
}
99+
}
100+
101+
fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> {
102+
unsafe { mem::transmute(&GLOBAL_ARGS_PTR) }
103+
}
104+
105+
}

src/libstd/sys/redox/backtrace.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use libc;
2+
use io;
3+
use sys_common::backtrace::output;
4+
5+
#[inline(never)]
6+
pub fn write(w: &mut io::Write) -> io::Result<()> {
7+
output(w, 0, 0 as *mut libc::c_void, None)
8+
}

src/libstd/sys/redox/condvar.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use cell::UnsafeCell;
2+
use intrinsics::{atomic_cxchg, atomic_xadd, atomic_xchg};
3+
use ptr;
4+
use time::Duration;
5+
6+
use super::mutex::{mutex_lock, mutex_unlock, Mutex};
7+
8+
use libc::{futex, FUTEX_WAIT, FUTEX_WAKE, FUTEX_REQUEUE};
9+
10+
pub struct Condvar {
11+
lock: UnsafeCell<*mut i32>,
12+
seq: UnsafeCell<i32>
13+
}
14+
15+
impl Condvar {
16+
pub const fn new() -> Condvar {
17+
Condvar {
18+
lock: UnsafeCell::new(ptr::null_mut()),
19+
seq: UnsafeCell::new(0)
20+
}
21+
}
22+
23+
pub unsafe fn init(&self) {
24+
25+
}
26+
27+
pub fn notify_one(&self) {
28+
unsafe {
29+
let seq = self.seq.get();
30+
31+
atomic_xadd(seq, 1);
32+
33+
let _ = futex(seq, FUTEX_WAKE, 1, 0, ptr::null_mut());
34+
}
35+
}
36+
37+
pub fn notify_all(&self) {
38+
unsafe {
39+
let lock = self.lock.get();
40+
let seq = self.seq.get();
41+
42+
if *lock == ptr::null_mut() {
43+
return;
44+
}
45+
46+
atomic_xadd(seq, 1);
47+
48+
let _ = futex(seq, FUTEX_REQUEUE, 1, ::usize::MAX, *lock);
49+
}
50+
}
51+
52+
pub fn wait(&self, mutex: &Mutex) {
53+
unsafe {
54+
let lock = self.lock.get();
55+
let seq = self.seq.get();
56+
57+
if *lock != mutex.lock.get() {
58+
if *lock != ptr::null_mut() {
59+
panic!("Condvar used with more than one Mutex");
60+
}
61+
62+
atomic_cxchg(lock as *mut usize, 0, mutex.lock.get() as usize);
63+
}
64+
65+
mutex_unlock(*lock);
66+
67+
let _ = futex(seq, FUTEX_WAIT, *seq, 0, ptr::null_mut());
68+
69+
while atomic_xchg(*lock, 2) != 0 {
70+
let _ = futex(*lock, FUTEX_WAIT, 2, 0, ptr::null_mut());
71+
}
72+
73+
mutex_lock(*lock);
74+
}
75+
}
76+
77+
pub fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
78+
unimplemented!();
79+
}
80+
81+
pub unsafe fn destroy(&self) {
82+
83+
}
84+
}
85+
86+
unsafe impl Send for Condvar {}
87+
88+
unsafe impl Sync for Condvar {}

src/libstd/sys/redox/env.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub mod os {
12+
pub const FAMILY: &'static str = "redox";
13+
pub const OS: &'static str = "redox";
14+
pub const DLL_PREFIX: &'static str = "lib";
15+
pub const DLL_SUFFIX: &'static str = ".so";
16+
pub const DLL_EXTENSION: &'static str = "so";
17+
pub const EXE_SUFFIX: &'static str = "";
18+
pub const EXE_EXTENSION: &'static str = "";
19+
}

src/libstd/sys/redox/ext/ffi.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Unix-specific extension to the primitives in the `std::ffi` module
12+
13+
#![stable(feature = "rust1", since = "1.0.0")]
14+
15+
use ffi::{OsStr, OsString};
16+
use mem;
17+
use sys::os_str::Buf;
18+
use sys_common::{FromInner, IntoInner, AsInner};
19+
20+
/// Unix-specific extensions to `OsString`.
21+
#[stable(feature = "rust1", since = "1.0.0")]
22+
pub trait OsStringExt {
23+
/// Creates an `OsString` from a byte vector.
24+
#[stable(feature = "rust1", since = "1.0.0")]
25+
fn from_vec(vec: Vec<u8>) -> Self;
26+
27+
/// Yields the underlying byte vector of this `OsString`.
28+
#[stable(feature = "rust1", since = "1.0.0")]
29+
fn into_vec(self) -> Vec<u8>;
30+
}
31+
32+
#[stable(feature = "rust1", since = "1.0.0")]
33+
impl OsStringExt for OsString {
34+
fn from_vec(vec: Vec<u8>) -> OsString {
35+
FromInner::from_inner(Buf { inner: vec })
36+
}
37+
fn into_vec(self) -> Vec<u8> {
38+
self.into_inner().inner
39+
}
40+
}
41+
42+
/// Unix-specific extensions to `OsStr`.
43+
#[stable(feature = "rust1", since = "1.0.0")]
44+
pub trait OsStrExt {
45+
#[stable(feature = "rust1", since = "1.0.0")]
46+
fn from_bytes(slice: &[u8]) -> &Self;
47+
48+
/// Gets the underlying byte view of the `OsStr` slice.
49+
#[stable(feature = "rust1", since = "1.0.0")]
50+
fn as_bytes(&self) -> &[u8];
51+
}
52+
53+
#[stable(feature = "rust1", since = "1.0.0")]
54+
impl OsStrExt for OsStr {
55+
fn from_bytes(slice: &[u8]) -> &OsStr {
56+
unsafe { mem::transmute(slice) }
57+
}
58+
fn as_bytes(&self) -> &[u8] {
59+
&self.as_inner().inner
60+
}
61+
}

0 commit comments

Comments
 (0)