From 05d43b00b41073200e4b8e41f90983f8cadfca98 Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Wed, 20 Dec 2023 09:34:19 -0800 Subject: [PATCH] [#51] Adjust windows to new concurrency primitives api --- iceoryx2-pal/posix/src/windows/pthread.rs | 64 ++++++++++--------- iceoryx2-pal/posix/src/windows/semaphore.rs | 17 ++--- iceoryx2-pal/posix/src/windows/signal.rs | 5 +- .../src/windows/win32_handle_translator.rs | 3 +- 4 files changed, 47 insertions(+), 42 deletions(-) diff --git a/iceoryx2-pal/posix/src/windows/pthread.rs b/iceoryx2-pal/posix/src/windows/pthread.rs index e214c4a30..d1f01dc8c 100644 --- a/iceoryx2-pal/posix/src/windows/pthread.rs +++ b/iceoryx2-pal/posix/src/windows/pthread.rs @@ -25,6 +25,7 @@ use std::{ use iceoryx2_pal_concurrency_sync::rwlock::*; use iceoryx2_pal_concurrency_sync::{barrier::Barrier, mutex::Mutex}; +use iceoryx2_pal_concurrency_sync::{WaitAction, WaitResult}; use windows_sys::Win32::{ Foundation::{CloseHandle, ERROR_TIMEOUT, STILL_ACTIVE, WAIT_FAILED}, System::{ @@ -92,7 +93,7 @@ impl ThreadStates { INFINITE, ) }; } - true + WaitAction::Continue }); } @@ -492,7 +493,7 @@ pub unsafe fn pthread_rwlock_rdlock(lock: *mut pthread_rwlock_t) -> int { 4, INFINITE, )}; - true + WaitAction::Continue }), RwLockType::PreferWriter(ref l) => l.read_lock(|atomic, value| { win32call! {WaitOnAddress( @@ -501,7 +502,7 @@ pub unsafe fn pthread_rwlock_rdlock(lock: *mut pthread_rwlock_t) -> int { 4, INFINITE, )}; - true + WaitAction::Continue }), _ => { return Errno::EINVAL as _; @@ -512,7 +513,7 @@ pub unsafe fn pthread_rwlock_rdlock(lock: *mut pthread_rwlock_t) -> int { } pub unsafe fn pthread_rwlock_tryrdlock(lock: *mut pthread_rwlock_t) -> int { - let has_lock = match (*lock).lock { + let wait_result = match (*lock).lock { RwLockType::PreferReader(ref l) => l.try_read_lock(), RwLockType::PreferWriter(ref l) => l.try_read_lock(), _ => { @@ -520,7 +521,7 @@ pub unsafe fn pthread_rwlock_tryrdlock(lock: *mut pthread_rwlock_t) -> int { } }; - if has_lock { + if wait_result == WaitResult::Success { Errno::ESUCCES as _ } else { Errno::EBUSY as _ @@ -557,7 +558,7 @@ pub unsafe fn pthread_rwlock_wrlock(lock: *mut pthread_rwlock_t) -> int { 4, INFINITE, )}; - true + WaitAction::Continue }), RwLockType::PreferWriter(ref l) => l.write_lock( |atomic, value| { @@ -567,7 +568,7 @@ pub unsafe fn pthread_rwlock_wrlock(lock: *mut pthread_rwlock_t) -> int { 4, INFINITE, )}; - true + WaitAction::Continue }, |atomic| { win32call! { WakeByAddressSingle((atomic as *const AtomicU32).cast()) }; @@ -585,7 +586,7 @@ pub unsafe fn pthread_rwlock_wrlock(lock: *mut pthread_rwlock_t) -> int { } pub unsafe fn pthread_rwlock_trywrlock(lock: *mut pthread_rwlock_t) -> int { - let has_lock = match (*lock).lock { + let wait_result = match (*lock).lock { RwLockType::PreferReader(ref l) => l.try_write_lock(), RwLockType::PreferWriter(ref l) => l.try_write_lock(), _ => { @@ -593,7 +594,7 @@ pub unsafe fn pthread_rwlock_trywrlock(lock: *mut pthread_rwlock_t) -> int { } }; - if has_lock { + if wait_result == WaitResult::Success { Errno::ESUCCES as _ } else { Errno::EBUSY as _ @@ -611,7 +612,7 @@ pub unsafe fn pthread_rwlock_timedwrlock( - now.as_millis() as i64, ); - let has_lock = match (*lock).lock { + let wait_result = match (*lock).lock { RwLockType::PreferReader(ref l) => l.write_lock(|atomic, value| { win32call! { WaitOnAddress( (atomic as *const AtomicU32).cast(), @@ -619,7 +620,7 @@ pub unsafe fn pthread_rwlock_timedwrlock( 4, timeout as _, ), ignore ERROR_TIMEOUT }; - true + WaitAction::Continue }), RwLockType::PreferWriter(ref l) => l.write_lock( |atomic, value| { @@ -629,7 +630,7 @@ pub unsafe fn pthread_rwlock_timedwrlock( 4, timeout as _, ), ignore ERROR_TIMEOUT}; - true + WaitAction::Continue }, |atomic| { WakeByAddressSingle((atomic as *const AtomicU32).cast()); @@ -643,7 +644,7 @@ pub unsafe fn pthread_rwlock_timedwrlock( } }; - if has_lock { + if wait_result == WaitResult::Success { Errno::ESUCCES as _ } else { Errno::ETIMEDOUT as _ @@ -661,7 +662,7 @@ pub unsafe fn pthread_rwlock_timedrdlock( - now.as_millis() as i64, ); - let has_lock = match (*lock).lock { + let wait_result = match (*lock).lock { RwLockType::PreferReader(ref l) => l.read_lock(|atomic, value| { win32call! { WaitOnAddress( (atomic as *const AtomicU32).cast(), @@ -669,7 +670,7 @@ pub unsafe fn pthread_rwlock_timedrdlock( 4, timeout as _, ), ignore ERROR_TIMEOUT }; - true + WaitAction::Continue }), RwLockType::PreferWriter(ref l) => l.read_lock(|atomic, value| { win32call! {WaitOnAddress( @@ -678,14 +679,14 @@ pub unsafe fn pthread_rwlock_timedrdlock( 4, timeout as _, ), ignore ERROR_TIMEOUT}; - true + WaitAction::Continue }), _ => { return Errno::EINVAL as _; } }; - if has_lock { + if wait_result == WaitResult::Success { Errno::ESUCCES as _ } else { Errno::ETIMEDOUT as _ @@ -728,7 +729,7 @@ pub unsafe fn pthread_cond_wait(cond: *mut pthread_cond_t, mutex: *mut pthread_m 4, INFINITE, )}; - true + WaitAction::Continue }, |atomic, value| { win32call! {WaitOnAddress( @@ -737,7 +738,7 @@ pub unsafe fn pthread_cond_wait(cond: *mut pthread_cond_t, mutex: *mut pthread_m 4, INFINITE, )}; - false + WaitAction::Continue }, ); @@ -755,7 +756,7 @@ pub unsafe fn pthread_cond_timedwait( (*abstime).tv_sec * 1000 + (*abstime).tv_nsec as i64 / 1000000 - now.as_millis() as i64, ); - (*cond).cv.wait( + match (*cond).cv.wait( &(*mutex).mtx, |atomic| { win32call! { WakeByAddressSingle((atomic as *const AtomicU32).cast()) }; @@ -767,7 +768,7 @@ pub unsafe fn pthread_cond_timedwait( 4, timeout as _, ), ignore ERROR_TIMEOUT }; - false + WaitAction::Abort }, |atomic, value| { win32call! { WaitOnAddress( @@ -776,11 +777,12 @@ pub unsafe fn pthread_cond_timedwait( 4, timeout as _, ), ignore ERROR_TIMEOUT }; - false + WaitAction::Abort }, - ); - - Errno::ESUCCES as _ + ) { + WaitResult::Success => Errno::ESUCCES as _, + WaitResult::Interrupted => Errno::ETIMEDOUT as _, + } } pub unsafe fn pthread_condattr_init(attr: *mut pthread_condattr_t) -> int { @@ -898,7 +900,7 @@ pub unsafe fn pthread_mutex_lock(mtx: *mut pthread_mutex_t) -> int { 4, INFINITE, ) }; - true + WaitAction::Continue }); if (*mtx).track_thread_id { @@ -937,9 +939,9 @@ pub unsafe fn pthread_mutex_timedlock( 4, timeout as _, ), ignore ERROR_TIMEOUT }; - false + WaitAction::Abort }) { - true => { + WaitResult::Success => { if (*mtx).track_thread_id { (*mtx) .current_owner @@ -948,7 +950,7 @@ pub unsafe fn pthread_mutex_timedlock( Errno::ESUCCES as _ } - false => Errno::ETIMEDOUT as _, + WaitResult::Interrupted => Errno::ETIMEDOUT as _, } } @@ -960,7 +962,7 @@ pub unsafe fn pthread_mutex_trylock(mtx: *mut pthread_mutex_t) -> int { }; match (*mtx).mtx.try_lock() { - true => { + WaitResult::Success => { if (*mtx).track_thread_id { (*mtx) .current_owner @@ -969,7 +971,7 @@ pub unsafe fn pthread_mutex_trylock(mtx: *mut pthread_mutex_t) -> int { Errno::ESUCCES as _ } - false => Errno::EBUSY as _, + WaitResult::Interrupted => Errno::EBUSY as _, } } diff --git a/iceoryx2-pal/posix/src/windows/semaphore.rs b/iceoryx2-pal/posix/src/windows/semaphore.rs index 540d53b3f..7e68c823c 100644 --- a/iceoryx2-pal/posix/src/windows/semaphore.rs +++ b/iceoryx2-pal/posix/src/windows/semaphore.rs @@ -19,6 +19,7 @@ use std::time::SystemTime; use std::time::UNIX_EPOCH; use iceoryx2_pal_concurrency_sync::semaphore::Semaphore; +use iceoryx2_pal_concurrency_sync::{WaitAction, WaitResult}; use windows_sys::Win32::System::Threading::WaitOnAddress; use windows_sys::Win32::System::Threading::WakeByAddressSingle; use windows_sys::Win32::System::Threading::INFINITE; @@ -49,7 +50,7 @@ pub unsafe fn sem_post(sem: *mut sem_t) -> int { } pub unsafe fn sem_wait(sem: *mut sem_t) -> int { - (*sem).semaphore.wait(|atomic, value| -> bool { + (*sem).semaphore.wait(|atomic, value| -> WaitAction { WaitOnAddress( (atomic as *const AtomicU32).cast(), (value as *const u32).cast(), @@ -57,7 +58,7 @@ pub unsafe fn sem_wait(sem: *mut sem_t) -> int { INFINITE, ); - true + WaitAction::Continue }); Errno::set(Errno::ESUCCES); @@ -66,11 +67,11 @@ pub unsafe fn sem_wait(sem: *mut sem_t) -> int { pub unsafe fn sem_trywait(sem: *mut sem_t) -> int { match (*sem).semaphore.try_wait() { - true => { + WaitResult::Success => { Errno::set(Errno::ESUCCES); 0 } - false => { + WaitResult::Interrupted => { Errno::set(Errno::EAGAIN); -1 } @@ -82,7 +83,7 @@ pub unsafe fn sem_timedwait(sem: *mut sem_t, abs_timeout: *const timespec) -> in let milli_seconds = (*abs_timeout).tv_sec * 1000 + (*abs_timeout).tv_nsec as i64 / 1000000 - now.as_millis() as i64; - match (*sem).semaphore.wait(|atomic, value| -> bool { + match (*sem).semaphore.wait(|atomic, value| -> WaitAction { WaitOnAddress( (atomic as *const AtomicU32).cast(), (value as *const u32).cast(), @@ -90,13 +91,13 @@ pub unsafe fn sem_timedwait(sem: *mut sem_t, abs_timeout: *const timespec) -> in milli_seconds as _, ); - false + WaitAction::Abort }) { - true => { + WaitResult::Success => { Errno::set(Errno::ESUCCES); 0 } - false => { + WaitResult::Interrupted => { Errno::set(Errno::ETIMEDOUT); -1 } diff --git a/iceoryx2-pal/posix/src/windows/signal.rs b/iceoryx2-pal/posix/src/windows/signal.rs index 3964b68f2..83eb3c991 100644 --- a/iceoryx2-pal/posix/src/windows/signal.rs +++ b/iceoryx2-pal/posix/src/windows/signal.rs @@ -15,6 +15,7 @@ #![allow(unused_variables)] use iceoryx2_pal_concurrency_sync::mutex::Mutex; +use iceoryx2_pal_concurrency_sync::WaitAction; use windows_sys::Win32::{ Foundation::{FALSE, TRUE}, System::{ @@ -54,14 +55,14 @@ impl SigAction { } fn get(&self) -> sigaction_t { - self.mtx.lock(|_, _| true); + self.mtx.lock(|_, _| WaitAction::Continue); let ret_val = unsafe { *self.action.get() }; self.mtx.unlock(|_| {}); ret_val } fn set(&self, value: sigaction_t) -> sigaction_t { - self.mtx.lock(|_, _| true); + self.mtx.lock(|_, _| WaitAction::Continue); let ret_val = unsafe { *self.action.get() }; unsafe { *self.action.get() = value }; self.mtx.unlock(|_| {}); diff --git a/iceoryx2-pal/posix/src/windows/win32_handle_translator.rs b/iceoryx2-pal/posix/src/windows/win32_handle_translator.rs index 06b68f441..e8a8ac0a0 100644 --- a/iceoryx2-pal/posix/src/windows/win32_handle_translator.rs +++ b/iceoryx2-pal/posix/src/windows/win32_handle_translator.rs @@ -19,6 +19,7 @@ use windows_sys::Win32::{ use crate::posix::{c_string_length, ntohs, types::*}; use core::{cell::UnsafeCell, panic}; use iceoryx2_pal_concurrency_sync::mutex::Mutex; +use iceoryx2_pal_concurrency_sync::WaitAction; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering}; use super::win32_udp_port_to_uds_name::{PortToUds, MAX_UDS_NAME_LEN}; @@ -141,7 +142,7 @@ impl HandleTranslator { INFINITE, ); } - true + WaitAction::Continue }); }