pw_sync_freertos: remove the unnecessary handles

Optimizes the mutex and semaphore implementations to remove the
handles (pointers) from the native types as the FreeRTOS
implementation guarantees that the returned handle is a pointer
to the static buffer used for the type.

Change-Id: I6027843315e50d9230b44b869ab1e5b745837141
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/36820
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
Pigweed-Auto-Submit: Ewout van Bekkum <ewout@google.com>
diff --git a/pw_sync_freertos/binary_semaphore.cc b/pw_sync_freertos/binary_semaphore.cc
index 680eb11..16a1fb2 100644
--- a/pw_sync_freertos/binary_semaphore.cc
+++ b/pw_sync_freertos/binary_semaphore.cc
@@ -46,14 +46,12 @@
   constexpr SystemClock::duration kMaxTimeoutMinusOne =
       pw::chrono::freertos::kMaxTimeout - SystemClock::duration(1);
   while (for_at_least > kMaxTimeoutMinusOne) {
-    if (xSemaphoreTake(native_type_.handle, kMaxTimeoutMinusOne.count()) ==
-        pdTRUE) {
+    if (xSemaphoreTake(&native_type_, kMaxTimeoutMinusOne.count()) == pdTRUE) {
       return true;
     }
     for_at_least -= kMaxTimeoutMinusOne;
   }
-  return xSemaphoreTake(native_type_.handle, for_at_least.count() + 1) ==
-         pdTRUE;
+  return xSemaphoreTake(&native_type_, for_at_least.count() + 1) == pdTRUE;
 }
 
 }  // namespace pw::sync
diff --git a/pw_sync_freertos/counting_semaphore.cc b/pw_sync_freertos/counting_semaphore.cc
index 0ed7a10..501c47d 100644
--- a/pw_sync_freertos/counting_semaphore.cc
+++ b/pw_sync_freertos/counting_semaphore.cc
@@ -41,13 +41,13 @@
     for (; update > 0; --update) {
       BaseType_t woke_higher_task = pdFALSE;
       const BaseType_t result =
-          xSemaphoreGiveFromISR(native_type_.handle, &woke_higher_task);
+          xSemaphoreGiveFromISR(&native_type_, &woke_higher_task);
       PW_DCHECK_UINT_EQ(result, pdTRUE, "Overflowed counting semaphore.");
       portYIELD_FROM_ISR(woke_higher_task);
     }
   } else {  // Task context
     for (; update > 0; --update) {
-      const BaseType_t result = xSemaphoreGive(native_type_.handle);
+      const BaseType_t result = xSemaphoreGive(&native_type_);
       PW_DCHECK_UINT_EQ(result, pdTRUE, "Overflowed counting semaphore.");
     }
   }
@@ -66,14 +66,12 @@
   constexpr SystemClock::duration kMaxTimeoutMinusOne =
       pw::chrono::freertos::kMaxTimeout - SystemClock::duration(1);
   while (for_at_least > kMaxTimeoutMinusOne) {
-    if (xSemaphoreTake(native_type_.handle, kMaxTimeoutMinusOne.count()) ==
-        pdTRUE) {
+    if (xSemaphoreTake(&native_type_, kMaxTimeoutMinusOne.count()) == pdTRUE) {
       return true;
     }
     for_at_least -= kMaxTimeoutMinusOne;
   }
-  return xSemaphoreTake(native_type_.handle, for_at_least.count() + 1) ==
-         pdTRUE;
+  return xSemaphoreTake(&native_type_, for_at_least.count() + 1) == pdTRUE;
 }
 
 }  // namespace pw::sync
diff --git a/pw_sync_freertos/mutex.cc b/pw_sync_freertos/mutex.cc
index 5ef6fc3..e8bb50b 100644
--- a/pw_sync_freertos/mutex.cc
+++ b/pw_sync_freertos/mutex.cc
@@ -48,14 +48,12 @@
   constexpr SystemClock::duration kMaxTimeoutMinusOne =
       pw::chrono::freertos::kMaxTimeout - SystemClock::duration(1);
   while (for_at_least > kMaxTimeoutMinusOne) {
-    if (xSemaphoreTake(native_type_.handle, kMaxTimeoutMinusOne.count()) ==
-        pdTRUE) {
+    if (xSemaphoreTake(&native_type_, kMaxTimeoutMinusOne.count()) == pdTRUE) {
       return true;
     }
     for_at_least -= kMaxTimeoutMinusOne;
   }
-  return xSemaphoreTake(native_type_.handle, for_at_least.count() + 1) ==
-         pdTRUE;
+  return xSemaphoreTake(&native_type_, for_at_least.count() + 1) == pdTRUE;
 }
 
 }  // namespace pw::sync
