update atomic function and workaround the LLVM compiling bug(maybe)

master
chenqiuhao 6 years ago
parent 0a81014007
commit 9474ad7220

@ -69,4 +69,27 @@ pub unsafe extern fn __atomic_compare_exchange_1(dst: *mut u8, expected: *mut u8
#[no_mangle] #[no_mangle]
pub unsafe extern fn __atomic_compare_exchange_4(dst: *mut u32, expected: *mut u32, desired: u32) -> bool { pub unsafe extern fn __atomic_compare_exchange_4(dst: *mut u32, expected: *mut u32, desired: u32) -> bool {
__atomic_compare_exchange(dst, expected, desired) __atomic_compare_exchange(dst, expected, desired)
}
#[no_mangle]
pub unsafe extern fn __atomic_fetch_add_4(dst: *mut u32, delta: u32) -> u32 {
use super::interrupt;
let flags = interrupt::disable_and_store();
let val = read(dst);
let new_val = val + delta;
write(dst, new_val);
interrupt::restore(flags);
val
}
#[no_mangle]
pub unsafe extern fn __atomic_fetch_sub_4(dst: *mut u32, delta: u32) -> u32 {
use super::interrupt;
let flags = interrupt::disable_and_store();
let val = read(dst);
let new_val = val - delta;
write(dst, new_val);
interrupt::restore(flags);
val
} }

@ -69,8 +69,8 @@ pub fn kmain() -> ! {
// the test is not supported in riscv32(maybe) // the test is not supported in riscv32(maybe)
//thread::test::local_key(); //thread::test::local_key();
//thread::test::unpack(); //thread::test::unpack();
//sync::test::philosopher_using_mutex(); sync::test::philosopher_using_mutex();
//sync::test::philosopher_using_monitor(); sync::test::philosopher_using_monitor();
//sync::mpsc::test::test_all(); //sync::mpsc::test::test_all();
// come into shell // come into shell

@ -96,11 +96,17 @@ impl<T, S: MutexSupport> Mutex<T, S>
impl<T: ?Sized, S: MutexSupport> Mutex<T, S> impl<T: ?Sized, S: MutexSupport> Mutex<T, S>
{ {
fn obtain_lock(&self) { fn obtain_lock(&self) {
while self.lock.compare_and_swap(false, true, Ordering::Acquire) != false { while true {
// Wait until the lock looks unlocked before retrying match self.lock.compare_exchange(false, true, Ordering::Acquire, Ordering::Acquire) {
while self.lock.load(Ordering::Relaxed) { Ok(X) => break,
self.support.cpu_relax(); Err(X) => {
} // Wait until the lock looks unlocked before retrying
while self.lock.load(Ordering::Relaxed) {
self.support.cpu_relax();
}
},
};
} }
} }
@ -144,13 +150,13 @@ impl<T: ?Sized, S: MutexSupport> Mutex<T, S>
/// a guard within Some. /// a guard within Some.
pub fn try_lock(&self) -> Option<MutexGuard<T, S>> { pub fn try_lock(&self) -> Option<MutexGuard<T, S>> {
let support_guard = S::before_lock(); let support_guard = S::before_lock();
if self.lock.compare_and_swap(false, true, Ordering::Acquire) == false { match self.lock.compare_exchange(false, true, Ordering::Acquire, Ordering::Acquire) {
Some(MutexGuard { Ok(X) =>
mutex: self, Some(MutexGuard {
support_guard, mutex: self,
}) support_guard,
} else { }),
None Err(X) => None,
} }
} }
} }

Loading…
Cancel
Save