Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create mutexes in heap memory #2202

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions esp-wifi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Feature `sys-logs` doesn't break the build anymore (#2117)
- Fixed a panic when overflow-checks are enabled (#2164)
- Create mutexes in heap memory, fixes running out of mutexes when connecting and disconnecting to a WPA2-ENTERPRISE ap multiple times (#2202)

### Removed

Expand Down
54 changes: 24 additions & 30 deletions esp-wifi/src/compat/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ struct Mutex {
recursive: bool,
}

static mut MUTEXES: [Mutex; 10] = [Mutex {
locking_pid: 0xffff_ffff,
count: 0,
recursive: false,
}; 10];
static mut MUTEX_IDX_CURRENT: usize = 0;

/// A naive and pretty much unsafe queue to back the queues used in drivers and
/// supplicant code
struct RawQueue {
Expand Down Expand Up @@ -117,33 +110,21 @@ unsafe extern "C" fn strnlen(chars: *const u8, maxlen: usize) -> usize {
}

pub fn sem_create(max: u32, init: u32) -> *mut c_void {
critical_section::with(|_| unsafe {
unsafe {
let ptr = malloc(4) as *mut u32;
ptr.write_volatile(init);

trace!("sem created res = {:?}", ptr);
ptr.cast()
})
}
}

pub fn sem_delete(semphr: *mut c_void) {
trace!(">>> sem delete");

// TODO remove this once fixed in esp_supplicant AND we updated to the fixed -
// JIRA: WIFI-6676
unsafe {
MabezDev marked this conversation as resolved.
Show resolved Hide resolved
if semphr as usize > addr_of!(MUTEXES) as usize
&& semphr as usize
<= unsafe { addr_of!(MUTEXES).byte_add(size_of_val(&*addr_of!(MUTEXES))) } as usize
{
warn!("trying to remove a mutex via sem_delete");
return;
}
}

critical_section::with(|_| unsafe {
free(semphr.cast());
})
}
}

pub fn sem_take(semphr: *mut c_void, tick: u32) -> i32 {
Expand Down Expand Up @@ -200,14 +181,27 @@ pub fn thread_sem_get() -> *mut c_void {
}

pub fn create_recursive_mutex() -> *mut c_void {
critical_section::with(|_| unsafe {
let ptr = &mut MUTEXES[MUTEX_IDX_CURRENT] as *mut Mutex;
(*ptr).recursive = true;
MUTEX_IDX_CURRENT += 1;
memory_fence();
trace!("recursive_mutex_create called {:?}", ptr);
ptr as *mut c_void
})
let mutex = Mutex {
locking_pid: 0xffff_ffff,
count: 0,
recursive: true,
};

let ptr = unsafe { malloc(size_of_val(&mutex) as u32) as *mut Mutex };
unsafe {
ptr.write(mutex);
}
memory_fence();
MabezDev marked this conversation as resolved.
Show resolved Hide resolved

trace!("recursive_mutex_create called {:?}", ptr);
ptr as *mut c_void
}

pub fn mutex_delete(mutex: *mut c_void) {
let ptr = mutex as *mut Mutex;
unsafe {
free(mutex.cast());
}
}

/// Lock a mutex. Block until successful.
Expand Down
4 changes: 2 additions & 2 deletions esp-wifi/src/wifi/os_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ pub unsafe extern "C" fn recursive_mutex_create() -> *mut crate::binary::c_types
/// None
///
/// *************************************************************************
pub unsafe extern "C" fn mutex_delete(_mutex: *mut crate::binary::c_types::c_void) {
todo!("mutex_delete")
pub unsafe extern "C" fn mutex_delete(mutex: *mut crate::binary::c_types::c_void) {
crate::compat::common::mutex_delete(mutex);
}

/// **************************************************************************
Expand Down
Loading