1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_sync/thread_notification.h"
16
17 #include <chrono>
18
19 #include "FreeRTOS.h"
20 #include "gtest/gtest.h"
21 #include "pw_chrono/system_clock.h"
22 #include "pw_thread/sleep.h"
23 #include "pw_thread/test_threads.h"
24 #include "pw_thread/thread.h"
25 #include "pw_thread/thread_core.h"
26 #include "task.h"
27
28 namespace pw::sync::freertos {
29 namespace {
30
31 using pw::chrono::SystemClock;
32 using pw::thread::Thread;
33
34 } // namespace
35
36 // These tests are targeted specifically to verify interactions between suspend
37 // and being blocked on direct task notifications and how they impact usage of
38 // the FreeRTOS optimized ThreadNotification backend.
39 #if INCLUDE_vTaskSuspend == 1
40
41 class NotificationAcquirer : public thread::ThreadCore {
42 public:
WaitUntilRunning()43 void WaitUntilRunning() { started_notification_.acquire(); }
Release()44 void Release() { unblock_notification_.release(); }
WaitUntilFinished()45 void WaitUntilFinished() { finished_notification_.acquire(); }
notified_time() const46 std::optional<SystemClock::time_point> notified_time() const {
47 return notified_time_;
48 }
task_handle() const49 TaskHandle_t task_handle() const { return task_handle_; }
50
51 private:
Run()52 void Run() final {
53 task_handle_ = xTaskGetCurrentTaskHandle();
54 started_notification_.release();
55 unblock_notification_.acquire();
56 notified_time_ = SystemClock::now();
57 finished_notification_.release();
58 }
59
60 TaskHandle_t task_handle_;
61 ThreadNotification started_notification_;
62 ThreadNotification unblock_notification_;
63 ThreadNotification finished_notification_;
64 std::optional<SystemClock::time_point> notified_time_;
65 };
66
TEST(ThreadNotification,AcquireWithoutSuspend)67 TEST(ThreadNotification, AcquireWithoutSuspend) {
68 NotificationAcquirer notification_acquirer;
69 Thread thread =
70 Thread(thread::test::TestOptionsThread0(), notification_acquirer);
71
72 notification_acquirer.WaitUntilRunning();
73 // At this point the thread is blocked and waiting on the notification.
74 const SystemClock::time_point release_time = SystemClock::now();
75 notification_acquirer.Release();
76 notification_acquirer.WaitUntilFinished();
77 ASSERT_TRUE(notification_acquirer.notified_time().has_value());
78 EXPECT_GE(notification_acquirer.notified_time().value(), release_time);
79
80 // Clean up the test thread context.
81 #if PW_THREAD_JOINING_ENABLED
82 thread.join();
83 #else
84 thread.detach();
85 thread::test::WaitUntilDetachedThreadsCleanedUp();
86 #endif // PW_THREAD_JOINING_ENABLED
87 }
88
TEST(ThreadNotification,AcquireWithSuspend)89 TEST(ThreadNotification, AcquireWithSuspend) {
90 NotificationAcquirer notification_acquirer;
91 Thread thread =
92 Thread(thread::test::TestOptionsThread0(), notification_acquirer);
93
94 notification_acquirer.WaitUntilRunning();
95
96 // Suspend and resume the task before notifying it, which should cause the
97 // internal xTaskNotifyWait to stop blocking and return pdFALSE upon resume.
98 vTaskSuspend(notification_acquirer.task_handle());
99 vTaskResume(notification_acquirer.task_handle());
100
101 // Sleep for at least one tick to ensure the time moved forward to let us
102 // observe the unblock time is in fact after resumed it.
103 this_thread::sleep_for(SystemClock::duration(1));
104
105 // At this point the thread is blocked and waiting on the notification.
106 const SystemClock::time_point release_time = SystemClock::now();
107 notification_acquirer.Release();
108 notification_acquirer.WaitUntilFinished();
109 ASSERT_TRUE(notification_acquirer.notified_time().has_value());
110 EXPECT_GE(notification_acquirer.notified_time().value(), release_time);
111
112 // Clean up the test thread context.
113 #if PW_THREAD_JOINING_ENABLED
114 thread.join();
115 #else
116 thread.detach();
117 thread::test::WaitUntilDetachedThreadsCleanedUp();
118 #endif // PW_THREAD_JOINING_ENABLED
119 }
120
121 #endif // INCLUDE_vTaskSuspend == 1
122
123 } // namespace pw::sync::freertos
124