Skip to content

Commit

Permalink
Remove left-over functions from merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Arshia001 committed Aug 7, 2024
1 parent 1b8fecb commit 54139d5
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 182 deletions.
38 changes: 0 additions & 38 deletions library/std/src/sys/pal/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::io;
use crate::mem;
use crate::num::NonZero;
use crate::ptr;
use crate::sync::Arc;
use crate::sys::{os, stack_overflow};
use crate::time::Duration;

Expand Down Expand Up @@ -113,43 +112,6 @@ impl Thread {
}
}

pub unsafe fn new_reactor<F>(p: F) -> io::Result<Thread>
where F: Fn() + Send + Sync + 'static {
let p = Arc::new(p);
let p = Arc::into_raw(p);
let mut native: libc::pthread_t = mem::zeroed();
let mut attr: libc::pthread_attr_t = mem::zeroed();
assert_eq!(libc::pthread_attr_init(&mut attr), 0);

let ret = libc::pthread_create(&mut native, &attr, reactor_start, p as *mut _);
// Note: if the thread creation fails and this assert fails, then p will
// be leaked. However, an alternative design could cause double-free
// which is clearly worse.
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);

return if ret != 0 {
// The thread failed to start and as a result p was not consumed. Therefore, it is
// safe to reconstruct the box so that it gets deallocated.
drop(Arc::from_raw(p));
Err(io::Error::from_raw_os_error(ret))
} else {
Ok(Thread { id: native })
};

extern "C" fn reactor_start(main: *mut libc::c_void) -> *mut libc::c_void {
unsafe {
// Next, set up our stack overflow handler which may get triggered if we run
// out of stack.
let _handler = stack_overflow::Handler::new();
// Finally, let's run some code.
let f = Arc::from_raw(main as *mut Arc<dyn Fn()>);
f();
crate::mem::forget(f);
}
ptr::null_mut()
}
}

pub fn yield_now() {
let ret = unsafe { libc::sched_yield() };
debug_assert_eq!(ret, 0);
Expand Down
5 changes: 0 additions & 5 deletions library/std/src/sys/pal/unsupported/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ impl Thread {
unsupported()
}

pub unsafe fn new_reactor<F>(p: F) -> io::Result<Thread>
where F: Fn() + Send + Sync + 'static {
unsupported()
}

pub fn yield_now() {
// do nothing
}
Expand Down
7 changes: 0 additions & 7 deletions library/std/src/sys/pal/wasi/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,6 @@ impl Thread {
}
}

pub unsafe fn new_reactor<F>(_p: F) -> io::Result<Thread>
where
F: Fn() + Send + Sync + 'static,
{
unsupported()
}

pub fn yield_now() {
let ret = unsafe { wasi::sched_yield() };
debug_assert_eq!(ret, Ok(()));
Expand Down
5 changes: 0 additions & 5 deletions library/std/src/sys/pal/wasm/atomics/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ impl Thread {
unsupported()
}

pub unsafe fn new_reactor<F>(p: F) -> io::Result<Thread>
where F: Fn() + Send + Sync + 'static {
unsupported()
}

pub fn yield_now() {}

pub fn set_name(_name: &CStr) {}
Expand Down
4 changes: 0 additions & 4 deletions library/std/src/sys/pal/windows/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ impl Socket {
}
}

pub fn accept_timeout(&self, _storage: *mut c::SOCKADDR, _len: *mut c_int, _timeout: crate::time::Duration) -> io::Result<Socket> {
super::unsupported::unsupported()
}

pub fn duplicate(&self) -> io::Result<Socket> {
Ok(Self(self.0.try_clone()?))
}
Expand Down
123 changes: 0 additions & 123 deletions library/std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,46 +387,6 @@ impl Builder {
unsafe { self.spawn_unchecked(f) }
}

