Skip to content

Commit

Permalink
Merge 7497f96 into c1ca759
Browse files Browse the repository at this point in the history
  • Loading branch information
tx2rx authored Sep 21, 2023
2 parents c1ca759 + 7497f96 commit 1533577
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 13 deletions.
29 changes: 29 additions & 0 deletions src/platform/ASR/SystemPlatformMutex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <lega_rtos_api.h>

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

#define platform_mutex_t lega_mutex_t
#define platform_init_mutex lega_rtos_init_mutex
#define platform_lock_mutex lega_rtos_lock_mutex
#define platform_unlock_mutex lega_rtos_unlock_mutex
#define platform_delay_milliseconds lega_rtos_delay_milliseconds
#define PLATFORM_WAIT_FOREVER LEGA_WAIT_FOREVER
2 changes: 2 additions & 0 deletions src/platform/ASR/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ chip_build_tests = false
chip_inet_config_enable_ipv4 = true
chip_inet_config_enable_tcp_endpoint = true
chip_inet_config_enable_udp_endpoint = true

chip_system_config_locking = "platform"
4 changes: 4 additions & 0 deletions src/platform/device.gni
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ if (chip_device_platform != "external") {
chip_platform_config_include = ""
chip_inet_platform_config_include = ""
chip_system_platform_config_include = ""
chip_system_platform_mutex_include = ""
chip_system_layer_impl_config_file = ""
} else {
declare_args() {
Expand All @@ -190,6 +191,7 @@ if (chip_device_platform != "external") {
chip_platform_config_include = ""
chip_inet_platform_config_include = ""
chip_system_platform_config_include = ""
chip_system_platform_mutex_include = ""
chip_system_layer_impl_config_file = ""
}
}
Expand Down Expand Up @@ -217,6 +219,8 @@ if (_chip_device_layer != "none" && chip_device_platform != "external") {
"<platform/" + _chip_device_layer + "/InetPlatformConfig.h>"
chip_system_platform_config_include =
"<platform/" + _chip_device_layer + "/SystemPlatformConfig.h>"
chip_system_platform_mutex_include =
"<platform/" + _chip_device_layer + "/SystemPlatformMutex.h>"
}

declare_args() {
Expand Down
7 changes: 7 additions & 0 deletions src/system/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ buildconfig_header("system_buildconfig") {
config_device_layer = chip_device_platform != "none"
chip_system_config_posix_locking = chip_system_config_locking == "posix"
chip_system_config_freertos_locking = chip_system_config_locking == "freertos"
chip_system_config_platform_locking = chip_system_config_locking == "platform"
chip_system_config_mbed_locking = chip_system_config_locking == "mbed"
chip_system_config_cmsis_rtos_locking =
chip_system_config_locking == "cmsis-rtos"
Expand All @@ -94,6 +95,7 @@ buildconfig_header("system_buildconfig") {
"CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK=false",
"CHIP_SYSTEM_CONFIG_POSIX_LOCKING=${chip_system_config_posix_locking}",
"CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING=${chip_system_config_freertos_locking}",
"CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING=${chip_system_config_platform_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}",
Expand Down Expand Up @@ -127,6 +129,11 @@ buildconfig_header("system_buildconfig") {
"SYSTEM_PLATFORM_CONFIG_INCLUDE=${chip_system_platform_config_include}",
]
}
if (chip_system_platform_mutex_include != "") {
defines += [
"SYSTEM_PLATFORM_MUTEX_INCLUDE=${chip_system_platform_mutex_include}",
]
}
if (chip_enable_thread_safety_checks) {
defines += [ "SYSTEM_ENABLE_CLANG_THREAD_SAFETY_ANALYSIS=1" ]
}
Expand Down
42 changes: 32 additions & 10 deletions src/system/SystemConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@
#define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING INET_CONFIG_FREERTOS_LOCKING
#endif // !defined(CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING) && defined(INET_CONFIG_FREERTOS_LOCKING)

#if !defined(CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING) && defined(INET_CONFIG_PLATFORM_LOCKING)
#define CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING INET_CONFIG_PLATFORM_LOCKING
#endif // !defined(CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING) && defined(INET_CONFIG_PLATFORM_LOCKING)

#if !defined(CHIP_SYSTEM_CONFIG_MBED_LOCKING) && defined(INET_CONFIG_MBED_LOCKING)
#define CHIP_SYSTEM_CONFIG_MBED_LOCKING INET_CONFIG_MBED_LOCKING
#endif // !defined(CHIP_SYSTEM_CONFIG_MBED_LOCKING) && defined(INET_CONFIG_MBED_LOCKING)
Expand Down Expand Up @@ -204,6 +208,20 @@
#define CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING 0
#endif /* CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING */

/**
* @def CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING
*
* @brief
* Use PLATFORM locking.
*
* This should be generally asserted (1) for platform customization.
*
* However, if you are simulating an LwIP-based system atop POSIX threads and BSD sockets, this should also be deasserted (0).
*/
#ifndef CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING
#define CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING 0
#endif /* CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING */

/**
* @def CHIP_SYSTEM_CONFIG_MBED_LOCKING
*
Expand Down Expand Up @@ -262,23 +280,27 @@
* @brief
* Disable the use of locking within the system layer.
*
* This value is mutually exclusive with CHIP_SYSTEM_CONFIG_POSIX_LOCKING and CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING and CHIP_SYSTEM_CONFIG_MBED_LOCKING.
* This value is mutually exclusive with CHIP_SYSTEM_CONFIG_POSIX_LOCKING and CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING and CHIP_SYSTEM_CONFIG_MBED_LOCKING and CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING.
*/
#ifndef CHIP_SYSTEM_CONFIG_NO_LOCKING
#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_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_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING || CHIP_SYSTEM_CONFIG_PLATFORM_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 || CHIP_SYSTEM_CONFIG_PLATFORM_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 || CHIP_SYSTEM_CONFIG_PLATFORM_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 || CHIP_SYSTEM_CONFIG_PLATFORM_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 || CHIP_SYSTEM_CONFIG_PLATFORM_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 || CHIP_SYSTEM_CONFIG_PLATFORM_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 || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_ZEPHYR_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING || CHIP_SYSTEM_CONFIG_PLATFORM_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 || CHIP_SYSTEM_CONFIG_PLATFORM_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 || CHIP_SYSTEM_CONFIG_PLATFORM_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_PLATFORM_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_PLATFORM_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_PLATFORM_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_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)"
Expand Down
36 changes: 36 additions & 0 deletions src/system/SystemMutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,42 @@ DLL_EXPORT void Mutex::Lock(void)
}
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING
DLL_EXPORT CHIP_ERROR Mutex::Init(Mutex & aThis)
{
restart:
if (__sync_bool_compare_and_swap(&aThis.mInitialized, 0, 1))
{
platform_init_mutex((platform_mutex_t *) &aThis.mSemaphore);
if (aThis.mSemaphore == nullptr)
{
aThis.mInitialized = 0;

return CHIP_ERROR_NO_MEMORY;
}
}
else
{
while (aThis.mSemaphore == nullptr)
{
platform_delay_milliseconds(1);

if (aThis.mInitialized == 0)
{
goto restart;
}
}
}

return CHIP_NO_ERROR;
}

DLL_EXPORT void Mutex::Lock(void)
{
platform_lock_mutex((platform_mutex_t *) &this->mSemaphore, PLATFORM_WAIT_FOREVER);
}
#endif // CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING

#if CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
DLL_EXPORT CHIP_ERROR Mutex::Init(Mutex & aThis)
{
Expand Down
18 changes: 17 additions & 1 deletion src/system/SystemMutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
#endif
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING
#include SYSTEM_PLATFORM_MUTEX_INCLUDE
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_MBED_LOCKING
#include <rtos/Mutex.h>
#endif // CHIP_SYSTEM_CONFIG_MBED_LOCKING
Expand Down Expand Up @@ -112,7 +116,7 @@ class DLL_EXPORT CHIP_CAPABILITY("mutex") Mutex
Mutex() = default;

static CHIP_ERROR Init(Mutex & aMutex);
#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING
#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING
inline bool isInitialized() { return mInitialized; }
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING

Expand All @@ -136,6 +140,11 @@ class DLL_EXPORT CHIP_CAPABILITY("mutex") Mutex
volatile bool mInitialized = 0;
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING
volatile platform_mutex_t mSemaphore = nullptr;
volatile bool mInitialized = 0;
#endif // CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING

#if CHIP_SYSTEM_CONFIG_MBED_LOCKING
rtos::Mutex mMbedMutex;
#endif // CHIP_SYSTEM_CONFIG_MBED_LOCKING
Expand Down Expand Up @@ -180,6 +189,13 @@ inline void Mutex::Unlock(void)
}
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING
inline void Mutex::Unlock(void)
{
platform_unlock_mutex((platform_mutex_t *) &this->mSemaphore);
}
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_MBED_LOCKING
inline CHIP_ERROR Mutex::Init(Mutex & aMutex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/system/SystemPacketBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ PacketBufferHandle PacketBufferHandle::New(size_t aAvailableSize, uint16_t aRese
#elif CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_POOL

static_cast<void>(lBlockSize);
#if !CHIP_SYSTEM_CONFIG_NO_LOCKING && CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING
#if !CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_PLATFORM_LOCKING)
if (!sBufferPoolMutex.isInitialized())
{
Mutex::Init(sBufferPoolMutex);
Expand Down
3 changes: 2 additions & 1 deletion src/system/system.gni
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ if (chip_system_config_locking == "") {
assert(
chip_system_config_locking == "posix" ||
chip_system_config_locking == "freertos" ||
chip_system_config_locking == "platform" ||
chip_system_config_locking == "none" ||
chip_system_config_locking == "mbed" ||
chip_system_config_locking == "cmsis-rtos" ||
chip_system_config_locking == "zephyr",
"Please select a valid mutex implementation: posix, freertos, mbed, cmsis-rtos, zephyr, none")
"Please select a valid mutex implementation: posix, freertos, platform, mbed, cmsis-rtos, zephyr, none")

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

0 comments on commit 1533577

Please sign in to comment.