@@ -41,24 +41,90 @@ extern "C" void app_main(void) {
41
41
auto task0 = std::bind (task, 0 );
42
42
auto task1 = std::bind (task, 1 );
43
43
auto task2 = std::bind (task, 2 );
44
- runqueue.add_function (
44
+ auto task3 = std::bind (task, 3 );
45
+ auto task0_id = runqueue.add_function (
45
46
task0,
46
47
espp::RunQueue::MIN_PRIORITY); // this will run first because it was first to be added
47
- runqueue.add_function (task1, espp::RunQueue::MIN_PRIORITY + 1 ); // this will run third
48
- runqueue.add_function (task2, espp::RunQueue::MIN_PRIORITY + 2 ); // this will run second
49
- runqueue.add_function (stop_task, espp::RunQueue::MIN_PRIORITY); // this will run last
48
+ auto task1_id =
49
+ runqueue.add_function (task1, espp::RunQueue::MIN_PRIORITY + 1 ); // this will run fourth
50
+ auto task3_id =
51
+ runqueue.add_function (task3, espp::RunQueue::MAX_PRIORITY); // this will run second
52
+ auto task2_id =
53
+ runqueue.add_function (task2, espp::RunQueue::MIN_PRIORITY + 2 ); // this will run third
54
+ auto stop_task_id =
55
+ runqueue.add_function (stop_task, espp::RunQueue::MIN_PRIORITY); // this will run last
50
56
logger.info (" All tasks scheduled!" );
51
57
58
+ // print our the task ids
59
+ logger.info (" Task 0 id: {}" , task0_id);
60
+ logger.info (" Task 1 id: {}" , task1_id);
61
+ logger.info (" Task 2 id: {}" , task2_id);
62
+ logger.info (" Task 3 id: {}" , task3_id);
63
+ logger.info (" Stop task id: {}" , stop_task_id);
64
+
52
65
// check the API for queue_size
53
66
logger.info (" Queue size: {}" , runqueue.queue_size ());
54
67
55
68
// check the API for is_running
56
69
logger.info (" RunQueue is running: {}" , runqueue.is_running ());
57
70
71
+ // check the API for is_function_queued
72
+ logger.info (" Task 0 is queued: {}" , runqueue.is_function_queued (task0_id));
73
+ logger.info (" Task 1 is queued: {}" , runqueue.is_function_queued (task1_id));
74
+ logger.info (" Task 2 is queued: {}" , runqueue.is_function_queued (task2_id));
75
+ logger.info (" Task 3 is queued: {}" , runqueue.is_function_queued (task3_id));
76
+ logger.info (" Stop task is queued: {}" , runqueue.is_function_queued (stop_task_id));
77
+
78
+ // check the API for removing an invalid id
79
+ if (runqueue.remove_function (espp::RunQueue::INVALID_ID)) {
80
+ logger.error (" Incorrectly Removed invalid id!" );
81
+ } else {
82
+ logger.info (" Correctly failed to remove invalid id!" );
83
+ }
84
+
85
+ // check the api for removing a valid but non-existent id
86
+ if (runqueue.remove_function (999 )) {
87
+ logger.error (" Incorrectly Removed non-existent id!" );
88
+ } else {
89
+ logger.info (" Correctly failed to remove non-existent id!" );
90
+ }
91
+
92
+ // check the api for removing the currently running id (which should fail)
93
+ if (runqueue.remove_function (runqueue.get_running_id ().value ())) {
94
+ logger.error (" Incorrectly Removed currently running id!" );
95
+ } else {
96
+ logger.info (" Correctly failed to remove currently running id!" );
97
+ }
98
+
99
+ // NOTE: in the next example (below) we'll check removing a valid ID
100
+
101
+ // check the API for get_queued_ids(bool include_running)
102
+ auto queued_ids = runqueue.get_queued_ids (true );
103
+ logger.info (" Queued ids (including running): {}" , queued_ids);
104
+
105
+ // check the API for get_running_id
106
+ auto running_id = runqueue.get_running_id ();
107
+ logger.info (" Running id: {}" , running_id);
108
+
58
109
logger.info (" Waiting for stop task to complete..." );
59
110
std::unique_lock lock (done_mutex);
60
111
done_cv.wait (lock, [&done] { return done; });
61
112
113
+ // check the API for get_running_id again (should return nullopt)
114
+ running_id = runqueue.get_running_id ();
115
+ logger.info (" Running id: {}" , running_id);
116
+
117
+ // check the api for get_queued_ids again
118
+ queued_ids = runqueue.get_queued_ids (true );
119
+ logger.info (" Queued ids (including running): {}" , queued_ids);
120
+
121
+ // check the api for is_function_queued again
122
+ logger.info (" Task 0 is queued: {}" , runqueue.is_function_queued (task0_id));
123
+ logger.info (" Task 1 is queued: {}" , runqueue.is_function_queued (task1_id));
124
+ logger.info (" Task 2 is queued: {}" , runqueue.is_function_queued (task2_id));
125
+ logger.info (" Task 3 is queued: {}" , runqueue.is_function_queued (task3_id));
126
+ logger.info (" Stop task is queued: {}" , runqueue.is_function_queued (stop_task_id));
127
+
62
128
logger.info (" All tasks done!" );
63
129
// ! [runqueue example]
64
130
}
@@ -99,6 +165,8 @@ extern "C" void app_main(void) {
99
165
espp::RunQueue::MIN_PRIORITY); // this will run first because it was first to be added
100
166
runqueue0.add_function (task1, espp::RunQueue::MIN_PRIORITY + 1 ); // this will run third
101
167
runqueue0.add_function (task2, espp::RunQueue::MIN_PRIORITY + 2 ); // this will run second
168
+ auto id_to_remove = runqueue0.add_function (stop_task,
169
+ espp::RunQueue::MIN_PRIORITY); // this will run last
102
170
103
171
runqueue1.add_function (
104
172
task0,
@@ -108,6 +176,13 @@ extern "C" void app_main(void) {
108
176
runqueue1.add_function (stop_task, espp::RunQueue::MIN_PRIORITY); // this will run last
109
177
logger.info (" All tasks scheduled!" );
110
178
179
+ // now remove the stop task from runqueue0
180
+ if (!runqueue0.remove_function (id_to_remove)) {
181
+ logger.error (" Failed to remove task from runqueue0!" );
182
+ } else {
183
+ logger.info (" Removed task from runqueue0!" );
184
+ }
185
+
111
186
logger.info (" Waiting for stop task to complete..." );
112
187
std::unique_lock lock (done_mutex);
113
188
done_cv.wait (lock, [&done] { return done; });
0 commit comments