-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathrtx_thread.c
1901 lines (1630 loc) · 55.9 KB
/
rtx_thread.c
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013-2019 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* -----------------------------------------------------------------------------
*
* Project: CMSIS-RTOS RTX
* Title: Thread functions
*
* -----------------------------------------------------------------------------
*/
#include "rtx_lib.h"
// OS Runtime Object Memory Usage
#if ((defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0)))
osRtxObjectMemUsage_t osRtxThreadMemUsage \
__attribute__((section(".data.os.thread.obj"))) =
{ 0U, 0U, 0U };
#endif
// ==== Helper functions ====
/// Set Thread Flags.
/// \param[in] thread thread object.
/// \param[in] flags specifies the flags to set.
/// \return thread flags after setting.
static uint32_t ThreadFlagsSet (os_thread_t *thread, uint32_t flags) {
#if (EXCLUSIVE_ACCESS == 0)
uint32_t primask = __get_PRIMASK();
#endif
uint32_t thread_flags;
#if (EXCLUSIVE_ACCESS == 0)
__disable_irq();
thread->thread_flags |= flags;
thread_flags = thread->thread_flags;
if (primask == 0U) {
__enable_irq();
}
#else
thread_flags = atomic_set32(&thread->thread_flags, flags);
#endif
return thread_flags;
}
/// Clear Thread Flags.
/// \param[in] thread thread object.
/// \param[in] flags specifies the flags to clear.
/// \return thread flags before clearing.
static uint32_t ThreadFlagsClear (os_thread_t *thread, uint32_t flags) {
#if (EXCLUSIVE_ACCESS == 0)
uint32_t primask = __get_PRIMASK();
#endif
uint32_t thread_flags;
#if (EXCLUSIVE_ACCESS == 0)
__disable_irq();
thread_flags = thread->thread_flags;
thread->thread_flags &= ~flags;
if (primask == 0U) {
__enable_irq();
}
#else
thread_flags = atomic_clr32(&thread->thread_flags, flags);
#endif
return thread_flags;
}
/// Check Thread Flags.
/// \param[in] thread thread object.
/// \param[in] flags specifies the flags to check.
/// \param[in] options specifies flags options (osFlagsXxxx).
/// \return thread flags before clearing or 0 if specified flags have not been set.
static uint32_t ThreadFlagsCheck (os_thread_t *thread, uint32_t flags, uint32_t options) {
#if (EXCLUSIVE_ACCESS == 0)
uint32_t primask;
#endif
uint32_t thread_flags;
if ((options & osFlagsNoClear) == 0U) {
#if (EXCLUSIVE_ACCESS == 0)
primask = __get_PRIMASK();
__disable_irq();
thread_flags = thread->thread_flags;
if ((((options & osFlagsWaitAll) != 0U) && ((thread_flags & flags) != flags)) ||
(((options & osFlagsWaitAll) == 0U) && ((thread_flags & flags) == 0U))) {
thread_flags = 0U;
} else {
thread->thread_flags &= ~flags;
}
if (primask == 0U) {
__enable_irq();
}
#else
if ((options & osFlagsWaitAll) != 0U) {
thread_flags = atomic_chk32_all(&thread->thread_flags, flags);
} else {
thread_flags = atomic_chk32_any(&thread->thread_flags, flags);
}
#endif
} else {
thread_flags = thread->thread_flags;
if ((((options & osFlagsWaitAll) != 0U) && ((thread_flags & flags) != flags)) ||
(((options & osFlagsWaitAll) == 0U) && ((thread_flags & flags) == 0U))) {
thread_flags = 0U;
}
}
return thread_flags;
}
// ==== Library functions ====
/// Put a Thread into specified Object list sorted by Priority (Highest at Head).
/// \param[in] object generic object.
/// \param[in] thread thread object.
void osRtxThreadListPut (os_object_t *object, os_thread_t *thread) {
os_thread_t *prev, *next;
int32_t priority;
priority = thread->priority;
prev = osRtxThreadObject(object);
next = prev->thread_next;
while ((next != NULL) && (next->priority >= priority)) {
prev = next;
next = next->thread_next;
}
thread->thread_prev = prev;
thread->thread_next = next;
prev->thread_next = thread;
if (next != NULL) {
next->thread_prev = thread;
}
}
/// Get a Thread with Highest Priority from specified Object list and remove it.
/// \param[in] object generic object.
/// \return thread object.
os_thread_t *osRtxThreadListGet (os_object_t *object) {
os_thread_t *thread;
thread = object->thread_list;
object->thread_list = thread->thread_next;
if (thread->thread_next != NULL) {
thread->thread_next->thread_prev = osRtxThreadObject(object);
}
thread->thread_prev = NULL;
return thread;
}
/// Retrieve Thread list root object.
/// \param[in] thread thread object.
/// \return root object.
static void *osRtxThreadListRoot (os_thread_t *thread) {
os_thread_t *thread0;
thread0 = thread;
while (thread0->id == osRtxIdThread) {
thread0 = thread0->thread_prev;
}
return thread0;
}
/// Re-sort a Thread in linked Object list by Priority (Highest at Head).
/// \param[in] thread thread object.
void osRtxThreadListSort (os_thread_t *thread) {
os_object_t *object;
os_thread_t *thread0;
// Search for object
thread0 = thread;
while ((thread0 != NULL) && (thread0->id == osRtxIdThread)) {
thread0 = thread0->thread_prev;
}
object = osRtxObject(thread0);
if (object != NULL) {
osRtxThreadListRemove(thread);
osRtxThreadListPut(object, thread);
}
}
/// Remove a Thread from linked Object list.
/// \param[in] thread thread object.
void osRtxThreadListRemove (os_thread_t *thread) {
if (thread->thread_prev != NULL) {
thread->thread_prev->thread_next = thread->thread_next;
if (thread->thread_next != NULL) {
thread->thread_next->thread_prev = thread->thread_prev;
}
thread->thread_prev = NULL;
}
}
/// Unlink a Thread from specified linked list.
/// \param[in] thread thread object.
static void osRtxThreadListUnlink (os_thread_t **thread_list, os_thread_t *thread) {
if (thread->thread_next != NULL) {
thread->thread_next->thread_prev = thread->thread_prev;
}
if (thread->thread_prev != NULL) {
thread->thread_prev->thread_next = thread->thread_next;
thread->thread_prev = NULL;
} else {
*thread_list = thread->thread_next;
}
}
/// Mark a Thread as Ready and put it into Ready list (sorted by Priority).
/// \param[in] thread thread object.
void osRtxThreadReadyPut (os_thread_t *thread) {
thread->state = osRtxThreadReady;
osRtxThreadListPut(&osRtxInfo.thread.ready, thread);
}
/// Insert a Thread into the Delay list sorted by Delay (Lowest at Head).
/// \param[in] thread thread object.
/// \param[in] delay delay value.
static void osRtxThreadDelayInsert (os_thread_t *thread, uint32_t delay) {
os_thread_t *prev, *next;
if (delay == osWaitForever) {
prev = NULL;
next = osRtxInfo.thread.wait_list;
while (next != NULL) {
prev = next;
next = next->delay_next;
}
thread->delay = delay;
thread->delay_prev = prev;
thread->delay_next = NULL;
if (prev != NULL) {
prev->delay_next = thread;
} else {
osRtxInfo.thread.wait_list = thread;
}
} else {
prev = NULL;
next = osRtxInfo.thread.delay_list;
while ((next != NULL) && (next->delay <= delay)) {
delay -= next->delay;
prev = next;
next = next->delay_next;
}
thread->delay = delay;
thread->delay_prev = prev;
thread->delay_next = next;
if (prev != NULL) {
prev->delay_next = thread;
} else {
osRtxInfo.thread.delay_list = thread;
}
if (next != NULL) {
next->delay -= delay;
next->delay_prev = thread;
}
}
}
/// Remove a Thread from the Delay list.
/// \param[in] thread thread object.
static void osRtxThreadDelayRemove (os_thread_t *thread) {
if (thread->delay == osWaitForever) {
if (thread->delay_next != NULL) {
thread->delay_next->delay_prev = thread->delay_prev;
}
if (thread->delay_prev != NULL) {
thread->delay_prev->delay_next = thread->delay_next;
thread->delay_prev = NULL;
} else {
osRtxInfo.thread.wait_list = thread->delay_next;
}
} else {
if (thread->delay_next != NULL) {
thread->delay_next->delay += thread->delay;
thread->delay_next->delay_prev = thread->delay_prev;
}
if (thread->delay_prev != NULL) {
thread->delay_prev->delay_next = thread->delay_next;
thread->delay_prev = NULL;
} else {
osRtxInfo.thread.delay_list = thread->delay_next;
}
}
}
/// Process Thread Delay Tick (executed each System Tick).
void osRtxThreadDelayTick (void) {
os_thread_t *thread;
os_object_t *object;
thread = osRtxInfo.thread.delay_list;
if (thread == NULL) {
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return;
}
thread->delay--;
if (thread->delay == 0U) {
do {
switch (thread->state) {
case osRtxThreadWaitingDelay:
EvrRtxDelayCompleted(thread);
break;
case osRtxThreadWaitingThreadFlags:
EvrRtxThreadFlagsWaitTimeout(thread);
break;
case osRtxThreadWaitingEventFlags:
object = osRtxObject(osRtxThreadListRoot(thread));
EvrRtxEventFlagsWaitTimeout(osRtxEventFlagsObject(object));
break;
case osRtxThreadWaitingMutex:
object = osRtxObject(osRtxThreadListRoot(thread));
osRtxMutexOwnerRestore(osRtxMutexObject(object), thread);
EvrRtxMutexAcquireTimeout(osRtxMutexObject(object));
break;
case osRtxThreadWaitingSemaphore:
object = osRtxObject(osRtxThreadListRoot(thread));
EvrRtxSemaphoreAcquireTimeout(osRtxSemaphoreObject(object));
break;
case osRtxThreadWaitingMemoryPool:
object = osRtxObject(osRtxThreadListRoot(thread));
EvrRtxMemoryPoolAllocTimeout(osRtxMemoryPoolObject(object));
break;
case osRtxThreadWaitingMessageGet:
object = osRtxObject(osRtxThreadListRoot(thread));
EvrRtxMessageQueueGetTimeout(osRtxMessageQueueObject(object));
break;
case osRtxThreadWaitingMessagePut:
object = osRtxObject(osRtxThreadListRoot(thread));
EvrRtxMessageQueuePutTimeout(osRtxMessageQueueObject(object));
break;
default:
// Invalid
break;
}
EvrRtxThreadUnblocked(thread, (osRtxThreadRegPtr(thread))[0]);
osRtxThreadListRemove(thread);
osRtxThreadReadyPut(thread);
thread = thread->delay_next;
} while ((thread != NULL) && (thread->delay == 0U));
if (thread != NULL) {
thread->delay_prev = NULL;
}
osRtxInfo.thread.delay_list = thread;
}
}
/// Get pointer to Thread registers (R0..R3)
/// \param[in] thread thread object.
/// \return pointer to registers R0-R3.
uint32_t *osRtxThreadRegPtr (const os_thread_t *thread) {
uint32_t addr = thread->sp + StackOffsetR0(thread->stack_frame);
//lint -e{923} -e{9078} "cast from unsigned int to pointer"
return ((uint32_t *)addr);
}
/// Block running Thread execution and register it as Ready to Run.
/// \param[in] thread running thread object.
static void osRtxThreadBlock (os_thread_t *thread) {
os_thread_t *prev, *next;
int32_t priority;
thread->state = osRtxThreadReady;
priority = thread->priority;
prev = osRtxThreadObject(&osRtxInfo.thread.ready);
next = prev->thread_next;
while ((next != NULL) && (next->priority >= priority)) {
prev = next;
next = next->thread_next;
}
thread->thread_prev = prev;
thread->thread_next = next;
prev->thread_next = thread;
if (next != NULL) {
next->thread_prev = thread;
}
EvrRtxThreadPreempted(thread);
}
/// Switch to specified Thread.
/// \param[in] thread thread object.
void osRtxThreadSwitch (os_thread_t *thread) {
thread->state = osRtxThreadRunning;
osRtxInfo.thread.run.next = thread;
osRtxThreadStackCheck();
EvrRtxThreadSwitched(thread);
}
/// Dispatch specified Thread or Ready Thread with Highest Priority.
/// \param[in] thread thread object or NULL.
void osRtxThreadDispatch (os_thread_t *thread) {
uint8_t kernel_state;
os_thread_t *thread_running;
os_thread_t *thread_ready;
kernel_state = osRtxKernelGetState();
thread_running = osRtxThreadGetRunning();
if (thread == NULL) {
thread_ready = osRtxInfo.thread.ready.thread_list;
if ((kernel_state == osRtxKernelRunning) &&
(thread_ready != NULL) &&
(thread_ready->priority > thread_running->priority)) {
// Preempt running Thread
osRtxThreadListRemove(thread_ready);
osRtxThreadBlock(thread_running);
osRtxThreadSwitch(thread_ready);
}
} else {
if ((kernel_state == osRtxKernelRunning) &&
(thread->priority > thread_running->priority)) {
// Preempt running Thread
osRtxThreadBlock(thread_running);
osRtxThreadSwitch(thread);
} else {
// Put Thread into Ready list
osRtxThreadReadyPut(thread);
}
}
}
/// Exit Thread wait state.
/// \param[in] thread thread object.
/// \param[in] ret_val return value.
/// \param[in] dispatch dispatch flag.
void osRtxThreadWaitExit (os_thread_t *thread, uint32_t ret_val, bool_t dispatch) {
uint32_t *reg;
EvrRtxThreadUnblocked(thread, ret_val);
reg = osRtxThreadRegPtr(thread);
reg[0] = ret_val;
osRtxThreadDelayRemove(thread);
if (dispatch) {
osRtxThreadDispatch(thread);
} else {
osRtxThreadReadyPut(thread);
}
}
/// Enter Thread wait state.
/// \param[in] state new thread state.
/// \param[in] timeout timeout.
/// \return true - success, false - failure.
bool_t osRtxThreadWaitEnter (uint8_t state, uint32_t timeout) {
os_thread_t *thread;
// Check if Kernel is running
if (osRtxKernelGetState() != osRtxKernelRunning) {
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return FALSE;
}
// Check if any thread is ready
if (osRtxInfo.thread.ready.thread_list == NULL) {
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return FALSE;
}
// Get running thread
thread = osRtxThreadGetRunning();
EvrRtxThreadBlocked(thread, timeout);
thread->state = state;
osRtxThreadDelayInsert(thread, timeout);
thread = osRtxThreadListGet(&osRtxInfo.thread.ready);
osRtxThreadSwitch(thread);
return TRUE;
}
/// Check current running Thread Stack.
//lint -esym(759,osRtxThreadStackCheck) "Prototype in header"
//lint -esym(765,osRtxThreadStackCheck) "Global scope (can be overridden)"
__WEAK void osRtxThreadStackCheck (void) {
os_thread_t *thread;
thread = osRtxThreadGetRunning();
if (thread != NULL) {
//lint -e{923} "cast from pointer to unsigned int"
//lint -e{9079} -e{9087} "cast between pointers to different object types"
if ((thread->sp <= (uint32_t)thread->stack_mem) ||
(*((uint32_t *)thread->stack_mem) != osRtxStackMagicWord)) {
(void)osRtxErrorNotify(osRtxErrorStackUnderflow, thread);
}
}
}
#ifdef RTX_TF_M_EXTENSION
/// Get TrustZone Module Identifier of running Thread.
/// \return TrustZone Module Identifier.
uint32_t osRtxTzGetModuleId (void) {
os_thread_t *thread;
uint32_t tz_module;
thread = osRtxThreadGetRunning();
if (thread != NULL) {
tz_module = thread->tz_module;
} else {
tz_module = 0U;
}
return tz_module;
}
#endif
// ==== Post ISR processing ====
/// Thread post ISR processing.
/// \param[in] thread thread object.
static void osRtxThreadPostProcess (os_thread_t *thread) {
uint32_t thread_flags;
// Check if Thread is waiting for Thread Flags
if (thread->state == osRtxThreadWaitingThreadFlags) {
thread_flags = ThreadFlagsCheck(thread, thread->wait_flags, thread->flags_options);
if (thread_flags != 0U) {
osRtxThreadWaitExit(thread, thread_flags, FALSE);
EvrRtxThreadFlagsWaitCompleted(thread->wait_flags, thread->flags_options, thread_flags, thread);
}
}
}
// ==== Service Calls ====
/// Create a thread and add it to Active Threads.
/// \note API identical to osThreadNew
static osThreadId_t svcRtxThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
os_thread_t *thread;
uint32_t attr_bits;
void *stack_mem;
uint32_t stack_size;
osPriority_t priority;
uint8_t flags;
const char *name;
uint32_t *ptr;
uint32_t n;
#if (DOMAIN_NS == 1)
TZ_ModuleId_t tz_module;
TZ_MemoryId_t tz_memory;
#endif
// Check parameters
if (func == NULL) {
EvrRtxThreadError(NULL, (int32_t)osErrorParameter);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return NULL;
}
// Process attributes
if (attr != NULL) {
name = attr->name;
attr_bits = attr->attr_bits;
//lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
thread = attr->cb_mem;
//lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
stack_mem = attr->stack_mem;
stack_size = attr->stack_size;
priority = attr->priority;
#if (DOMAIN_NS == 1)
tz_module = attr->tz_module;
#endif
if (thread != NULL) {
//lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
if ((((uint32_t)thread & 3U) != 0U) || (attr->cb_size < sizeof(os_thread_t))) {
EvrRtxThreadError(NULL, osRtxErrorInvalidControlBlock);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return NULL;
}
} else {
if (attr->cb_size != 0U) {
EvrRtxThreadError(NULL, osRtxErrorInvalidControlBlock);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return NULL;
}
}
if (stack_mem != NULL) {
//lint -e(923) -e(9078) "cast from pointer to unsigned int" [MISRA Note 7]
if ((((uint32_t)stack_mem & 7U) != 0U) || (stack_size == 0U)) {
EvrRtxThreadError(NULL, osRtxErrorInvalidThreadStack);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return NULL;
}
}
if (priority == osPriorityNone) {
priority = osPriorityNormal;
} else {
if ((priority < osPriorityIdle) || (priority > osPriorityISR)) {
EvrRtxThreadError(NULL, osRtxErrorInvalidPriority);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return NULL;
}
}
} else {
name = NULL;
attr_bits = 0U;
thread = NULL;
stack_mem = NULL;
stack_size = 0U;
priority = osPriorityNormal;
#if (DOMAIN_NS == 1)
tz_module = 0U;
#endif
}
// Check stack size
if ((stack_size != 0U) && (((stack_size & 7U) != 0U) || (stack_size < (64U + 8U)))) {
EvrRtxThreadError(NULL, osRtxErrorInvalidThreadStack);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return NULL;
}
// Allocate object memory if not provided
if (thread == NULL) {
if (osRtxInfo.mpi.thread != NULL) {
//lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
thread = osRtxMemoryPoolAlloc(osRtxInfo.mpi.thread);
} else {
//lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
thread = osRtxMemoryAlloc(osRtxInfo.mem.common, sizeof(os_thread_t), 1U);
}
#if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
if (thread != NULL) {
uint32_t used;
osRtxThreadMemUsage.cnt_alloc++;
used = osRtxThreadMemUsage.cnt_alloc - osRtxThreadMemUsage.cnt_free;
if (osRtxThreadMemUsage.max_used < used) {
osRtxThreadMemUsage.max_used = used;
}
}
#endif
flags = osRtxFlagSystemObject;
} else {
flags = 0U;
}
// Allocate stack memory if not provided
if ((thread != NULL) && (stack_mem == NULL)) {
if (stack_size == 0U) {
stack_size = osRtxConfig.thread_stack_size;
if (osRtxInfo.mpi.stack != NULL) {
//lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
stack_mem = osRtxMemoryPoolAlloc(osRtxInfo.mpi.stack);
if (stack_mem != NULL) {
flags |= osRtxThreadFlagDefStack;
}
} else {
//lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
stack_mem = osRtxMemoryAlloc(osRtxInfo.mem.stack, stack_size, 0U);
}
} else {
//lint -e{9079} "conversion from pointer to void to pointer to other type" [MISRA Note 5]
stack_mem = osRtxMemoryAlloc(osRtxInfo.mem.stack, stack_size, 0U);
}
if (stack_mem == NULL) {
if ((flags & osRtxFlagSystemObject) != 0U) {
if (osRtxInfo.mpi.thread != NULL) {
(void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
} else {
(void)osRtxMemoryFree(osRtxInfo.mem.common, thread);
}
#if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
osRtxThreadMemUsage.cnt_free++;
#endif
}
thread = NULL;
}
flags |= osRtxFlagSystemMemory;
}
#if (DOMAIN_NS == 1)
// Allocate secure process stack
if ((thread != NULL) && (tz_module != 0U)) {
tz_memory = TZ_AllocModuleContext_S(tz_module);
if (tz_memory == 0U) {
EvrRtxThreadError(NULL, osRtxErrorTZ_AllocContext_S);
if ((flags & osRtxFlagSystemMemory) != 0U) {
if ((flags & osRtxThreadFlagDefStack) != 0U) {
(void)osRtxMemoryPoolFree(osRtxInfo.mpi.stack, thread->stack_mem);
} else {
(void)osRtxMemoryFree(osRtxInfo.mem.stack, thread->stack_mem);
}
}
if ((flags & osRtxFlagSystemObject) != 0U) {
if (osRtxInfo.mpi.thread != NULL) {
(void)osRtxMemoryPoolFree(osRtxInfo.mpi.thread, thread);
} else {
(void)osRtxMemoryFree(osRtxInfo.mem.common, thread);
}
#if (defined(OS_OBJ_MEM_USAGE) && (OS_OBJ_MEM_USAGE != 0))
osRtxThreadMemUsage.cnt_free++;
#endif
}
thread = NULL;
}
} else {
tz_memory = 0U;
}
#endif
if (thread != NULL) {
// Initialize control block
//lint --e{923} --e{9078} "cast between pointers and unsigned int"
//lint --e{9079} --e{9087} "cast between pointers to different object types"
//lint --e{9074} "conversion between a pointer to function and another type"
thread->id = osRtxIdThread;
thread->state = osRtxThreadReady;
thread->flags = flags;
thread->attr = (uint8_t)attr_bits;
thread->name = name;
thread->thread_next = NULL;
thread->thread_prev = NULL;
thread->delay_next = NULL;
thread->delay_prev = NULL;
thread->thread_join = NULL;
thread->delay = 0U;
thread->priority = (int8_t)priority;
thread->priority_base = (int8_t)priority;
thread->stack_frame = STACK_FRAME_INIT_VAL;
thread->flags_options = 0U;
thread->wait_flags = 0U;
thread->thread_flags = 0U;
thread->mutex_list = NULL;
thread->stack_mem = stack_mem;
thread->stack_size = stack_size;
thread->sp = (uint32_t)stack_mem + stack_size - 64U;
thread->thread_addr = (uint32_t)func;
#if (DOMAIN_NS == 1)
thread->tz_memory = tz_memory;
#ifdef RTX_TF_M_EXTENSION
thread->tz_module = tz_module;
#endif
#endif
// Initialize stack
//lint --e{613} false detection: "Possible use of null pointer"
ptr = (uint32_t *)stack_mem;
ptr[0] = osRtxStackMagicWord;
if ((osRtxConfig.flags & osRtxConfigStackWatermark) != 0U) {
for (n = (stack_size/4U) - (16U + 1U); n != 0U; n--) {
ptr++;
*ptr = osRtxStackFillPattern;
}
}
ptr = (uint32_t *)thread->sp;
for (n = 0U; n != 13U; n++) {
ptr[n] = 0U; // R4..R11, R0..R3, R12
}
ptr[13] = (uint32_t)osThreadExit; // LR
ptr[14] = (uint32_t)func; // PC
ptr[15] = xPSR_InitVal(
(bool_t)((osRtxConfig.flags & osRtxConfigPrivilegedMode) != 0U),
(bool_t)(((uint32_t)func & 1U) != 0U)
); // xPSR
ptr[8] = (uint32_t)argument; // R0
// Register post ISR processing function
osRtxInfo.post_process.thread = osRtxThreadPostProcess;
EvrRtxThreadCreated(thread, thread->thread_addr, thread->name);
} else {
EvrRtxThreadError(NULL, (int32_t)osErrorNoMemory);
}
if (thread != NULL) {
osRtxThreadDispatch(thread);
}
return thread;
}
/// Get name of a thread.
/// \note API identical to osThreadGetName
static const char *svcRtxThreadGetName (osThreadId_t thread_id) {
os_thread_t *thread = osRtxThreadId(thread_id);
// Check parameters
if ((thread == NULL) || (thread->id != osRtxIdThread)) {
EvrRtxThreadGetName(thread, NULL);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return NULL;
}
EvrRtxThreadGetName(thread, thread->name);
return thread->name;
}
/// Return the thread ID of the current running thread.
/// \note API identical to osThreadGetId
static osThreadId_t svcRtxThreadGetId (void) {
os_thread_t *thread;
thread = osRtxThreadGetRunning();
EvrRtxThreadGetId(thread);
return thread;
}
/// Get current thread state of a thread.
/// \note API identical to osThreadGetState
static osThreadState_t svcRtxThreadGetState (osThreadId_t thread_id) {
os_thread_t *thread = osRtxThreadId(thread_id);
osThreadState_t state;
// Check parameters
if ((thread == NULL) || (thread->id != osRtxIdThread)) {
EvrRtxThreadGetState(thread, osThreadError);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return osThreadError;
}
state = osRtxThreadState(thread);
EvrRtxThreadGetState(thread, state);
return state;
}
/// Get stack size of a thread.
/// \note API identical to osThreadGetStackSize
static uint32_t svcRtxThreadGetStackSize (osThreadId_t thread_id) {
os_thread_t *thread = osRtxThreadId(thread_id);
// Check parameters
if ((thread == NULL) || (thread->id != osRtxIdThread)) {
EvrRtxThreadGetStackSize(thread, 0U);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return 0U;
}
EvrRtxThreadGetStackSize(thread, thread->stack_size);
return thread->stack_size;
}
/// Get available stack space of a thread based on stack watermark recording during execution.
/// \note API identical to osThreadGetStackSpace
static uint32_t svcRtxThreadGetStackSpace (osThreadId_t thread_id) {
os_thread_t *thread = osRtxThreadId(thread_id);
const uint32_t *stack;
uint32_t space;
// Check parameters
if ((thread == NULL) || (thread->id != osRtxIdThread)) {
EvrRtxThreadGetStackSpace(thread, 0U);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return 0U;
}
// Check if stack watermark is not enabled
if ((osRtxConfig.flags & osRtxConfigStackWatermark) == 0U) {
EvrRtxThreadGetStackSpace(thread, 0U);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return 0U;
}
//lint -e{9079} "conversion from pointer to void to pointer to other type"
stack = thread->stack_mem;
if (*stack++ == osRtxStackMagicWord) {
for (space = 4U; space < thread->stack_size; space += 4U) {
if (*stack++ != osRtxStackFillPattern) {
break;
}
}
} else {
space = 0U;
}
EvrRtxThreadGetStackSpace(thread, space);
return space;
}
/// Change priority of a thread.
/// \note API identical to osThreadSetPriority
static osStatus_t svcRtxThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
os_thread_t *thread = osRtxThreadId(thread_id);
// Check parameters
if ((thread == NULL) || (thread->id != osRtxIdThread) ||
(priority < osPriorityIdle) || (priority > osPriorityISR)) {
EvrRtxThreadError(thread, (int32_t)osErrorParameter);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return osErrorParameter;
}
// Check object state
if (thread->state == osRtxThreadTerminated) {
EvrRtxThreadError(thread, (int32_t)osErrorResource);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return osErrorResource;
}
if (thread->priority != (int8_t)priority) {
thread->priority = (int8_t)priority;
thread->priority_base = (int8_t)priority;
EvrRtxThreadPriorityUpdated(thread, priority);
osRtxThreadListSort(thread);
osRtxThreadDispatch(NULL);
}
return osOK;
}
/// Get current priority of a thread.
/// \note API identical to osThreadGetPriority
static osPriority_t svcRtxThreadGetPriority (osThreadId_t thread_id) {
os_thread_t *thread = osRtxThreadId(thread_id);
osPriority_t priority;
// Check parameters
if ((thread == NULL) || (thread->id != osRtxIdThread)) {
EvrRtxThreadGetPriority(thread, osPriorityError);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return osPriorityError;
}
// Check object state
if (thread->state == osRtxThreadTerminated) {
EvrRtxThreadGetPriority(thread, osPriorityError);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return osPriorityError;
}
priority = osRtxThreadPriority(thread);
EvrRtxThreadGetPriority(thread, priority);
return priority;
}
/// Pass control to next thread that is in state READY.
/// \note API identical to osThreadYield
static osStatus_t svcRtxThreadYield (void) {
os_thread_t *thread_running;
os_thread_t *thread_ready;
if (osRtxKernelGetState() == osRtxKernelRunning) {
thread_running = osRtxThreadGetRunning();
thread_ready = osRtxInfo.thread.ready.thread_list;
if ((thread_ready != NULL) &&
(thread_ready->priority == thread_running->priority)) {
osRtxThreadListRemove(thread_ready);
osRtxThreadReadyPut(thread_running);
EvrRtxThreadPreempted(thread_running);
osRtxThreadSwitch(thread_ready);
}
}
return osOK;
}
/// Suspend execution of a thread.
/// \note API identical to osThreadSuspend
static osStatus_t svcRtxThreadSuspend (osThreadId_t thread_id) {
os_thread_t *thread = osRtxThreadId(thread_id);
osStatus_t status;