diff options
Diffstat (limited to 'pw_sync_zephyr')
17 files changed, 399 insertions, 40 deletions
diff --git a/pw_sync_zephyr/CMakeLists.txt b/pw_sync_zephyr/CMakeLists.txt index ce23060ca..c52ffc160 100644 --- a/pw_sync_zephyr/CMakeLists.txt +++ b/pw_sync_zephyr/CMakeLists.txt @@ -14,39 +14,93 @@ include($ENV{PW_ROOT}/pw_build/pigweed.cmake) -if(CONFIG_PIGWEED_SYNC_MUTEX) - pw_add_library(pw_sync_zephyr.mutex_backend INTERFACE - HEADERS - public/pw_sync_zephyr/mutex_inline.h - public/pw_sync_zephyr/mutex_native.h - public_overrides/pw_sync_backend/mutex_inline.h - public_overrides/pw_sync_backend/mutex_native.h - PUBLIC_INCLUDES - public - public_overrides - PUBLIC_DEPS - pw_sync.mutex.facade - ) - zephyr_link_libraries(pw_sync_zephyr.mutex_backend) - zephyr_link_interface(pw_sync_zephyr.mutex_backend) -endif() +pw_add_library(pw_sync_zephyr.mutex_backend INTERFACE + HEADERS + public/pw_sync_zephyr/mutex_inline.h + public/pw_sync_zephyr/mutex_native.h + public_overrides/pw_sync_backend/mutex_inline.h + public_overrides/pw_sync_backend/mutex_native.h + PUBLIC_INCLUDES + public + public_overrides + PUBLIC_DEPS + pw_sync.mutex.facade +) +pw_zephyrize_libraries_ifdef( + CONFIG_PIGWEED_SYNC_MUTEX + pw_sync_zephyr.mutex_backend +) -if(CONFIG_PIGWEED_SYNC_BINARY_SEMAPHORE) - pw_add_library(pw_sync_zephyr.binary_semaphore_backend STATIC - HEADERS - public/pw_sync_zephyr/binary_semaphore_native.h - public/pw_sync_zephyr/binary_semaphore_inline.h - public_overrides/pw_sync_backend/binary_semaphore_native.h - public_overrides/pw_sync_backend/binary_semaphore_inline.h - PUBLIC_INCLUDES - public - public_overrides - PUBLIC_DEPS - pw_chrono.system_clock - pw_sync.binary_semaphore.facade - SOURCES - binary_semaphore.cc - ) - zephyr_link_libraries(pw_sync_zephyr.binary_semaphore_backend) - zephyr_link_interface(pw_sync_zephyr.binary_semaphore_backend) -endif() +pw_add_library(pw_sync_zephyr.binary_semaphore_backend STATIC + HEADERS + public/pw_sync_zephyr/binary_semaphore_native.h + public/pw_sync_zephyr/binary_semaphore_inline.h + public_overrides/pw_sync_backend/binary_semaphore_native.h + public_overrides/pw_sync_backend/binary_semaphore_inline.h + PUBLIC_INCLUDES + public + public_overrides + PUBLIC_DEPS + pw_chrono.system_clock + pw_sync.binary_semaphore.facade + SOURCES + binary_semaphore.cc +) +pw_zephyrize_libraries_ifdef( + CONFIG_PIGWEED_SYNC_BINARY_SEMAPHORE + pw_sync_zephyr.binary_semaphore_backend +) + +pw_add_library(pw_sync_zephyr.interrupt_spin_lock_backend STATIC + HEADERS + public/pw_sync_zephyr/interrupt_spin_lock_inline.h + public/pw_sync_zephyr/interrupt_spin_lock_native.h + public_overrides/pw_sync_backend/interrupt_spin_lock_inline.h + public_overrides/pw_sync_backend/interrupt_spin_lock_native.h + PUBLIC_INCLUDES + public + public_overrides + PUBLIC_DEPS + pw_sync.interrupt_spin_lock.facade + SOURCES + interrupt_spin_lock.cc +) +pw_zephyrize_libraries_ifdef( + CONFIG_PIGWEED_SYNC_INTERRUPT_SPIN_LOCK + pw_sync_zephyr.interrupt_spin_lock_backend +) + +pw_add_library(pw_sync_zephyr.thread_notification_backend INTERFACE + HEADERS + public/pw_sync_zephyr/thread_notification_inline.h + public/pw_sync_zephyr/thread_notification_native.h + public_overrides/thread_notification/pw_sync_backend/thread_notification_inline.h + public_overrides/thread_notification/pw_sync_backend/thread_notification_native.h + PUBLIC_INCLUDES + public + public_overrides/thread_notification + PUBLIC_DEPS + pw_sync.binary_semaphore + pw_sync.thread_notification.facade +) +pw_zephyrize_libraries_ifdef( + CONFIG_PIGWEED_SYNC_THREAD_NOTIFICATION + pw_sync_zephyr.thread_notification_backend +) + +pw_add_library(pw_sync_zephyr.timed_thread_notification_backend INTERFACE + HEADERS + public/pw_sync_zephyr/timed_thread_notification_inline.h + public_overrides/timed_thread_notification/pw_sync_backend/timed_thread_notification_inline.h + PUBLIC_INCLUDES + public + public_overrides/timed_thread_notification + PUBLIC_DEPS + pw_sync.binary_semaphore + pw_sync.timed_thread_notification.facade +) + +pw_zephyrize_libraries_ifdef( + CONFIG_PIGWEED_SYNC_TIMED_THREAD_NOTIFICATION + pw_sync_zephyr.timed_thread_notification_backend +) diff --git a/pw_sync_zephyr/Kconfig b/pw_sync_zephyr/Kconfig index 15a9ec5ae..10a6e64d3 100644 --- a/pw_sync_zephyr/Kconfig +++ b/pw_sync_zephyr/Kconfig @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations under # the License. +menu "pw_sync" + config PIGWEED_SYNC bool select PIGWEED_CHRONO_SYSTEM_CLOCK @@ -19,10 +21,33 @@ config PIGWEED_SYNC select PIGWEED_INTERRUPT_CONTEXT config PIGWEED_SYNC_MUTEX - bool "Enable Pigweed mutex library (pw_sync.mutex)" + bool "Link pw_sync.mutex library" select PIGWEED_SYNC select PIGWEED_POLYFILL + help + See :ref:`module-pw_sync` for module details. config PIGWEED_SYNC_BINARY_SEMAPHORE - bool "Enable Pigweed binary semaphore library (pw_sync.binary_semaphore)" + bool "Link pw_sync.binary_semaphore library" select PIGWEED_SYNC + help + See :ref:`module-pw_sync` for module details. + +config PIGWEED_SYNC_INTERRUPT_SPIN_LOCK + bool "Link pw_sync.interrupt_spin_lock library" + help + See :ref:`module-pw_sync` for module details. + +config PIGWEED_SYNC_THREAD_NOTIFICATION + bool "Link pw_sync.thread_notification library" + select PIGWEED_SYNC_BINARY_SEMAPHORE + help + See :ref:`module-pw_sync` for module details. + +config PIGWEED_SYNC_TIMED_THREAD_NOTIFICATION + bool "Link pw_sync.timed_thread_notification library" + select PIGWEED_SYNC_BINARY_SEMAPHORE + help + See :ref:`module-pw_sync` for module details. + +endmenu diff --git a/pw_sync_zephyr/binary_semaphore.cc b/pw_sync_zephyr/binary_semaphore.cc index 082813769..9e3e33efc 100644 --- a/pw_sync_zephyr/binary_semaphore.cc +++ b/pw_sync_zephyr/binary_semaphore.cc @@ -14,7 +14,7 @@ #include "pw_sync/binary_semaphore.h" -#include <kernel.h> +#include <zephyr/kernel.h> namespace pw::sync { diff --git a/pw_sync_zephyr/docs.rst b/pw_sync_zephyr/docs.rst index 705c144f7..9e96a431b 100644 --- a/pw_sync_zephyr/docs.rst +++ b/pw_sync_zephyr/docs.rst @@ -14,3 +14,4 @@ the Kconfig menu. * ``pw_sync.mutex`` can be enabled via ``CONFIG_PIGWEED_SYNC_MUTEX``. * ``pw_sync.binary_semaphore`` can be enabled via ``CONFIG_PIGWEED_SYNC_BINARY_SEMAPHORE``. +* ``pw_sync.interrupt_spin_lock`` can be enabled via ``CONFIG_PIGWEED_SYNC_INTERRUPT_SPIN_LOCK``. diff --git a/pw_sync_zephyr/interrupt_spin_lock.cc b/pw_sync_zephyr/interrupt_spin_lock.cc new file mode 100644 index 000000000..0d4674433 --- /dev/null +++ b/pw_sync_zephyr/interrupt_spin_lock.cc @@ -0,0 +1,35 @@ +// Copyright 2023 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 "pw_sync/interrupt_spin_lock.h" + +#include <zephyr/spinlock.h> + +#include "pw_assert/check.h" + +namespace pw::sync { + +void InterruptSpinLock::lock() { + PW_DCHECK(!native_type_.locked, + "Recursive InterruptSpinLock::lock() detected"); + native_type_.key = k_spin_lock(&native_type_.lock); + native_type_.locked = true; +} + +void InterruptSpinLock::unlock() { + native_type_.locked = false; + k_spin_unlock(&native_type_.lock, native_type_.key); +} + +} // namespace pw::sync diff --git a/pw_sync_zephyr/public/pw_sync_zephyr/binary_semaphore_inline.h b/pw_sync_zephyr/public/pw_sync_zephyr/binary_semaphore_inline.h index 9afe7ceb1..7bd05509a 100644 --- a/pw_sync_zephyr/public/pw_sync_zephyr/binary_semaphore_inline.h +++ b/pw_sync_zephyr/public/pw_sync_zephyr/binary_semaphore_inline.h @@ -13,7 +13,7 @@ // the License. #pragma once -#include <kernel.h> +#include <zephyr/kernel.h> #include "pw_assert/assert.h" #include "pw_chrono/system_clock.h" diff --git a/pw_sync_zephyr/public/pw_sync_zephyr/binary_semaphore_native.h b/pw_sync_zephyr/public/pw_sync_zephyr/binary_semaphore_native.h index 946a93433..fbe82a7c4 100644 --- a/pw_sync_zephyr/public/pw_sync_zephyr/binary_semaphore_native.h +++ b/pw_sync_zephyr/public/pw_sync_zephyr/binary_semaphore_native.h @@ -13,7 +13,7 @@ // the License. #pragma once -#include <kernel.h> +#include <zephyr/kernel.h> namespace pw::sync::backend { diff --git a/pw_sync_zephyr/public/pw_sync_zephyr/interrupt_spin_lock_inline.h b/pw_sync_zephyr/public/pw_sync_zephyr/interrupt_spin_lock_inline.h new file mode 100644 index 000000000..c98f6d9ad --- /dev/null +++ b/pw_sync_zephyr/public/pw_sync_zephyr/interrupt_spin_lock_inline.h @@ -0,0 +1,36 @@ +// Copyright 2023 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/interrupt_spin_lock.h" + +namespace pw::sync { + +constexpr InterruptSpinLock::InterruptSpinLock() + : native_type_{.lock = {}, .locked = false, .key = {0}} {} + +inline bool InterruptSpinLock::try_lock() { + // This backend will spin the current processor until the deadlock is + // available and does not implement a 'try' mechanism. Recursive locking is + // already detected by lock(). + lock(); + return true; +} + +inline InterruptSpinLock::native_handle_type +InterruptSpinLock::native_handle() { + return native_type_; +} + +} // namespace pw::sync diff --git a/pw_sync_zephyr/public/pw_sync_zephyr/interrupt_spin_lock_native.h b/pw_sync_zephyr/public/pw_sync_zephyr/interrupt_spin_lock_native.h new file mode 100644 index 000000000..4cd71e037 --- /dev/null +++ b/pw_sync_zephyr/public/pw_sync_zephyr/interrupt_spin_lock_native.h @@ -0,0 +1,31 @@ +// Copyright 2023 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 <zephyr/spinlock.h> + +#include "pw_sync/interrupt_spin_lock.h" + +namespace pw::sync::backend { + +struct ZephyrSpinLock { + struct k_spinlock lock; + bool locked; // Used to detect recursion. + k_spinlock_key_t key; +}; + +using NativeInterruptSpinLock = struct ZephyrSpinLock; +using NativeInterruptSpinLockHandle = NativeInterruptSpinLock&; + +} // namespace pw::sync::backend diff --git a/pw_sync_zephyr/public/pw_sync_zephyr/thread_notification_inline.h b/pw_sync_zephyr/public/pw_sync_zephyr/thread_notification_inline.h new file mode 100644 index 000000000..f14028ce2 --- /dev/null +++ b/pw_sync_zephyr/public/pw_sync_zephyr/thread_notification_inline.h @@ -0,0 +1,41 @@ +// Copyright 2023 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 <zephyr/sys/sem.h> + +#include "pw_assert/assert.h" +#include "pw_interrupt/context.h" +#include "pw_sync/thread_notification.h" + +namespace pw::sync { + +inline ThreadNotification::ThreadNotification() : native_type_() {} + +inline ThreadNotification::~ThreadNotification() = default; + +inline void ThreadNotification::release() { native_type_.release(); } + +inline void ThreadNotification::acquire() { native_type_.acquire(); } + +inline bool ThreadNotification::try_acquire() { + return native_type_.try_acquire(); +} + +inline ThreadNotification::native_handle_type +ThreadNotification::native_handle() { + return native_type_; +} + +} // namespace pw::sync diff --git a/pw_sync_zephyr/public/pw_sync_zephyr/thread_notification_native.h b/pw_sync_zephyr/public/pw_sync_zephyr/thread_notification_native.h new file mode 100644 index 000000000..5fb0495ef --- /dev/null +++ b/pw_sync_zephyr/public/pw_sync_zephyr/thread_notification_native.h @@ -0,0 +1,23 @@ +// Copyright 2023 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/binary_semaphore.h> + +namespace pw::sync::backend { + +using NativeThreadNotification = pw::sync::BinarySemaphore; +using NativeThreadNotificationHandle = NativeThreadNotification&; + +} // namespace pw::sync::backend diff --git a/pw_sync_zephyr/public/pw_sync_zephyr/timed_thread_notification_inline.h b/pw_sync_zephyr/public/pw_sync_zephyr/timed_thread_notification_inline.h new file mode 100644 index 000000000..c24388f92 --- /dev/null +++ b/pw_sync_zephyr/public/pw_sync_zephyr/timed_thread_notification_inline.h @@ -0,0 +1,33 @@ +// Copyright 2023 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/binary_semaphore.h> + +#include "pw_chrono/system_clock.h" +#include "pw_sync/timed_thread_notification.h" + +namespace pw::sync { + +inline bool TimedThreadNotification::try_acquire_for( + chrono::SystemClock::duration timeout) { + return native_handle().try_acquire_for(timeout); +} + +inline bool TimedThreadNotification::try_acquire_until( + chrono::SystemClock::time_point deadline) { + return try_acquire_for(deadline - chrono::SystemClock::now()); +} + +} // namespace pw::sync diff --git a/pw_sync_zephyr/public_overrides/pw_sync_backend/interrupt_spin_lock_inline.h b/pw_sync_zephyr/public_overrides/pw_sync_backend/interrupt_spin_lock_inline.h new file mode 100644 index 000000000..af2ba03b3 --- /dev/null +++ b/pw_sync_zephyr/public_overrides/pw_sync_backend/interrupt_spin_lock_inline.h @@ -0,0 +1,16 @@ +// Copyright 2023 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_zephyr/interrupt_spin_lock_inline.h" diff --git a/pw_sync_zephyr/public_overrides/pw_sync_backend/interrupt_spin_lock_native.h b/pw_sync_zephyr/public_overrides/pw_sync_backend/interrupt_spin_lock_native.h new file mode 100644 index 000000000..02cc000fe --- /dev/null +++ b/pw_sync_zephyr/public_overrides/pw_sync_backend/interrupt_spin_lock_native.h @@ -0,0 +1,16 @@ +// Copyright 2023 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_zephyr/interrupt_spin_lock_native.h" diff --git a/pw_sync_zephyr/public_overrides/thread_notification/pw_sync_backend/thread_notification_inline.h b/pw_sync_zephyr/public_overrides/thread_notification/pw_sync_backend/thread_notification_inline.h new file mode 100644 index 000000000..f18bf3aaf --- /dev/null +++ b/pw_sync_zephyr/public_overrides/thread_notification/pw_sync_backend/thread_notification_inline.h @@ -0,0 +1,16 @@ +// Copyright 2023 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_zephyr/thread_notification_inline.h" diff --git a/pw_sync_zephyr/public_overrides/thread_notification/pw_sync_backend/thread_notification_native.h b/pw_sync_zephyr/public_overrides/thread_notification/pw_sync_backend/thread_notification_native.h new file mode 100644 index 000000000..482807ea4 --- /dev/null +++ b/pw_sync_zephyr/public_overrides/thread_notification/pw_sync_backend/thread_notification_native.h @@ -0,0 +1,16 @@ +// Copyright 2023 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_zephyr/thread_notification_native.h" diff --git a/pw_sync_zephyr/public_overrides/timed_thread_notification/pw_sync_backend/timed_thread_notification_inline.h b/pw_sync_zephyr/public_overrides/timed_thread_notification/pw_sync_backend/timed_thread_notification_inline.h new file mode 100644 index 000000000..08062389d --- /dev/null +++ b/pw_sync_zephyr/public_overrides/timed_thread_notification/pw_sync_backend/timed_thread_notification_inline.h @@ -0,0 +1,16 @@ +// Copyright 2023 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_zephyr/timed_thread_notification_inline.h" |