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

newlib: add thread safe implementation #8619

Closed
wants to merge 4 commits into from
Closed
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
7 changes: 7 additions & 0 deletions core/include/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
#ifndef THREAD_H
#define THREAD_H

#ifdef MODULE_NEWLIB_THREAD_SAFE
#include <sys/reent.h>
#endif

#include "clist.h"
#include "cib.h"
#include "msg.h"
Expand Down Expand Up @@ -168,6 +172,9 @@ struct _thread {
msg_t *msg_array; /**< memory holding messages sent
to this thread's message queue */
#endif
#if defined(MODULE_NEWLIB_THREAD_SAFE) || defined(DOXYGEN)
struct _reent newlib_reent; /**< thread's re-entrant object */
#endif
#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) \
|| defined(MODULE_MPU_STACK_GUARD) || defined(DOXYGEN)
char *stack_start; /**< thread's stack start address */
Expand Down
4 changes: 4 additions & 0 deletions core/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ int __attribute__((used)) sched_run(void)
mpu_enable();
#endif

#ifdef MODULE_NEWLIB_THREAD_SAFE
_impure_ptr = &(next_thread->newlib_reent);
#endif

DEBUG("sched_run: done, changed sched_active_thread.\n");

return 1;
Expand Down
8 changes: 8 additions & 0 deletions core/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

#include <errno.h>
#include <stdio.h>
#ifdef MODULE_NEWLIB_THREAD_SAFE
#include <string.h>
#endif

#include "assert.h"
#include "thread.h"
Expand Down Expand Up @@ -235,6 +238,11 @@ kernel_pid_t thread_create(char *stack, int stacksize, char priority, int flags,
thread->msg_array = NULL;
#endif

#ifdef MODULE_NEWLIB_THREAD_SAFE
/* initialize the reent context */
_REENT_INIT_PTR(&(cb->newlib_reent));
#endif

sched_num_threads++;

DEBUG("Created thread %s. PID: %" PRIkernel_pid ". Priority: %u.\n", name, thread->pid, priority);
Expand Down
8 changes: 8 additions & 0 deletions cpu/cortexm_common/include/cpu_conf_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@ extern "C" {
#define THREAD_EXTRA_STACKSIZE_PRINTF (512)
#endif
#ifndef THREAD_STACKSIZE_DEFAULT
#ifdef MODULE_NEWLIB_THREAD_SAFE
#define THREAD_STACKSIZE_DEFAULT (1152)
#else
#define THREAD_STACKSIZE_DEFAULT (1024)
#endif
#endif
#ifndef THREAD_STACKSIZE_IDLE
#ifdef MODULE_NEWLIB_THREAD_SAFE
#define THREAD_STACKSIZE_IDLE (384)
#else
#define THREAD_STACKSIZE_IDLE (256)
#endif
#endif
/** @} */

/**
Expand Down
3 changes: 3 additions & 0 deletions makefiles/arch/cortexm.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ include $(RIOTCPU)/cortexm_common/Makefile.include

# use the nano-specs of Newlib when available
USEMODULE += newlib_nano
# use thread-safe features
USEMODULE += newlib_thread_safe

# Avoid overriding the default rule:
all:

Expand Down
4 changes: 4 additions & 0 deletions sys/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ ifneq (,$(filter newlib_syscalls_default,$(USEMODULE)))
include $(RIOTBASE)/sys/newlib_syscalls_default/Makefile.include
endif

ifneq (,$(filter newlib_thread_safe,$(USEMODULE)))
include $(RIOTBASE)/sys/newlib_thread_safe/Makefile.include
endif

ifneq (,$(filter arduino,$(USEMODULE)))
include $(RIOTBASE)/sys/arduino/Makefile.include
endif
Expand Down
1 change: 1 addition & 0 deletions sys/newlib_thread_safe/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
26 changes: 26 additions & 0 deletions sys/newlib_thread_safe/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
UNDEF += $(BINDIR)/newlib_thread_safe/syscalls.o

# define the compile test files
define __has_reent_test
#include <sys/reent.h>\n
endef

define __has_reent_small_test
#include <sys/reent.h>\n
#ifndef _REENT_SMALL\n
#error wasting 1KB\n
#endif\n
endef

# compile the tests
__has_reent = $(shell printf "$(__has_reent_test)" | $(CC) -E $(CFLAGS) $(INCLUDES) -Wno-missing-include-dirs - > /dev/null 2>&1 ; echo $$?)
__has_reent_small = $(shell printf "$(__has_reent_small_test)" | $(CC) -E $(CFLAGS) $(INCLUDES) -Wno-missing-include-dirs - > /dev/null 2>&1 ; echo $$?)

# check if we use REENT_SMALL
ifeq (0, $(__has_reent))
ifneq (0, $(__has_reent_small))
$(error "MODULE_NEWLIB_THREAD_SAFE defined but _REENT_SMALL not defined")
endif
else
$(error "MODULE_NEWLIB_THREAD_SAFE defined but <sys/reent.h> not found in toolchain path")
endif
54 changes: 54 additions & 0 deletions sys/newlib_thread_safe/syscalls.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2018 OTA keys S.A.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup sys_newlib
* @{
*
* @file
* @brief Newlib system calls for thread safety
*
* @author Vincent Dupont <[email protected]>
*
* @}
*/

#include <sys/reent.h>

#include "rmutex.h"

static rmutex_t _env = RMUTEX_INIT;
static rmutex_t _malloc = RMUTEX_INIT;

void __env_lock(struct _reent *_r)
{
(void)_r;

rmutex_lock(&_env);
}

void __env_unlock(struct _reent *_r)
{
(void)_r;

rmutex_unlock(&_env);
}

void __malloc_lock(struct _reent *_r)
{
(void)_r;

rmutex_lock(&_malloc);
}

void __malloc_unlock(struct _reent *_r)
{
(void)_r;

rmutex_unlock(&_malloc);
}