Skip to content

Commit e531b52

Browse files
authored
Merge pull request #261 from ZR233/fix/smp
[fix] Update memory ordering for atomic
2 parents 803e866 + 8550e2a commit e531b52

File tree

7 files changed

+22
-22
lines changed

7 files changed

+22
-22
lines changed

modules/axruntime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! {
186186
ctor_bare::call_ctors();
187187

188188
info!("Primary CPU {} init OK.", cpu_id);
189-
INITED_CPUS.fetch_add(1, Ordering::Relaxed);
189+
INITED_CPUS.fetch_add(1, Ordering::Release);
190190

191191
while !is_init_ok() {
192192
core::hint::spin_loop();

modules/axruntime/src/mp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn start_secondary_cpus(primary_cpu_id: usize) {
3333
/// It is called from the bootstrapping code in [axhal].
3434
#[unsafe(no_mangle)]
3535
pub extern "C" fn rust_main_secondary(cpu_id: usize) -> ! {
36-
ENTERED_CPUS.fetch_add(1, Ordering::Relaxed);
36+
ENTERED_CPUS.fetch_add(1, Ordering::Release);
3737
info!("Secondary CPU {:x} started.", cpu_id);
3838

3939
#[cfg(feature = "paging")]
@@ -45,7 +45,7 @@ pub extern "C" fn rust_main_secondary(cpu_id: usize) -> ! {
4545
axtask::init_scheduler_secondary();
4646

4747
info!("Secondary CPU {:x} init OK.", cpu_id);
48-
super::INITED_CPUS.fetch_add(1, Ordering::Relaxed);
48+
super::INITED_CPUS.fetch_add(1, Ordering::Release);
4949

5050
while !super::is_init_ok() {
5151
core::hint::spin_loop();

modules/axtask/src/task.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,13 @@ impl TaskInner {
361361
#[inline]
362362
#[cfg(feature = "preempt")]
363363
pub(crate) fn disable_preempt(&self) {
364-
self.preempt_disable_count.fetch_add(1, Ordering::Relaxed);
364+
self.preempt_disable_count.fetch_add(1, Ordering::Release);
365365
}
366366

367367
#[inline]
368368
#[cfg(feature = "preempt")]
369369
pub(crate) fn enable_preempt(&self, resched: bool) {
370-
if self.preempt_disable_count.fetch_sub(1, Ordering::Relaxed) == 1 && resched {
370+
if self.preempt_disable_count.fetch_sub(1, Ordering::Release) == 1 && resched {
371371
// If current task is pending to be preempted, do rescheduling.
372372
Self::current_check_preempt_pending();
373373
}

modules/axtask/src/tests.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ fn test_sched_fifo() {
1919
move || {
2020
println!("sched-fifo: Hello, task {}! ({})", i, current().id_name());
2121
axtask::yield_now();
22-
let order = FINISHED_TASKS.fetch_add(1, Ordering::Relaxed);
22+
let order = FINISHED_TASKS.fetch_add(1, Ordering::Release);
2323
assert_eq!(order, i); // FIFO scheduler
2424
},
2525
format!("T{}", i),
2626
0x1000,
2727
);
2828
}
2929

30-
while FINISHED_TASKS.load(Ordering::Relaxed) < NUM_TASKS {
30+
while FINISHED_TASKS.load(Ordering::Acquire) < NUM_TASKS {
3131
axtask::yield_now();
3232
}
3333
}
@@ -55,10 +55,10 @@ fn test_fp_state_switch() {
5555

5656
println!("fp_state_switch: Float {} = {}", i, value);
5757
assert!((value - float).abs() < 1e-9);
58-
FINISHED_TASKS.fetch_add(1, Ordering::Relaxed);
58+
FINISHED_TASKS.fetch_add(1, Ordering::Release);
5959
});
6060
}
61-
while FINISHED_TASKS.load(Ordering::Relaxed) < NUM_TASKS {
61+
while FINISHED_TASKS.load(Ordering::Acquire) < NUM_TASKS {
6262
axtask::yield_now();
6363
}
6464
}
@@ -76,31 +76,31 @@ fn test_wait_queue() {
7676

7777
for _ in 0..NUM_TASKS {
7878
axtask::spawn(move || {
79-
COUNTER.fetch_add(1, Ordering::Relaxed);
79+
COUNTER.fetch_add(1, Ordering::Release);
8080
println!("wait_queue: task {:?} started", current().id());
8181
WQ1.notify_one(true); // WQ1.wait_until()
8282
WQ2.wait();
8383

8484
assert!(!current().in_wait_queue());
8585

86-
COUNTER.fetch_sub(1, Ordering::Relaxed);
86+
COUNTER.fetch_sub(1, Ordering::Release);
8787
println!("wait_queue: task {:?} finished", current().id());
8888
WQ1.notify_one(true); // WQ1.wait_until()
8989
});
9090
}
9191

9292
println!("task {:?} is waiting for tasks to start...", current().id());
93-
WQ1.wait_until(|| COUNTER.load(Ordering::Relaxed) == NUM_TASKS);
94-
assert_eq!(COUNTER.load(Ordering::Relaxed), NUM_TASKS);
93+
WQ1.wait_until(|| COUNTER.load(Ordering::Acquire) == NUM_TASKS);
94+
assert_eq!(COUNTER.load(Ordering::Acquire), NUM_TASKS);
9595
assert!(!current().in_wait_queue());
9696
WQ2.notify_all(true); // WQ2.wait()
9797

9898
println!(
9999
"task {:?} is waiting for tasks to finish...",
100100
current().id()
101101
);
102-
WQ1.wait_until(|| COUNTER.load(Ordering::Relaxed) == 0);
103-
assert_eq!(COUNTER.load(Ordering::Relaxed), 0);
102+
WQ1.wait_until(|| COUNTER.load(Ordering::Acquire) == 0);
103+
assert_eq!(COUNTER.load(Ordering::Acquire), 0);
104104
assert!(!current().in_wait_queue());
105105
}
106106

modules/axtask/src/wait_queue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ use crate::{AxTaskRef, CurrentTask, current_run_queue, select_run_queue};
2121
/// axtask::init_scheduler();
2222
/// // spawn a new task that updates `VALUE` and notifies the main task
2323
/// axtask::spawn(|| {
24-
/// assert_eq!(VALUE.load(Ordering::Relaxed), 0);
25-
/// VALUE.fetch_add(1, Ordering::Relaxed);
24+
/// assert_eq!(VALUE.load(Ordering::Acquire), 0);
25+
/// VALUE.fetch_add(1, Ordering::Release);
2626
/// WQ.notify_one(true); // wake up the main task
2727
/// });
2828
///
2929
/// WQ.wait(); // block until `notify()` is called
30-
/// assert_eq!(VALUE.load(Ordering::Relaxed), 1);
30+
/// assert_eq!(VALUE.load(Ordering::Acquire), 1);
3131
/// ```
3232
pub struct WaitQueue {
3333
queue: SpinNoIrq<VecDeque<AxTaskRef>>,

tools/raspi4/chainloader/src/bsp/raspberrypi/driver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ fn driver_gpio() -> Result<(), &'static str> {
5959
/// See child function calls.
6060
pub unsafe fn init() -> Result<(), &'static str> {
6161
static INIT_DONE: AtomicBool = AtomicBool::new(false);
62-
if INIT_DONE.load(Ordering::Relaxed) {
62+
if INIT_DONE.load(Ordering::Acquire) {
6363
return Err("Init already done");
6464
}
6565

6666
driver_uart()?;
6767
driver_gpio()?;
6868

69-
INIT_DONE.store(true, Ordering::Relaxed);
69+
INIT_DONE.store(true, Ordering::Release);
7070
Ok(())
7171
}

tools/raspi4/chainloader/src/panic_wait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ fn panic_prevent_reenter() {
3131

3232
static PANIC_IN_PROGRESS: AtomicBool = AtomicBool::new(false);
3333

34-
if !PANIC_IN_PROGRESS.load(Ordering::Relaxed) {
35-
PANIC_IN_PROGRESS.store(true, Ordering::Relaxed);
34+
if !PANIC_IN_PROGRESS.load(Ordering::Acquire) {
35+
PANIC_IN_PROGRESS.store(true, Ordering::Release);
3636

3737
return;
3838
}

0 commit comments

Comments
 (0)