-
Notifications
You must be signed in to change notification settings - Fork 265
Add SGX thread parker #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc0de4a
Add SGX thread parker
faern b636cfc
Panic in park_until instead of using timestamps
faern ffc1869
Reverse SGX cfg check to solve experimental target_vendor
faern 7ffd6b3
Don't use thread::sleep in tests on SGX
faern c61f895
Test SGX on nightly travis build
faern 9050a28
Only panic in send if error is not InvalidInput
faern 3996e3e
Only panic in park when debug_assertions are enabled
faern 8844366
Only panic in send when debug_assertions are enabled
faern File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright 2016 Amanieu d'Antras | ||
| // | ||
| // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or | ||
| // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or | ||
| // http://opensource.org/licenses/MIT>, at your option. This file may not be | ||
| // copied, modified, or distributed except according to those terms. | ||
|
|
||
| use core::sync::atomic::{AtomicBool, Ordering}; | ||
| use std::{ | ||
| io, | ||
| os::fortanix_sgx::{ | ||
| thread::current as current_tcs, | ||
| usercalls::{ | ||
| self, | ||
| raw::{Tcs, EV_UNPARK, WAIT_INDEFINITE}, | ||
| }, | ||
| }, | ||
| thread, | ||
| time::Instant, | ||
| }; | ||
|
|
||
| // Helper type for putting a thread to sleep until some other thread wakes it up | ||
| pub struct ThreadParker { | ||
| parked: AtomicBool, | ||
| tcs: Tcs, | ||
| } | ||
|
|
||
| impl ThreadParker { | ||
| pub const IS_CHEAP_TO_CONSTRUCT: bool = true; | ||
|
|
||
| #[inline] | ||
| pub fn new() -> ThreadParker { | ||
| ThreadParker { | ||
| parked: AtomicBool::new(false), | ||
| tcs: current_tcs(), | ||
| } | ||
| } | ||
|
|
||
| // Prepares the parker. This should be called before adding it to the queue. | ||
| #[inline] | ||
| pub fn prepare_park(&self) { | ||
| self.parked.store(true, Ordering::Relaxed); | ||
| } | ||
|
|
||
| // Checks if the park timed out. This should be called while holding the | ||
| // queue lock after park_until has returned false. | ||
| #[inline] | ||
| pub fn timed_out(&self) -> bool { | ||
| self.parked.load(Ordering::Relaxed) | ||
| } | ||
|
|
||
| // Parks the thread until it is unparked. This should be called after it has | ||
| // been added to the queue, after unlocking the queue. | ||
| #[inline] | ||
| pub fn park(&self) { | ||
| while self.parked.load(Ordering::Acquire) { | ||
| let result = usercalls::wait(EV_UNPARK, WAIT_INDEFINITE); | ||
| debug_assert_eq!(result.expect("wait returned error") & EV_UNPARK, EV_UNPARK); | ||
| } | ||
| } | ||
|
|
||
| // Parks the thread until it is unparked or the timeout is reached. This | ||
| // should be called after it has been added to the queue, after unlocking | ||
| // the queue. Returns true if we were unparked and false if we timed out. | ||
| #[inline] | ||
| pub fn park_until(&self, _timeout: Instant) -> bool { | ||
| // FIXME: https://github.com/fortanix/rust-sgx/issues/31 | ||
| panic!("timeout not supported in SGX"); | ||
| } | ||
|
|
||
| // Locks the parker to prevent the target thread from exiting. This is | ||
| // necessary to ensure that thread-local ThreadData objects remain valid. | ||
| // This should be called while holding the queue lock. | ||
| #[inline] | ||
| pub fn unpark_lock(&self) -> UnparkHandle { | ||
| // We don't need to lock anything, just clear the state | ||
| self.parked.store(false, Ordering::Release); | ||
| UnparkHandle(self.tcs) | ||
| } | ||
| } | ||
|
|
||
| // Handle for a thread that is about to be unparked. We need to mark the thread | ||
| // as unparked while holding the queue lock, but we delay the actual unparking | ||
| // until after the queue lock is released. | ||
| pub struct UnparkHandle(Tcs); | ||
|
|
||
| impl UnparkHandle { | ||
| // Wakes up the parked thread. This should be called after the queue lock is | ||
| // released to avoid blocking the queue for too long. | ||
| #[inline] | ||
| pub fn unpark(self) { | ||
| let result = usercalls::send(EV_UNPARK, Some(self.0)); | ||
| if cfg!(debug_assertions) { | ||
| if let Err(error) = result { | ||
| // `InvalidInput` may be returned if the thread we send to has | ||
| // already been unparked and exited. | ||
| if error.kind() != io::ErrorKind::InvalidInput { | ||
| panic!("send returned an unexpected error: {:?}", error); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn thread_yield() { | ||
| thread::yield_now(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.