pw_sync: Migrate upstream to use InterruptSpinLock
Migrates Pigweed off of SpinLock to use the new InterruptSpinLock
name.
Also updates some stale documentation regarding SystemClock backends
which use InterruptSpinLocks before C++ construction now that there
is a constexpr constructor, as it was already getting updated for
the rename.
Change-Id: Ie821b7c5c59f21a16bcc9bd9674c5df6a85e27b1
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/37561
Commit-Queue: Ewout van Bekkum <ewout@google.com>
Pigweed-Auto-Submit: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
diff --git a/pw_chrono/BUILD b/pw_chrono/BUILD
index 7bc077b..ea76d5b 100644
--- a/pw_chrono/BUILD
+++ b/pw_chrono/BUILD
@@ -72,7 +72,7 @@
],
deps = [
":system_clock",
- "//pw_sync:spin_lock",
+ "//pw_sync:interrupt_spin_lock",
],
)
diff --git a/pw_chrono/BUILD.gn b/pw_chrono/BUILD.gn
index 8ed15db..33176db 100644
--- a/pw_chrono/BUILD.gn
+++ b/pw_chrono/BUILD.gn
@@ -49,7 +49,7 @@
public = [ "public/pw_chrono/simulated_system_clock.h" ]
public_deps = [
":system_clock",
- "$dir_pw_sync:spin_lock",
+ "$dir_pw_sync:interrupt_spin_lock",
]
}
diff --git a/pw_chrono/public/pw_chrono/simulated_system_clock.h b/pw_chrono/public/pw_chrono/simulated_system_clock.h
index 477b34a..8fefe47 100644
--- a/pw_chrono/public/pw_chrono/simulated_system_clock.h
+++ b/pw_chrono/public/pw_chrono/simulated_system_clock.h
@@ -17,7 +17,7 @@
#include <mutex>
#include "pw_chrono/system_clock.h"
-#include "pw_sync/spin_lock.h"
+#include "pw_sync/interrupt_spin_lock.h"
namespace pw::chrono {
@@ -45,25 +45,25 @@
: current_timestamp_(timestamp) {}
void AdvanceTime(SystemClock::duration duration) {
- std::lock_guard lock(spin_lock_);
+ std::lock_guard lock(interrupt_spin_lock_);
current_timestamp_ += duration;
}
// WARNING: Use of this function may violate the is_monotonic clock attribute.
void SetTime(SystemClock::time_point timestamp) {
- std::lock_guard lock(spin_lock_);
+ std::lock_guard lock(interrupt_spin_lock_);
current_timestamp_ = timestamp;
}
SystemClock::time_point now() override {
- std::lock_guard lock(spin_lock_);
+ std::lock_guard lock(interrupt_spin_lock_);
return current_timestamp_;
};
private:
// In theory atomics could be used if 64bit atomics are supported, however
// performance of this test object shouldn't matter.
- sync::SpinLock spin_lock_;
+ sync::InterruptSpinLock interrupt_spin_lock_;
SystemClock::time_point current_timestamp_;
};
diff --git a/pw_chrono_embos/BUILD.gn b/pw_chrono_embos/BUILD.gn
index 0d9e63a..13a8a0c 100644
--- a/pw_chrono_embos/BUILD.gn
+++ b/pw_chrono_embos/BUILD.gn
@@ -64,7 +64,7 @@
sources = [ "system_clock.cc" ]
deps = [
"$dir_pw_chrono:system_clock.facade",
- "$dir_pw_sync:spin_lock",
+ "$dir_pw_sync:interrupt_spin_lock",
]
}
diff --git a/pw_chrono_embos/docs.rst b/pw_chrono_embos/docs.rst
index aedc5b4..c6be81b 100644
--- a/pw_chrono_embos/docs.rst
+++ b/pw_chrono_embos/docs.rst
@@ -13,12 +13,11 @@
SystemClock backend
-------------------
The embOS based ``system_clock`` backend implements the
-``pw_chrono:system_clock`` facade by using ``OS_GetTime32()``. Before the global
-singleton SystemClock's SpinLock is constructed the raw result is returned,
-after the overflows are managed in a thread and IRQ safe manner to produce a
-signed 64 bit timestamp. Note that this does NOT use ``OS_GetTime_us64()`` which
-is not always available, this could be considered for a future alternative
-backend for the SystemClock.
+``pw_chrono:system_clock`` facade by using ``OS_GetTime32()``. An
+InterruptSpinLock is used to manage overflows in a thread and interrupt safe
+manner to produce a signed 64 bit timestamp. Note that this does NOT use
+``OS_GetTime_us64()`` which is not always available, this could be considered
+for a future alternative backend for the SystemClock.
The ``SystemClock::now()`` must be used more than once per overflow of the
native embOS ``OS_GetTime32()`` overflow. Note that this duration may
diff --git a/pw_chrono_embos/system_clock.cc b/pw_chrono_embos/system_clock.cc
index 4506172..a6816a9 100644
--- a/pw_chrono_embos/system_clock.cc
+++ b/pw_chrono_embos/system_clock.cc
@@ -20,12 +20,12 @@
#include <mutex>
#include "RTOS.h"
-#include "pw_sync/spin_lock.h"
+#include "pw_sync/interrupt_spin_lock.h"
namespace pw::chrono::backend {
namespace {
-sync::SpinLock system_clock_spin_lock;
+sync::InterruptSpinLock system_clock_interrupt_spin_lock;
int64_t overflow_tick_count = 0;
uint32_t native_tick_count = 0;
static_assert(!SystemClock::is_nmi_safe,
@@ -48,7 +48,7 @@
} // namespace
int64_t GetSystemClockTickCount() {
- std::lock_guard lock(system_clock_spin_lock);
+ std::lock_guard lock(system_clock_interrupt_spin_lock);
const uint32_t new_native_tick_count = GetUint32TickCount();
// WARNING: This must be called more than once per overflow period!
if (new_native_tick_count < native_tick_count) {
diff --git a/pw_chrono_freertos/BUILD.gn b/pw_chrono_freertos/BUILD.gn
index b204749..5b4c275 100644
--- a/pw_chrono_freertos/BUILD.gn
+++ b/pw_chrono_freertos/BUILD.gn
@@ -65,7 +65,7 @@
deps = [
"$dir_pw_chrono:system_clock.facade",
"$dir_pw_interrupt:context",
- "$dir_pw_sync:spin_lock",
+ "$dir_pw_sync:interrupt_spin_lock",
]
}
diff --git a/pw_chrono_freertos/docs.rst b/pw_chrono_freertos/docs.rst
index d09bebf..cb0d318 100644
--- a/pw_chrono_freertos/docs.rst
+++ b/pw_chrono_freertos/docs.rst
@@ -14,9 +14,8 @@
-------------------
The FreeRTOS based ``system_clock`` backend implements the
``pw_chrono:system_clock`` facade by using ``xTaskGetTickCountFromISR()`` and
-``xTaskGetTickCount()`` based on the current context. Before the global
-singleton SystemClock's SpinLock is constructed the raw result is returned,
-after the overflows are managed in a thread and IRQ safe manner to produce a
+``xTaskGetTickCount()`` based on the current context. An InterruptSpinLock is
+used to manage overflows in a thread and interrupt safe manner to produce a
signed 64 bit timestamp.
The ``SystemClock::now()`` must be used more than once per overflow of the
diff --git a/pw_chrono_freertos/system_clock.cc b/pw_chrono_freertos/system_clock.cc
index 84f2e43..b05a46c 100644
--- a/pw_chrono_freertos/system_clock.cc
+++ b/pw_chrono_freertos/system_clock.cc
@@ -21,13 +21,13 @@
#include "FreeRTOS.h"
#include "pw_interrupt/context.h"
-#include "pw_sync/spin_lock.h"
+#include "pw_sync/interrupt_spin_lock.h"
#include "task.h"
namespace pw::chrono::backend {
namespace {
-sync::SpinLock system_clock_spin_lock;
+sync::InterruptSpinLock system_clock_interrupt_spin_lock;
int64_t overflow_tick_count = 0;
TickType_t native_tick_count = 0;
static_assert(!SystemClock::is_nmi_safe,
@@ -40,7 +40,7 @@
} // namespace
int64_t GetSystemClockTickCount() {
- std::lock_guard lock(system_clock_spin_lock);
+ std::lock_guard lock(system_clock_interrupt_spin_lock);
const TickType_t new_native_tick_count = interrupt::InInterruptContext()
? xTaskGetTickCountFromISR()
: xTaskGetTickCount();
diff --git a/pw_chrono_threadx/BUILD.gn b/pw_chrono_threadx/BUILD.gn
index d7bdace..3cd0434 100644
--- a/pw_chrono_threadx/BUILD.gn
+++ b/pw_chrono_threadx/BUILD.gn
@@ -64,7 +64,7 @@
sources = [ "system_clock.cc" ]
deps = [
"$dir_pw_chrono:system_clock.facade",
- "$dir_pw_sync:spin_lock",
+ "$dir_pw_sync:interrupt_spin_lock",
]
}
diff --git a/pw_chrono_threadx/docs.rst b/pw_chrono_threadx/docs.rst
index 9913eff..8b83a2b 100644
--- a/pw_chrono_threadx/docs.rst
+++ b/pw_chrono_threadx/docs.rst
@@ -13,10 +13,9 @@
SystemClock backend
-------------------
The ThreadX based ``system_clock`` backend implements the
-``pw_chrono:system_clock`` facade by using ``tx_time_get()``. Before the global
-singleton SystemClock's SpinLock is constructed the raw result is returned,
-after the overflows are managed in a thread and IRQ safe manner to produce a
-signed 64 bit timestamp.
+``pw_chrono:system_clock`` facade by using ``tx_time_get()``. An
+InterruptSpinLock is used to manage overflows in a thread and interrupt safe
+manner to produce a signed 64 bit timestamp.
The ``SystemClock::now()`` must be used more than once per overflow of the
native ThreadX ``tx_time_get()`` overflow. Note that this duration may vary if
diff --git a/pw_chrono_threadx/system_clock.cc b/pw_chrono_threadx/system_clock.cc
index 3017c33..bcb5792 100644
--- a/pw_chrono_threadx/system_clock.cc
+++ b/pw_chrono_threadx/system_clock.cc
@@ -19,7 +19,7 @@
#include <limits>
#include <mutex>
-#include "pw_sync/spin_lock.h"
+#include "pw_sync/interrupt_spin_lock.h"
#include "tx_api.h"
namespace pw::chrono::backend {
@@ -29,7 +29,7 @@
#error "This backend is not compatible with TX_NO_TIMER"
#endif // defined(TX_NO_TIMER) && TX_NO_TIMER
-sync::SpinLock system_clock_spin_lock;
+sync::InterruptSpinLock system_clock_interrupt_spin_lock;
int64_t overflow_tick_count = 0;
ULONG native_tick_count = 0;
static_assert(!SystemClock::is_nmi_safe,
@@ -42,7 +42,7 @@
} // namespace
int64_t GetSystemClockTickCount() {
- std::lock_guard lock(system_clock_spin_lock);
+ std::lock_guard lock(system_clock_interrupt_spin_lock);
const ULONG new_native_tick_count = tx_time_get();
// WARNING: This must be called more than once per overflow period!
if (new_native_tick_count < native_tick_count) {
diff --git a/pw_log_sink/BUILD b/pw_log_sink/BUILD
index 483f529..22334a7 100644
--- a/pw_log_sink/BUILD
+++ b/pw_log_sink/BUILD
@@ -32,7 +32,7 @@
"//pw_log_multisink:log_queue",
"//pw_preprocessor",
"//pw_status",
- "//pw_sync:spin_lock",
+ "//pw_sync:interrupt_spin_lock",
],
hdrs = [
"public/pw_log_sink/log_sink.h",
diff --git a/pw_log_sink/BUILD.gn b/pw_log_sink/BUILD.gn
index 775ef88..cb2c330 100644
--- a/pw_log_sink/BUILD.gn
+++ b/pw_log_sink/BUILD.gn
@@ -47,7 +47,7 @@
deps = [
"$dir_pw_log:protos.pwpb",
"$dir_pw_string",
- "$dir_pw_sync:spin_lock",
+ "$dir_pw_sync:interrupt_spin_lock",
]
}
diff --git a/pw_log_sink/log_sink.cc b/pw_log_sink/log_sink.cc
index 7df1436..774c390 100644
--- a/pw_log_sink/log_sink.cc
+++ b/pw_log_sink/log_sink.cc
@@ -23,7 +23,7 @@
#include "pw_protobuf/wire_format.h"
#include "pw_status/try.h"
#include "pw_string/string_builder.h"
-#include "pw_sync/spin_lock.h"
+#include "pw_sync/interrupt_spin_lock.h"
namespace pw::log_sink {
namespace {
@@ -41,10 +41,10 @@
return sink_list;
}
-pw::sync::SpinLock& sink_list_lock() {
+pw::sync::InterruptSpinLock& sink_list_lock() {
// TODO(pwbug/304): Make lock selection configurable, some applications may
// not be able to tolerate interrupt jitter and may prefer a pw::sync::Mutex.
- static pw::sync::SpinLock sink_list_lock;
+ static pw::sync::InterruptSpinLock sink_list_lock;
return sink_list_lock;
}
@@ -89,7 +89,7 @@
// TODO(pwbug/305): Consider using a shared buffer between users. For now,
// only lock after completing the encoding.
{
- const std::lock_guard<pw::sync::SpinLock> lock(sink_list_lock());
+ const std::lock_guard<pw::sync::InterruptSpinLock> lock(sink_list_lock());
// If no sinks are configured, ignore the message. When sinks are attached,
// they will receive this drop count to indicate logs drop to early boot.
@@ -125,12 +125,12 @@
}
void AddSink(Sink& sink) {
- const std::lock_guard<pw::sync::SpinLock> lock(sink_list_lock());
+ const std::lock_guard lock(sink_list_lock());
sink_list().push_back(sink);
}
void RemoveSink(Sink& sink) {
- const std::lock_guard<pw::sync::SpinLock> lock(sink_list_lock());
+ const std::lock_guard lock(sink_list_lock());
sink_list().remove(sink);
}
diff --git a/targets/arduino/target_toolchains.gni b/targets/arduino/target_toolchains.gni
index 0b68152..469a95f 100644
--- a/targets/arduino/target_toolchains.gni
+++ b/targets/arduino/target_toolchains.gni
@@ -44,7 +44,8 @@
# Facade backends
pw_assert_BACKEND = dir_pw_assert_basic
pw_log_BACKEND = dir_pw_log_basic
- pw_sync_SPIN_LOCK_BACKEND = "$dir_pw_sync_baremetal:interrupt_spin_lock"
+ pw_sync_INTERRUPT_SPIN_LOCK_BACKEND =
+ "$dir_pw_sync_baremetal:interrupt_spin_lock"
pw_sys_io_BACKEND = dir_pw_sys_io_arduino
pw_rpc_system_server_BACKEND =
"$dir_pigweed/targets/arduino:system_rpc_server"
diff --git a/targets/host/target_toolchains.gni b/targets/host/target_toolchains.gni
index 98fd14e..7f3411a 100644
--- a/targets/host/target_toolchains.gni
+++ b/targets/host/target_toolchains.gni
@@ -37,7 +37,7 @@
pw_log_BACKEND = "$dir_pw_log_basic"
# Configure backends for pw_sync's facades.
- pw_sync_SPIN_LOCK_BACKEND = "$dir_pw_sync_stl:interrupt_spin_lock"
+ pw_sync_INTERRUPT_SPIN_LOCK_BACKEND = "$dir_pw_sync_stl:interrupt_spin_lock"
# Configure backend for pw_sys_io facade.
pw_sys_io_BACKEND = "$dir_pw_sys_io_stdio"
diff --git a/targets/lm3s6965evb-qemu/target_toolchains.gni b/targets/lm3s6965evb-qemu/target_toolchains.gni
index 602d01a..a5c05ce 100644
--- a/targets/lm3s6965evb-qemu/target_toolchains.gni
+++ b/targets/lm3s6965evb-qemu/target_toolchains.gni
@@ -40,7 +40,8 @@
pw_boot_BACKEND = dir_pw_boot_armv7m
pw_log_BACKEND = dir_pw_log_basic
pw_sys_io_BACKEND = dir_pw_sys_io_baremetal_lm3s6965evb
- pw_sync_SPIN_LOCK_BACKEND = "$dir_pw_sync_baremetal:interrupt_spin_lock"
+ pw_sync_INTERRUPT_SPIN_LOCK_BACKEND =
+ "$dir_pw_sync_baremetal:interrupt_spin_lock"
# pw_cpu_exception_armv7m tests do not work as expected in QEMU. It does not
# appear the divide-by-zero traps as expected when enabled, which prevents the
diff --git a/targets/stm32f429i-disc1/target_toolchains.gni b/targets/stm32f429i-disc1/target_toolchains.gni
index f2bebda..e87245e 100644
--- a/targets/stm32f429i-disc1/target_toolchains.gni
+++ b/targets/stm32f429i-disc1/target_toolchains.gni
@@ -50,7 +50,8 @@
pw_cpu_exception_HANDLER_BACKEND = "$dir_pw_cpu_exception:basic_handler"
pw_cpu_exception_SUPPORT_BACKEND =
"$dir_pw_cpu_exception_cortex_m:support_armv7m"
- pw_sync_SPIN_LOCK_BACKEND = "$dir_pw_sync_baremetal:interrupt_spin_lock"
+ pw_sync_INTERRUPT_SPIN_LOCK_BACKEND =
+ "$dir_pw_sync_baremetal:interrupt_spin_lock"
pw_log_BACKEND = dir_pw_log_basic
pw_sys_io_BACKEND = dir_pw_sys_io_baremetal_stm32f429
pw_rpc_system_server_BACKEND =