Skip to content

Commit

Permalink
Block until mutex is locked
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 29, 2023
1 parent b866506 commit 9567b5d
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions esp-wifi/src/compat/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,30 +353,33 @@ pub fn create_recursive_mutex() -> *mut crate::binary::c_types::c_void {
})
}

/// Lock a mutex. Block until successful.
pub fn lock_mutex(mutex: *mut crate::binary::c_types::c_void) -> i32 {
trace!("mutex_lock ptr = {:?}", mutex);

let ptr = mutex as *mut Mutex;
let current_task = current_task();

let mutex_locked = critical_section::with(|_| unsafe {
if (*ptr).count == 0 {
(*ptr).locking_pid = current_task;
(*ptr).count += 1;
true
} else if (*ptr).locking_pid == current_task {
(*ptr).count += 1;
true
} else {
false
loop {
let mutex_locked = critical_section::with(|_| unsafe {
if (*ptr).count == 0 {
(*ptr).locking_pid = current_task;
(*ptr).count += 1;
true
} else if (*ptr).locking_pid == current_task {
(*ptr).count += 1;
true
} else {
false
}
});
memory_fence();

if mutex_locked {
return 1;
}
});
memory_fence();

if mutex_locked {
1
} else {
0
yield_task();
}
}

Expand Down

0 comments on commit 9567b5d

Please sign in to comment.