You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2022/09/29 07:34:34 UTC
[mynewt-core] branch master updated: baselibc: Add option to have thread-safe heap
This is an automated email from the ASF dual-hosted git repository.
jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
The following commit(s) were added to refs/heads/master by this push:
new 76c01ffae baselibc: Add option to have thread-safe heap
76c01ffae is described below
commit 76c01ffaec18d01cfebd59c60bd6e07c5134fc73
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Tue Sep 27 20:39:37 2022 +0200
baselibc: Add option to have thread-safe heap
baselibc has option to lock the heap so malloc/free/calloc
can be called from multi-threaded application.
This adds implementation of this using os_mutex.
When BASELIBC_THREAD_SAFE_HEAP_ALLOCATION is set to 1
baselibc has package initialization functions that puts
locking functionality in place.
For now to preserve compatibility this functionality
is not enabled by default leaving heap allocation
not thread unsafe.
---
libc/baselibc/pkg.yml | 3 +++
libc/baselibc/src/mynewt.c | 26 ++++++++++++++++++++++++++
libc/baselibc/syscfg.yml | 5 +++++
3 files changed, 34 insertions(+)
diff --git a/libc/baselibc/pkg.yml b/libc/baselibc/pkg.yml
index 5c5a74230..1ec133b1e 100644
--- a/libc/baselibc/pkg.yml
+++ b/libc/baselibc/pkg.yml
@@ -27,3 +27,6 @@ pkg.deps:
- "@apache-mynewt-core/kernel/os"
pkg.req_apis:
- console
+
+pkg.init.BASELIBC_THREAD_SAFE_HEAP_ALLOCATION:
+ baselibc_init: 0
diff --git a/libc/baselibc/src/mynewt.c b/libc/baselibc/src/mynewt.c
index 056c7cfe5..99b451335 100644
--- a/libc/baselibc/src/mynewt.c
+++ b/libc/baselibc/src/mynewt.c
@@ -19,6 +19,8 @@
#include <stdio.h>
#include <console/console.h>
+#include <os/os_time.h>
+#include <os/os_mutex.h>
static size_t
stdin_read(FILE *fp, char *bp, size_t n)
@@ -45,3 +47,27 @@ static struct File _stdin = {
struct File *const stdin = &_stdin;
struct File *const stdout = &_stdin;
struct File *const stderr = &_stdin;
+
+#if MYNEWT_VAL(BASELIBC_THREAD_SAFE_HEAP_ALLOCATION)
+
+static struct os_mutex heap_mutex;
+
+bool heap_lock(void)
+{
+ return os_mutex_pend(&heap_mutex, OS_TIMEOUT_NEVER) == 0;
+}
+
+void heap_unlock(void)
+{
+ os_mutex_release(&heap_mutex);
+}
+
+void
+baselibc_init(void)
+{
+ /* Setup mutex to be used for malloc/calloc/free */
+ os_mutex_init(&heap_mutex);
+ set_malloc_locking(heap_lock, heap_unlock);
+}
+
+#endif
diff --git a/libc/baselibc/syscfg.yml b/libc/baselibc/syscfg.yml
index c300f0ba7..9820d8ed3 100644
--- a/libc/baselibc/syscfg.yml
+++ b/libc/baselibc/syscfg.yml
@@ -36,3 +36,8 @@ syscfg.defs:
It is still possible to use C++ code when this is set to 0 but
global variable can be in wrong state.
value: 1
+
+ BASELIBC_THREAD_SAFE_HEAP_ALLOCATION:
+ description: >
+ Set to 1 if project requires malloc/calloc/free to be thread safe.
+ value: 0