Skip to content

Commit 1466598

Browse files
committed
Address review comments
1 parent c457b67 commit 1466598

File tree

4 files changed

+35
-39
lines changed

4 files changed

+35
-39
lines changed

src/libstd/sys/sgx/mod.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,9 @@ pub fn decode_error_kind(code: i32) -> ErrorKind {
115115
// of time and timeouts in SGX model. The enclave runner serving usercalls may
116116
// lie about current time and/or ignore timeout values.
117117
//
118-
// Once the event is observed, `woken_up` will be used to determine whether or
119-
// not the event was spurious.
120-
//
121-
// FIXME: note these caveats in documentation of all public types that use this
122-
// function in their execution path.
123-
pub fn wait_timeout_sgx<F>(event_mask: u64, duration: crate::time::Duration, woken_up: F)
118+
// Once the event is observed, `should_wake_up` will be used to determine
119+
// whether or not the event was spurious.
120+
pub fn usercall_wait_timeout<F>(event_mask: u64, duration: crate::time::Duration, should_wake_up: F)
124121
where
125122
F: Fn() -> bool,
126123
{
@@ -141,7 +138,9 @@ where
141138
if event_mask == 0 {
142139
rtabort!("expected usercalls::wait() to return Err, found Ok.");
143140
}
144-
rtassert!(eventset & event_mask == event_mask);
141+
// A matching event is one whose bits are equal to or a subset
142+
// of `event_mask`.
143+
rtassert!(eventset & !event_mask == 0);
145144
true
146145
}
147146
Err(e) => {
@@ -152,18 +151,18 @@ where
152151
}
153152

154153
match wait_checked(event_mask, Some(duration)) {
155-
false => return, // timed out
156-
true if woken_up() => return, // woken up
157-
true => {} // spurious event
154+
false => return, // timed out
155+
true if should_wake_up() => return, // woken up
156+
true => {} // spurious event
158157
}
159158

160159
// Drain all cached events.
161160
// Note that `event_mask != 0` is implied if we get here.
162161
loop {
163162
match wait_checked(event_mask, None) {
164-
false => break, // no more cached events
165-
true if woken_up() => return, // woken up
166-
true => {} // spurious event
163+
false => break, // no more cached events
164+
true if should_wake_up() => return, // woken up
165+
true => {} // spurious event
167166
}
168167
}
169168

@@ -176,9 +175,9 @@ where
176175
let mut remaining = duration;
177176
loop {
178177
match wait_checked(event_mask, Some(remaining)) {
179-
false => return, // timed out
180-
true if woken_up() => return, // woken up
181-
true => {} // spurious event
178+
false => return, // timed out
179+
true if should_wake_up() => return, // woken up
180+
true => {} // spurious event
182181
}
183182
remaining = match duration.checked_sub(start.elapsed()) {
184183
Some(remaining) => remaining,

src/libstd/sys/sgx/thread.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#![cfg_attr(test, allow(dead_code))] // why is this necessary?
2-
32
use crate::ffi::CStr;
43
use crate::io;
5-
use crate::sys::wait_timeout_sgx;
4+
use crate::sys::usercall_wait_timeout;
65
use crate::time::Duration;
76

87
use super::abi::usercalls;
@@ -76,7 +75,7 @@ impl Thread {
7675
}
7776

7877
pub fn sleep(dur: Duration) {
79-
wait_timeout_sgx(0, dur, || true);
78+
usercall_wait_timeout(0, dur, || true);
8079
}
8180

8281
pub fn join(self) {

src/libstd/sys/sgx/waitqueue.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
/// A simple queue implementation for synchronization primitives.
2-
///
3-
/// This queue is used to implement condition variable and mutexes.
4-
///
5-
/// Users of this API are expected to use the `WaitVariable<T>` type. Since
6-
/// that type is not `Sync`, it needs to be protected by e.g., a `SpinMutex` to
7-
/// allow shared access.
8-
///
9-
/// Since userspace may send spurious wake-ups, the wakeup event state is
10-
/// recorded in the enclave. The wakeup event state is protected by a spinlock.
11-
/// The queue and associated wait state are stored in a `WaitVariable`.
1+
//! A simple queue implementation for synchronization primitives.
2+
//!
3+
//! This queue is used to implement condition variable and mutexes.
4+
//!
5+
//! Users of this API are expected to use the `WaitVariable<T>` type. Since
6+
//! that type is not `Sync`, it needs to be protected by e.g., a `SpinMutex` to
7+
//! allow shared access.
8+
//!
9+
//! Since userspace may send spurious wake-ups, the wakeup event state is
10+
//! recorded in the enclave. The wakeup event state is protected by a spinlock.
11+
//! The queue and associated wait state are stored in a `WaitVariable`.
1212
use crate::num::NonZeroUsize;
1313
use crate::ops::{Deref, DerefMut};
14-
use crate::sys::wait_timeout_sgx;
14+
use crate::sys::usercall_wait_timeout;
1515
use crate::time::Duration;
1616

1717
use super::abi::thread;
@@ -176,15 +176,12 @@ impl WaitQueue {
176176
}));
177177
let entry_lock = lock.lock().queue.inner.push(&mut entry);
178178
before_wait();
179-
// don't panic, this would invalidate `entry` during unwinding
180-
wait_timeout_sgx(EV_UNPARK, timeout, || entry_lock.lock().wake);
179+
usercall_wait_timeout(EV_UNPARK, timeout, || entry_lock.lock().wake);
181180
// acquire the wait queue's lock first to avoid deadlock.
182181
let mut guard = lock.lock();
183-
let entry_guard = entry_lock.lock();
184-
let success = entry_guard.wake;
182+
let success = entry_lock.lock().wake;
185183
if !success {
186-
// nobody is waking us up, so remove the entry from the wait queue.
187-
drop(entry_guard);
184+
// nobody is waking us up, so remove our entry from the wait queue.
188185
guard.queue.inner.remove(&mut entry);
189186
}
190187
success
@@ -363,8 +360,8 @@ mod unsafe_list {
363360
///
364361
/// # Safety
365362
///
366-
/// The caller must ensure that entry has been pushed prior to this
367-
/// call and has not moved since push.
363+
/// The caller must ensure that `entry` has been pushed onto `self`
364+
/// prior to this call and has not moved since then.
368365
pub unsafe fn remove(&mut self, entry: &mut UnsafeListEntry<T>) {
369366
rtassert!(!self.is_empty());
370367
// BEFORE:

src/test/ui/mpsc_stress.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl Barrier {
3636
fn wait(self) {
3737
self.shared.fetch_add(1, Ordering::SeqCst);
3838
while self.shared.load(Ordering::SeqCst) != self.count {
39+
#[cfg(target_env = "sgx")]
3940
thread::yield_now();
4041
}
4142
}

0 commit comments

Comments
 (0)