Skip to content

Commit 70cae5a

Browse files
vmframe: use an atomic usize for the refcount
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 77053c3 commit 70cae5a

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/aero_kernel/src/mem/paging/frame.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* along with Aero. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20+
use core::sync::atomic::{AtomicUsize, Ordering};
21+
2022
use alloc::vec::Vec;
2123
use bit_field::BitField;
2224
use spin::Once;
@@ -538,35 +540,31 @@ pub fn get_vm_frames() -> Option<&'static Vec<VmFrame>> {
538540
VM_FRAMES.get()
539541
}
540542

541-
struct VmFrameInner {
542-
use_count: usize,
543-
}
544-
545543
pub struct VmFrame {
546-
lock: Mutex<VmFrameInner>,
544+
ref_count: AtomicUsize,
547545
}
548546

549547
impl VmFrame {
550548
fn new() -> Self {
551549
Self {
552-
lock: Mutex::new(VmFrameInner { use_count: 0 }),
550+
ref_count: AtomicUsize::new(0),
553551
}
554552
}
555553

556554
pub fn dec_ref_count(&self) {
557-
let mut this = self.lock.lock();
555+
let ref_count = self.ref_count.load(Ordering::SeqCst);
558556

559-
if this.use_count > 0 {
560-
this.use_count -= 1;
557+
if ref_count != 0 {
558+
self.ref_count.store(ref_count - 1, Ordering::SeqCst);
561559
}
562560
}
563561

564562
pub fn inc_ref_count(&self) {
565-
self.lock.lock().use_count += 1;
563+
self.ref_count.fetch_add(1, Ordering::SeqCst);
566564
}
567565

568566
pub fn ref_count(&self) -> usize {
569-
self.lock.lock().use_count
567+
self.ref_count.load(Ordering::SeqCst)
570568
}
571569
}
572570

0 commit comments

Comments
 (0)