pw_sync_zephyr: Add thread_notifications

Add thread_notification backends

Change-Id: I20ee525ba2feecc47012814c18b493a3a6efa170
Signed-off-by: Al Semjonovs <asemjonovs@google.com>
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/141951
Reviewed-by: Anthony DiGirolamo <tonymd@google.com>
Reviewed-by: Yuval Peress <peress@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c4c2caa..d27082f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -46,6 +46,10 @@
       pw_sync.mutex pw_sync_zephyr.binary_semaphore_backend pw_sync/backend.cmake)
   pw_set_zephyr_backend_ifdef(CONFIG_PIGWEED_SYNC_INTERRUPT_SPIN_LOCK
       pw_sync.interrupt_spin_lock pw_sync_zephyr.interrupt_spin_lock_backend pw_sync/backend.cmake)
+  pw_set_zephyr_backend_ifdef(CONFIG_PIGWEED_SYNC_THREAD_NOTIFICATION
+    pw_sync.thread_notification pw_sync_zephyr.thread_notification_backend pw_sync/backend.cmake)
+  pw_set_zephyr_backend_ifdef(CONFIG_PIGWEED_SYNC_TIMED_THREAD_NOTIFICATION
+      pw_sync.timed_thread_notification pw_sync_zephyr.timed_thread_notification_backend pw_sync/backend.cmake)
   pw_set_zephyr_backend_ifdef(CONFIG_PIGWEED_SYNC_MUTEX
       pw_sync.mutex pw_sync_zephyr.mutex_backend pw_sync/backend.cmake)
   pw_set_zephyr_backend_ifdef(CONFIG_PIGWEED_SYS_IO
diff --git a/pw_sync_zephyr/CMakeLists.txt b/pw_sync_zephyr/CMakeLists.txt
index 9c41120..8aad59e 100644
--- a/pw_sync_zephyr/CMakeLists.txt
+++ b/pw_sync_zephyr/CMakeLists.txt
@@ -69,3 +69,38 @@
     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.facade
+    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.facade
+    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 111e72d..ed748df 100644
--- a/pw_sync_zephyr/Kconfig
+++ b/pw_sync_zephyr/Kconfig
@@ -29,3 +29,11 @@
 
 config PIGWEED_SYNC_INTERRUPT_SPIN_LOCK
     bool "Link pw_sync.interrupt_spin_lock and set the Zephyr backend"
+
+config PIGWEED_SYNC_THREAD_NOTIFICATION
+    bool "Link pw_sync.thread_notification and set the Zephyr backend"
+    select PIGWEED_SYNC_BINARY_SEMAPHORE
+
+config PIGWEED_SYNC_TIMED_THREAD_NOTIFICATION
+    bool "Link pw_sync.timed_thread_notification and set the Zephyr backend"
+    select PIGWEED_SYNC_BINARY_SEMAPHORE
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 0000000..f14028c
--- /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 0000000..5fb0495
--- /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 0000000..1d06697
--- /dev/null
+++ b/pw_sync_zephyr/public/pw_sync_zephyr/timed_thread_notification_inline.h
@@ -0,0 +1,28 @@
+// 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);
+}
+
+}  // namespace pw::sync
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 0000000..f18bf3a
--- /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 0000000..482807e
--- /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 0000000..0806238
--- /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"