3
3
use core:: ops:: Deref ;
4
4
5
5
use alloc:: { collections:: btree_map:: BTreeMap , sync:: Arc } ;
6
+ use axsync:: Mutex ;
6
7
use axtask:: { TaskExtRef , WaitQueue , current} ;
7
8
8
9
/// A table mapping memory addresses to futex wait queues.
9
- #[ derive( Default ) ]
10
- pub struct FutexTable ( BTreeMap < usize , Arc < WaitQueue > > ) ;
10
+ pub struct FutexTable ( Mutex < BTreeMap < usize , Arc < WaitQueue > > > ) ;
11
11
impl FutexTable {
12
+ /// Creates a new `FutexTable`.
13
+ pub fn new ( ) -> Self {
14
+ Self ( Mutex :: new ( BTreeMap :: new ( ) ) )
15
+ }
16
+
12
17
/// Gets the wait queue associated with the given address.
13
18
pub fn get ( & self , addr : usize ) -> Option < WaitQueueGuard > {
14
- let wq = self . 0 . get ( & addr) . cloned ( ) ?;
19
+ let wq = self . 0 . lock ( ) . get ( & addr) . cloned ( ) ?;
15
20
Some ( WaitQueueGuard {
16
21
key : addr,
17
22
inner : wq,
@@ -20,9 +25,9 @@ impl FutexTable {
20
25
21
26
/// Gets the wait queue associated with the given address, or inserts a a
22
27
/// new one if it doesn't exist.
23
- pub fn get_or_insert ( & mut self , addr : usize ) -> WaitQueueGuard {
24
- let wq = self
25
- . 0
28
+ pub fn get_or_insert ( & self , addr : usize ) -> WaitQueueGuard {
29
+ let mut table = self . 0 . lock ( ) ;
30
+ let wq = table
26
31
. entry ( addr)
27
32
. or_insert_with ( || Arc :: new ( WaitQueue :: new ( ) ) ) ;
28
33
WaitQueueGuard {
@@ -47,9 +52,9 @@ impl Deref for WaitQueueGuard {
47
52
impl Drop for WaitQueueGuard {
48
53
fn drop ( & mut self ) {
49
54
let curr = current ( ) ;
50
- let mut table = curr. task_ext ( ) . process_data ( ) . futex_table . lock ( ) ;
55
+ let mut table = curr. task_ext ( ) . process_data ( ) . futex_table . 0 . lock ( ) ;
51
56
if Arc :: strong_count ( & self . inner ) == 1 && self . inner . is_empty ( ) {
52
- table. 0 . remove ( & self . key ) ;
57
+ table. remove ( & self . key ) ;
53
58
}
54
59
}
55
60
}
0 commit comments