pw_thread_embos: adds pw::this_thread support

Adds thread identification, yield, and sleep support for embOS v4.

Change-Id: I9ed552cf83035b4589740f88283b335194dcdc1a
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/36421
Reviewed-by: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
Pigweed-Auto-Submit: Ewout van Bekkum <ewout@google.com>
Commit-Queue: Ewout van Bekkum <ewout@google.com>
diff --git a/modules.gni b/modules.gni
index caf19fe..54c92a3 100644
--- a/modules.gni
+++ b/modules.gni
@@ -92,6 +92,7 @@
   dir_pw_target_runner = get_path_info("pw_target_runner", "abspath")
   dir_pw_thread = get_path_info("pw_thread", "abspath")
   dir_pw_thread_stl = get_path_info("pw_thread_stl", "abspath")
+  dir_pw_thread_embos = get_path_info("pw_thread_embos", "abspath")
   dir_pw_thread_freertos = get_path_info("pw_thread_freertos", "abspath")
   dir_pw_thread_threadx = get_path_info("pw_thread_threadx", "abspath")
   dir_pw_third_party = get_path_info("third_party", "abspath")
diff --git a/pw_thread_embos/BUILD b/pw_thread_embos/BUILD
new file mode 100644
index 0000000..dd35c30
--- /dev/null
+++ b/pw_thread_embos/BUILD
@@ -0,0 +1,99 @@
+# Copyright 2021 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.
+
+load(
+    "//pw_build:pigweed.bzl",
+    "pw_cc_library",
+)
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])  # Apache License 2.0
+
+pw_cc_library(
+    name = "id_headers",
+    hdrs = [
+        "public/pw_thread_embos/id_inline.h",
+        "public/pw_thread_embos/id_native.h",
+        "public_overrides/pw_thread_backend/id_inline.h",
+        "public_overrides/pw_thread_backend/id_native.h",
+    ],
+    includes = [
+        "public",
+        "public_overrides",
+    ],
+)
+
+pw_cc_library(
+    name = "id",
+    deps = [
+        ":id_headers",
+        "//pw_thread:id_facade",
+    ],
+    # TODO(pwbug/317): This should depend on embOS but our third parties
+    # currently do not have Bazel support.
+)
+
+pw_cc_library(
+    name = "sleep_headers",
+    hdrs = [
+        "public/pw_thread_embos/sleep_inline.h",
+        "public_overrides/pw_thread_backend/sleep_inline.h",
+    ],
+    includes = [
+        "public",
+        "public_overrides",
+    ],
+    deps = [
+        "//pw_chrono:system_clock",
+    ],
+)
+
+pw_cc_library(
+    name = "sleep",
+    srcs = [
+        "sleep.cc",
+    ],
+    deps = [
+        ":sleep_headers",
+        "//pw_chrono_embos:system_clock_headers",
+        "//pw_assert",
+        "//pw_chrono:system_clock",
+        "//pw_thread:sleep_facade",
+    ],
+    # TODO(pwbug/317): This should depend on embOS but our third parties
+    # currently do not have Bazel support.
+)
+
+pw_cc_library(
+    name = "yield_headers",
+    hdrs = [
+        "public/pw_thread_embos/yield_inline.h",
+        "public_overrides/pw_thread_backend/yield_inline.h",
+    ],
+    includes = [
+        "public",
+        "public_overrides",
+    ],
+    # TODO(pwbug/317): This should depend on embOS but our third parties
+    # currently do not have Bazel support.
+)
+
+pw_cc_library(
+    name = "yield",
+    deps = [
+        ":yield_headers",
+        "//pw_thread:yield_facade",
+    ],
+)
diff --git a/pw_thread_embos/BUILD.gn b/pw_thread_embos/BUILD.gn
new file mode 100644
index 0000000..4e76548
--- /dev/null
+++ b/pw_thread_embos/BUILD.gn
@@ -0,0 +1,100 @@
+# Copyright 2021 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.
+
+import("//build_overrides/pigweed.gni")
+
+import("$dir_pw_build/target_types.gni")
+import("$dir_pw_chrono/backend.gni")
+import("$dir_pw_docgen/docs.gni")
+import("$dir_pw_thread/backend.gni")
+
+config("public_include_path") {
+  include_dirs = [ "public" ]
+  visibility = [ ":*" ]
+}
+
+config("backend_config") {
+  include_dirs = [ "public_overrides" ]
+  visibility = [ ":*" ]
+}
+
+# This target provides the backend for pw::thread::Id.
+pw_source_set("id") {
+  public_configs = [
+    ":public_include_path",
+    ":backend_config",
+  ]
+  public_deps = [
+    "$dir_pw_assert",
+    "$dir_pw_interrupt:context",
+    "$dir_pw_third_party/embos",
+  ]
+  public = [
+    "public/pw_thread_embos/id_inline.h",
+    "public/pw_thread_embos/id_native.h",
+    "public_overrides/pw_thread_backend/id_inline.h",
+    "public_overrides/pw_thread_backend/id_native.h",
+  ]
+  deps = [ "$dir_pw_thread:id.facade" ]
+}
+
+if (pw_chrono_SYSTEM_CLOCK_BACKEND != "" && pw_thread_SLEEP_BACKEND != "") {
+  # This target provides the backend for pw::thread::sleep_{for,until}.
+  pw_source_set("sleep") {
+    public_configs = [
+      ":public_include_path",
+      ":backend_config",
+    ]
+    public = [
+      "public/pw_thread_embos/sleep_inline.h",
+      "public_overrides/pw_thread_backend/sleep_inline.h",
+    ]
+    public_deps = [ "$dir_pw_chrono:system_clock" ]
+    sources = [ "sleep.cc" ]
+    deps = [
+      "$dir_pw_assert",
+      "$dir_pw_chrono_embos:system_clock",
+      "$dir_pw_third_party/embos",
+      "$dir_pw_thread:id",
+      "$dir_pw_thread:sleep.facade",
+    ]
+    assert(pw_thread_OVERRIDE_SYSTEM_CLOCK_BACKEND_CHECK ||
+               pw_chrono_SYSTEM_CLOCK_BACKEND ==
+                   "$dir_pw_chrono_embos:system_clock",
+           "The embOS pw::thread::sleep_{for,until} backend only works with " +
+               "the embOS pw::chrono::SystemClock backend.")
+  }
+}
+
+# This target provides the backend for pw::thread::yield.
+pw_source_set("yield") {
+  public_configs = [
+    ":public_include_path",
+    ":backend_config",
+  ]
+  public = [
+    "public/pw_thread_embos/yield_inline.h",
+    "public_overrides/pw_thread_backend/yield_inline.h",
+  ]
+  public_deps = [
+    "$dir_pw_assert",
+    "$dir_pw_third_party/embos",
+    "$dir_pw_thread:id",
+  ]
+  deps = [ "$dir_pw_thread:yield.facade" ]
+}
+
+pw_doc_group("docs") {
+  sources = [ "docs.rst" ]
+}
diff --git a/pw_thread_embos/docs.rst b/pw_thread_embos/docs.rst
new file mode 100644
index 0000000..cad1908
--- /dev/null
+++ b/pw_thread_embos/docs.rst
@@ -0,0 +1,8 @@
+.. _module-pw_thread_embos:
+
+---------------
+pw_thread_embos
+---------------
+This is a set of backends for pw_thread based on embOS v4. It is not ready for
+use, and is under construction.
+
diff --git a/pw_thread_embos/public/pw_thread_embos/id_inline.h b/pw_thread_embos/public/pw_thread_embos/id_inline.h
new file mode 100644
index 0000000..b11078a
--- /dev/null
+++ b/pw_thread_embos/public/pw_thread_embos/id_inline.h
@@ -0,0 +1,27 @@
+// Copyright 2021 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 "RTOS.h"
+#include "pw_assert/light.h"
+#include "pw_thread/id.h"
+
+namespace pw::this_thread {
+
+inline thread::Id get_id() {
+  PW_DASSERT(OS_IsRunning() != 0);
+  return thread::Id(OS_GetTaskID());
+}
+
+}  // namespace pw::this_thread
diff --git a/pw_thread_embos/public/pw_thread_embos/id_native.h b/pw_thread_embos/public/pw_thread_embos/id_native.h
new file mode 100644
index 0000000..4d30539
--- /dev/null
+++ b/pw_thread_embos/public/pw_thread_embos/id_native.h
@@ -0,0 +1,51 @@
+// Copyright 2021 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 "RTOS.h"
+
+namespace pw::thread::backend {
+
+// Instead of using a pw::thread::embos specific identifier, the ThreadX
+// thread pointer is used as this means pw::this_thread::id works correctly on
+// threads started with the native ThreadX APIs as well as those started
+// using the pw::thread APIs.
+class NativeId {
+ public:
+  constexpr NativeId(OS_TASK* task_ptr = nullptr) : task_ptr_(task_ptr) {}
+
+  constexpr bool operator==(NativeId other) const {
+    return task_ptr_ == other.task_ptr_;
+  }
+  constexpr bool operator!=(NativeId other) const {
+    return task_ptr_ != other.task_ptr_;
+  }
+  constexpr bool operator<(NativeId other) const {
+    return task_ptr_ < other.task_ptr_;
+  }
+  constexpr bool operator<=(NativeId other) const {
+    return task_ptr_ <= other.task_ptr_;
+  }
+  constexpr bool operator>(NativeId other) const {
+    return task_ptr_ > other.task_ptr_;
+  }
+  constexpr bool operator>=(NativeId other) const {
+    return task_ptr_ >= other.task_ptr_;
+  }
+
+ private:
+  OS_TASK* task_ptr_;
+};
+
+}  // namespace pw::thread::backend
diff --git a/pw_thread_embos/public/pw_thread_embos/sleep_inline.h b/pw_thread_embos/public/pw_thread_embos/sleep_inline.h
new file mode 100644
index 0000000..e39ad17
--- /dev/null
+++ b/pw_thread_embos/public/pw_thread_embos/sleep_inline.h
@@ -0,0 +1,24 @@
+// Copyright 2021 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_chrono/system_clock.h"
+
+namespace pw::this_thread {
+
+inline void sleep_until(chrono::SystemClock::time_point until_at_least) {
+  return sleep_for(until_at_least - chrono::SystemClock::now());
+}
+
+}  // namespace pw::this_thread
diff --git a/pw_thread_embos/public/pw_thread_embos/yield_inline.h b/pw_thread_embos/public/pw_thread_embos/yield_inline.h
new file mode 100644
index 0000000..2ad21fb
--- /dev/null
+++ b/pw_thread_embos/public/pw_thread_embos/yield_inline.h
@@ -0,0 +1,27 @@
+// Copyright 2021 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 "RTOS.h"
+#include "pw_assert/light.h"
+#include "pw_thread/id.h"
+
+namespace pw::this_thread {
+
+inline void yield() {
+  PW_DASSERT(get_id() != thread::Id());
+  OS_Yield();
+}
+
+}  // namespace pw::this_thread
diff --git a/pw_thread_embos/public_overrides/pw_thread_backend/id_inline.h b/pw_thread_embos/public_overrides/pw_thread_backend/id_inline.h
new file mode 100644
index 0000000..3ce10e0
--- /dev/null
+++ b/pw_thread_embos/public_overrides/pw_thread_backend/id_inline.h
@@ -0,0 +1,16 @@
+// Copyright 2021 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_thread_embos/id_inline.h"
diff --git a/pw_thread_embos/public_overrides/pw_thread_backend/id_native.h b/pw_thread_embos/public_overrides/pw_thread_backend/id_native.h
new file mode 100644
index 0000000..9dd53ec
--- /dev/null
+++ b/pw_thread_embos/public_overrides/pw_thread_backend/id_native.h
@@ -0,0 +1,16 @@
+// Copyright 2021 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_thread_embos/id_native.h"
diff --git a/pw_thread_embos/public_overrides/pw_thread_backend/sleep_inline.h b/pw_thread_embos/public_overrides/pw_thread_backend/sleep_inline.h
new file mode 100644
index 0000000..12db456
--- /dev/null
+++ b/pw_thread_embos/public_overrides/pw_thread_backend/sleep_inline.h
@@ -0,0 +1,16 @@
+// Copyright 2021 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_thread_embos/sleep_inline.h"
diff --git a/pw_thread_embos/public_overrides/pw_thread_backend/yield_inline.h b/pw_thread_embos/public_overrides/pw_thread_backend/yield_inline.h
new file mode 100644
index 0000000..6b07796
--- /dev/null
+++ b/pw_thread_embos/public_overrides/pw_thread_backend/yield_inline.h
@@ -0,0 +1,16 @@
+// Copyright 2021 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_thread_embos/yield_inline.h"
diff --git a/pw_thread_embos/sleep.cc b/pw_thread_embos/sleep.cc
new file mode 100644
index 0000000..410967c
--- /dev/null
+++ b/pw_thread_embos/sleep.cc
@@ -0,0 +1,50 @@
+// Copyright 2021 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_thread/sleep.h"
+
+#include <algorithm>
+
+#include "RTOS.h"
+#include "pw_assert/assert.h"
+#include "pw_chrono/system_clock.h"
+#include "pw_chrono_embos/system_clock_constants.h"
+#include "pw_thread/id.h"
+
+using pw::chrono::embos::kMaxTimeout;
+
+namespace pw::this_thread {
+
+void sleep_for(chrono::SystemClock::duration for_at_least) {
+  PW_DCHECK(get_id() != thread::Id());
+
+  // Clamp negative durations to be 0 which maps to non-blocking.
+  for_at_least = std::max(for_at_least, chrono::SystemClock::duration::zero());
+
+  // The pw::sleep_{for,until} API contract is to yield if we attempt to sleep
+  // for a duration of 0. The embOS delay does not explicitly yield if 0 is
+  // passed, ergo we explicitly check for the yield condition.
+  if (for_at_least == chrono::SystemClock::duration::zero()) {
+    OS_Yield();  // Direct API is used to reduce overhead.
+    return;
+  }
+
+  while (for_at_least > kMaxTimeout) {
+    OS_Delay(kMaxTimeout.count());
+    for_at_least -= kMaxTimeout;
+  }
+  OS_Delay(for_at_least.count());
+}
+
+}  // namespace pw::this_thread