Skip to content

Commit b1b35dd

Browse files
committed
Implement env, reentrant mutex, and partially implement scoped thread locals. Better error messages for unsupported features
1 parent 68fd7ee commit b1b35dd

File tree

10 files changed

+174
-49
lines changed

10 files changed

+174
-49
lines changed

src/libstd/sys/redox/condvar.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ impl Condvar {
2020
}
2121
}
2222

23+
#[inline]
2324
pub unsafe fn init(&self) {
24-
25+
*self.lock.get() = ptr::null_mut();
26+
*self.seq.get() = 0;
2527
}
2628

29+
#[inline]
2730
pub fn notify_one(&self) {
31+
::sys_common::util::dumb_print(format_args!("condvar notify_one\n"));
2832
unsafe {
2933
let seq = self.seq.get();
3034

@@ -34,7 +38,9 @@ impl Condvar {
3438
}
3539
}
3640

41+
#[inline]
3742
pub fn notify_all(&self) {
43+
::sys_common::util::dumb_print(format_args!("condvar notify_all\n"));
3844
unsafe {
3945
let lock = self.lock.get();
4046
let seq = self.seq.get();
@@ -49,7 +55,9 @@ impl Condvar {
4955
}
5056
}
5157

58+
#[inline]
5259
pub fn wait(&self, mutex: &Mutex) {
60+
::sys_common::util::dumb_print(format_args!("condvar wait\n"));
5361
unsafe {
5462
let lock = self.lock.get();
5563
let seq = self.seq.get();
@@ -74,12 +82,16 @@ impl Condvar {
7482
}
7583
}
7684

85+
#[inline]
7786
pub fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
87+
::sys_common::util::dumb_print(format_args!("condvar wait_timeout\n"));
7888
unimplemented!();
7989
}
8090

91+
#[inline]
8192
pub unsafe fn destroy(&self) {
82-
93+
*self.lock.get() = ptr::null_mut();
94+
*self.seq.get() = 0;
8395
}
8496
}
8597

src/libstd/sys/redox/fd.rs

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl FileDesc {
4949
}
5050

5151
pub fn set_cloexec(&self) -> io::Result<()> {
52+
::sys_common::util::dumb_print(format_args!("Set cloexec\n"));
5253
unimplemented!();
5354
/*
5455
unsafe {
@@ -60,6 +61,7 @@ impl FileDesc {
6061
}
6162

6263
pub fn set_nonblocking(&self, _nonblocking: bool) -> io::Result<()> {
64+
::sys_common::util::dumb_print(format_args!("Set nonblocking\n"));
6365
unimplemented!();
6466
/*
6567
unsafe {

src/libstd/sys/redox/fs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,12 @@ pub fn unlink(p: &Path) -> io::Result<()> {
380380
}
381381

382382
pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> {
383+
::sys_common::util::dumb_print(format_args!("Rename\n"));
383384
unimplemented!();
384385
}
385386

386387
pub fn set_perm(_p: &Path, _perm: FilePermissions) -> io::Result<()> {
388+
::sys_common::util::dumb_print(format_args!("Set perm\n"));
387389
unimplemented!();
388390
}
389391

@@ -418,10 +420,12 @@ pub fn readlink(p: &Path) -> io::Result<PathBuf> {
418420
}
419421

420422
pub fn symlink(_src: &Path, _dst: &Path) -> io::Result<()> {
423+
::sys_common::util::dumb_print(format_args!("Symlink\n"));
421424
unimplemented!();
422425
}
423426

424427
pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> {
428+
::sys_common::util::dumb_print(format_args!("Link\n"));
425429
unimplemented!();
426430
}
427431

src/libstd/sys/redox/mutex.rs

+60-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use cell::UnsafeCell;
22
use intrinsics::{atomic_cxchg, atomic_xchg};
33
use ptr;
44

5-
use libc::{futex, FUTEX_WAIT, FUTEX_WAKE};
5+
use libc::{futex, getpid, FUTEX_WAIT, FUTEX_WAKE};
66

77
pub unsafe fn mutex_try_lock(m: *mut i32) -> bool {
88
atomic_cxchg(m, 0, 1).0 == 0
@@ -57,27 +57,36 @@ impl Mutex {
5757
}
5858
}
5959

60+
#[inline]
6061
pub unsafe fn init(&self) {
61-
62+
*self.lock.get() = 0;
6263
}
6364

6465
/// Try to lock the mutex
66+
#[inline]
6567
pub unsafe fn try_lock(&self) -> bool {
68+
::sys_common::util::dumb_print(format_args!("mutex try lock\n"));
6669
mutex_try_lock(self.lock.get())
6770
}
6871

6972
/// Lock the mutex
73+
#[inline]
7074
pub unsafe fn lock(&self) {
71-
mutex_lock(self.lock.get());
75+
::sys_common::util::dumb_print(format_args!("mutex lock\n"));
76+
mutex_try_lock(self.lock.get());
77+
//mutex_lock(self.lock.get());
7278
}
7379

7480
/// Unlock the mutex
81+
#[inline]
7582
pub unsafe fn unlock(&self) {
83+
::sys_common::util::dumb_print(format_args!("mutex unlock\n"));
7684
mutex_unlock(self.lock.get());
7785
}
7886

87+
#[inline]
7988
pub unsafe fn destroy(&self) {
80-
89+
*self.lock.get() = 0;
8190
}
8291
}
8392

@@ -87,36 +96,78 @@ unsafe impl Sync for Mutex {}
8796

8897
pub struct ReentrantMutex {
8998
pub lock: UnsafeCell<i32>,
99+
pub owner: UnsafeCell<usize>,
100+
pub own_count: UnsafeCell<usize>,
90101
}
91102

92103
impl ReentrantMutex {
93104
pub const fn uninitialized() -> Self {
94105
ReentrantMutex {
95106
lock: UnsafeCell::new(0),
107+
owner: UnsafeCell::new(0),
108+
own_count: UnsafeCell::new(0),
96109
}
97110
}
98111

112+
#[inline]
99113
pub unsafe fn init(&mut self) {
100-
114+
*self.lock.get() = 0;
115+
*self.owner.get() = 0;
116+
*self.own_count.get() = 0;
101117
}
102118

103119
/// Try to lock the mutex
120+
#[inline]
104121
pub unsafe fn try_lock(&self) -> bool {
105-
mutex_try_lock(self.lock.get())
122+
::sys_common::util::dumb_print(format_args!("remutex try_lock\n"));
123+
let pid = getpid().unwrap();
124+
if *self.own_count.get() > 0 && *self.owner.get() == pid {
125+
*self.own_count.get() += 1;
126+
true
127+
} else {
128+
if mutex_try_lock(self.lock.get()) {
129+
*self.owner.get() = pid;
130+
*self.own_count.get() = 1;
131+
true
132+
} else {
133+
false
134+
}
135+
}
106136
}
107137

108138
/// Lock the mutex
139+
#[inline]
109140
pub unsafe fn lock(&self) {
110-
mutex_lock(self.lock.get());
141+
::sys_common::util::dumb_print(format_args!("remutex lock\n"));
142+
let pid = getpid().unwrap();
143+
if *self.own_count.get() > 0 && *self.owner.get() == pid {
144+
*self.own_count.get() += 1;
145+
} else {
146+
mutex_lock(self.lock.get());
147+
*self.owner.get() = pid;
148+
*self.own_count.get() = 1;
149+
}
111150
}
112151

113152
/// Unlock the mutex
153+
#[inline]
114154
pub unsafe fn unlock(&self) {
115-
mutex_unlock(self.lock.get());
155+
::sys_common::util::dumb_print(format_args!("remutex unlock\n"));
156+
let pid = getpid().unwrap();
157+
if *self.own_count.get() > 0 && *self.owner.get() == pid {
158+
*self.own_count.get() -= 1;
159+
if *self.own_count.get() == 0 {
160+
*self.owner.get() = 0;
161+
mutex_unlock(self.lock.get());
162+
}
163+
}
116164
}
117165

166+
#[inline]
118167
pub unsafe fn destroy(&self) {
119-
168+
*self.lock.get() = 0;
169+
*self.owner.get() = 0;
170+
*self.own_count.get() = 0;
120171
}
121172
}
122173

src/libstd/sys/redox/os.rs

+37-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use os::unix::prelude::*;
1717
use error::Error as StdError;
1818
use ffi::{CString, CStr, OsString, OsStr};
1919
use fmt;
20-
use io;
20+
use io::{self, Read, Write};
2121
use iter;
2222
use libc::{self, c_int, c_char, c_void};
2323
use marker::PhantomData;
@@ -131,19 +131,48 @@ impl Iterator for Env {
131131
/// Returns a vector of (variable, value) byte-vector pairs for all the
132132
/// environment variables of the current process.
133133
pub fn env() -> Env {
134-
unimplemented!();
134+
let mut variables: Vec<(OsString, OsString)> = Vec::new();
135+
if let Ok(mut file) = ::fs::File::open("env:") {
136+
let mut string = String::new();
137+
if file.read_to_string(&mut string).is_ok() {
138+
for line in string.lines() {
139+
if let Some(equal_sign) = line.chars().position(|c| c == '=') {
140+
let name = line.chars().take(equal_sign).collect::<String>();
141+
let value = line.chars().skip(equal_sign+1).collect::<String>();
142+
variables.push((OsString::from(name), OsString::from(value)));
143+
}
144+
}
145+
}
146+
}
147+
Env { iter: variables.into_iter(), _dont_send_or_sync_me: PhantomData }
135148
}
136149

137-
pub fn getenv(_k: &OsStr) -> io::Result<Option<OsString>> {
138-
unimplemented!();
150+
pub fn getenv(key: &OsStr) -> io::Result<Option<OsString>> {
151+
if ! key.is_empty() {
152+
if let Ok(mut file) = ::fs::File::open(&("env:".to_owned() + key.to_str().unwrap())) {
153+
let mut string = String::new();
154+
file.read_to_string(&mut string)?;
155+
Ok(Some(OsString::from(string)))
156+
} else {
157+
Ok(None)
158+
}
159+
} else {
160+
Ok(None)
161+
}
139162
}
140163

141-
pub fn setenv(_k: &OsStr, _v: &OsStr) -> io::Result<()> {
142-
unimplemented!();
164+
pub fn setenv(key: &OsStr, value: &OsStr) -> io::Result<()> {
165+
if ! key.is_empty() {
166+
let mut file = ::fs::File::open(&("env:".to_owned() + key.to_str().unwrap()))?;
167+
file.write_all(value.as_bytes())?;
168+
file.set_len(value.len() as u64)?;
169+
}
170+
Ok(())
143171
}
144172

145-
pub fn unsetenv(_n: &OsStr) -> io::Result<()> {
146-
unimplemented!();
173+
pub fn unsetenv(key: &OsStr) -> io::Result<()> {
174+
::fs::remove_file(&("env:".to_owned() + key.to_str().unwrap()))?;
175+
Ok(())
147176
}
148177

149178
pub fn page_size() -> usize {

src/libstd/sys/redox/pipe.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub fn read2(_p1: AnonPipe,
5454
_v1: &mut Vec<u8>,
5555
_p2: AnonPipe,
5656
_v2: &mut Vec<u8>) -> io::Result<()> {
57+
::sys_common::util::dumb_print(format_args!("read2\n"));
5758
unimplemented!();
5859
/*
5960
// Set both pipes into nonblocking mode as we're gonna be reading from both

src/libstd/sys/redox/rwlock.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,54 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
pub struct RWLock;
11+
use super::mutex::Mutex;
12+
13+
pub struct RWLock {
14+
mutex: Mutex
15+
}
1216

1317
unsafe impl Send for RWLock {}
1418
unsafe impl Sync for RWLock {}
1519

1620
impl RWLock {
1721
pub const fn new() -> RWLock {
18-
RWLock
22+
RWLock {
23+
mutex: Mutex::new()
24+
}
1925
}
2026

2127
#[inline]
2228
pub unsafe fn read(&self) {
23-
unimplemented!();
29+
self.mutex.lock();
2430
}
2531

2632
#[inline]
2733
pub unsafe fn try_read(&self) -> bool {
28-
unimplemented!();
34+
self.mutex.try_lock()
2935
}
3036

3137
#[inline]
3238
pub unsafe fn write(&self) {
33-
unimplemented!();
39+
self.mutex.lock();
3440
}
3541

3642
#[inline]
3743
pub unsafe fn try_write(&self) -> bool {
38-
unimplemented!();
44+
self.mutex.try_lock()
3945
}
4046

4147
#[inline]
4248
pub unsafe fn read_unlock(&self) {
43-
unimplemented!();
49+
self.mutex.unlock();
4450
}
4551

4652
#[inline]
4753
pub unsafe fn write_unlock(&self) {
48-
unimplemented!();
54+
self.mutex.unlock();
4955
}
5056

5157
#[inline]
5258
pub unsafe fn destroy(&self) {
53-
59+
self.mutex.destroy();
5460
}
5561
}

src/libstd/sys/redox/stack_overflow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct Handler;
1414

1515
impl Handler {
1616
pub unsafe fn new() -> Handler {
17-
unimplemented!();
17+
Handler
1818
}
1919
}
2020

@@ -23,5 +23,5 @@ pub unsafe fn init() {
2323
}
2424

2525
pub unsafe fn cleanup() {
26-
unimplemented!();
26+
2727
}

0 commit comments

Comments
 (0)