diff options
author | Xin Li <delphij@google.com> | 2023-08-14 15:38:30 -0700 |
---|---|---|
committer | Xin Li <delphij@google.com> | 2023-08-14 15:38:30 -0700 |
commit | bddf63953e111d742b591c1c0c7c34bcda8a51c7 (patch) | |
tree | 3a93128bff4b737b24b0c9581922c0b20410f0f4 /pw_sync_baremetal | |
parent | ee890da55c82b95deca3518d5f3777e3d8ca9f0e (diff) | |
parent | fbb9890f8922aa55fde183655a0017e69127ea4b (diff) | |
download | pigweed-tmp_amf_298295554.tar.gz |
Merge Android U (ab/10368041)tmp_amf_298295554
Bug: 291102124
Merged-In: I10c41adb8fe3e126cfa4ff2f49b15863fff379de
Change-Id: I66f7a6cccaafc173d3924dae62a736c6c53520c7
Diffstat (limited to 'pw_sync_baremetal')
-rw-r--r-- | pw_sync_baremetal/Android.bp | 27 | ||||
-rw-r--r-- | pw_sync_baremetal/BUILD.bazel | 20 | ||||
-rw-r--r-- | pw_sync_baremetal/BUILD.gn | 26 | ||||
-rw-r--r-- | pw_sync_baremetal/CMakeLists.txt | 29 | ||||
-rw-r--r-- | pw_sync_baremetal/docs.rst | 37 | ||||
-rw-r--r-- | pw_sync_baremetal/public/pw_sync_baremetal/interrupt_spin_lock_inline.h | 2 | ||||
-rw-r--r-- | pw_sync_baremetal/public/pw_sync_baremetal/mutex_inline.h | 2 | ||||
-rw-r--r-- | pw_sync_baremetal/public/pw_sync_baremetal/recursive_mutex_inline.h | 46 | ||||
-rw-r--r-- | pw_sync_baremetal/public/pw_sync_baremetal/recursive_mutex_native.h | 23 | ||||
-rw-r--r-- | pw_sync_baremetal/public_overrides/pw_sync_backend/recursive_mutex_inline.h | 16 | ||||
-rw-r--r-- | pw_sync_baremetal/public_overrides/pw_sync_backend/recursive_mutex_native.h | 16 |
11 files changed, 227 insertions, 17 deletions
diff --git a/pw_sync_baremetal/Android.bp b/pw_sync_baremetal/Android.bp new file mode 100644 index 000000000..691bf7bcb --- /dev/null +++ b/pw_sync_baremetal/Android.bp @@ -0,0 +1,27 @@ +// Copyright 2022 The Pigweed 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 +// +// https://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. + +package { + default_applicable_licenses: ["external_pigweed_license"], +} + +cc_library_headers { + name: "pw_sync_baremetal_headers", + vendor_available: true, + export_include_dirs: [ + "public", + "public_overrides", + ], + host_supported: true, +} diff --git a/pw_sync_baremetal/BUILD.bazel b/pw_sync_baremetal/BUILD.bazel index e23c3c7a5..d401d99d7 100644 --- a/pw_sync_baremetal/BUILD.bazel +++ b/pw_sync_baremetal/BUILD.bazel @@ -71,3 +71,23 @@ pw_cc_library( "//pw_sync:mutex_facade", ], ) + +pw_cc_library( + name = "recursive_mutex", + hdrs = [ + "public/pw_sync_baremetal/recursive_mutex_inline.h", + "public/pw_sync_baremetal/recursive_mutex_native.h", + "public_overrides/pw_sync_backend/recursive_mutex_inline.h", + "public_overrides/pw_sync_backend/recursive_mutex_native.h", + ], + includes = [ + "public", + "public_overrides", + ], + target_compatible_with = ["@platforms//os:none"], + visibility = ["//pw_sync:__pkg__"], + deps = [ + "//pw_assert", + "//pw_sync:recursive_mutex_facade", + ], +) diff --git a/pw_sync_baremetal/BUILD.gn b/pw_sync_baremetal/BUILD.gn index e862556d8..a857c7af6 100644 --- a/pw_sync_baremetal/BUILD.gn +++ b/pw_sync_baremetal/BUILD.gn @@ -17,6 +17,7 @@ import("//build_overrides/pigweed.gni") import("$dir_pw_build/target_types.gni") import("$dir_pw_chrono/backend.gni") import("$dir_pw_docgen/docs.gni") +import("$dir_pw_unit_test/test.gni") config("public_include_path") { include_dirs = [ "public" ] @@ -74,6 +75,31 @@ pw_source_set("mutex") { ] } +# This target provides the backend for pw::sync::RecursiveMutex. +# The provided implementation makes a single attempt to acquire the lock and +# asserts if it is unavailable. This implementation is not yet set up to support +# hardware multi-threading (SMP, SMT, etc). +pw_source_set("recursive_mutex") { + public_configs = [ + ":public_include_path", + ":backend_config", + ] + public = [ + "public/pw_sync_baremetal/recursive_mutex_inline.h", + "public/pw_sync_baremetal/recursive_mutex_native.h", + "public_overrides/pw_sync_backend/recursive_mutex_inline.h", + "public_overrides/pw_sync_backend/recursive_mutex_native.h", + ] + public_deps = [ + "$dir_pw_assert", + "$dir_pw_sync:recursive_mutex.facade", + ] + visibility = [ "$dir_pw_sync:*" ] +} + pw_doc_group("docs") { sources = [ "docs.rst" ] } + +pw_test_group("tests") { +} diff --git a/pw_sync_baremetal/CMakeLists.txt b/pw_sync_baremetal/CMakeLists.txt new file mode 100644 index 000000000..574a35fa2 --- /dev/null +++ b/pw_sync_baremetal/CMakeLists.txt @@ -0,0 +1,29 @@ +# Copyright 2022 The Pigweed 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 +# +# https://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. + +include($ENV{PW_ROOT}/pw_build/pigweed.cmake) + +pw_add_library(pw_sync_baremetal.recursive_mutex INTERFACE + HEADERS + public/pw_sync_baremetal/recursive_mutex_inline.h + public/pw_sync_baremetal/recursive_mutex_native.h + public_overrides/pw_sync_backend/recursive_mutex_inline.h + public_overrides/pw_sync_backend/recursive_mutex_native.h + PUBLIC_INCLUDES + public + public_overrides + PUBLIC_DEPS + pw_assert + pw_sync.recursive_mutex.facade +) diff --git a/pw_sync_baremetal/docs.rst b/pw_sync_baremetal/docs.rst index 827679140..421af51a9 100644 --- a/pw_sync_baremetal/docs.rst +++ b/pw_sync_baremetal/docs.rst @@ -7,22 +7,29 @@ This is a set of backends for pw_sync that works on baremetal targets. It is not ready for use, and is under construction. .. note:: - All constructs in this baremetal backend do not support hardware multi-threading - (SMP, SMT, etc). + All constructs in this baremetal backend do not support hardware + multi-threading (SMP, SMT, etc). .. warning:: - It does not perform interrupt masking or disable global interrupts. This is not - safe to use yet! + It does not perform interrupt masking or disable global interrupts. This is + not safe to use yet! -------------------------------------- -pw_sync_baremetal's InterruptSpinLock -------------------------------------- -The interrupt spin-lock implementation makes a single attempt to acquire the lock -and asserts if it is unavailable. It does not perform interrupt masking or disable global -interrupts. +----------------- +InterruptSpinLock +----------------- +The interrupt spin-lock implementation makes a single attempt to acquire the +lock and asserts if it is unavailable. It does not perform interrupt masking or +disable global interrupts. -------------------------- -pw_sync_baremetal's Mutex -------------------------- -The mutex implementation makes a single attempt to acquire the lock and asserts if -it is unavailable. +----- +Mutex +----- +The mutex implementation makes a single attempt to acquire the lock and asserts +if it is unavailable. + +-------------- +RecursiveMutex +-------------- +The recursive mutex implementation counts the number of lock and unlock calls +and asserts if the mutex is unlocked too many times or destroyed while locked. +Note that recursive mutexes are not available for general use in Pigweed. diff --git a/pw_sync_baremetal/public/pw_sync_baremetal/interrupt_spin_lock_inline.h b/pw_sync_baremetal/public/pw_sync_baremetal/interrupt_spin_lock_inline.h index 505cef396..70b286bc2 100644 --- a/pw_sync_baremetal/public/pw_sync_baremetal/interrupt_spin_lock_inline.h +++ b/pw_sync_baremetal/public/pw_sync_baremetal/interrupt_spin_lock_inline.h @@ -24,7 +24,7 @@ constexpr InterruptSpinLock::InterruptSpinLock() : native_type_() {} inline void InterruptSpinLock::lock() { PW_ASSERT(try_lock()); } inline bool InterruptSpinLock::try_lock() { - // TODO(pwbug/303): Use the pw_interrupt API here to disable interrupts. + // TODO(b/235352722): Use the pw_interrupt API here to disable interrupts. return !native_type_.test_and_set(std::memory_order_acquire); } diff --git a/pw_sync_baremetal/public/pw_sync_baremetal/mutex_inline.h b/pw_sync_baremetal/public/pw_sync_baremetal/mutex_inline.h index 70a10c21e..a04306557 100644 --- a/pw_sync_baremetal/public/pw_sync_baremetal/mutex_inline.h +++ b/pw_sync_baremetal/public/pw_sync_baremetal/mutex_inline.h @@ -20,7 +20,7 @@ namespace pw::sync { inline Mutex::Mutex() : native_type_() {} -inline Mutex::~Mutex() {} +inline Mutex::~Mutex() = default; inline void Mutex::lock() { PW_ASSERT(try_lock()); } diff --git a/pw_sync_baremetal/public/pw_sync_baremetal/recursive_mutex_inline.h b/pw_sync_baremetal/public/pw_sync_baremetal/recursive_mutex_inline.h new file mode 100644 index 000000000..4a40e9b8f --- /dev/null +++ b/pw_sync_baremetal/public/pw_sync_baremetal/recursive_mutex_inline.h @@ -0,0 +1,46 @@ +// Copyright 2022 The Pigweed 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 +// +// https://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 <limits> + +#include "pw_assert/assert.h" +#include "pw_sync/recursive_mutex.h" + +namespace pw::sync { + +inline RecursiveMutex::RecursiveMutex() : native_type_(0) {} + +inline RecursiveMutex::~RecursiveMutex() = default; + +inline void RecursiveMutex::lock() { + try_lock(); // Locking always succeeds +} + +inline bool RecursiveMutex::try_lock() { + int lock_count = native_type_.fetch_add(1, std::memory_order_acquire); + PW_DASSERT(lock_count != std::numeric_limits<int>::max()); // Detect overflow + return true; // No threads, so you can always acquire a recursive mutex. +} + +inline void RecursiveMutex::unlock() { + int lock_count = native_type_.fetch_sub(1, std::memory_order_release); + PW_ASSERT(lock_count > 0); // Unlocked mutex that wasn't held +} + +inline RecursiveMutex::native_handle_type RecursiveMutex::native_handle() { + return native_type_; +} + +} // namespace pw::sync diff --git a/pw_sync_baremetal/public/pw_sync_baremetal/recursive_mutex_native.h b/pw_sync_baremetal/public/pw_sync_baremetal/recursive_mutex_native.h new file mode 100644 index 000000000..8fb87048a --- /dev/null +++ b/pw_sync_baremetal/public/pw_sync_baremetal/recursive_mutex_native.h @@ -0,0 +1,23 @@ +// Copyright 2022 The Pigweed 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 +// +// https://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 <atomic> + +namespace pw::sync::backend { + +using NativeRecursiveMutex = std::atomic<int>; +using NativeRecursiveMutexHandle = std::atomic<int>&; + +} // namespace pw::sync::backend diff --git a/pw_sync_baremetal/public_overrides/pw_sync_backend/recursive_mutex_inline.h b/pw_sync_baremetal/public_overrides/pw_sync_backend/recursive_mutex_inline.h new file mode 100644 index 000000000..db2784709 --- /dev/null +++ b/pw_sync_baremetal/public_overrides/pw_sync_backend/recursive_mutex_inline.h @@ -0,0 +1,16 @@ +// Copyright 2022 The Pigweed 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 +// +// https://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 "pw_sync_baremetal/recursive_mutex_inline.h" diff --git a/pw_sync_baremetal/public_overrides/pw_sync_backend/recursive_mutex_native.h b/pw_sync_baremetal/public_overrides/pw_sync_backend/recursive_mutex_native.h new file mode 100644 index 000000000..96afb844d --- /dev/null +++ b/pw_sync_baremetal/public_overrides/pw_sync_backend/recursive_mutex_native.h @@ -0,0 +1,16 @@ +// Copyright 2022 The Pigweed 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 +// +// https://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 "pw_sync_baremetal/recursive_mutex_native.h" |