SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_c_sync.cpp
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#include <stk.h>
11#include "stk_c.h" // must be included AFTER stk.h
12#include <sync/stk_sync.h>
13#include <memory/stk_memory.h> // for placement new
14
15using namespace stk;
16using namespace stk::sync;
17
18// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19// Anonymous namespace to make local functions file-local.
20namespace {
21// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22
23// maps kernel wait result to the C API wait result
24stk_wait_result_t ToCWaitResult(EWaitResult wr)
25{
26 stk_wait_result_t result;
27
28 switch (wr)
29 {
30 case WAIT_RESULT_SIGNAL: result = STK_WAIT_RESULT_SIGNAL; break;
31 case WAIT_RESULT_TIMEOUT: result = STK_WAIT_RESULT_TIMEOUT; break;
33 default: result = STK_WAIT_RESULT_FAIL; break;
34 }
35
36 return result;
37}
38
39// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
40} // namespace
41// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
42
43// =============================================================================
44// C-interface
45// =============================================================================
46extern "C" {
47
48// -----------------------------------------------------------------------------
49// Mutex
50// -----------------------------------------------------------------------------
52{
54};
55
57{
58 STK_ASSERT(membuf != nullptr);
59
60 stk_mutex_t *result = nullptr;
61 if (membuf != nullptr)
62 {
63 result = ConstructIn<stk_mutex_t>(*membuf);
64 }
65
66 return result;
67}
68
70{
71 if (mtx != nullptr)
72 {
73 mtx->~stk_mutex_t();
74 }
75}
76
78{
79 STK_ASSERT(mtx != nullptr);
80
81 mtx->handle.Lock();
82}
83
85{
86 STK_ASSERT(mtx != nullptr);
87
88 return mtx->handle.TryLock();
89}
90
92{
93 STK_ASSERT(mtx != nullptr);
94
95 mtx->handle.Unlock();
96}
97
99{
100 STK_ASSERT(mtx != nullptr);
101
102 return mtx->handle.TimedLock(timeout);
103}
104
106{
107 STK_ASSERT(mtx != nullptr);
108
109 return ToCWaitResult(mtx->handle.TimedLockEx(timeout));
110}
111
113{
114 STK_ASSERT(mtx != nullptr);
115
116 return &mtx->handle;
117}
118
119// -----------------------------------------------------------------------------
120// FastMutex
121// -----------------------------------------------------------------------------
126
128{
129 STK_ASSERT(membuf != nullptr);
130
131 stk_fastmutex_t *result = nullptr;
132 if (membuf != nullptr)
133 {
134 result = ConstructIn<stk_fastmutex_t>(*membuf);
135 }
136
137 return result;
138}
139
141{
142 if (mtx != nullptr)
143 {
144 mtx->~stk_fastmutex_t();
145 }
146}
147
149{
150 STK_ASSERT(mtx != nullptr);
151
152 mtx->handle.Lock();
153}
154
156{
157 STK_ASSERT(mtx != nullptr);
158
159 return mtx->handle.TryLock();
160}
161
163{
164 STK_ASSERT(mtx != nullptr);
165
166 mtx->handle.Unlock();
167}
168
170{
171 STK_ASSERT(mtx != nullptr);
172
173 return mtx->handle.TimedLock(timeout);
174}
175
177{
178 STK_ASSERT(mtx != nullptr);
179
180 return ToCWaitResult(mtx->handle.TimedLockEx(timeout));
181}
182
184{
185 STK_ASSERT(mtx != nullptr);
186
187 return &mtx->handle;
188}
189
190// -----------------------------------------------------------------------------
191// SpinLock
192// -----------------------------------------------------------------------------
197
199{
200 STK_ASSERT(membuf != nullptr);
201
202 stk_spinlock_t *result = nullptr;
203 if (membuf != nullptr)
204 {
205 result = ConstructIn<stk_spinlock_t>(*membuf);
206 }
207
208 return result;
209}
210
212{
213 if (slock != nullptr)
214 {
215 slock->~stk_spinlock_t();
216 }
217}
218
220{
221 STK_ASSERT(slock != nullptr);
222 slock->handle.Lock();
223}
224
226{
227 STK_ASSERT(slock != nullptr);
228 return slock->handle.TryLock();
229}
230
232{
233 STK_ASSERT(slock != nullptr);
234 slock->handle.Unlock();
235}
236
238{
239 STK_ASSERT(slock != nullptr);
240
241 return &slock->handle;
242}
243
244// -----------------------------------------------------------------------------
245// ConditionVariable
246// -----------------------------------------------------------------------------
251
253{
254 STK_ASSERT(membuf != nullptr);
255
256 stk_cv_t *result = nullptr;
257 if (membuf != nullptr)
258 {
259 result = ConstructIn<stk_cv_t>(*membuf);
260 }
261
262 return result;
263}
264
266{
267 if (cv != nullptr)
268 {
269 cv->~stk_cv_t();
270 }
271}
272
274{
275 STK_ASSERT(cv != nullptr);
276 STK_ASSERT(mtx != nullptr);
277
278 return cv->handle.Wait(mtx->handle, timeout);
279}
280
282{
283 STK_ASSERT(cv != nullptr);
284 STK_ASSERT(mtx != nullptr);
285
286 return ToCWaitResult(cv->handle.WaitEx(mtx->handle, timeout));
287}
288
290{
291 STK_ASSERT(cv != nullptr);
292 STK_ASSERT(mtx != nullptr);
293
294 return cv->handle.Wait(mtx->handle, timeout);
295}
296
298{
299 STK_ASSERT(cv != nullptr);
300 STK_ASSERT(mtx != nullptr);
301
302 return ToCWaitResult(cv->handle.WaitEx(mtx->handle, timeout));
303}
304
306{
307 STK_ASSERT(cv != nullptr);
308
309 cv->handle.NotifyOne();
310}
311
313{
314 STK_ASSERT(cv != nullptr);
315
316 cv->handle.NotifyAll();
317}
318
320{
321 STK_ASSERT(cv != nullptr);
322
323 return &cv->handle;
324}
325
326// -----------------------------------------------------------------------------
327// Event
328// -----------------------------------------------------------------------------
330{
331 stk_event_t(bool manual_reset) : handle(manual_reset)
332 {}
333
335};
336
338 bool manual_reset)
339{
340 STK_ASSERT(membuf != nullptr);
341
342 stk_event_t *result = nullptr;
343 if (membuf != nullptr)
344 {
345 result = ConstructIn<stk_event_t>(*membuf, manual_reset);
346 }
347
348 return result;
349}
350
352{
353 if (ev != nullptr)
354 {
355 ev->~stk_event_t();
356 }
357}
358
360{
361 STK_ASSERT(ev != nullptr);
362
363 return ev->handle.Wait(timeout);
364}
365
367{
368 STK_ASSERT(ev != nullptr);
369
370 return ToCWaitResult(ev->handle.WaitEx(timeout));
371}
372
374{
375 STK_ASSERT(ev != nullptr);
376
377 return ev->handle.TryWait();
378}
379
381{
382 STK_ASSERT(ev != nullptr);
383
384 return ev->handle.Set();
385}
386
388{
389 STK_ASSERT(ev != nullptr);
390
391 return ev->handle.Reset();
392}
393
395{
396 STK_ASSERT(ev != nullptr);
397
398 ev->handle.Pulse();
399}
400
402{
403 STK_ASSERT(ev != nullptr);
404
405 return &ev->handle;
406}
407
408// -----------------------------------------------------------------------------
409// Semaphore
410// -----------------------------------------------------------------------------
412{
413 stk_sem_t(stk_sem_val_t initial_count, stk_sem_val_t max_count)
414 : handle(initial_count, max_count)
415 {}
416
418};
419
421 stk_sem_val_t initial_count,
422 stk_sem_val_t max_count)
423{
424 STK_ASSERT(membuf != nullptr);
425
426 const stk_sem_val_t effective_max = ((max_count == 0U) ? SemaphoreT<stk_sem_val_t>::COUNT_MAX : max_count);
427
428 stk_sem_t *result = nullptr;
429 if (membuf != nullptr)
430 {
431 result = ConstructIn<stk_sem_t>(*membuf, initial_count, effective_max);
432 }
433
434 return result;
435}
436
438{
439 if (sem != nullptr)
440 {
441 sem->~stk_sem_t();
442 }
443}
444
446{
447 STK_ASSERT(sem != nullptr);
448
449 return sem->handle.Wait(timeout);
450}
451
453{
454 STK_ASSERT(sem != nullptr);
455
456 return ToCWaitResult(sem->handle.WaitEx(timeout));
457}
458
460{
461 STK_ASSERT(sem != nullptr);
462
463 return sem->handle.TryWait();
464}
465
467{
468 STK_ASSERT(sem != nullptr);
469
470 sem->handle.Signal();
471}
472
474{
475 STK_ASSERT(sem != nullptr);
476
477 return sem->handle.TrySignal();
478}
479
481{
482 STK_ASSERT(sem != nullptr);
483
484 return sem->handle.TrySignal(ceiling);
485}
486
488{
489 STK_ASSERT(sem != nullptr);
490
491 return sem->handle.GetCount();
492}
493
495{
496 STK_ASSERT(sem != nullptr);
497
498 return &sem->handle;
499}
500
501// -----------------------------------------------------------------------------
502// EventFlags
503// -----------------------------------------------------------------------------
505{
506 stk_ef_t(stk_ef_val_t initial_flags) : handle(initial_flags)
507 {}
508
510};
511
513 stk_ef_val_t initial_flags)
514{
515 STK_ASSERT(membuf != nullptr);
516
517 stk_ef_t *result = nullptr;
518 if (membuf != nullptr)
519 {
520 result = ConstructIn<stk_ef_t>(*membuf, initial_flags);
521 }
522
523 return result;
524}
525
527{
528 if (ef != nullptr)
529 {
530 ef->~stk_ef_t();
531 }
532}
533
535{
536 STK_ASSERT(ef != nullptr);
537
538 return ef->handle.Set(flags);
539}
540
542{
543 STK_ASSERT(ef != nullptr);
544
545 return ef->handle.Clear(flags);
546}
547
549{
550 STK_ASSERT(ef != nullptr);
551
552 return ef->handle.And(mask);
553}
554
556{
557 STK_ASSERT(ef != nullptr);
558
559 return ef->handle.Get();
560}
561
562stk_ef_val_t stk_ef_wait(stk_ef_t *ef, stk_ef_val_t flags, uint32_t options, stk_timeout_t timeout)
563{
564 STK_ASSERT(ef != nullptr);
565
566 return ef->handle.Wait(flags, options, timeout);
567}
568
570{
571 STK_ASSERT(ef != nullptr);
572
573 return ef->handle.TryWait(flags, options);
574}
575
577 stk_ef_val_t *actual_flags)
578{
579 STK_ASSERT(ef != nullptr);
580
581 stk_ef_val_t actual = 0U;
582 const stk_wait_result_t result = ToCWaitResult(ef->handle.WaitEx(flags, options, timeout, actual));
583
584 if (actual_flags != nullptr)
585 {
586 *actual_flags = actual;
587 }
588
589 return result;
590}
591
592stk_wait_result_t stk_ef_trywait_ex(stk_ef_t *ef, stk_ef_val_t flags, uint32_t options, stk_ef_val_t *actual_flags)
593{
594 return stk_ef_wait_ex(ef, flags, options, STK_NO_WAIT, actual_flags);
595}
596
601
603{
604 STK_ASSERT(membuf != nullptr);
605
606 stk_ef_sync_ctx_t *result = nullptr;
607 if (membuf != nullptr)
608 {
609 result = ConstructIn<stk_ef_sync_ctx_t>(*membuf);
610 }
611
612 return result;
613}
614
616{
617 if (ctx != nullptr)
618 {
619 ctx->~stk_ef_sync_ctx_t();
620 }
621}
622
624 stk_timeout_t timeout, stk_ef_val_t *actual_flags)
625{
626 STK_ASSERT(ef != nullptr);
627 STK_ASSERT(ctx != nullptr);
628
629 stk_ef_val_t actual = 0U;
630 const stk_wait_result_t result = ToCWaitResult(ef->handle.Sync(ctx->handle, set_flags, wait_flags, timeout, actual));
631
632 if (actual_flags != nullptr)
633 {
634 *actual_flags = actual;
635 }
636
637 return result;
638}
639
641{
642 STK_ASSERT(ef != nullptr);
643
644 return &ef->handle;
645}
646
647// -----------------------------------------------------------------------------
648// Pipe (runtime-sized, external-buffer; wraps sync::Pipe)
649// -----------------------------------------------------------------------------
651{
653
654 stk_pipe_t(uint8_t *buf, size_t capacity, size_t element_size)
655 : handle(buf, capacity, element_size)
656 {}
657};
658
660 uint8_t *buf,
661 uint32_t buf_size,
662 size_t capacity,
663 size_t element_size)
664{
665 STK_ASSERT(membuf != nullptr);
666 STK_ASSERT(buf != nullptr);
667 STK_ASSERT(capacity >= 1U);
668 STK_ASSERT(element_size >= 1U);
669 STK_ASSERT(buf_size >= capacity * element_size);
670
671 stk_pipe_t *result = nullptr;
672 if ((membuf != nullptr) && (buf_size >= (capacity * element_size)))
673 {
674 result = ConstructIn<stk_pipe_t>(*membuf, buf, capacity, element_size);
675 }
676
677 return result;
678}
679
681{
682 if (pipe != nullptr)
683 {
684 pipe->~stk_pipe_t();
685 }
686}
687
688bool stk_pipe_write(stk_pipe_t *pipe, const void *data, stk_timeout_t timeout)
689{
690 STK_ASSERT(pipe != nullptr);
691 STK_ASSERT(data != nullptr);
692
693 return pipe->handle.Write(data, timeout);
694}
695
697{
698 STK_ASSERT(pipe != nullptr);
699 STK_ASSERT(data != nullptr);
700
701 return ToCWaitResult(pipe->handle.WriteEx(data, timeout));
702}
703
704bool stk_pipe_trywrite(stk_pipe_t *pipe, const void *data)
705{
706 STK_ASSERT(pipe != nullptr);
707 STK_ASSERT(data != nullptr);
708
709 return pipe->handle.TryWrite(data);
710}
711
712bool stk_pipe_read(stk_pipe_t *pipe, void *data, stk_timeout_t timeout)
713{
714 STK_ASSERT(pipe != nullptr);
715 STK_ASSERT(data != nullptr);
716
717 return pipe->handle.Read(data, timeout);
718}
719
721{
722 STK_ASSERT(pipe != nullptr);
723 STK_ASSERT(data != nullptr);
724
725 return ToCWaitResult(pipe->handle.ReadEx(data, timeout));
726}
727
728bool stk_pipe_tryread(stk_pipe_t *pipe, void *data)
729{
730 STK_ASSERT(pipe != nullptr);
731 STK_ASSERT(data != nullptr);
732
733 return pipe->handle.TryRead(data);
734}
735
736size_t stk_pipe_write_bulk(stk_pipe_t *pipe, const void *src, size_t count, stk_timeout_t timeout)
737{
738 STK_ASSERT(pipe != nullptr);
739
740 return pipe->handle.WriteBulk(src, count, timeout);
741}
742
743stk_wait_result_t stk_pipe_write_bulk_ex(stk_pipe_t *pipe, const void *src, size_t count,
744 size_t *written, stk_timeout_t timeout)
745{
746 STK_ASSERT(pipe != nullptr);
747
748 size_t n = 0U;
749 const EWaitResult wr = pipe->handle.WriteBulkEx(src, count, n, timeout);
750
751 if (written != nullptr)
752 {
753 *written = n;
754 }
755
756 return ToCWaitResult(wr);
757}
758
759size_t stk_pipe_trywrite_bulk(stk_pipe_t *pipe, const void *src, size_t count)
760{
761 STK_ASSERT(pipe != nullptr);
762
763 return pipe->handle.TryWriteBulk(src, count);
764}
765
766size_t stk_pipe_read_bulk(stk_pipe_t *pipe, void *dst, size_t count, stk_timeout_t timeout)
767{
768 STK_ASSERT(pipe != nullptr);
769
770 return pipe->handle.ReadBulk(dst, count, timeout);
771}
772
774 size_t *read_count, stk_timeout_t timeout)
775{
776 STK_ASSERT(pipe != nullptr);
777
778 size_t n = 0U;
779 const EWaitResult wr = pipe->handle.ReadBulkEx(dst, count, n, timeout);
780
781 if (read_count != nullptr)
782 {
783 *read_count = n;
784 }
785
786 return ToCWaitResult(wr);
787}
788
789size_t stk_pipe_tryread_bulk(stk_pipe_t *pipe, void *dst, size_t count)
790{
791 STK_ASSERT(pipe != nullptr);
792
793 return pipe->handle.TryReadBulk(dst, count);
794}
795
797 void *dst,
798 size_t trigger,
799 size_t max_count,
800 stk_timeout_t timeout)
801{
802 STK_ASSERT(pipe != nullptr);
803
804 return pipe->handle.ReadBulkTriggered(dst, trigger, max_count, timeout);
805}
806
808 void *dst,
809 size_t trigger,
810 size_t max_count,
811 size_t *read_count,
812 stk_timeout_t timeout)
813{
814 STK_ASSERT(pipe != nullptr);
815
816 size_t n = 0U;
817 const EWaitResult wr = pipe->handle.ReadBulkTriggeredEx(dst, trigger, max_count, n, timeout);
818
819 if (read_count != nullptr)
820 {
821 *read_count = n;
822 }
823
824 return ToCWaitResult(wr);
825}
826
827size_t stk_pipe_tryread_bulk_triggered(stk_pipe_t *pipe, void *dst, size_t max_count)
828{
829 STK_ASSERT(pipe != nullptr);
830
831 return pipe->handle.TryReadBulkTriggered(dst, max_count);
832}
833
835{
836 STK_ASSERT(pipe != nullptr);
837
838 pipe->handle.Reset();
839}
840
842{
843 STK_ASSERT(pipe != nullptr);
844
845 return pipe->handle.GetCapacity();
846}
847
849{
850 STK_ASSERT(pipe != nullptr);
851
852 return pipe->handle.GetElementSize();
853}
854
856{
857 STK_ASSERT(pipe != nullptr);
858
859 return pipe->handle.GetCount();
860}
861
863{
864 STK_ASSERT(pipe != nullptr);
865
866 return pipe->handle.GetSpace();
867}
868
870{
871 STK_ASSERT(pipe != nullptr);
872
873 return pipe->handle.IsEmpty();
874}
875
877{
878 STK_ASSERT(pipe != nullptr);
879
880 return pipe->handle.IsFull();
881}
882
884{
885 STK_ASSERT(pipe != nullptr);
886
887 return pipe->handle.IsStorageValid();
888}
889
891{
892 STK_ASSERT(pipe != nullptr);
893
894 return &pipe->handle;
895}
896
897// -----------------------------------------------------------------------------
898// MessageQueue
899// -----------------------------------------------------------------------------
901{
902 stk_msgq_t(uint8_t *buf, size_t capacity, size_t msg_size)
903 : handle(buf, capacity, msg_size)
904 {}
905
907};
908
910 uint8_t *buf,
911 uint32_t buf_size,
912 size_t capacity,
913 size_t msg_size)
914{
915 STK_ASSERT(membuf != nullptr);
916 STK_ASSERT(buf != nullptr);
917 STK_ASSERT(capacity >= 1U);
918 STK_ASSERT(msg_size >= 1U);
919 STK_ASSERT(buf_size >= capacity * msg_size);
920
921 stk_msgq_t *result = nullptr;
922 if ((membuf != nullptr) && (buf_size >= (capacity * msg_size)))
923 {
924 result = ConstructIn<stk_msgq_t>(*membuf, buf, capacity, msg_size);
925 }
926
927 return result;
928}
929
931{
932 if (mq != nullptr)
933 {
934 mq->~stk_msgq_t();
935 }
936}
937
938bool stk_msgq_put(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout)
939{
940 STK_ASSERT(mq != nullptr);
941 STK_ASSERT(msg != nullptr);
942
943 return mq->handle.Put(msg, timeout);
944}
945
947{
948 STK_ASSERT(mq != nullptr);
949 STK_ASSERT(msg != nullptr);
950
951 return ToCWaitResult(mq->handle.PutEx(msg, timeout));
952}
953
954bool stk_msgq_tryput(stk_msgq_t *mq, const void *msg)
955{
956 STK_ASSERT(mq != nullptr);
957 STK_ASSERT(msg != nullptr);
958
959 return mq->handle.TryPut(msg);
960}
961
962bool stk_msgq_putfront(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout)
963{
964 STK_ASSERT(mq != nullptr);
965 STK_ASSERT(msg != nullptr);
966
967 return mq->handle.PutFront(msg, timeout);
968}
969
971{
972 STK_ASSERT(mq != nullptr);
973 STK_ASSERT(msg != nullptr);
974
975 return ToCWaitResult(mq->handle.PutFrontEx(msg, timeout));
976}
977
978bool stk_msgq_tryputfront(stk_msgq_t *mq, const void *msg)
979{
980 STK_ASSERT(mq != nullptr);
981 STK_ASSERT(msg != nullptr);
982
983 return mq->handle.TryPutFront(msg);
984}
985
986bool stk_msgq_get(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
987{
988 STK_ASSERT(mq != nullptr);
989 STK_ASSERT(msg != nullptr);
990
991 return mq->handle.Get(msg, timeout);
992}
993
995{
996 STK_ASSERT(mq != nullptr);
997 STK_ASSERT(msg != nullptr);
998
999 return ToCWaitResult(mq->handle.GetEx(msg, timeout));
1000}
1001
1002bool stk_msgq_tryget(stk_msgq_t *mq, void *msg)
1003{
1004 STK_ASSERT(mq != nullptr);
1005 STK_ASSERT(msg != nullptr);
1006
1007 return mq->handle.TryGet(msg);
1008}
1009
1010bool stk_msgq_peek(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
1011{
1012 STK_ASSERT(mq != nullptr);
1013 STK_ASSERT(msg != nullptr);
1014
1015 return mq->handle.Peek(msg, timeout);
1016}
1017
1019{
1020 STK_ASSERT(mq != nullptr);
1021 STK_ASSERT(msg != nullptr);
1022
1023 return ToCWaitResult(mq->handle.PeekEx(msg, timeout));
1024}
1025
1026bool stk_msgq_trypeek(stk_msgq_t *mq, void *msg)
1027{
1028 STK_ASSERT(mq != nullptr);
1029 STK_ASSERT(msg != nullptr);
1030
1031 return mq->handle.TryPeek(msg);
1032}
1033
1034bool stk_msgq_peekfront(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
1035{
1036 STK_ASSERT(mq != nullptr);
1037 STK_ASSERT(msg != nullptr);
1038
1039 return mq->handle.PeekFront(msg, timeout);
1040}
1041
1043{
1044 STK_ASSERT(mq != nullptr);
1045 STK_ASSERT(msg != nullptr);
1046
1047 return ToCWaitResult(mq->handle.PeekFrontEx(msg, timeout));
1048}
1049
1051{
1052 STK_ASSERT(mq != nullptr);
1053 STK_ASSERT(msg != nullptr);
1054
1055 return mq->handle.TryPeekFront(msg);
1056}
1057
1059{
1060 STK_ASSERT(mq != nullptr);
1061
1062 mq->handle.Reset();
1063}
1064
1066{
1067 STK_ASSERT(mq != nullptr);
1068
1069 return mq->handle.GetCapacity();
1070}
1071
1073{
1074 STK_ASSERT(mq != nullptr);
1075
1076 return mq->handle.GetMsgSize();
1077}
1078
1080{
1081 STK_ASSERT(mq != nullptr);
1082
1083 return mq->handle.GetCount();
1084}
1085
1087{
1088 STK_ASSERT(mq != nullptr);
1089
1090 return mq->handle.GetSpace();
1091}
1092
1094{
1095 STK_ASSERT(mq != nullptr);
1096
1097 return mq->handle.IsEmpty();
1098}
1099
1101{
1102 STK_ASSERT(mq != nullptr);
1103
1104 return mq->handle.IsFull();
1105}
1106
1108{
1109 STK_ASSERT(mq != nullptr);
1110
1111 return mq->handle.GetBuffer();
1112}
1113
1115{
1116 STK_ASSERT(mq != nullptr);
1117
1118 return mq->handle.IsStorageValid();
1119}
1120
1122{
1123 STK_ASSERT(mq != nullptr);
1124
1125 return &mq->handle;
1126}
1127
1128// -----------------------------------------------------------------------------
1129// RWMutex (Reader-Writer Lock)
1130// -----------------------------------------------------------------------------
1135
1137{
1138 STK_ASSERT(membuf != nullptr);
1139
1140 stk_rwmutex_t *result = nullptr;
1141 if (membuf != nullptr)
1142 {
1143 result = ConstructIn<stk_rwmutex_t>(*membuf);
1144 }
1145
1146 return result;
1147}
1148
1150{
1151 if (rw != nullptr)
1152 {
1153 rw->~stk_rwmutex_t();
1154 }
1155}
1156
1158{
1159 STK_ASSERT(rw != nullptr);
1160
1161 rw->handle.ReadLock();
1162}
1163
1165{
1166 STK_ASSERT(rw != nullptr);
1167
1168 return rw->handle.TryReadLock();
1169}
1170
1172{
1173 STK_ASSERT(rw != nullptr);
1174
1175 return rw->handle.TimedReadLock(timeout);
1176}
1177
1179{
1180 STK_ASSERT(rw != nullptr);
1181
1182 return ToCWaitResult(rw->handle.TimedReadLockEx(timeout));
1183}
1184
1186{
1187 STK_ASSERT(rw != nullptr);
1188
1189 rw->handle.ReadUnlock();
1190}
1191
1193{
1194 STK_ASSERT(rw != nullptr);
1195
1196 rw->handle.Lock();
1197}
1198
1200{
1201 STK_ASSERT(rw != nullptr);
1202
1203 return rw->handle.TryLock();
1204}
1205
1207{
1208 STK_ASSERT(rw != nullptr);
1209
1210 return rw->handle.TimedLock(timeout);
1211}
1212
1214{
1215 STK_ASSERT(rw != nullptr);
1216
1217 return ToCWaitResult(rw->handle.TimedLockEx(timeout));
1218}
1219
1221{
1222 STK_ASSERT(rw != nullptr);
1223
1224 rw->handle.Unlock();
1225}
1226
1228{
1229 STK_ASSERT(rw != nullptr);
1230
1231 return &rw->handle;
1232}
1233
1234// -----------------------------------------------------------------------------
1235// Barrier
1236// -----------------------------------------------------------------------------
1238{
1239 explicit stk_barrier_t(uint32_t count) : handle(count)
1240 {}
1241
1243};
1244
1246 uint32_t count)
1247{
1248 STK_ASSERT(membuf != nullptr);
1249 STK_ASSERT(count != 0U);
1250
1251 stk_barrier_t *result = nullptr;
1252 if ((membuf != nullptr) && (count != 0U))
1253 {
1254 result = ConstructIn<stk_barrier_t>(*membuf, count);
1255 }
1256
1257 return result;
1258}
1259
1261{
1262 if (barrier != nullptr)
1263 {
1264 barrier->~stk_barrier_t();
1265 }
1266}
1267
1269{
1270 STK_ASSERT(barrier != nullptr);
1271
1272 return barrier->handle.Wait();
1273}
1274
1276{
1277 STK_ASSERT(barrier != nullptr);
1278
1279 stk_barrier_result_t result;
1280
1281 switch (barrier->handle.WaitEx())
1282 {
1284 case Barrier::BARRIER_CANCELED: result = STK_BARRIER_CANCELED; break;
1285 default: result = STK_BARRIER_RELEASED; break;
1286 }
1287
1288 return result;
1289}
1290
1292{
1293 STK_ASSERT(barrier != nullptr);
1294
1295 return barrier->handle.GetThreshold();
1296}
1297
1299{
1300 STK_ASSERT(barrier != nullptr);
1301
1302 return &barrier->handle;
1303}
1304
1305// =============================================================================
1306} // extern "C"
1307// =============================================================================
Collection of memory-related primitives (stk::memory namespace).
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
Collection of synchronization primitives (stk::sync namespace).
C language binding/interface for SuperTinyKernel RTOS.
Barrier * stk_barrier_get_instance(stk_barrier_t *barrier)
Get the underlying C++ stk::sync::Barrier object wrapped by a stk_barrier_t handle.
uint16_t stk_sem_val_t
Definition stk_c.h:1619
stk_ef_val_t stk_ef_and(stk_ef_t *ef, stk_ef_val_t mask)
AND the flags word with a mask (equivalent of ThreadX tx_event_flags_set(TX_AND)).
FastMutex * stk_fastmutex_get_instance(stk_fastmutex_t *mtx)
Get the underlying C++ stk::sync::FastMutex object wrapped by a stk_fastmutex_t handle.
void stk_barrier_destroy(stk_barrier_t *barrier)
Destroy a Barrier.
void stk_sem_signal(stk_sem_t *sem)
Signal/Release a semaphore resource.
size_t stk_pipe_get_capacity(const stk_pipe_t *pipe)
Get the maximum number of elements the pipe can hold.
void stk_msgq_reset(stk_msgq_t *mq)
Discard all messages and reset the queue to the empty state.
bool stk_msgq_is_full(const stk_msgq_t *mq)
Check whether the queue is currently full.
stk_pipe_t * stk_pipe_create(stk_pipe_mem_t *const membuf, uint8_t *buf, uint32_t buf_size, size_t capacity, size_t element_size)
Create a Pipe (using provided memory).
bool stk_event_set(stk_event_t *ev)
Set the event to signaled state.
stk_wait_result_t stk_rwmutex_timed_lock_ex(stk_rwmutex_t *rw, stk_timeout_t timeout)
Try to acquire the write lock with a timeout, preserving the full outcome.
void stk_mutex_destroy(stk_mutex_t *mtx)
Destroy a Mutex.
size_t stk_pipe_get_count(const stk_pipe_t *pipe)
Get the current number of elements in the pipe.
bool stk_sem_trysignal(stk_sem_t *sem)
Try to signal/release a semaphore resource without exceeding max_count.
bool stk_pipe_write(stk_pipe_t *pipe, const void *data, stk_timeout_t timeout)
Write a single element to the pipe.
SemaphoreT< stk_sem_val_t > * stk_sem_get_instance(stk_sem_t *sem)
Get the underlying C++ stk::sync::SemaphoreT<stk_sem_val_t> object wrapped by a stk_sem_t handle.
size_t stk_pipe_read_bulk(stk_pipe_t *pipe, void *dst, size_t count, stk_timeout_t timeout)
Read multiple elements from the pipe.
size_t stk_pipe_trywrite_bulk(stk_pipe_t *pipe, const void *src, size_t count)
Attempt to write multiple elements to the pipe without blocking.
stk_wait_result_t stk_pipe_read_bulk_triggered_ex(stk_pipe_t *pipe, void *dst, size_t trigger, size_t max_count, size_t *read_count, stk_timeout_t timeout)
Read at least trigger elements, then drain up to max_count, preserving the full outcome.
bool stk_rwmutex_try_read_lock(stk_rwmutex_t *rw)
Try to acquire the read lock without blocking.
stk_ef_val_t stk_ef_trywait(stk_ef_t *ef, stk_ef_val_t flags, uint32_t options)
Non-blocking flag poll.
size_t stk_pipe_get_space(const stk_pipe_t *pipe)
Get the number of free slots currently available.
void stk_ef_destroy(stk_ef_t *ef)
Destroy an EventFlags object.
bool stk_event_wait(stk_event_t *ev, stk_timeout_t timeout)
Wait for the event to become signaled.
stk_wait_result_t stk_msgq_put_ex(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout)
Put a message into the queue, preserving the full outcome.
stk_wait_result_t
Outcome of a cancellable, timed kernel wait (see stk_cv_wait_ex(), stk_mutex_timed_lock_ex(),...
Definition stk_c.h:210
stk_sem_val_t stk_sem_get_count(const stk_sem_t *sem)
Get the current counter value.
stk_ef_t * stk_ef_create(stk_ef_mem_t *const membuf, stk_ef_val_t initial_flags)
Create an EventFlags object (using provided memory).
bool stk_rwmutex_trylock(stk_rwmutex_t *rw)
Try to acquire the write lock without blocking.
bool stk_cv_wait(stk_cv_t *cv, stk_mutex_t *mtx, stk_timeout_t timeout)
Wait for a signal on the condition variable.
stk_wait_result_t stk_msgq_peekfront_ex(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Peek at the front-inserted message without removing it, preserving the full outcome.
void stk_spinlock_destroy(stk_spinlock_t *slock)
Destroy the SpinLock.
void stk_fastmutex_destroy(stk_fastmutex_t *mtx)
Destroy a FastMutex.
void stk_cv_notify_one(stk_cv_t *cv)
Wake one task waiting on the condition variable.
bool stk_spinlock_trylock(stk_spinlock_t *slock)
Attempt to acquire the SpinLock immediately.
uint32_t stk_ef_val_t
Definition stk_c.h:1762
stk_spinlock_t * stk_spinlock_create(stk_spinlock_mem_t *const membuf)
Create a recursive SpinLock.
bool stk_msgq_tryget(stk_msgq_t *mq, void *msg)
Attempt to get a message from the queue without blocking.
bool stk_event_trywait(stk_event_t *ev)
Wait for the event to become signaled.
stk_wait_result_t stk_pipe_read_bulk_ex(stk_pipe_t *pipe, void *dst, size_t count, size_t *read_count, stk_timeout_t timeout)
Read multiple elements from the pipe, preserving the full outcome.
sync::SpinLock * stk_spinlock_get_instance(stk_spinlock_t *slock)
Get the underlying C++ stk::sync::SpinLock object wrapped by a stk_spinlock_t handle.
size_t stk_msgq_get_space(const stk_msgq_t *mq)
Get the number of free slots currently available.
stk_wait_result_t stk_cv_wait_ex(stk_cv_t *cv, stk_mutex_t *mtx, stk_timeout_t timeout)
Wait for a signal on the condition variable, preserving the full outcome.
void stk_event_destroy(stk_event_t *ev)
Destroy an Event.
stk_wait_result_t stk_pipe_read_ex(stk_pipe_t *pipe, void *data, stk_timeout_t timeout)
Read a single element from the pipe, preserving the full outcome.
bool stk_pipe_is_storage_valid(const stk_pipe_t *pipe)
Verify that the backing storage is valid and the pipe is ready for use.
bool stk_msgq_trypeekfront(stk_msgq_t *mq, void *msg)
Attempt to peek at the front message without blocking.
#define STK_NO_WAIT
No timeout constant.
Definition stk_c.h:253
bool stk_mutex_timed_lock(stk_mutex_t *mtx, stk_timeout_t timeout)
Try to lock the mutex with a timeout.
EventFlagsT< stk_ef_val_t > * stk_ef_get_instance(stk_ef_t *ef)
Get the underlying C++ stk::sync::EventFlagsT<stk_ef_val_t> object wrapped by a stk_ef_t handle.
bool stk_msgq_peekfront(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Peek at the most recently front-inserted message without removing it.
void stk_ef_sync_ctx_destroy(stk_ef_sync_ctx_t *ctx)
Destroy a rendezvous context.
void stk_mutex_lock(stk_mutex_t *mtx)
Lock the mutex. Blocks until available.
Pipe * stk_pipe_get_instance(stk_pipe_t *pipe)
Get the underlying C++ stk::sync::Pipe object wrapped by a stk_pipe_t handle.
size_t stk_pipe_write_bulk(stk_pipe_t *pipe, const void *src, size_t count, stk_timeout_t timeout)
Write multiple elements to the pipe.
void stk_rwmutex_lock(stk_rwmutex_t *rw)
Acquire the lock for exclusive writing. Blocks until available.
stk_mutex_t * stk_mutex_create(stk_mutex_mem_t *const membuf)
Create a Mutex (using provided memory).
stk_wait_result_t stk_ef_sync(stk_ef_t *ef, stk_ef_sync_ctx_t *ctx, stk_ef_val_t set_flags, stk_ef_val_t wait_flags, stk_timeout_t timeout, stk_ef_val_t *actual_flags)
Atomically set flags and wait for a group of flags: a multi-task rendezvous (equivalent of FreeRTOS x...
stk_ef_val_t stk_ef_clear(stk_ef_t *ef, stk_ef_val_t flags)
Clear one or more flags.
void stk_cv_destroy(stk_cv_t *cv)
Destroy a Condition Variable.
stk_wait_result_t stk_ef_trywait_ex(stk_ef_t *ef, stk_ef_val_t flags, uint32_t options, stk_ef_val_t *actual_flags)
Non-blocking flag poll with stk_wait_result_t and full flags snapshot.
stk_barrier_result_t
Outcome of a stk_barrier_wait_ex() call.
Definition stk_c.h:2911
bool stk_msgq_is_empty(const stk_msgq_t *mq)
Check whether the queue is currently empty.
uint32_t stk_barrier_get_threshold(const stk_barrier_t *barrier)
Get the number of tasks required to trip the barrier.
bool stk_mutex_trylock(stk_mutex_t *mtx)
Try locking the mutex. Does not block if already locked.
void stk_rwmutex_read_lock(stk_rwmutex_t *rw)
Acquire the lock for shared reading. Blocks until available.
bool stk_sem_trysignal_ceiling(stk_sem_t *sem, stk_sem_val_t ceiling)
Try to signal/release a semaphore resource without exceeding a caller-supplied ceiling.
size_t stk_msgq_get_capacity(const stk_msgq_t *mq)
Get the maximum number of messages the queue can hold.
stk_msgq_t * stk_msgq_create(stk_msgq_mem_t *const membuf, uint8_t *buf, uint32_t buf_size, size_t capacity, size_t msg_size)
Create a MessageQueue (using provided memory).
void stk_mutex_unlock(stk_mutex_t *mtx)
Unlock the mutex.
stk_sem_t * stk_sem_create(stk_sem_mem_t *const membuf, stk_sem_val_t initial_count, stk_sem_val_t max_count)
Create a Semaphore (using provided memory).
void stk_pipe_destroy(stk_pipe_t *pipe)
Destroy a Pipe.
stk_wait_result_t stk_fastmutex_timed_lock_ex(stk_fastmutex_t *mtx, stk_timeout_t timeout)
Try to lock the FastMutex with a timeout, preserving the full outcome.
size_t stk_pipe_get_element_size(const stk_pipe_t *pipe)
Get the size of each element in bytes.
stk_wait_result_t stk_sem_wait_ex(stk_sem_t *sem, stk_timeout_t timeout)
Wait for a semaphore resource, preserving the full outcome.
stk_event_t * stk_event_create(stk_event_mem_t *const membuf, bool manual_reset)
Create an Event (using provided memory).
bool stk_sem_wait(stk_sem_t *sem, stk_timeout_t timeout)
Wait for a semaphore resource.
bool stk_msgq_putfront(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout)
Put a message into the front of the queue (priority insert).
size_t stk_msgq_get_count(const stk_msgq_t *mq)
Get the current number of messages waiting in the queue.
bool stk_fastmutex_timed_lock(stk_fastmutex_t *mtx, stk_timeout_t timeout)
Try to lock the mutex with a timeout.
uint8_t * stk_msgq_get_buffer(stk_msgq_t *mq)
Get a pointer to the raw message data buffer.
stk_wait_result_t stk_msgq_putfront_ex(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout)
Put a message into the front of the queue, preserving the full outcome.
void stk_spinlock_lock(stk_spinlock_t *slock)
Acquire the SpinLock (recursive).
bool stk_msgq_put(stk_msgq_t *mq, const void *msg, stk_timeout_t timeout)
Put a message into the queue.
bool stk_msgq_tryput(stk_msgq_t *mq, const void *msg)
Attempt to put a message into the queue without blocking.
stk_ef_val_t stk_ef_get(stk_ef_t *ef)
Read the current flags word without modifying it.
Event * stk_event_get_instance(stk_event_t *ev)
Get the underlying C++ stk::sync::Event object wrapped by a stk_event_t handle.
bool stk_cv_wait_fastmutex(stk_cv_t *cv, stk_fastmutex_t *mtx, stk_timeout_t timeout)
Wait for a signal on the condition variable, protected by a FastMutex.
stk_wait_result_t stk_ef_wait_ex(stk_ef_t *ef, stk_ef_val_t flags, uint32_t options, stk_timeout_t timeout, stk_ef_val_t *actual_flags)
Wait for one or more flags to be set, reporting the outcome as stk_wait_result_t.
void stk_pipe_reset(stk_pipe_t *pipe)
Discard all elements and reset the pipe to the empty state.
stk_barrier_result_t stk_barrier_wait_ex(stk_barrier_t *barrier)
Block the calling task until count tasks have arrived, preserving the full outcome.
stk_wait_result_t stk_rwmutex_timed_read_lock_ex(stk_rwmutex_t *rw, stk_timeout_t timeout)
Try to acquire the read lock with a timeout, preserving the full outcome.
void stk_sem_destroy(stk_sem_t *sem)
Destroy a Semaphore.
void stk_spinlock_unlock(stk_spinlock_t *slock)
Release the SpinLock.
size_t stk_pipe_tryread_bulk_triggered(stk_pipe_t *pipe, void *dst, size_t max_count)
Non-blocking variant of stk_pipe_read_bulk_triggered.
stk_cv_t * stk_cv_create(stk_cv_mem_t *const membuf)
Create a Condition Variable (using provided memory).
bool stk_sem_trywait(stk_sem_t *sem)
Poll the semaphore without blocking.
sync::RWMutex * stk_rwmutex_get_instance(stk_rwmutex_t *rw)
Get the underlying C++ stk::sync::RWMutex object wrapped by a stk_rwmutex_t handle.
void stk_rwmutex_read_unlock(stk_rwmutex_t *rw)
Release the shared reader lock.
size_t stk_msgq_get_msg_size(const stk_msgq_t *mq)
Get the size of each message in bytes.
bool stk_pipe_tryread(stk_pipe_t *pipe, void *data)
Attempt to read a single element from the pipe without blocking.
ConditionVariable * stk_cv_get_instance(stk_cv_t *cv)
Get the underlying C++ stk::sync::ConditionVariable object wrapped by a stk_cv_t handle.
stk_wait_result_t stk_msgq_peek_ex(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Peek at the next message without removing it, preserving the full outcome.
bool stk_fastmutex_trylock(stk_fastmutex_t *mtx)
Try locking the mutex. Does not block if already locked.
size_t stk_pipe_read_bulk_triggered(stk_pipe_t *pipe, void *dst, size_t trigger, size_t max_count, stk_timeout_t timeout)
Read at least trigger elements, then drain up to max_count without blocking.
bool stk_msgq_get(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Get a message from the queue.
void stk_event_pulse(stk_event_t *ev)
Pulse the event (signal then immediately reset).
bool stk_msgq_tryputfront(stk_msgq_t *mq, const void *msg)
Attempt to put a message into the front of the queue without blocking.
bool stk_pipe_is_full(const stk_pipe_t *pipe)
Check whether the pipe is currently full.
bool stk_pipe_is_empty(const stk_pipe_t *pipe)
Check whether the pipe is currently empty.
stk_ef_sync_ctx_t * stk_ef_sync_ctx_create(stk_ef_sync_ctx_mem_t *const membuf)
Create a rendezvous context (using provided memory).
bool stk_pipe_read(stk_pipe_t *pipe, void *data, stk_timeout_t timeout)
Read a single element from the pipe.
void stk_fastmutex_lock(stk_fastmutex_t *mtx)
Lock the mutex. Blocks until available.
stk_wait_result_t stk_pipe_write_ex(stk_pipe_t *pipe, const void *data, stk_timeout_t timeout)
Write a single element to the pipe, preserving the full outcome.
bool stk_event_reset(stk_event_t *ev)
Reset the event to non-signaled state.
void stk_fastmutex_unlock(stk_fastmutex_t *mtx)
Unlock the mutex.
stk_wait_result_t stk_msgq_get_ex(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Get a message from the queue, preserving the full outcome.
bool stk_msgq_is_storage_valid(const stk_msgq_t *mq)
Verify that the backing storage is valid and the queue is ready for use.
stk_fastmutex_t * stk_fastmutex_create(stk_fastmutex_mem_t *const membuf)
Create a FastMutex (using provided memory).
bool stk_rwmutex_timed_lock(stk_rwmutex_t *rw, stk_timeout_t timeout)
Try to acquire the write lock with a timeout.
void stk_rwmutex_destroy(stk_rwmutex_t *rw)
Destroy an RWMutex.
void stk_msgq_destroy(stk_msgq_t *mq)
Destroy a MessageQueue.
stk_wait_result_t stk_event_wait_ex(stk_event_t *ev, stk_timeout_t timeout)
Wait for the event to become signaled, preserving the full outcome.
stk_ef_val_t stk_ef_set(stk_ef_t *ef, stk_ef_val_t flags)
Set one or more flags.
stk_barrier_t * stk_barrier_create(stk_barrier_mem_t *const membuf, uint32_t count)
Create a Barrier (using provided memory).
bool stk_rwmutex_timed_read_lock(stk_rwmutex_t *rw, stk_timeout_t timeout)
Try to acquire the read lock with a timeout.
int32_t stk_timeout_t
Timeout value.
Definition stk_c.h:175
Mutex * stk_mutex_get_instance(stk_mutex_t *mtx)
Get the underlying C++ stk::sync::Mutex object wrapped by a stk_mutex_t handle.
bool stk_msgq_peek(stk_msgq_t *mq, void *msg, stk_timeout_t timeout)
Peek at the next message to be delivered without removing it.
stk_ef_val_t stk_ef_wait(stk_ef_t *ef, stk_ef_val_t flags, uint32_t options, stk_timeout_t timeout)
Wait for one or more flags to be set.
stk_wait_result_t stk_mutex_timed_lock_ex(stk_mutex_t *mtx, stk_timeout_t timeout)
Try to lock the mutex with a timeout, preserving the full outcome.
bool stk_msgq_trypeek(stk_msgq_t *mq, void *msg)
Attempt to peek at the next message without blocking.
void stk_cv_notify_all(stk_cv_t *cv)
Wake all tasks waiting on the condition variable.
stk_wait_result_t stk_pipe_write_bulk_ex(stk_pipe_t *pipe, const void *src, size_t count, size_t *written, stk_timeout_t timeout)
Write multiple elements to the pipe, preserving the full outcome.
stk_rwmutex_t * stk_rwmutex_create(stk_rwmutex_mem_t *const membuf)
Create an RWMutex (using provided memory).
stk_wait_result_t stk_cv_wait_ex_fastmutex(stk_cv_t *cv, stk_fastmutex_t *mtx, stk_timeout_t timeout)
Wait for a signal on the condition variable protected by a FastMutex, preserving the full outcome.
bool stk_pipe_trywrite(stk_pipe_t *pipe, const void *data)
Attempt to write a single element to the pipe without blocking.
void stk_rwmutex_unlock(stk_rwmutex_t *rw)
Release the exclusive writer lock.
MessageQueue * stk_msgq_get_instance(stk_msgq_t *mq)
Get the underlying C++ stk::sync::MessageQueue object wrapped by a stk_msgq_t handle.
bool stk_barrier_wait(stk_barrier_t *barrier)
Block the calling task until count tasks have called stk_barrier_wait().
size_t stk_pipe_tryread_bulk(stk_pipe_t *pipe, void *dst, size_t count)
Attempt to read multiple elements from the pipe without blocking.
@ STK_WAIT_RESULT_TIMEOUT
The wake was caused by a timeout expiry.
Definition stk_c.h:213
@ STK_WAIT_RESULT_SIGNAL
The wake was caused by a signal.
Definition stk_c.h:212
@ STK_WAIT_RESULT_CANCELED
The wait was cancelled by another task, independent of signal/timeout.
Definition stk_c.h:214
@ STK_WAIT_RESULT_FAIL
The kernel returned an error without actually waiting.
Definition stk_c.h:211
@ STK_BARRIER_CANCELED
Definition stk_c.h:2914
@ STK_BARRIER_RELEASED
Released because the barrier tripped; this task was not the last arrival.
Definition stk_c.h:2912
@ STK_BARRIER_LAST_ARRIVAL
This task was the last arrival and tripped the barrier.
Definition stk_c.h:2913
Namespace of STK package.
static T * ConstructIn(Mem &mem, Args &&... args)
Construct object T in-place inside a C memory container.
Definition stk_c.h:2970
EWaitResult
Wait result (see IKernelService::Wait).
Definition stk_common.h:138
@ WAIT_RESULT_CANCELED
The wait was cancelled by another task via IKernel::CancelTaskWait(), independent of signal/timeout.
Definition stk_common.h:142
@ WAIT_RESULT_TIMEOUT
The wake was caused by a timeout expiry.
Definition stk_common.h:141
@ WAIT_RESULT_SIGNAL
The wake was caused by a signal.
Definition stk_common.h:140
Synchronization primitives for task coordination and resource protection.
Cyclic barrier that blocks a fixed-size group of tasks until all of them have arrived.
uint32_t GetThreshold() const
Get the number of tasks required to trip the barrier.
bool Wait()
Block the calling task until count tasks have called Wait().
@ BARRIER_LAST_ARRIVAL
This task was the last arrival and tripped the barrier.
EResult WaitEx()
Block the calling task until count tasks have called Wait(), preserving the full outcome.
Condition Variable primitive for signaling between tasks based on specific predicates.
Definition stk_sync_cv.h:68
EWaitResult WaitEx(IMutex &mutex, Timeout timeout_ticks=WAIT_INFINITE)
Wait for a signal, preserving the full wait outcome.
void NotifyOne()
Wake one waiting task.
bool Wait(IMutex &mutex, Timeout timeout_ticks=WAIT_INFINITE)
Wait for a signal.
void NotifyAll()
Wake all waiting tasks.
Binary synchronization event (signaled / non-signaled) primitive.
bool Set()
Set event to signaled state.
void Pulse()
Pulse event: attempt to release waiters and then reset (Win32 PulseEvent() semantics).
bool TryWait()
Poll event state without blocking.
EWaitResult WaitEx(Timeout timeout_ticks=WAIT_INFINITE)
Wait until event becomes signaled or the timeout expires, preserving the full wait outcome.
bool Reset()
Reset event to non-signaled state.
bool Wait(Timeout timeout_ticks=WAIT_INFINITE)
Wait until event becomes signaled or the timeout expires.
Event flags group for multi-flag synchronization between tasks, with a selectable flags word type.
T And(T mask)
AND the flags word with a mask (equivalent of ThreadX tx_event_flags_set(TX_AND)).
T Set(T flags)
Set one or more flags.
EWaitResult WaitEx(T flags, uint32_t options, Timeout timeout_ticks, T &actual_flags)
Wait for one or more flags to be set, reporting the outcome via EWaitResult.
T Clear(T flags)
Clear one or more flags.
T Wait(T flags, uint32_t options=OPT_WAIT_ANY, Timeout timeout_ticks=WAIT_INFINITE)
Wait for one or more flags to be set.
T Get() const
Read the current flags word without modifying it.
T TryWait(T flags, uint32_t options=OPT_WAIT_ANY)
Non-blocking flag poll.
EWaitResult Sync(SyncContext &ctx, T set_flags, T wait_flags, Timeout timeout_ticks, T &actual_flags)
Atomically set flags and wait for a group of flags: a multi-task rendezvous (equivalent of FreeRTOS x...
State of one Sync() rendezvous group, supplied by the caller.
Non-recursive (binary) mutex primitive with ownership tracking.
bool TryLock()
Try to acquire the lock without blocking.
void Lock() override
Acquire lock.
bool TimedLock(Timeout timeout_ticks)
Acquire lock.
void Unlock() override
Release lock.
EWaitResult TimedLockEx(Timeout timeout_ticks)
Acquire lock, preserving the full wait outcome.
Fixed-capacity, fixed-message-size FIFO queue for inter-task communication.
bool TryPeek(void *msg_ptr)
Attempt to peek at the next message without blocking.
bool TryPeekFront(void *msg_ptr)
Attempt to peek at the front message without blocking.
bool IsEmpty() const
Check whether the queue is currently empty.
bool PeekFront(void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Peek at the front of the queue (the message Get() returns next) without removing it.
size_t GetCapacity() const
Get the maximum number of messages the queue can hold.
EWaitResult PutEx(const void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Put a message into the back of the queue (FIFO order), preserving the full wait outcome.
bool TryPutFront(const void *msg_ptr)
Attempt to put a message into the front of the queue without blocking.
size_t GetMsgSize() const
Get the size of each message in bytes.
bool TryPut(const void *msg_ptr)
Attempt to put a message into the back of the queue without blocking.
EWaitResult PeekFrontEx(void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Peek at the front of the queue without removing it, preserving the full wait outcome.
size_t GetSpace() const
Get the number of free slots currently available.
EWaitResult PutFrontEx(const void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Put a message into the front of the queue (LIFO / priority-insert order), preserving the full wait ou...
size_t GetCount() const
Get the current number of messages in the queue.
bool TryGet(void *msg_ptr)
Attempt to get a message from the queue without blocking.
uint8_t * GetBuffer()
Get pointer to the message buffer.
bool Peek(void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Peek at the next message to be delivered (the one Get() returns next) without removing it.
EWaitResult PeekEx(void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Peek at the next message to be delivered without removing it, preserving the full wait outcome.
EWaitResult GetEx(void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Get a message from the queue, preserving the full wait outcome.
bool PutFront(const void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Put a message into the front of the queue (LIFO / priority-insert order).
bool IsStorageValid() const
Verify that the backing storage is valid and the pool is ready for use.
bool Put(const void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Put a message into the back of the queue (FIFO order).
bool Get(void *msg_ptr, Timeout timeout_ticks=WAIT_INFINITE)
Get a message from the queue.
bool IsFull() const
Check whether the queue is currently full.
void Reset()
Discard all messages and reset the queue to the empty state.
Recursive mutex primitive that allows the same thread to acquire the lock multiple times.
EWaitResult TimedLockEx(Timeout timeout_ticks)
Acquire lock, preserving the full wait outcome.
bool TimedLock(Timeout timeout_ticks)
Acquire lock.
bool TryLock()
Try to acquire the lock without blocking.
void Unlock() override
Release lock.
void Lock() override
Acquire lock.
Thread-safe FIFO communication pipe for inter-task data passing.
size_t WriteBulk(const void *src, size_t count, Timeout timeout_ticks=WAIT_INFINITE)
Write multiple elements to the pipe.
bool TryRead(void *data)
Attempt to read a single element from the pipe without blocking.
size_t TryReadBulkTriggered(void *dst, size_t max_count)
Non-blocking variant of ReadBulkTriggered.
void Reset()
Discard all elements and reset the pipe to the empty state.
EWaitResult WriteBulkEx(const void *src, size_t count, size_t &written, Timeout timeout_ticks=WAIT_INFINITE)
Write multiple elements to the pipe, preserving the full wait outcome.
bool Read(void *data, Timeout timeout_ticks=WAIT_INFINITE)
Read a single element from the pipe.
EWaitResult ReadBulkTriggeredEx(void *dst, size_t trigger, size_t max_count, size_t &read_count, Timeout timeout_ticks=WAIT_INFINITE)
Read at least trigger elements, then drain up to max_count without blocking, preserving the full wait...
EWaitResult ReadBulkEx(void *dst, size_t count, size_t &read_count, Timeout timeout_ticks=WAIT_INFINITE)
Read multiple elements from the pipe, preserving the full wait outcome.
EWaitResult ReadEx(void *data, Timeout timeout_ticks=WAIT_INFINITE)
Read a single element from the pipe, preserving the full wait outcome.
size_t ReadBulk(void *dst, size_t count, Timeout timeout_ticks=WAIT_INFINITE)
Read multiple elements from the pipe.
bool Write(const void *data, Timeout timeout_ticks=WAIT_INFINITE)
Write a single element to the pipe.
size_t GetElementSize() const
Get the size of each element in bytes.
size_t TryWriteBulk(const void *src, size_t count)
Attempt to write multiple elements to the pipe without blocking.
bool IsEmpty() const
Check if the pipe is currently empty.
EWaitResult WriteEx(const void *data, Timeout timeout_ticks=WAIT_INFINITE)
Write a single element to the pipe, preserving the full wait outcome.
size_t GetCapacity() const
Get the maximum number of elements the pipe can hold.
bool TryWrite(const void *data)
Attempt to write a single element to the pipe without blocking.
size_t GetCount() const
Get the current number of elements in the pipe.
bool IsFull() const
Check if the pipe is currently full.
size_t TryReadBulk(void *dst, size_t count)
Attempt to read multiple elements from the pipe without blocking.
size_t GetSpace() const
Get the number of free slots currently available.
size_t ReadBulkTriggered(void *dst, size_t trigger, size_t max_count, Timeout timeout_ticks=WAIT_INFINITE)
Read at least trigger elements, then drain up to max_count without blocking.
bool IsStorageValid() const
Verify that the backing storage is valid and the pipe is ready for use.
Reader-Writer Lock synchronization primitive for non-recursive shared and exclusive access.
bool TryLock()
Attempt to acquire the lock for exclusive writing without blocking.
bool TimedLock(Timeout timeout_ticks)
Acquire the lock for exclusive writing with a timeout.
void Unlock() override
Release the exclusive writer lock (IMutex interface).
void Lock() override
Acquire the lock for exclusive writing (IMutex interface).
EWaitResult TimedReadLockEx(Timeout timeout_ticks)
Acquire the lock for shared reading with a timeout, preserving the full wait outcome.
EWaitResult TimedLockEx(Timeout timeout_ticks)
Acquire the lock for exclusive writing with a timeout, preserving the full wait outcome.
bool TimedReadLock(Timeout timeout_ticks)
Acquire the lock for shared reading with a timeout.
void ReadUnlock()
Release the shared reader lock.
void ReadLock()
Acquire the lock for shared reading.
bool TryReadLock()
Attempt to acquire the lock for shared reading without blocking.
Counting semaphore primitive for resource management and signaling, with a selectable counter type.
static constexpr T COUNT_MAX
Max count value supported (maximum value of T).
T GetCount() const
Get current counter value.
bool Wait(Timeout timeout_ticks=WAIT_INFINITE)
Wait for a signal (decrement counter).
bool TrySignal()
Try to post a signal without exceeding max_count.
void Signal()
Post a signal (increment counter).
bool TryWait()
Poll the semaphore without blocking (decrement counter if available).
EWaitResult WaitEx(Timeout timeout_ticks=WAIT_INFINITE)
Wait for a signal (decrement counter), preserving the full wait outcome.
Recursive spinlock.
void Unlock()
Release the lock or decrement the recursion counter.
bool TryLock()
Attempt to acquire the lock without blocking.
void Lock()
Acquire the lock.
Opaque memory container for a Mutex instance.
Definition stk_c.h:1203
Opaque memory container for a FastMutex instance.
Definition stk_c.h:1281
Opaque memory container for SpinLock object.
Definition stk_c.h:1370
Opaque memory container for a ConditionVariable instance.
Definition stk_c.h:1418
Opaque memory container for an Event instance.
Definition stk_c.h:1527
Opaque memory container for a Semaphore instance.
Definition stk_c.h:1632
Opaque memory container for an EventFlags instance.
Definition stk_c.h:1808
Opaque memory container for a rendezvous context.
Definition stk_c.h:1959
Opaque memory container for a Pipe control-block.
Definition stk_c.h:2024
Opaque memory container for a MessageQueue instance.
Definition stk_c.h:2401
Opaque memory container for an RWMutex instance.
Definition stk_c.h:2755
Opaque memory container for a Barrier instance.
Definition stk_c.h:2887
sync::SpinLock handle
ConditionVariable handle
stk_event_t(bool manual_reset)
stk_sem_t(stk_sem_val_t initial_count, stk_sem_val_t max_count)
SemaphoreT< stk_sem_val_t > handle
EventFlagsT< stk_ef_val_t > handle
stk_ef_t(stk_ef_val_t initial_flags)
EventFlagsT< stk_ef_val_t >::SyncContext handle
stk_pipe_t(uint8_t *buf, size_t capacity, size_t element_size)
MessageQueue handle
stk_msgq_t(uint8_t *buf, size_t capacity, size_t msg_size)
sync::RWMutex handle
stk_barrier_t(uint32_t count)