pw_thread_freertos: adds thread creation

Adds pw::thread::Thread creation for FreeRTOS.

Note that this also refactors the pw_thread thread facade tests
to use binary semaphores and adds a helper method to re-use
test threads even if detached. Note that this is by definition
flaky without join, ergo excessive target specific sleeps are
used when needed.

Change-Id: I3f2344c7a6ddbafa6781914991d6378e05ad0106
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/30921
Pigweed-Auto-Submit: Ewout van Bekkum <ewout@google.com>
Commit-Queue: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
diff --git a/pw_thread_freertos/BUILD.gn b/pw_thread_freertos/BUILD.gn
index 24bb882..7071804 100644
--- a/pw_thread_freertos/BUILD.gn
+++ b/pw_thread_freertos/BUILD.gn
@@ -14,9 +14,19 @@
 
 import("//build_overrides/pigweed.gni")
 
+import("$dir_pw_build/module_config.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")
+import("$dir_pw_unit_test/test.gni")
+
+declare_args() {
+  # The build target that overrides the default configuration options for this
+  # module. This should point to a source set that provides defines through a
+  # public config (which may -include a file or add defines directly).
+  pw_thread_freertos_CONFIG = pw_build_DEFAULT_MODULE_CONFIG
+}
 
 config("public_include_path") {
   include_dirs = [ "public" ]
@@ -28,6 +38,15 @@
   visibility = [ ":*" ]
 }
 
+pw_source_set("config") {
+  public = [ "public/pw_thread_freertos/config.h" ]
+  public_configs = [ ":public_include_path" ]
+  public_deps = [
+    "$dir_pw_third_party/freertos",
+    pw_thread_freertos_CONFIG,
+  ]
+}
+
 # This target provides the backend for pw::thread::Id & pw::this_thread::get_id.
 pw_source_set("id") {
   public_configs = [
@@ -75,6 +94,32 @@
              "\"$dir_pw_chrono_freertos:system_clock\")")
 }
 
+# This target provides the backend for pw::thread::Thread and the headers needed
+# for thread creation.
+pw_source_set("thread") {
+  public_configs = [
+    ":public_include_path",
+    ":backend_config",
+  ]
+  public_deps = [
+    ":config",
+    "$dir_pw_assert",
+    "$dir_pw_third_party/freertos",
+    "$dir_pw_thread:id",
+    "$dir_pw_thread:thread.facade",
+  ]
+  public = [
+    "public/pw_thread_freertos/context.h",
+    "public/pw_thread_freertos/options.h",
+    "public/pw_thread_freertos/thread_inline.h",
+    "public/pw_thread_freertos/thread_native.h",
+    "public_overrides/pw_thread_backend/thread_inline.h",
+    "public_overrides/pw_thread_backend/thread_native.h",
+  ]
+  allow_circular_includes_from = [ "$dir_pw_thread:thread.facade" ]
+  sources = [ "thread.cc" ]
+}
+
 # This target provides the backend for pw::this_thread::yield.
 pw_source_set("yield") {
   public_configs = [
@@ -93,6 +138,49 @@
   deps = [ "$dir_pw_thread:yield.facade" ]
 }
 
+pw_test_group("tests") {
+  tests = [
+    ":dynamic_thread_backend_test",
+    ":static_thread_backend_test",
+  ]
+}
+
+pw_source_set("dynamic_test_threads") {
+  public_deps = [ "$dir_pw_thread:test_threads" ]
+  sources = [ "dynamic_test_threads.cc" ]
+  deps = [
+    "$dir_pw_chrono:system_clock",
+    "$dir_pw_thread:sleep",
+    "$dir_pw_thread:thread",
+  ]
+}
+
+pw_test("dynamic_thread_backend_test") {
+  enable_if = pw_thread_THREAD_BACKEND == "$dir_pw_thread_freertos:thread"
+  deps = [
+    ":dynamic_test_threads",
+    "$dir_pw_thread:thread_facade_test",
+  ]
+}
+
+pw_source_set("static_test_threads") {
+  public_deps = [ "$dir_pw_thread:test_threads" ]
+  sources = [ "static_test_threads.cc" ]
+  deps = [
+    "$dir_pw_chrono:system_clock",
+    "$dir_pw_thread:sleep",
+    "$dir_pw_thread:thread",
+  ]
+}
+
+pw_test("static_thread_backend_test") {
+  enable_if = pw_thread_THREAD_BACKEND == "$dir_pw_thread_freertos:thread"
+  deps = [
+    ":static_test_threads",
+    "$dir_pw_thread:thread_facade_test",
+  ]
+}
+
 pw_doc_group("docs") {
   sources = [ "docs.rst" ]
 }