-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunAsyncEvents.cpp
More file actions
934 lines (780 loc) · 21.6 KB
/
RunAsyncEvents.cpp
File metadata and controls
934 lines (780 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
#include "ArduinoEvents.h"
#include <algorithm>
#include <any>
#include <memory>
#include <new>
#include <unordered_map>
#include <vector>
#if __has_include(<zephyr/kernel.h>)
#include <zephyr/kernel.h>
#else
#error "This library requires Zephyr environment."
#endif
namespace arduino_events {
Runtime& Events = Runtime::instance();
namespace {
constexpr size_t kMaxWorkerThreads = 4;
constexpr size_t kWorkerStackSize = 3072;
K_THREAD_STACK_ARRAY_DEFINE(g_workerStacks, kMaxWorkerThreads, kWorkerStackSize);
struct k_work_q g_workerQueues[kMaxWorkerThreads];
bool g_workerQueueStarted[kMaxWorkerThreads] = {false, false, false, false};
} // namespace
namespace {
template <typename MutexT>
class LockGuard {
public:
explicit LockGuard(MutexT& mutex) : mutex_(mutex) {
mutex_.lock();
}
~LockGuard() {
mutex_.unlock();
}
LockGuard(const LockGuard&) = delete;
LockGuard& operator=(const LockGuard&) = delete;
private:
MutexT& mutex_;
};
struct RuntimeMutex {
RuntimeMutex() {
k_mutex_init(&mutex_);
}
void lock() {
(void)k_mutex_lock(&mutex_, K_FOREVER);
}
bool try_lock() {
return k_mutex_lock(&mutex_, K_NO_WAIT) == 0;
}
void unlock() {
(void)k_mutex_unlock(&mutex_);
}
private:
struct k_mutex mutex_;
};
struct QueuedCallback {
void (*invoke)(void*) = nullptr;
void (*destroy)(void*) = nullptr;
void* context = nullptr;
};
struct CallbackPayload {
std::function<void()> callback;
};
struct WorkerJob {
struct k_work work;
std::function<void()> callback;
};
void invokeCallbackPayload(void* context) {
auto* payload = static_cast<CallbackPayload*>(context);
payload->callback();
}
void destroyCallbackPayload(void* context) {
auto* payload = static_cast<CallbackPayload*>(context);
payload->~CallbackPayload();
k_free(payload);
}
void workerJobHandler(struct k_work* work) {
auto* job = CONTAINER_OF(work, WorkerJob, work);
auto callback = std::move(job->callback);
job->~WorkerJob();
k_free(job);
if (callback) {
callback();
}
}
struct TimerEntry {
uint32_t id = 0;
uint32_t periodMs = 0;
bool repeat = false;
bool cancelled = true;
bool active = false;
bool pending = false;
uint32_t generation = 0;
std::function<void()> callback;
struct k_work_delayable work;
};
struct RuntimeState;
bool pushCallbackLocked(RuntimeState& state, std::function<void()> callback);
void drainCallbackQueueLocked(RuntimeState& state);
void timerWorkHandler(struct k_work* work);
uint64_t nowMs() {
return k_cyc_to_ms_floor64(static_cast<uint64_t>(sys_clock_cycle_get_32()));
}
struct HandlerEntry {
uint32_t id = 0;
size_t eventTypeId = 0;
std::function<void(const void*)> handler;
bool once = false;
bool removed = false;
};
struct FutureState {
bool ready = false;
bool cancelled = false;
bool hasError = false;
uint64_t timeoutAtMs = 0;
Error error;
std::any value;
std::vector<std::function<void(const std::any&)>> onSuccess;
std::vector<std::function<void(const Error&)>> onError;
std::vector<std::function<void()>> onFinally;
};
struct RuntimeState {
Config config;
bool running = false;
uint32_t nextSubscriptionId = 1;
uint32_t nextTimerId = 1;
uint32_t nextFutureToken = 1;
uint32_t timerGeneration = 0;
size_t workerQueueCount = 1;
uint32_t nextWorkerQueue = 0;
std::vector<HandlerEntry> handlers;
std::vector<std::unique_ptr<TimerEntry>> timers;
std::unordered_map<uint32_t, std::shared_ptr<FutureState>> futures;
struct k_msgq callbackQueue;
std::vector<char> callbackQueueStorage;
bool callbackQueueReady = false;
mutable RuntimeMutex mutex;
};
RuntimeState& rt() {
static RuntimeState s;
return s;
}
bool pushCallbackLocked(RuntimeState& state, std::function<void()> callback) {
if (!state.callbackQueueReady) {
return false;
}
void* rawPayload = k_malloc(sizeof(CallbackPayload));
if (rawPayload == nullptr) {
return false;
}
auto* payload = new (rawPayload) CallbackPayload{std::move(callback)};
QueuedCallback queued;
queued.invoke = invokeCallbackPayload;
queued.destroy = destroyCallbackPayload;
queued.context = payload;
if (k_msgq_put(&state.callbackQueue, &queued, K_NO_WAIT) != 0) {
destroyCallbackPayload(payload);
return false;
}
return true;
}
void drainCallbackQueueLocked(RuntimeState& state) {
if (!state.callbackQueueReady) {
return;
}
QueuedCallback queued;
while (k_msgq_get(&state.callbackQueue, &queued, K_NO_WAIT) == 0) {
if (queued.destroy != nullptr && queued.context != nullptr) {
queued.destroy(queued.context);
}
}
}
void timerWorkHandler(struct k_work* work) {
auto* delayable = k_work_delayable_from_work(work);
auto* timer = CONTAINER_OF(delayable, TimerEntry, work);
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
if (!timer->pending) {
return;
}
if (!state.running || !timer->active || timer->cancelled ||
timer->generation != state.timerGeneration) {
timer->pending = false;
timer->active = false;
timer->cancelled = true;
timer->callback = nullptr;
return;
}
(void)pushCallbackLocked(state, timer->callback);
if (timer->repeat && !timer->cancelled) {
const int rc = k_work_reschedule_for_queue(
&k_sys_work_q, &timer->work, K_MSEC(timer->periodMs));
if (rc < 0) {
timer->pending = false;
timer->active = false;
timer->cancelled = true;
timer->callback = nullptr;
}
return;
}
timer->pending = false;
timer->active = false;
timer->cancelled = true;
timer->callback = nullptr;
}
void queueFutureCallbacksLocked(RuntimeState& state, const std::shared_ptr<FutureState>& fs) {
const bool runSuccess = fs->ready && !fs->hasError && !fs->cancelled;
const bool runError = fs->ready && (fs->hasError || fs->cancelled);
std::shared_ptr<std::any> payload;
if (runSuccess) {
payload = std::make_shared<std::any>(fs->value);
}
Error err = fs->error;
auto successHandlers = std::move(fs->onSuccess);
auto errorHandlers = std::move(fs->onError);
auto finallyHandlers = std::move(fs->onFinally);
fs->onSuccess.clear();
fs->onError.clear();
fs->onFinally.clear();
if (runSuccess) {
for (auto& cb : successHandlers) {
if (!pushCallbackLocked(state, [cb = std::move(cb), payload]() { cb(*payload); })) {
break;
}
}
}
if (runError) {
for (auto& cb : errorHandlers) {
if (!pushCallbackLocked(state, [cb = std::move(cb), err]() { cb(err); })) {
break;
}
}
}
for (auto& cb : finallyHandlers) {
if (!pushCallbackLocked(state, std::move(cb))) {
break;
}
}
}
} // namespace
Result<void> Result<void>::ok() {
Result<void> out;
out.ok_ = true;
return out;
}
Result<void> Result<void>::err(Error error) {
Result<void> out;
out.ok_ = false;
out.error_ = std::move(error);
return out;
}
bool Result<void>::isOk() const {
return ok_;
}
const Error& Result<void>::error() const {
return error_;
}
Future<void>& Future<void>::onDone(SuccessHandler onSuccess) {
Runtime::instance().futureOnSuccess(token_, [handler = std::move(onSuccess)](const std::any&) {
handler();
});
return *this;
}
Future<void>& Future<void>::onError(ErrorHandler onError) {
Runtime::instance().futureOnError(token_, std::move(onError));
return *this;
}
Future<void>& Future<void>::onFinish(FinallyHandler onFinally) {
Runtime::instance().futureOnFinally(token_, std::move(onFinally));
return *this;
}
Future<void>& Future<void>::withTimeout(uint32_t timeoutMs) {
Runtime::instance().futureSetTimeout(token_, timeoutMs);
return *this;
}
bool Future<void>::cancel() {
return Runtime::instance().futureCancel(token_);
}
bool Future<void>::isReady() const {
return Runtime::instance().futureIsReady(token_);
}
bool Future<void>::hasError() const {
return Runtime::instance().futureHasError(token_);
}
bool Future<void>::isCancelled() const {
return Runtime::instance().futureIsCancelled(token_);
}
bool Future<void>::tryGet(Result<void>& out) const {
std::any value;
Error error;
bool hasError = false;
if (!Runtime::instance().futureTryGetAny(token_, value, error, hasError)) {
return false;
}
if (hasError) {
out = Result<void>::err(std::move(error));
} else {
out = Result<void>::ok();
}
return true;
}
bool Deferred<void>::resolve() {
return Runtime::instance().futureResolveAny(token_, std::any());
}
bool Deferred<void>::reject(Error error) {
return Runtime::instance().futureReject(token_, std::move(error));
}
bool Deferred<void>::isCancelled() const {
return Runtime::instance().futureIsCancelled(token_);
}
Runtime& Runtime::instance() {
static Runtime singleton;
return singleton;
}
Runtime::Runtime() = default;
bool Runtime::begin(const Config& config) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
drainCallbackQueueLocked(state);
state.config = config;
if (state.config.eventQueueCapacity == 0) {
state.config.eventQueueCapacity = 1;
}
if (state.config.workerQueueCapacity == 0) {
state.config.workerQueueCapacity = 1;
}
if (state.config.workerThreadCount == 0) {
state.config.workerThreadCount = 1;
}
state.workerQueueCount = static_cast<size_t>(state.config.workerThreadCount);
if (state.workerQueueCount > kMaxWorkerThreads) {
state.workerQueueCount = kMaxWorkerThreads;
}
state.nextWorkerQueue = 0;
for (size_t i = 0; i < state.workerQueueCount; ++i) {
if (g_workerQueueStarted[i]) {
continue;
}
k_work_queue_start(&g_workerQueues[i], g_workerStacks[i],
K_THREAD_STACK_SIZEOF(g_workerStacks[i]),
CONFIG_SYSTEM_WORKQUEUE_PRIORITY, nullptr);
g_workerQueueStarted[i] = true;
}
const size_t msgSize = sizeof(QueuedCallback);
state.callbackQueueStorage.assign(state.config.eventQueueCapacity * msgSize, 0);
k_msgq_init(&state.callbackQueue, state.callbackQueueStorage.data(), msgSize,
state.config.eventQueueCapacity);
state.callbackQueueReady = true;
if (state.timerGeneration == 0) {
state.timerGeneration = 1;
} else {
++state.timerGeneration;
if (state.timerGeneration == 0) {
state.timerGeneration = 1;
}
}
if (state.timers.size() != state.config.workerQueueCapacity) {
state.timers.clear();
state.timers.reserve(state.config.workerQueueCapacity);
for (size_t i = 0; i < state.config.workerQueueCapacity; ++i) {
auto timer = std::make_unique<TimerEntry>();
timer->generation = state.timerGeneration;
k_work_init_delayable(&timer->work, timerWorkHandler);
state.timers.push_back(std::move(timer));
}
} else {
for (auto& timer : state.timers) {
timer->id = 0;
timer->periodMs = 0;
timer->repeat = false;
timer->cancelled = true;
timer->active = false;
timer->generation = state.timerGeneration;
timer->callback = nullptr;
}
}
state.running = true;
state.nextSubscriptionId = 1;
state.nextTimerId = 1;
state.nextFutureToken = 1;
state.handlers.clear();
state.futures.clear();
return true;
}
void Runtime::end() {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
state.running = false;
state.handlers.clear();
for (auto& timer : state.timers) {
timer->cancelled = true;
timer->active = false;
timer->callback = nullptr;
}
state.futures.clear();
drainCallbackQueueLocked(state);
}
bool Runtime::isRunning() const {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
return state.running;
}
void Runtime::update(uint32_t budgetMs) {
auto& state = rt();
const uint64_t startedAt = nowMs();
{
LockGuard<RuntimeMutex> lock(state.mutex);
if (!state.running) {
return;
}
const uint64_t now = nowMs();
for (auto& item : state.futures) {
auto& fs = item.second;
if (!fs || fs->ready || fs->timeoutAtMs == 0 || fs->timeoutAtMs > now) {
continue;
}
fs->ready = true;
fs->hasError = true;
fs->cancelled = false;
fs->error.code = ErrorCode::Timeout;
fs->error.message = "future timeout";
queueFutureCallbacksLocked(state, fs);
}
}
while (true) {
QueuedCallback queued;
{
LockGuard<RuntimeMutex> lock(state.mutex);
if (!state.callbackQueueReady ||
k_msgq_get(&state.callbackQueue, &queued, K_NO_WAIT) != 0) {
break;
}
}
if (queued.invoke != nullptr && queued.context != nullptr) {
queued.invoke(queued.context);
}
if (queued.destroy != nullptr && queued.context != nullptr) {
queued.destroy(queued.context);
}
if (budgetMs > 0 && (nowMs() - startedAt) >= budgetMs) {
break;
}
}
}
Subscription Runtime::registerHandler(size_t eventTypeId, RawEventHandler handler, bool once) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
if (!state.running) {
return {};
}
HandlerEntry entry;
entry.id = state.nextSubscriptionId++;
entry.eventTypeId = eventTypeId;
entry.handler = std::move(handler);
entry.once = once;
state.handlers.push_back(std::move(entry));
return Subscription{state.handlers.back().id};
}
bool Runtime::emitRaw(size_t eventTypeId, const void* eventData, size_t eventSize) {
(void)eventSize;
auto& state = rt();
std::vector<uint32_t> consumed;
std::vector<std::function<void(const void*)>> toRun;
{
LockGuard<RuntimeMutex> lock(state.mutex);
if (!state.running) {
return false;
}
for (auto& h : state.handlers) {
if (h.removed || h.eventTypeId != eventTypeId) {
continue;
}
toRun.push_back(h.handler);
if (h.once) {
consumed.push_back(h.id);
}
}
if (!consumed.empty()) {
for (auto id : consumed) {
for (auto& h : state.handlers) {
if (h.id == id) {
h.removed = true;
break;
}
}
}
state.handlers.erase(
std::remove_if(state.handlers.begin(), state.handlers.end(),
[](const HandlerEntry& h) { return h.removed; }),
state.handlers.end());
}
}
for (auto& fn : toRun) {
fn(eventData);
}
return true;
}
bool Runtime::unlisten(Subscription subscription) {
if (!subscription) {
return false;
}
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = std::find_if(state.handlers.begin(), state.handlers.end(),
[&](const HandlerEntry& h) { return h.id == subscription.id; });
if (it == state.handlers.end()) {
return false;
}
state.handlers.erase(it);
return true;
}
uint32_t Runtime::after(uint32_t delayMs, std::function<void()> callback) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
if (!state.running) {
return 0;
}
for (auto& timer : state.timers) {
if (timer->active || timer->pending) {
continue;
}
timer->id = state.nextTimerId++;
timer->periodMs = delayMs;
timer->repeat = false;
timer->cancelled = false;
timer->active = true;
timer->pending = true;
timer->generation = state.timerGeneration;
timer->callback = std::move(callback);
const int rc = k_work_schedule_for_queue(&k_sys_work_q, &timer->work, K_MSEC(delayMs));
if (rc < 0) {
timer->cancelled = true;
timer->active = false;
timer->pending = false;
timer->callback = nullptr;
return 0;
}
return timer->id;
}
return 0;
}
uint32_t Runtime::every(uint32_t periodMs, std::function<void()> callback) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
if (!state.running || periodMs == 0) {
return 0;
}
for (auto& timer : state.timers) {
if (timer->active || timer->pending) {
continue;
}
timer->id = state.nextTimerId++;
timer->periodMs = periodMs;
timer->repeat = true;
timer->cancelled = false;
timer->active = true;
timer->pending = true;
timer->generation = state.timerGeneration;
timer->callback = std::move(callback);
const int rc = k_work_schedule_for_queue(&k_sys_work_q, &timer->work, K_MSEC(periodMs));
if (rc < 0) {
timer->cancelled = true;
timer->active = false;
timer->pending = false;
timer->callback = nullptr;
return 0;
}
return timer->id;
}
return 0;
}
bool Runtime::cancelTimer(uint32_t timerId) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
for (auto& timer : state.timers) {
if ((timer->active || timer->pending) && timer->id == timerId) {
timer->cancelled = true;
timer->active = false;
return true;
}
}
return false;
}
Future<void> Runtime::runAsync(std::function<void(Deferred<void>)> start) {
const uint32_t token = allocFutureToken();
if (token == 0) {
return Future<void>();
}
if (!enqueueWorker([start = std::move(start), token]() mutable {
start(Deferred<void>(token));
})) {
Error err;
err.code = ErrorCode::QueueFull;
err.message = "worker queue full";
futureReject(token, std::move(err));
}
return Future<void>(token);
}
void Runtime::futureOnSuccess(uint32_t token, std::function<void(const std::any&)> onSuccess) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
if (it == state.futures.end() || !it->second) {
return;
}
auto& fs = it->second;
if (fs->ready && !fs->hasError && !fs->cancelled) {
auto payload = std::make_shared<std::any>(fs->value);
(void)pushCallbackLocked(state,
[cb = std::move(onSuccess), payload]() mutable { cb(*payload); });
return;
}
if (!fs->ready) {
fs->onSuccess.push_back(std::move(onSuccess));
}
}
void Runtime::futureOnError(uint32_t token, std::function<void(const Error&)> onError) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
if (it == state.futures.end() || !it->second) {
return;
}
auto& fs = it->second;
if (fs->ready && (fs->hasError || fs->cancelled)) {
const Error err = fs->error;
(void)pushCallbackLocked(state, [cb = std::move(onError), err]() { cb(err); });
return;
}
if (!fs->ready) {
fs->onError.push_back(std::move(onError));
}
}
void Runtime::futureOnFinally(uint32_t token, std::function<void()> onFinally) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
if (it == state.futures.end() || !it->second) {
return;
}
auto& fs = it->second;
if (fs->ready) {
(void)pushCallbackLocked(state, std::move(onFinally));
return;
}
fs->onFinally.push_back(std::move(onFinally));
}
void Runtime::futureSetTimeout(uint32_t token, uint32_t timeoutMs) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
if (it == state.futures.end() || !it->second || it->second->ready) {
return;
}
if (timeoutMs == 0) {
it->second->timeoutAtMs = 0;
} else {
it->second->timeoutAtMs = nowMs() + timeoutMs;
}
}
bool Runtime::futureCancel(uint32_t token) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
if (it == state.futures.end() || !it->second || it->second->ready) {
return false;
}
auto& fs = it->second;
fs->ready = true;
fs->cancelled = true;
fs->hasError = true;
fs->error.code = ErrorCode::Cancelled;
fs->error.message = "future cancelled";
queueFutureCallbacksLocked(state, fs);
return true;
}
bool Runtime::futureIsReady(uint32_t token) const {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
return it != state.futures.end() && it->second && it->second->ready;
}
bool Runtime::futureHasError(uint32_t token) const {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
return it != state.futures.end() && it->second && it->second->ready && it->second->hasError;
}
bool Runtime::futureIsCancelled(uint32_t token) const {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
return it != state.futures.end() && it->second && it->second->cancelled;
}
bool Runtime::futureTryGetAny(uint32_t token, std::any& outValue, Error& outError,
bool& outHasError) const {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
if (it == state.futures.end() || !it->second || !it->second->ready) {
return false;
}
outHasError = it->second->hasError;
if (outHasError) {
outError = it->second->error;
} else {
outValue = it->second->value;
}
return true;
}
bool Runtime::futureResolveAny(uint32_t token, std::any value) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
if (it == state.futures.end() || !it->second || it->second->ready) {
return false;
}
auto& fs = it->second;
fs->ready = true;
fs->cancelled = false;
fs->hasError = false;
fs->value = std::move(value);
queueFutureCallbacksLocked(state, fs);
return true;
}
bool Runtime::futureReject(uint32_t token, Error error) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
auto it = state.futures.find(token);
if (it == state.futures.end() || !it->second || it->second->ready) {
return false;
}
auto& fs = it->second;
fs->ready = true;
fs->cancelled = false;
fs->hasError = true;
fs->error = std::move(error);
queueFutureCallbacksLocked(state, fs);
return true;
}
uint32_t Runtime::allocFutureToken() {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
if (!state.running) {
return 0;
}
const uint32_t token = state.nextFutureToken++;
state.futures.emplace(token, std::make_shared<FutureState>());
return token;
}
bool Runtime::enqueue(std::function<void()> callback) {
auto& state = rt();
LockGuard<RuntimeMutex> lock(state.mutex);
if (!state.running) {
return false;
}
return pushCallbackLocked(state, std::move(callback));
}
bool Runtime::enqueueWorker(std::function<void()> callback) {
auto& state = rt();
uint32_t queueIndex = 0;
{
LockGuard<RuntimeMutex> lock(state.mutex);
if (!state.running || state.workerQueueCount == 0) {
return false;
}
queueIndex = state.nextWorkerQueue++ % state.workerQueueCount;
}
void* raw = k_malloc(sizeof(WorkerJob));
if (raw == nullptr) {
return false;
}
auto* job = new (raw) WorkerJob{};
job->callback = std::move(callback);
k_work_init(&job->work, workerJobHandler);
if (k_work_submit_to_queue(&g_workerQueues[queueIndex], &job->work) < 0) {
job->~WorkerJob();
k_free(job);
return false;
}
return true;
}
} // namespace arduino_events