Skip to content

Commit

Permalink
[nrf fromtree] [zephyr] Use native locking primitives (project-chip#2…
Browse files Browse the repository at this point in the history
…4231)

* [zephyr] Use native locking primitives

Switch from Zephyr POSIX layer to Zephyr native locking
primitives to reduce the memory overhead.

* Code review

(cherry picked from commit ac82038)
  • Loading branch information
Damian-Nordic committed Feb 21, 2023
1 parent 9f722d2 commit 64909f2
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 48 deletions.
4 changes: 4 additions & 0 deletions config/nrfconnect/chip-module/Kconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ config NEWLIB_LIBC_NANO
bool
default y

config PTHREAD_IPC
bool
default n

# Generic networking options
config NET_SOCKETS_POSIX_NAMES
default n
Expand Down
3 changes: 2 additions & 1 deletion src/ble/BLEEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
#pragma once

#include <system/SystemLayer.h>
#include <system/SystemMutex.h>

#include <ble/BleRole.h>
#include <ble/BtpEngine.h>

#if CHIP_ENABLE_CHIPOBLE_TEST
#include <ble/BtpEngineTest.h>
#include <system/SystemMutex.h>
#endif

namespace chip {
Expand Down
4 changes: 0 additions & 4 deletions src/platform/Zephyr/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ struct ChipDeviceEvent;

// ==================== Platform Adaptations ====================

#define CHIP_SYSTEM_CONFIG_POSIX_LOCKING 1
#define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING 0
#define CHIP_SYSTEM_CONFIG_NO_LOCKING 0

#ifndef CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
#define CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS 0
#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
Expand Down
4 changes: 0 additions & 4 deletions src/platform/nrfconnect/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ struct ChipDeviceEvent;

// ==================== Platform Adaptations ====================

#define CHIP_SYSTEM_CONFIG_POSIX_LOCKING 1
#define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING 0
#define CHIP_SYSTEM_CONFIG_NO_LOCKING 0

#ifndef CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
#define CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS 0
#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
Expand Down
4 changes: 0 additions & 4 deletions src/platform/telink/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ struct ChipDeviceEvent;

// ==================== Platform Adaptations ====================

#define CHIP_SYSTEM_CONFIG_POSIX_LOCKING 1
#define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING 0
#define CHIP_SYSTEM_CONFIG_NO_LOCKING 0

#ifndef CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
#define CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS 0
#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS
Expand Down
2 changes: 2 additions & 0 deletions src/system/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ buildconfig_header("system_buildconfig") {
chip_system_config_mbed_locking = chip_system_config_locking == "mbed"
chip_system_config_cmsis_rtos_locking =
chip_system_config_locking == "cmsis-rtos"
chip_system_config_zephyr_locking = chip_system_config_locking == "zephyr"
chip_system_config_no_locking = chip_system_config_locking == "none"
have_clock_gettime = chip_system_config_clock == "clock_gettime"
have_clock_settime = have_clock_gettime
Expand All @@ -85,6 +86,7 @@ buildconfig_header("system_buildconfig") {
"CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING=${chip_system_config_freertos_locking}",
"CHIP_SYSTEM_CONFIG_MBED_LOCKING=${chip_system_config_mbed_locking}",
"CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING=${chip_system_config_cmsis_rtos_locking}",
"CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING=${chip_system_config_zephyr_locking}",
"CHIP_SYSTEM_CONFIG_NO_LOCKING=${chip_system_config_no_locking}",
"CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS=${chip_system_config_provide_statistics}",
"HAVE_CLOCK_GETTIME=${have_clock_gettime}",
Expand Down
58 changes: 32 additions & 26 deletions src/system/SystemConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,20 @@
#define CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING 0
#endif /* CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING */

/**
* @def CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING
*
* @brief
* Use Zephyr native locking primitives.
*
* This is recommended and enabled by default for Zephyr RTOS.
* Alternatively, you can use CHIP_SYSTEM_CONFIG_POSIX_LOCKING and Zephyr POSIX compatibility
* layer, but that solution has higher memory overhead.
*/
#ifndef CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING
#define CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING 0
#endif /* CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING */

/**
* @def CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
*
Expand All @@ -254,37 +268,29 @@
#define CHIP_SYSTEM_CONFIG_NO_LOCKING 0
#endif /* CHIP_SYSTEM_CONFIG_NO_LOCKING */

#if !(CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING ||CHIP_SYSTEM_CONFIG_NO_LOCKING)
#error "REQUIRED: CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING"
#endif // !(CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING)

#if CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING)
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING)"
#endif // CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING)

#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING
#if !(CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)
#error "REQUIRED: CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING"
#endif // !(CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)

#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_MBED_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_MBED_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_MBED_LOCKING
#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING && (CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_POSIX_LOCKING && (CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)"
#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING && (CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)

#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_MBED_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_MBED_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_MBED_LOCKING
#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && (CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && (CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)"
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && (CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)

#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#if CHIP_SYSTEM_CONFIG_MBED_LOCKING && (CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_MBED_LOCKING && (CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)"
#endif // CHIP_SYSTEM_CONFIG_MBED_LOCKING && (CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)

#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#if CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING && (CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING && (CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)"
#endif // CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING && (CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)

#if CHIP_SYSTEM_CONFIG_MBED_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_MBED_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_MBED_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#if CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING && CHIP_SYSTEM_CONFIG_NO_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING && CHIP_SYSTEM_CONFIG_NO_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING && CHIP_SYSTEM_CONFIG_NO_LOCKING

/**
* @def CHIP_SYSTEM_HEADER_RESERVE_SIZE
Expand Down
35 changes: 28 additions & 7 deletions src/system/SystemMutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
#include <system/SystemConfig.h>

// Include dependent headers
#include <system/SystemError.h>

#include <lib/support/CodeUtils.h>
#include <lib/support/DLLUtil.h>
#include <system/SystemError.h>

#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING
#include <pthread.h>
Expand All @@ -56,6 +56,10 @@
#include <cmsis_os2.h>
#endif // CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING
#include <zephyr/kernel.h>
#endif

namespace chip {
namespace System {

Expand All @@ -73,8 +77,7 @@ namespace System {
class DLL_EXPORT Mutex
{
public:
Mutex();
~Mutex();
Mutex() = default;

static CHIP_ERROR Init(Mutex & aMutex);
#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING
Expand Down Expand Up @@ -109,13 +112,14 @@ class DLL_EXPORT Mutex
osMutexId_t mCmsisRTOSMutex;
#endif // CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING
k_mutex mZephyrMutex;
#endif // CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING

Mutex(const Mutex &) = delete;
Mutex & operator=(const Mutex &) = delete;
};

inline Mutex::Mutex() {}
inline Mutex::~Mutex() {}

#if CHIP_SYSTEM_CONFIG_NO_LOCKING
inline CHIP_ERROR Init(Mutex & aMutex)
{
Expand Down Expand Up @@ -163,5 +167,22 @@ inline void Mutex::Unlock(void)
}
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING
inline CHIP_ERROR Mutex::Init(Mutex & aMutex)
{
return System::MapErrorZephyr(k_mutex_init(&aMutex.mZephyrMutex));
}

inline void Mutex::Lock()
{
VerifyOrDie(0 == k_mutex_lock(&mZephyrMutex, K_FOREVER));
}

inline void Mutex::Unlock(void)
{
VerifyOrDie(0 == k_mutex_unlock(&mZephyrMutex));
}
#endif // CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING

} // namespace System
} // namespace chip
7 changes: 5 additions & 2 deletions src/system/system.gni
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ if (chip_system_config_locking == "") {
chip_system_config_locking = "mbed"
} else if (current_os == "cmsis-rtos") {
chip_system_config_locking = "cmsis-rtos"
} else if (current_os == "zephyr") {
chip_system_config_locking = "zephyr"
} else if (chip_system_config_use_dispatch == false) {
chip_system_config_locking = "posix"
} else {
Expand All @@ -71,8 +73,9 @@ assert(
chip_system_config_locking == "freertos" ||
chip_system_config_locking == "none" ||
chip_system_config_locking == "mbed" ||
chip_system_config_locking == "cmsis-rtos",
"Please select a valid mutex implementation: posix, freertos, mbed, cmsis-rtos, none")
chip_system_config_locking == "cmsis-rtos" ||
chip_system_config_locking == "zephyr",
"Please select a valid mutex implementation: posix, freertos, mbed, cmsis-rtos, zephyr, none")

assert(
chip_system_config_clock == "clock_gettime" ||
Expand Down

0 comments on commit 64909f2

Please sign in to comment.