Skip to content

Commit

Permalink
[#3] Remove thread_exit and thread_cancel features (can be realized w…
Browse files Browse the repository at this point in the history
…ith more idiomatic approaches)
  • Loading branch information
elfenpiff committed Oct 30, 2024
1 parent 5d96bfa commit 980189b
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 86 deletions.
2 changes: 1 addition & 1 deletion iceoryx2-bb/posix/src/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl<'a> Barrier<'a> {
}

impl<'a> IpcConstructible<'a, BarrierHandle> for Barrier<'a> {
fn new(handle: &'a BarrierHandle) -> Barrier {
fn new(handle: &BarrierHandle) -> Barrier {
Barrier { handle }
}
}
Expand Down
10 changes: 0 additions & 10 deletions iceoryx2-bb/posix/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ enum_gen! {
FailedToSetAffinity <= ThreadSetAffinityError
}

/// Terminates the callers thread
pub fn thread_exit() -> ! {
unsafe { posix::pthread_exit(std::ptr::null_mut::<posix::void>()) }
}

/// Describes the scope in which a thread is competing with other threads for CPU time.
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
#[repr(i32)]
Expand Down Expand Up @@ -803,11 +798,6 @@ impl<'thread> Thread<'thread> {
v => (UnknownError(v as i32), "{} {:?} since an unknown error occurred ({})", msg, signal, v)
);
}

/// Terminates the current thread.
pub fn cancel(&mut self) -> bool {
unsafe { posix::pthread_cancel(self.handle.handle) != 0 }
}
}

impl<'thread> ThreadProperties for Thread<'thread> {
Expand Down
41 changes: 0 additions & 41 deletions iceoryx2-bb/posix/tests/thread_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,44 +198,3 @@ fn thread_destructor_does_block_on_busy_thread() {
drop(thread);
assert_that!(start.elapsed(), time_at_least SLEEP_DURATION);
}

#[test]
fn thread_cancel_works() {
let barrier = Arc::new(Barrier::new(2));
let mut thread = {
let barrier = barrier.clone();
ThreadBuilder::new()
.affinity(0)
.spawn(move || {
barrier.wait();
loop {
// NOTE we have to use 'sleep' instead of 'barrier.wait()' since the latter blocks cancellation
std::thread::sleep(Duration::from_millis(100));
}
})
.unwrap()
};

// if the thread is not executed we observe a deadlock here
barrier.wait();
thread.cancel();
}

#[test]
fn thread_exit_works() {
let _thread = {
ThreadBuilder::new()
.affinity(0)
.spawn(move || {
// TODO: [#258] This sleep prevents a 'SIGABRT' on Linux with release builds with rustc v1.81.0-nightly!
#[cfg(target_os = "linux")]
std::thread::sleep(Duration::from_nanos(1));
thread_exit();
#[allow(unreachable_code)]
{
panic!("This should not happen when 'thread_exit' works as intended");
}
})
.unwrap()
};
}
8 changes: 0 additions & 8 deletions iceoryx2-pal/posix/src/freebsd/pthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,6 @@ pub unsafe fn pthread_getname_np(thread: pthread_t, name: *mut c_char, len: size
internal::pthread_getname_np(thread, name, len)
}

pub unsafe fn pthread_cancel(thread: pthread_t) -> int {
crate::internal::pthread_cancel(thread)
}

pub unsafe fn pthread_exit(value_ptr: *mut void) -> ! {
crate::internal::pthread_exit(value_ptr)
}

pub unsafe fn pthread_kill(thread: pthread_t, sig: int) -> int {
internal::pthread_kill(thread, sig)
}
Expand Down
8 changes: 0 additions & 8 deletions iceoryx2-pal/posix/src/linux/pthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,6 @@ pub unsafe fn pthread_getname_np(thread: pthread_t, name: *mut c_char, len: size
internal::pthread_getname_np(thread, name, len)
}

pub unsafe fn pthread_cancel(thread: pthread_t) -> int {
crate::internal::pthread_cancel(thread)
}

pub unsafe fn pthread_exit(value_ptr: *mut void) -> ! {
crate::internal::pthread_exit(value_ptr)
}

pub unsafe fn pthread_kill(thread: pthread_t, sig: int) -> int {
internal::pthread_kill(thread, sig)
}
Expand Down
8 changes: 0 additions & 8 deletions iceoryx2-pal/posix/src/macos/pthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,6 @@ pub unsafe fn pthread_getname_np(thread: pthread_t, name: *mut c_char, len: size
Errno::ESUCCES as _
}

pub unsafe fn pthread_cancel(thread: pthread_t) -> int {
crate::internal::pthread_cancel(thread)
}

pub unsafe fn pthread_exit(value_ptr: *mut void) -> ! {
crate::internal::pthread_exit(value_ptr)
}

pub unsafe fn pthread_kill(_thread: pthread_t, _sig: int) -> int {
todo!()
}
Expand Down
11 changes: 1 addition & 10 deletions iceoryx2-pal/posix/src/windows/pthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use windows_sys::Win32::{
System::{
Memory::LocalFree,
Threading::{
CreateThread, ExitThread, GetCurrentThread, GetCurrentThreadId, GetExitCodeThread,
CreateThread, GetCurrentThread, GetCurrentThreadId, GetExitCodeThread,
GetThreadDescription, GetThreadId, SetThreadAffinityMask, SetThreadDescription,
SetThreadPriority, TerminateThread, WaitForSingleObject, WaitOnAddress,
WakeByAddressAll, WakeByAddressSingle, INFINITE, THREAD_PRIORITY_ABOVE_NORMAL,
Expand Down Expand Up @@ -396,15 +396,6 @@ pub unsafe fn pthread_getname_np(thread: pthread_t, name: *mut c_char, len: size
Errno::ESUCCES as int
}

pub unsafe fn pthread_cancel(thread: pthread_t) -> int {
TerminateThread(thread.handle, 0);
Errno::ESUCCES as int
}

pub unsafe fn pthread_exit(value_ptr: *mut void) -> ! {
ExitThread(0);
}

pub unsafe fn pthread_kill(thread: pthread_t, sig: int) -> int {
TerminateThread(thread.handle, 0);
Errno::ESUCCES as int
Expand Down

0 comments on commit 980189b

Please sign in to comment.