diff --git a/pw_sync_freertos/public/pw_sync_freertos/binary_semaphore_inline.h b/pw_sync_freertos/public/pw_sync_freertos/binary_semaphore_inline.h
index 1f0037f..143ef0f 100644
--- a/pw_sync_freertos/public/pw_sync_freertos/binary_semaphore_inline.h
+++ b/pw_sync_freertos/public/pw_sync_freertos/binary_semaphore_inline.h
@@ -24,39 +24,37 @@
 namespace pw::sync {
 
 inline BinarySemaphore::BinarySemaphore() : native_type_() {
-  native_type_.handle = xSemaphoreCreateBinaryStatic(&native_type_.buffer);
-  // This should never fail since the pointer provided was not null.
-  PW_DASSERT(native_type_.handle != nullptr);
+  const SemaphoreHandle_t handle = xSemaphoreCreateBinaryStatic(&native_type_);
+  // This should never fail since the pointer provided was not null and it
+  // should return a pointer to the StaticSemaphore_t.
+  PW_DASSERT(handle == &native_type_);
 }
 
-inline BinarySemaphore::~BinarySemaphore() {
-  vSemaphoreDelete(native_type_.handle);
-}
+inline BinarySemaphore::~BinarySemaphore() { vSemaphoreDelete(&native_type_); }
 
 inline void BinarySemaphore::release() {
   if (interrupt::InInterruptContext()) {
     BaseType_t woke_higher_task = pdFALSE;
     // It's perfectly fine if the semaphore already has a count of 1.
     [[maybe_unused]] BaseType_t already_full =
-        xSemaphoreGiveFromISR(native_type_.handle, &woke_higher_task);
+        xSemaphoreGiveFromISR(&native_type_, &woke_higher_task);
     portYIELD_FROM_ISR(woke_higher_task);
   } else {  // Task context
     // It's perfectly fine if the semaphore already has a count of 1.
-    [[maybe_unused]] BaseType_t already_full =
-        xSemaphoreGive(native_type_.handle);
+    [[maybe_unused]] BaseType_t already_full = xSemaphoreGive(&native_type_);
   }
 }
 
 inline void BinarySemaphore::acquire() {
   PW_ASSERT(!interrupt::InInterruptContext());
 #if INCLUDE_vTaskSuspend == 1  // This means portMAX_DELAY is indefinite.
-  const BaseType_t result = xSemaphoreTake(native_type_.handle, portMAX_DELAY);
+  const BaseType_t result = xSemaphoreTake(&native_type_, portMAX_DELAY);
   PW_DASSERT(result == pdTRUE);
 #else
   // In case we need to block for longer than the FreeRTOS delay can represent
   // repeatedly hit take until success.
-  while (xSemaphoreTake(native_type_.handle,
-                        chrono::freertos::kMaxTimeout.count()) == pdFALSE) {
+  while (xSemaphoreTake(&native_type_, chrono::freertos::kMaxTimeout.count()) ==
+         pdFALSE) {
   }
 #endif  // INCLUDE_vTaskSuspend
 }
@@ -65,13 +63,13 @@
   if (interrupt::InInterruptContext()) {
     BaseType_t woke_higher_task = pdFALSE;
     const bool success =
-        xSemaphoreTakeFromISR(native_type_.handle, &woke_higher_task) == pdTRUE;
+        xSemaphoreTakeFromISR(&native_type_, &woke_higher_task) == pdTRUE;
     portYIELD_FROM_ISR(woke_higher_task);
     return success;
   }
 
   // Task Context
-  return xSemaphoreTake(native_type_.handle, 0) == pdTRUE;
+  return xSemaphoreTake(&native_type_, 0) == pdTRUE;
 }
 
 inline bool BinarySemaphore::try_acquire_until(
diff --git a/pw_sync_freertos/public/pw_sync_freertos/binary_semaphore_native.h b/pw_sync_freertos/public/pw_sync_freertos/binary_semaphore_native.h
index 065fbe7..dcaead6 100644
--- a/pw_sync_freertos/public/pw_sync_freertos/binary_semaphore_native.h
+++ b/pw_sync_freertos/public/pw_sync_freertos/binary_semaphore_native.h
@@ -20,10 +20,7 @@
 
 namespace pw::sync::backend {
 
-struct NativeBinarySemaphore {
-  StaticSemaphore_t buffer;
-  SemaphoreHandle_t handle;
-};
+using NativeBinarySemaphore = StaticSemaphore_t;
 using NativeBinarySemaphoreHandle = NativeBinarySemaphore&;
 
 inline constexpr ptrdiff_t kBinarySemaphoreMaxValue =
diff --git a/pw_sync_freertos/public/pw_sync_freertos/counting_semaphore_inline.h b/pw_sync_freertos/public/pw_sync_freertos/counting_semaphore_inline.h
index 8fdf584..e5fb377 100644
--- a/pw_sync_freertos/public/pw_sync_freertos/counting_semaphore_inline.h
+++ b/pw_sync_freertos/public/pw_sync_freertos/counting_semaphore_inline.h
@@ -24,26 +24,27 @@
 namespace pw::sync {
 
 inline CountingSemaphore::CountingSemaphore() : native_type_() {
-  native_type_.handle =
-      xSemaphoreCreateCountingStatic(max(), 0, &native_type_.buffer);
-  // This should never fail since the pointer provided was not null.
-  PW_DASSERT(native_type_.handle != nullptr);
+  const SemaphoreHandle_t handle =
+      xSemaphoreCreateCountingStatic(max(), 0, &native_type_);
+  // This should never fail since the pointer provided was not null and it
+  // should return a pointer to the StaticSemaphore_t.
+  PW_DASSERT(handle == &native_type_);
 }
 
 inline CountingSemaphore::~CountingSemaphore() {
-  vSemaphoreDelete(native_type_.handle);
+  vSemaphoreDelete(&native_type_);
 }
 
 inline void CountingSemaphore::acquire() {
   PW_ASSERT(!interrupt::InInterruptContext());
 #if INCLUDE_vTaskSuspend == 1  // This means portMAX_DELAY is indefinite.
-  const BaseType_t result = xSemaphoreTake(native_type_.handle, portMAX_DELAY);
+  const BaseType_t result = xSemaphoreTake(&native_type_, portMAX_DELAY);
   PW_DASSERT(result == pdTRUE);
 #else
   // In case we need to block for longer than the FreeRTOS delay can represent
   // repeatedly hit take until success.
-  while (xSemaphoreTake(native_type_.handle,
-                        chrono::freertos::kMaxTimeout.count()) == pdFALSE) {
+  while (xSemaphoreTake(&native_type_, chrono::freertos::kMaxTimeout.count()) ==
+         pdFALSE) {
   }
 #endif  // INCLUDE_vTaskSuspend
 }
@@ -52,13 +53,13 @@
   if (interrupt::InInterruptContext()) {
     BaseType_t woke_higher_task = pdFALSE;
     const bool success =
-        xSemaphoreTakeFromISR(native_type_.handle, &woke_higher_task) == pdTRUE;
+        xSemaphoreTakeFromISR(&native_type_, &woke_higher_task) == pdTRUE;
     portYIELD_FROM_ISR(woke_higher_task);
     return success;
   }
 
   // Task Context
-  return xSemaphoreTake(native_type_.handle, 0) == pdTRUE;
+  return xSemaphoreTake(&native_type_, 0) == pdTRUE;
 }
 
 inline bool CountingSemaphore::try_acquire_until(
diff --git a/pw_sync_freertos/public/pw_sync_freertos/counting_semaphore_native.h b/pw_sync_freertos/public/pw_sync_freertos/counting_semaphore_native.h
index 8d6ba75..c9d436e 100644
--- a/pw_sync_freertos/public/pw_sync_freertos/counting_semaphore_native.h
+++ b/pw_sync_freertos/public/pw_sync_freertos/counting_semaphore_native.h
@@ -20,10 +20,7 @@
 
 namespace pw::sync::backend {
 
-struct NativeCountingSemaphore {
-  StaticSemaphore_t buffer;
-  SemaphoreHandle_t handle;
-};
+using NativeCountingSemaphore = StaticSemaphore_t;
 using NativeCountingSemaphoreHandle = NativeCountingSemaphore&;
 
 inline constexpr ptrdiff_t kCountingSemaphoreMaxValue =
diff --git a/pw_sync_freertos/public/pw_sync_freertos/mutex_inline.h b/pw_sync_freertos/public/pw_sync_freertos/mutex_inline.h
index 31bcf14..534e9ce 100644
--- a/pw_sync_freertos/public/pw_sync_freertos/mutex_inline.h
+++ b/pw_sync_freertos/public/pw_sync_freertos/mutex_inline.h
@@ -24,30 +24,31 @@
 namespace pw::sync {
 
 inline Mutex::Mutex() : native_type_() {
-  native_type_.handle = xSemaphoreCreateMutexStatic(&native_type_.buffer);
-  // This should never fail since the pointer provided was not null.
-  PW_DASSERT(native_type_.handle != nullptr);
+  const SemaphoreHandle_t handle = xSemaphoreCreateMutexStatic(&native_type_);
+  // This should never fail since the pointer provided was not null and it
+  // should return a pointer to the StaticSemaphore_t.
+  PW_DASSERT(handle == &native_type_);
 }
 
-inline Mutex::~Mutex() { vSemaphoreDelete(native_type_.handle); }
+inline Mutex::~Mutex() { vSemaphoreDelete(&native_type_); }
 
 inline void Mutex::lock() {
   PW_ASSERT(!interrupt::InInterruptContext());
 #if INCLUDE_vTaskSuspend == 1  // This means portMAX_DELAY is indefinite.
-  const BaseType_t result = xSemaphoreTake(native_type_.handle, portMAX_DELAY);
+  const BaseType_t result = xSemaphoreTake(&native_type_, portMAX_DELAY);
   PW_DASSERT(result == pdTRUE);
 #else
   // In case we need to block for longer than the FreeRTOS delay can represent
   // repeatedly hit take until success.
-  while (xSemaphoreTake(native_type_.handle,
-                        chrono::freertos::kMaxTimeout.count()) == pdFALSE) {
+  while (xSemaphoreTake(&native_type_, chrono::freertos::kMaxTimeout.count()) ==
+         pdFALSE) {
   }
 #endif  // INCLUDE_vTaskSuspend
 }
 
 inline bool Mutex::try_lock() {
   PW_ASSERT(!interrupt::InInterruptContext());
-  return xSemaphoreTake(native_type_.handle, 0) == pdTRUE;
+  return xSemaphoreTake(&native_type_, 0) == pdTRUE;
 }
 
 inline bool Mutex::try_lock_until(
@@ -60,7 +61,7 @@
 inline void Mutex::unlock() {
   PW_ASSERT(!interrupt::InInterruptContext());
   // Unlocking only fails if it was not locked first.
-  PW_ASSERT(xSemaphoreGive(native_type_.handle) == pdTRUE);
+  PW_ASSERT(xSemaphoreGive(&native_type_) == pdTRUE);
 }
 
 inline Mutex::native_handle_type Mutex::native_handle() { return native_type_; }
diff --git a/pw_sync_freertos/public/pw_sync_freertos/mutex_native.h b/pw_sync_freertos/public/pw_sync_freertos/mutex_native.h
index 17613b9..973ff8f 100644
--- a/pw_sync_freertos/public/pw_sync_freertos/mutex_native.h
+++ b/pw_sync_freertos/public/pw_sync_freertos/mutex_native.h
@@ -18,10 +18,7 @@
 
 namespace pw::sync::backend {
 
-struct NativeMutex {
-  StaticSemaphore_t buffer;
-  SemaphoreHandle_t handle;
-};
+using NativeMutex = StaticSemaphore_t;
 using NativeMutexHandle = NativeMutex&;
 
 }  // namespace pw::sync::backend