/// Spawns a new reactor by taking ownership of the `Builder`, and returns an
/// [`io::Result`].
///
/// The spawned reactor may outlive the caller (unless the caller thread
/// is the main thread; the whole process is terminated when the main
/// thread finishes).
///
/// # Errors
///
/// Unlike the [`spawn`] free function, this method yields an
/// [`io::Result`] to capture any failure to create the thread at
/// the OS level.
///
/// [`io::Result`]: crate::io::Result
///
/// # Panics
///
/// Panics if a reactor name was set and it contained null bytes.
///
/// # Examples
///
/// ```
/// use std::thread;
///
/// let builder = thread::Builder::new();
///
/// builder.reactor(|| {
/// // reactor code
/// }).unwrap();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn reactor<F>(self, f: F) -> io::Result<()>
where
F: Fn(),
F: Send + Sync + 'static,
{
unsafe { imp::Thread::new_reactor(f)? };
Ok(())
}

/// Spawns a new thread without any lifetime restrictions by taking ownership
/// of the `Builder`, and returns an [`io::Result`] to its [`JoinHandle`].
///
Expand Down Expand Up @@ -762,89 +722,6 @@ pub(crate) fn try_current() -> Option<Thread> {
CURRENT.try_with(|current| current.get_or_init(|| Thread::new_unnamed()).clone()).ok()
}

////////////////////////////////////////////////////////////////////////////////
// Free functions
////////////////////////////////////////////////////////////////////////////////

/// Spawns a new reactor
///
/// This call will create a reactor using default parameters of [`Builder`], if you
/// want to specify the stack size or the name of the reactor, use this API
/// instead.
///
/// As you can see in the signature of `reactor` there are two constraints on
/// the closure given to `reactor`, let's explain them:
///
/// - The `'static` constraint means that the closure must have a lifetime of the
/// whole program execution. The reason for this is that reactors will be
/// continuously invoked by outside of the program until it exists
///
/// Indeed if the reactor, can outlive their caller, we need to make sure that
/// they will be valid afterwards, thus is until the end of the program, hence
/// he `'static` lifetime.
/// - The [`Send`] constraint is because the closure will need to be passed
/// *by value* from the thread where it is spawned to the new thread. Its
/// return value will need to be passed from the new thread to the thread
/// where it is `join`ed.
/// As a reminder, the [`Send`] marker trait expresses that it is safe to be
/// passed from thread to thread. [`Sync`] expresses that it is safe to have a
/// reference be passed from thread to thread.
///
/// # Panics
///
/// Panics if the OS fails to create a thread; use [`Builder::reactor`]
/// to recover from such errors.
///
/// # Examples
///
/// Creating a thread.
///
/// ```
/// use std::thread;
///
/// thread::reactor(|| {
/// // reactor code
/// });
/// ```
///
/// As mentioned in the module documentation, reactors are usually made to
/// communicate using [`channels`], here is how it usually looks.
///
/// This example also shows how to use `move`, in order to give ownership
/// of values to a reactor.
///
/// ```
/// use std::thread;
/// use std::sync::{Arc, Mutex};
/// use std::sync::mpsc::channel;
///
/// let (tx, rx) = channel();
/// let tx = Arc::new(Mutex::new(tx));
///
/// let sender = thread::reactor(move || {
/// tx.lock().unwrap().send("Hello, thread".to_owned())
/// .expect("Unable to send on channel");
/// });
///
/// let receiver = thread::thread(move || {
/// let value = rx.recv().expect("Unable to receive from channel");
/// println!("{value}");
/// });
///
/// receiver.join().expect("The receiver thread has panicked");
/// ```
///
/// [`channels`]: crate::sync::mpsc
/// [`Err`]: crate::result::Result::Err
#[stable(feature = "rust1", since = "1.0.0")]
pub fn reactor<F>(f: F)
where
F: Fn(),
F: Send + Sync + 'static,
{
Builder::new().reactor(f).expect("failed to spawn reactor")
}

/// Gets a handle to the thread that invokes it.
///
/// # Examples
Expand Down

0 comments on commit 54139d5

Please sign in to comment.