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

Fix newlib malloc thread safety issue #35227

Merged
merged 3 commits into from May 13, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/libc/newlib/libc-hooks.c
Expand Up @@ -16,6 +16,7 @@
#include <app_memory/app_memdomain.h>
#include <init.h>
#include <sys/sem.h>
#include <sys/mutex.h>
#include <sys/mem_manage.h>

#define LIBC_BSS K_APP_BMEM(z_libc_partition)
Expand Down Expand Up @@ -291,6 +292,18 @@ void *_sbrk(intptr_t count)
}
__weak FUNC_ALIAS(_sbrk, sbrk, void *);

static LIBC_DATA SYS_MUTEX_DEFINE(heap_mutex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When CONFIG_USERSPACE=n, making this a spinlock would be much smaller/faster. But they I guess someday we'll get around to implementing a futex backend for sys_mutex which would have the same property.


void __malloc_lock(struct _reent *reent)
{
sys_mutex_lock(&heap_mutex, K_FOREVER);
}

void __malloc_unlock(struct _reent *reent)
{
sys_mutex_unlock(&heap_mutex);
}

__weak int *__errno(void)
{
return z_errno();
Expand Down