rename rust rt::sync::Semaphore to rt::sync::Sem

This commit is contained in:
Chris Copeland 2024-04-14 17:16:53 -07:00
parent 9ba2ebb196
commit 4b18d53d7a
Signed by: chrisnc
GPG Key ID: 14550DA72485DF30
14 changed files with 39 additions and 40 deletions

View File

@ -73,8 +73,8 @@ name = "rwlock"
path = "rust/examples/rwlock.rs"
[[example]]
name = "semaphore"
path = "rust/examples/semaphore.rs"
name = "sem"
path = "rust/examples/sem.rs"
[[example]]
name = "simple"
@ -85,8 +85,8 @@ name = "sleep"
path = "rust/examples/sleep.rs"
[[example]]
name = "water-semaphore"
path = "rust/examples/water/semaphore.rs"
name = "water-sem"
path = "rust/examples/water/sem.rs"
[[example]]
name = "water-condvar"

View File

@ -6,7 +6,7 @@ const NUM_TASKS: u32 = 3;
const ITERATIONS: u32 = 10000;
fn trap_last() {
rt::semaphore!(TRAP_SEM, NUM_TASKS as i32 - 1);
rt::sem!(TRAP_SEM, NUM_TASKS as i32 - 1);
if !TRAP_SEM.try_wait() {
assert_eq!(
*MUTEX.try_lock().expect("mutex should be unlocked"),

View File

@ -5,7 +5,7 @@ use core::sync::atomic::{AtomicUsize, Ordering};
const ITERATIONS: usize = 100;
rt::once!(ONCE);
rt::semaphore!(SEM);
rt::sem!(SEM);
static X: AtomicUsize = AtomicUsize::new(0);

View File

@ -1,7 +1,7 @@
#![no_main]
const N: i32 = 10;
rt::semaphore!(SEM);
rt::sem!(SEM);
fn poster() {
rt::task::drop_privilege();

View File

@ -16,7 +16,7 @@ fn sleep_periodic(period: rt::tick::Utick) {
}
// Only the second task to finish will call rt::trap.
rt::semaphore!(TRAP_SEM, 1);
rt::sem!(TRAP_SEM, 1);
if !TRAP_SEM.try_wait() {
rt::trap();
}

View File

@ -2,8 +2,8 @@
use core::sync::atomic::{AtomicU32, Ordering};
rt::semaphore!(H2READY);
rt::semaphore!(HDONE);
rt::sem!(H2READY);
rt::sem!(HDONE);
static H: AtomicU32 = AtomicU32::new(0);
fn hydrogen() {

View File

@ -7,7 +7,7 @@ use core::{
use crate::{
bindings::{rt_barrier, rt_barrier_wait, rt_cond, rt_mutex},
list::list_init,
sync::semaphore::c_sem_init,
sync::sem::c_sem_init,
};
pub struct Barrier {

View File

@ -7,7 +7,7 @@ use core::{
use crate::{
bindings::{rt_cond, rt_cond_broadcast, rt_cond_signal, rt_cond_timedwait, rt_cond_wait},
sync::{semaphore::c_sem_init, MutexGuard},
sync::{sem::c_sem_init, MutexGuard},
tick::Utick,
};

View File

@ -5,7 +5,7 @@ pub use notify::Notify;
pub use once::Once;
pub use queue::Queue;
pub use rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
pub use semaphore::{Semaphore, SemaphoreGuard};
pub use sem::{Sem, SemGuard};
mod barrier;
mod condvar;
@ -14,4 +14,4 @@ mod notify;
mod once;
mod queue;
mod rwlock;
mod semaphore;
mod sem;

View File

@ -11,7 +11,7 @@ use crate::{
rt_notify_timedwait_clear, rt_notify_trywait, rt_notify_trywait_clear, rt_notify_wait,
rt_notify_wait_clear,
},
sync::semaphore::c_sem_init,
sync::sem::c_sem_init,
tick::Utick,
};

View File

@ -12,7 +12,7 @@ use crate::{
rt_queue, rt_queue_peek, rt_queue_pop, rt_queue_push, rt_queue_timedpeek,
rt_queue_timedpop, rt_queue_timedpush, rt_queue_trypeek, rt_queue_trypop, rt_queue_trypush,
},
sync::semaphore::c_sem_init,
sync::sem::c_sem_init,
tick::Utick,
};

View File

@ -12,7 +12,7 @@ use crate::{
rt_rwlock_wrunlock,
},
list::list_init,
sync::semaphore::c_sem_init,
sync::sem::c_sem_init,
tick::Utick,
};

View File

@ -17,15 +17,15 @@ use crate::{
tick::Utick,
};
pub struct Semaphore {
pub struct Sem {
s: UnsafeCell<rt_sem>,
_pin_marker: PhantomPinned,
}
unsafe impl Send for Semaphore {}
unsafe impl Sync for Semaphore {}
impl UnwindSafe for Semaphore {}
impl RefUnwindSafe for Semaphore {}
unsafe impl Send for Sem {}
unsafe impl Sync for Sem {}
impl UnwindSafe for Sem {}
impl RefUnwindSafe for Sem {}
// Also used by cond and queue.
pub(crate) const fn c_sem_init(s: &'static rt_sem, value: i32, max_value: i32) -> rt_sem {
@ -50,10 +50,10 @@ pub(crate) const fn c_sem_init(s: &'static rt_sem, value: i32, max_value: i32) -
}
}
impl Semaphore {
pub const fn init(sem: &'static Semaphore, value: i32, max_value: i32) -> Semaphore {
impl Sem {
pub const fn init(sem: &'static Sem, value: i32, max_value: i32) -> Sem {
let s = unsafe { &*sem.s.get().cast_const() };
Semaphore {
Sem {
s: UnsafeCell::new(c_sem_init(s, value, max_value)),
_pin_marker: PhantomPinned,
}
@ -87,41 +87,41 @@ impl Semaphore {
self.post()
}
pub fn access(&self) -> SemaphoreGuard {
pub fn access(&self) -> SemGuard {
self.acquire();
SemaphoreGuard { sem: self }
SemGuard { sem: self }
}
}
pub struct SemaphoreGuard<'a> {
sem: &'a Semaphore,
pub struct SemGuard<'a> {
sem: &'a Sem,
}
impl<'a> Drop for SemaphoreGuard<'a> {
impl<'a> Drop for SemGuard<'a> {
fn drop(&mut self) {
self.sem.release();
}
}
#[macro_export]
macro_rules! semaphore {
macro_rules! sem {
($name: ident, $count: expr, $max: expr) => {
static $name: $crate::sync::Semaphore = $crate::sync::Semaphore::init(&$name, $count, $max);
static $name: $crate::sync::Sem = $crate::sync::Sem::init(&$name, $count, $max);
};
($name: ident, $count: expr) => {
$crate::semaphore!($name, $count, i32::MAX);
$crate::sem!($name, $count, i32::MAX);
};
($name: ident) => {
$crate::semaphore!($name, 0, i32::MAX);
$crate::sem!($name, 0, i32::MAX);
};
}
#[macro_export]
macro_rules! semaphore_binary {
macro_rules! sem_binary {
($name: ident) => {
$crate::semaphore!($name, 0, 1);
$crate::sem!($name, 0, 1);
};
}
@ -129,7 +129,7 @@ macro_rules! semaphore_binary {
mod tests {
#[test]
fn fast_path() {
semaphore!(SEM);
sem!(SEM);
SEM.post();
SEM.post();
assert!(SEM.try_wait());
@ -139,7 +139,7 @@ mod tests {
#[test]
fn binary_fast_path() {
semaphore_binary!(SEM);
sem_binary!(SEM);
SEM.post();
SEM.post();
assert!(SEM.try_wait());

View File

@ -7,8 +7,7 @@ cargo build --release
trap 'if [ "$?" != "0" ]; then echo "test failed!"; fi' EXIT
examples=(
donate empty float mutex notify once queue rwlock semaphore simple
sleep water-semaphore
donate empty float mutex notify once queue rwlock sem simple sleep water-sem
)
for e in "${examples[@]}"; do