pw_thread_backends: Expand comments in the implementations
Expands comments in the backend implementations to better explain
why things are done the way they are as a reference for future
backends.
Also updates the IRQ and kernel contract asserts to consistently
debug assert the contexts.
Change-Id: I4d742c5d2eccaeb1500680405c6959c735ce351f
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/67304
Pigweed-Auto-Submit: Ewout van Bekkum <ewout@google.com>
Commit-Queue: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Keir Mierle <keir@google.com>
diff --git a/pw_thread_threadx/sleep.cc b/pw_thread_threadx/sleep.cc
index 404ad67..3de24c0 100644
--- a/pw_thread_threadx/sleep.cc
+++ b/pw_thread_threadx/sleep.cc
@@ -27,6 +27,7 @@
namespace pw::this_thread {
void sleep_for(chrono::SystemClock::duration for_at_least) {
+ // Ensure this is being called by a thread.
PW_DCHECK(get_id() != thread::Id());
// Yield for negative and zero length durations.
@@ -35,8 +36,11 @@
return;
}
- // On a tick based kernel we cannot tell how far along we are on the current
- // tick, ergo we add one whole tick to the final duration.
+ // In case the timeout is too long for us to express through the native
+ // ThreadX API, we repeatedly wait with shorter durations. Note that on a tick
+ // based kernel we cannot tell how far along we are on the current tick, ergo
+ // we add one whole tick to the final duration. However, this also means that
+ // the loop must ensure that timeout + 1 is less than the max timeout.
constexpr SystemClock::duration kMaxTimeoutMinusOne =
pw::chrono::threadx::kMaxTimeout - SystemClock::duration(1);
while (for_at_least > kMaxTimeoutMinusOne) {
@@ -45,6 +49,8 @@
PW_CHECK_UINT_EQ(TX_SUCCESS, result);
for_at_least -= kMaxTimeoutMinusOne;
}
+ // On a tick based kernel we cannot tell how far along we are on the current
+ // tick, ergo we add one whole tick to the final duration.
const UINT result =
tx_thread_sleep(static_cast<ULONG>(for_at_least.count() + 1));
PW_CHECK_UINT_EQ(TX_SUCCESS, result);