Repository navigation
Expand file tree
/
Copy pathos_task.c
More file actions
2843 lines (2483 loc) · 109 KB
/
Copy pathos_task.c
File metadata and controls
2843 lines (2483 loc) · 109 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
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
/*
*********************************************************************************************************
* uC/OS-III
* The Real-Time Kernel
*
* Copyright 2009-2020 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* TASK MANAGEMENT
*
* File : os_task.c
* Version : V3.08.00
*********************************************************************************************************
*/
#define MICRIUM_SOURCE
#include "os.h"
#ifdef VSC_INCLUDE_SOURCE_FILE_NAMES
const CPU_CHAR *os_task__c = "$Id: $";
#endif
/*
************************************************************************************************************************
* CHANGE PRIORITY OF A TASK
*
* Description: This function allows you to change the priority of a task dynamically. Note that the new
* priority MUST be available.
*
* Arguments : p_tcb is the TCB of the tack to change the priority for
*
* prio_new is the new priority
*
* p_err is a pointer to an error code returned by this function:
*
* OS_ERR_NONE Is the call was successful
* OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
* OS_ERR_PRIO_INVALID If the priority you specify is higher that the maximum allowed
* (i.e. >= (OS_CFG_PRIO_MAX-1)) or already in use by a kernel
* task
* OS_ERR_STATE_INVALID If the task is in an invalid state
* OS_ERR_TASK_CHANGE_PRIO_ISR If you tried to change the task's priority from an ISR
*
* Returns : none
*
* Note(s) : none
************************************************************************************************************************
*/
#if (OS_CFG_TASK_CHANGE_PRIO_EN > 0u)
void OSTaskChangePrio (OS_TCB *p_tcb,
OS_PRIO prio_new,
OS_ERR *p_err)
{
#if (OS_CFG_MUTEX_EN > 0u)
OS_PRIO prio_high;
#endif
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return;
}
#endif
#if (OS_CFG_ARG_CHK_EN > 0u)
if ((p_tcb != (OS_TCB *)0) && (p_tcb->TaskState == OS_TASK_STATE_DEL)) {
*p_err = OS_ERR_STATE_INVALID;
return;
}
#endif
#if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
if (OSIntNestingCtr > 0u) { /* Not allowed to call from an ISR */
*p_err = OS_ERR_TASK_CHANGE_PRIO_ISR;
return;
}
#endif
if (prio_new >= (OS_CFG_PRIO_MAX - 1u)) { /* Cannot set to Idle Task priority */
*p_err = OS_ERR_PRIO_INVALID;
return;
}
CPU_CRITICAL_ENTER();
if (p_tcb == (OS_TCB *)0) { /* Are we changing the priority of 'self'? */
if (OSRunning != OS_STATE_OS_RUNNING) {
CPU_CRITICAL_EXIT();
*p_err = OS_ERR_OS_NOT_RUNNING;
return;
}
p_tcb = OSTCBCurPtr;
}
#if (OS_CFG_MUTEX_EN > 0u)
p_tcb->BasePrio = prio_new; /* Update base priority */
if (p_tcb->MutexGrpHeadPtr != (OS_MUTEX *)0) { /* Owning a mutex? */
if (prio_new > p_tcb->Prio) {
prio_high = OS_MutexGrpPrioFindHighest(p_tcb);
if (prio_new > prio_high) {
prio_new = prio_high;
}
}
}
#endif
OS_TaskChangePrio(p_tcb, prio_new);
OS_TRACE_TASK_PRIO_CHANGE(p_tcb, prio_new);
CPU_CRITICAL_EXIT();
if (OSRunning == OS_STATE_OS_RUNNING) {
OSSched(); /* Run highest priority task ready */
}
*p_err = OS_ERR_NONE;
}
#endif
/*
************************************************************************************************************************
* CREATE A TASK
*
* Description: This function is used to have uC/OS-III manage the execution of a task. Tasks can either be created
* prior to the start of multitasking or by a running task. A task cannot be created by an ISR.
*
* Arguments : p_tcb is a pointer to the task's TCB
*
* p_name is a pointer to an ASCII string to provide a name to the task.
*
* p_task is a pointer to the task's code
*
* p_arg is a pointer to an optional data area which can be used to pass parameters to
* the task when the task first executes. Where the task is concerned it thinks
* it was invoked and passed the argument 'p_arg' as follows:
*
* void Task (void *p_arg)
* {
* for (;;) {
* Task code;
* }
* }
*
* prio is the task's priority. A unique priority MUST be assigned to each task and the
* lower the number, the higher the priority.
*
* p_stk_base is a pointer to the base address of the stack (i.e. low address).
*
* stk_limit is the number of stack elements to set as 'watermark' limit for the stack. This value
* represents the number of CPU_STK entries left before the stack is full. For example,
* specifying 10% of the 'stk_size' value indicates that the stack limit will be reached
* when the stack reaches 90% full.
*
* stk_size is the size of the stack in number of elements. If CPU_STK is set to CPU_INT08U,
* 'stk_size' corresponds to the number of bytes available. If CPU_STK is set to
* CPU_INT16U, 'stk_size' contains the number of 16-bit entries available. Finally, if
* CPU_STK is set to CPU_INT32U, 'stk_size' contains the number of 32-bit entries
* available on the stack.
*
* q_size is the maximum number of messages that can be sent to the task
*
* time_quanta amount of time (in ticks) for time slice when round-robin between tasks. Specify 0 to use
* the default.
*
* p_ext is a pointer to a user supplied memory location which is used as a TCB extension.
* For example, this user memory can hold the contents of floating-point registers
* during a context switch, the time each task takes to execute, the number of times
* the task has been switched-in, etc.
*
* opt contains additional information (or options) about the behavior of the task.
* See OS_OPT_TASK_xxx in OS.H. Current choices are:
*
* OS_OPT_TASK_NONE No option selected
* OS_OPT_TASK_STK_CHK Stack checking to be allowed for the task
* OS_OPT_TASK_STK_CLR Clear the stack when the task is created
* OS_OPT_TASK_SAVE_FP If the CPU has floating-point registers, save them
* during a context switch.
* OS_OPT_TASK_NO_TLS If the caller doesn't want or need TLS (Thread Local
* Storage) support for the task. If you do not include this
* option, TLS will be supported by default.
*
* p_err is a pointer to an error code that will be set during this call. The value pointer
* to by 'p_err' can be:
*
* OS_ERR_NONE If the function was successful
* OS_ERR_ILLEGAL_CREATE_RUN_TIME If you are trying to create the task after you called
* OSSafetyCriticalStart()
* OS_ERR_PRIO_INVALID If the priority you specify is higher that the maximum
* allowed (i.e. >= OS_CFG_PRIO_MAX-1) or,
* OS_ERR_STK_OVF If the stack was overflowed during stack init
* OS_ERR_STK_INVALID If you specified a NULL pointer for 'p_stk_base'
* OS_ERR_STK_SIZE_INVALID If you specified zero for the 'stk_size'
* OS_ERR_STK_LIMIT_INVALID If you specified a 'stk_limit' greater than or equal
* to 'stk_size'
* OS_ERR_TASK_CREATE_ISR If you tried to create a task from an ISR
* OS_ERR_TASK_INVALID If you specified a NULL pointer for 'p_task'
* OS_ERR_TCB_INVALID If you specified a NULL pointer for 'p_tcb'
*
* Returns : none
*
* Note(s) : 1) OSTaskCreate() will return with the error OS_ERR_STK_OVF when a stack overflow is detected
* during stack initialization. In that specific case some memory may have been corrupted. It is
* therefore recommended to treat OS_ERR_STK_OVF as a fatal error.
************************************************************************************************************************
*/
void OSTaskCreate (OS_TCB *p_tcb,
CPU_CHAR *p_name,
OS_TASK_PTR p_task,
void *p_arg,
OS_PRIO prio,
CPU_STK *p_stk_base,
CPU_STK_SIZE stk_limit,
CPU_STK_SIZE stk_size,
OS_MSG_QTY q_size,
OS_TICK time_quanta,
void *p_ext,
OS_OPT opt,
OS_ERR *p_err)
{
CPU_STK_SIZE i;
#if (OS_CFG_TASK_REG_TBL_SIZE > 0u)
OS_REG_ID reg_nbr;
#endif
#if defined(OS_CFG_TLS_TBL_SIZE) && (OS_CFG_TLS_TBL_SIZE > 0u)
OS_TLS_ID id;
#endif
CPU_STK *p_sp;
CPU_STK *p_stk_limit;
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return;
}
#endif
#ifdef OS_SAFETY_CRITICAL_IEC61508
if (OSSafetyCriticalStartFlag == OS_TRUE) {
*p_err = OS_ERR_ILLEGAL_CREATE_RUN_TIME;
return;
}
#endif
#if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
if (OSIntNestingCtr > 0u) { /* --------- CANNOT CREATE A TASK FROM AN ISR --------- */
OS_TRACE_TASK_CREATE_FAILED(p_tcb);
*p_err = OS_ERR_TASK_CREATE_ISR;
return;
}
#endif
#if (OS_CFG_ARG_CHK_EN > 0u) /* ---------------- VALIDATE ARGUMENTS ---------------- */
if (p_tcb == (OS_TCB *)0) { /* User must supply a valid OS_TCB */
OS_TRACE_TASK_CREATE_FAILED(p_tcb);
*p_err = OS_ERR_TCB_INVALID;
return;
}
if (p_task == (OS_TASK_PTR)0u) { /* User must supply a valid task */
OS_TRACE_TASK_CREATE_FAILED(p_tcb);
*p_err = OS_ERR_TASK_INVALID;
return;
}
if (p_stk_base == (CPU_STK *)0) { /* User must supply a valid stack base address */
OS_TRACE_TASK_CREATE_FAILED(p_tcb);
*p_err = OS_ERR_STK_INVALID;
return;
}
if (stk_size < OSCfg_StkSizeMin) { /* User must supply a valid minimum stack size */
OS_TRACE_TASK_CREATE_FAILED(p_tcb);
*p_err = OS_ERR_STK_SIZE_INVALID;
return;
}
if (stk_limit >= stk_size) { /* User must supply a valid stack limit */
OS_TRACE_TASK_CREATE_FAILED(p_tcb);
*p_err = OS_ERR_STK_LIMIT_INVALID;
return;
}
if ((prio > (OS_CFG_PRIO_MAX - 2u)) && /* Priority must be within 0 and OS_CFG_PRIO_MAX-1 */
(prio != (OS_CFG_PRIO_MAX - 1u))) {
OS_TRACE_TASK_CREATE_FAILED(p_tcb);
*p_err = OS_ERR_PRIO_INVALID;
return;
}
#endif
if (prio == (OS_CFG_PRIO_MAX - 1u)) {
#if (OS_CFG_TASK_IDLE_EN > 0u)
if (p_tcb != &OSIdleTaskTCB) {
OS_TRACE_TASK_CREATE_FAILED(p_tcb);
*p_err = OS_ERR_PRIO_INVALID; /* Not allowed to use same priority as idle task */
return;
}
#else
OS_TRACE_TASK_CREATE_FAILED(p_tcb);
*p_err = OS_ERR_PRIO_INVALID; /* Not allowed to use same priority as idle task */
return;
#endif
}
OS_TaskInitTCB(p_tcb); /* Initialize the TCB to default values */
*p_err = OS_ERR_NONE;
/* -------------- CLEAR THE TASK'S STACK -------------- */
if (((opt & OS_OPT_TASK_STK_CHK) != 0u) || /* See if stack checking has been enabled */
((opt & OS_OPT_TASK_STK_CLR) != 0u)) { /* See if stack needs to be cleared */
if ((opt & OS_OPT_TASK_STK_CLR) != 0u) {
p_sp = p_stk_base;
for (i = 0u; i < stk_size; i++) { /* Stack grows from HIGH to LOW memory */
*p_sp = 0u; /* Clear from bottom of stack and up! */
p_sp++;
}
}
}
/* ------ INITIALIZE THE STACK FRAME OF THE TASK ------ */
#if (CPU_CFG_STK_GROWTH == CPU_STK_GROWTH_HI_TO_LO)
p_stk_limit = p_stk_base + stk_limit;
#else
p_stk_limit = p_stk_base + (stk_size - 1u) - stk_limit;
#endif
p_sp = OSTaskStkInit(p_task,
p_arg,
p_stk_base,
p_stk_limit,
stk_size,
opt);
#if (CPU_CFG_STK_GROWTH == CPU_STK_GROWTH_HI_TO_LO) /* Check if we overflown the stack during init */
if (p_sp < p_stk_base) {
*p_err = OS_ERR_STK_OVF;
return;
}
#else
if (p_sp > (p_stk_base + stk_size)) {
*p_err = OS_ERR_STK_OVF;
return;
}
#endif
#if (OS_CFG_TASK_STK_REDZONE_EN > 0u) /* Initialize Redzoned stack */
OS_TaskStkRedzoneInit(p_stk_base, stk_size);
#endif
/* ------------ INITIALIZE THE TCB FIELDS ------------- */
#if (OS_CFG_DBG_EN > 0u)
p_tcb->TaskEntryAddr = p_task; /* Save task entry point address */
p_tcb->TaskEntryArg = p_arg; /* Save task entry argument */
#endif
#if (OS_CFG_DBG_EN > 0u)
p_tcb->NamePtr = p_name; /* Save task name */
#else
(void)p_name;
#endif
p_tcb->Prio = prio; /* Save the task's priority */
#if (OS_CFG_MUTEX_EN > 0u)
p_tcb->BasePrio = prio; /* Set the base priority */
#endif
p_tcb->StkPtr = p_sp; /* Save the new top-of-stack pointer */
p_tcb->StkLimitPtr = p_stk_limit; /* Save the stack limit pointer */
#if (OS_CFG_SCHED_ROUND_ROBIN_EN > 0u)
p_tcb->TimeQuanta = time_quanta; /* Save the #ticks for time slice (0 means not sliced) */
if (time_quanta == 0u) {
p_tcb->TimeQuantaCtr = OSSchedRoundRobinDfltTimeQuanta;
} else {
p_tcb->TimeQuantaCtr = time_quanta;
}
#else
(void)time_quanta;
#endif
p_tcb->ExtPtr = p_ext; /* Save pointer to TCB extension */
#if ((OS_CFG_DBG_EN > 0u) || (OS_CFG_STAT_TASK_STK_CHK_EN > 0u) || (OS_CFG_TASK_STK_REDZONE_EN > 0u))
p_tcb->StkBasePtr = p_stk_base; /* Save pointer to the base address of the stack */
p_tcb->StkSize = stk_size; /* Save the stack size (in number of CPU_STK elements) */
#endif
p_tcb->Opt = opt; /* Save task options */
#if (OS_CFG_TASK_REG_TBL_SIZE > 0u)
for (reg_nbr = 0u; reg_nbr < OS_CFG_TASK_REG_TBL_SIZE; reg_nbr++) {
p_tcb->RegTbl[reg_nbr] = 0u;
}
#endif
#if (OS_CFG_TASK_Q_EN > 0u)
OS_MsgQInit(&p_tcb->MsgQ, /* Initialize the task's message queue */
q_size);
#else
(void)q_size;
#endif
OSTaskCreateHook(p_tcb); /* Call user defined hook */
OS_TRACE_TASK_CREATE(p_tcb);
OS_TRACE_TASK_SEM_CREATE(p_tcb, p_name);
#if (OS_CFG_TASK_Q_EN > 0u)
OS_TRACE_TASK_MSG_Q_CREATE(&p_tcb->MsgQ, p_name);
#endif
#if defined(OS_CFG_TLS_TBL_SIZE) && (OS_CFG_TLS_TBL_SIZE > 0u)
for (id = 0u; id < OS_CFG_TLS_TBL_SIZE; id++) {
p_tcb->TLS_Tbl[id] = 0u;
}
OS_TLS_TaskCreate(p_tcb); /* Call TLS hook */
#endif
/* -------------- ADD TASK TO READY LIST -------------- */
CPU_CRITICAL_ENTER();
OS_PrioInsert(p_tcb->Prio);
OS_RdyListInsertTail(p_tcb);
#if (OS_CFG_DBG_EN > 0u)
OS_TaskDbgListAdd(p_tcb);
#endif
OSTaskQty++; /* Increment the #tasks counter */
if (OSRunning != OS_STATE_OS_RUNNING) { /* Return if multitasking has not started */
CPU_CRITICAL_EXIT();
return;
}
CPU_CRITICAL_EXIT();
OSSched();
}
/*
************************************************************************************************************************
* DELETE A TASK
*
* Description: This function allows you to delete a task. The calling task can delete itself by specifying a NULL
* pointer for 'p_tcb'. The deleted task is returned to the dormant state and can be re-activated by
* creating the deleted task again.
*
* Arguments : p_tcb is the TCB of the tack to delete
*
* p_err is a pointer to an error code returned by this function:
*
* OS_ERR_NONE If the call is successful
* OS_ERR_ILLEGAL_DEL_RUN_TIME If you are trying to delete the task after you called
* OSStart()
* OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
* OS_ERR_STATE_INVALID If the state of the task is invalid
* OS_ERR_TASK_DEL_IDLE If you attempted to delete uC/OS-III's idle task
* OS_ERR_TASK_DEL_INVALID If you attempted to delete uC/OS-III's ISR handler task
* OS_ERR_TASK_DEL_ISR If you tried to delete a task from an ISR
*
* Returns : none
*
* Note(s) : 1) 'p_err' gets set to OS_ERR_NONE before OSSched() to allow the returned err or code to be monitored even
* for a task that is deleting itself. In this case, 'p_err' MUST point to a global variable that can be
* accessed by another task.
************************************************************************************************************************
*/
#if (OS_CFG_TASK_DEL_EN > 0u)
void OSTaskDel (OS_TCB *p_tcb,
OS_ERR *p_err)
{
#if (OS_CFG_MUTEX_EN > 0u)
OS_TCB *p_tcb_owner;
OS_PRIO prio_new;
#endif
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return;
}
#endif
#ifdef OS_SAFETY_CRITICAL_IEC61508
if (OSSafetyCriticalStartFlag == OS_TRUE) {
*p_err = OS_ERR_ILLEGAL_DEL_RUN_TIME;
return;
}
#endif
#if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
if (OSIntNestingCtr > 0u) { /* See if trying to delete from ISR */
*p_err = OS_ERR_TASK_DEL_ISR;
return;
}
#endif
#if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
*p_err = OS_ERR_OS_NOT_RUNNING;
return;
}
#endif
#if (OS_CFG_TASK_IDLE_EN > 0u)
if (p_tcb == &OSIdleTaskTCB) { /* Not allowed to delete the idle task */
*p_err = OS_ERR_TASK_DEL_IDLE;
return;
}
#endif
if (p_tcb == (OS_TCB *)0) { /* Delete 'Self'? */
CPU_CRITICAL_ENTER();
p_tcb = OSTCBCurPtr; /* Yes. */
CPU_CRITICAL_EXIT();
}
CPU_CRITICAL_ENTER();
switch (p_tcb->TaskState) {
case OS_TASK_STATE_RDY:
OS_RdyListRemove(p_tcb);
break;
case OS_TASK_STATE_SUSPENDED:
break;
case OS_TASK_STATE_DLY: /* Task is only delayed, not on any wait list */
case OS_TASK_STATE_DLY_SUSPENDED:
#if (OS_CFG_TICK_EN > 0u)
OS_TickListRemove(p_tcb);
#endif
break;
case OS_TASK_STATE_PEND:
case OS_TASK_STATE_PEND_SUSPENDED:
case OS_TASK_STATE_PEND_TIMEOUT:
case OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED:
switch (p_tcb->PendOn) { /* See what we are pending on */
case OS_TASK_PEND_ON_NOTHING:
case OS_TASK_PEND_ON_TASK_Q: /* There is no wait list for these two */
case OS_TASK_PEND_ON_TASK_SEM:
break;
case OS_TASK_PEND_ON_FLAG: /* Remove from pend list */
case OS_TASK_PEND_ON_Q:
case OS_TASK_PEND_ON_SEM:
OS_PendListRemove(p_tcb);
break;
#if (OS_CFG_MUTEX_EN > 0u)
case OS_TASK_PEND_ON_MUTEX:
p_tcb_owner = ((OS_MUTEX *)((void *)p_tcb->PendObjPtr))->OwnerTCBPtr;
prio_new = p_tcb_owner->Prio;
OS_PendListRemove(p_tcb);
if ((p_tcb_owner->Prio != p_tcb_owner->BasePrio) &&
(p_tcb_owner->Prio == p_tcb->Prio)) { /* Has the owner inherited a priority? */
prio_new = OS_MutexGrpPrioFindHighest(p_tcb_owner);
prio_new = (prio_new > p_tcb_owner->BasePrio) ? p_tcb_owner->BasePrio : prio_new;
}
p_tcb->PendOn = OS_TASK_PEND_ON_NOTHING;
if (prio_new != p_tcb_owner->Prio) {
OS_TaskChangePrio(p_tcb_owner, prio_new);
OS_TRACE_MUTEX_TASK_PRIO_DISINHERIT(p_tcb_owner, p_tcb_owner->Prio);
}
break;
#endif
default:
/* Default case. */
break;
}
#if (OS_CFG_TICK_EN > 0u)
if ((p_tcb->TaskState == OS_TASK_STATE_PEND_TIMEOUT) ||
(p_tcb->TaskState == OS_TASK_STATE_PEND_TIMEOUT_SUSPENDED)) {
OS_TickListRemove(p_tcb);
}
#endif
break;
default:
CPU_CRITICAL_EXIT();
*p_err = OS_ERR_STATE_INVALID;
return;
}
#if (OS_CFG_MUTEX_EN > 0u)
if(p_tcb->MutexGrpHeadPtr != (OS_MUTEX *)0) {
OS_MutexGrpPostAll(p_tcb);
}
#endif
#if (OS_CFG_TASK_Q_EN > 0u)
(void)OS_MsgQFreeAll(&p_tcb->MsgQ); /* Free task's message queue messages */
#endif
OSTaskDelHook(p_tcb); /* Call user defined hook */
#if defined(OS_CFG_TLS_TBL_SIZE) && (OS_CFG_TLS_TBL_SIZE > 0u)
OS_TLS_TaskDel(p_tcb); /* Call TLS hook */
#endif
#if (OS_CFG_DBG_EN > 0u)
OS_TaskDbgListRemove(p_tcb);
#endif
OSTaskQty--; /* One less task being managed */
OS_TRACE_TASK_DEL(p_tcb);
#if (OS_CFG_TASK_STK_REDZONE_EN == 0u) /* Don't clear the TCB before checking the red-zone */
OS_TaskInitTCB(p_tcb); /* Initialize the TCB to default values */
#endif
p_tcb->TaskState = (OS_STATE)OS_TASK_STATE_DEL; /* Indicate that the task was deleted */
*p_err = OS_ERR_NONE; /* See Note #1. */
CPU_CRITICAL_EXIT();
OSSched(); /* Find new highest priority task */
}
#endif
/*
************************************************************************************************************************
* FLUSH TASK's QUEUE
*
* Description: This function is used to flush the task's internal message queue.
*
* Arguments : p_tcb is a pointer to the task's OS_TCB. Specifying a NULL pointer indicates that you wish to
* flush the message queue of the calling task.
*
* p_err is a pointer to a variable that will contain an error code returned by this function.
*
* OS_ERR_NONE Upon success
* OS_ERR_FLUSH_ISR If you called this function from an ISR
* OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
*
* Returns : The number of entries freed from the queue
*
* Note(s) : 1) You should use this function with great care because, when to flush the queue, you LOOSE the
* references to what the queue entries are pointing to and thus, you could cause 'memory leaks'. In
* other words, the data you are pointing to that's being referenced by the queue entries should, most
* likely, need to be de-allocated (i.e. freed).
************************************************************************************************************************
*/
#if (OS_CFG_TASK_Q_EN > 0u)
OS_MSG_QTY OSTaskQFlush (OS_TCB *p_tcb,
OS_ERR *p_err)
{
OS_MSG_QTY entries;
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return (0u);
}
#endif
#if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
if (OSIntNestingCtr > 0u) { /* Can't flush a message queue from an ISR */
*p_err = OS_ERR_FLUSH_ISR;
return (0u);
}
#endif
#if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
*p_err = OS_ERR_OS_NOT_RUNNING;
return (0u);
}
#endif
if (p_tcb == (OS_TCB *)0) { /* Flush message queue of calling task? */
CPU_CRITICAL_ENTER();
p_tcb = OSTCBCurPtr;
CPU_CRITICAL_EXIT();
}
CPU_CRITICAL_ENTER();
entries = OS_MsgQFreeAll(&p_tcb->MsgQ); /* Return all OS_MSGs to the OS_MSG pool */
CPU_CRITICAL_EXIT();
*p_err = OS_ERR_NONE;
return (entries);
}
#endif
/*
************************************************************************************************************************
* WAIT FOR A MESSAGE
*
* Description: This function causes the current task to wait for a message to be posted to it.
*
* Arguments : timeout is an optional timeout period (in clock ticks). If non-zero, your task will wait for a
* message to arrive up to the amount of time specified by this argument.
* If you specify 0, however, your task will wait forever or, until a message arrives.
*
* opt determines whether the user wants to block if the task's queue is empty or not:
*
* OS_OPT_PEND_BLOCKING
* OS_OPT_PEND_NON_BLOCKING
*
* p_msg_size is a pointer to a variable that will receive the size of the message
*
* p_ts is a pointer to a variable that will receive the timestamp of when the message was
* received. If you pass a NULL pointer (i.e. (CPU_TS *)0) then you will not get the
* timestamp. In other words, passing a NULL pointer is valid and indicates that you don't
* need the timestamp.
*
* p_err is a pointer to where an error message will be deposited. Possible error
* messages are:
*
* OS_ERR_NONE The call was successful and your task received a message.
* OS_ERR_OPT_INVALID If you specified an invalid option
* OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
* OS_ERR_PEND_ABORT If the pend was aborted
* OS_ERR_PEND_ISR If you called this function from an ISR and the result
* OS_ERR_PEND_WOULD_BLOCK If you specified non-blocking but the queue was not empty
* OS_ERR_PTR_INVALID If 'p_msg_size' is NULL
* OS_ERR_SCHED_LOCKED If the scheduler is locked
* OS_ERR_TIMEOUT A message was not received within the specified timeout
* would lead to a suspension
* OS_ERR_TICK_DISABLED If kernel ticks are disabled and a timeout is specified
*
* Returns : A pointer to the message received or a NULL pointer upon error.
*
* Note(s) : 1) It is possible to receive NULL pointers when there are no errors.
*
* : 2) This API 'MUST NOT' be called from a timer callback function.
************************************************************************************************************************
*/
#if (OS_CFG_TASK_Q_EN > 0u)
void *OSTaskQPend (OS_TICK timeout,
OS_OPT opt,
OS_MSG_SIZE *p_msg_size,
CPU_TS *p_ts,
OS_ERR *p_err)
{
OS_MSG_Q *p_msg_q;
void *p_void;
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return ((void *)0);
}
#endif
OS_TRACE_TASK_MSG_Q_PEND_ENTER(&OSTCBCurPtr->MsgQ, timeout, opt, p_msg_size, p_ts);
#if (OS_CFG_TICK_EN == 0u)
if (timeout != 0u) {
*p_err = OS_ERR_TICK_DISABLED;
OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_TICK_DISABLED);
return ((void *)0);
}
#endif
#if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
if (OSIntNestingCtr > 0u) { /* Can't Pend from an ISR */
OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_PEND_ISR);
*p_err = OS_ERR_PEND_ISR;
return ((void *)0);
}
#endif
#if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_OS_NOT_RUNNING);
*p_err = OS_ERR_OS_NOT_RUNNING;
return ((void *)0);
}
#endif
#if (OS_CFG_ARG_CHK_EN > 0u) /* ---------------- VALIDATE ARGUMENTS ---------------- */
if (p_msg_size == (OS_MSG_SIZE *)0) { /* User must supply a valid destination for msg size */
OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_PTR_INVALID);
*p_err = OS_ERR_PTR_INVALID;
return ((void *)0);
}
switch (opt) { /* User must supply a valid option */
case OS_OPT_PEND_BLOCKING:
case OS_OPT_PEND_NON_BLOCKING:
break;
default:
OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_OPT_INVALID);
*p_err = OS_ERR_OPT_INVALID;
return ((void *)0);
}
#endif
if (p_ts != (CPU_TS *)0) {
*p_ts = 0u; /* Initialize the returned timestamp */
}
CPU_CRITICAL_ENTER();
p_msg_q = &OSTCBCurPtr->MsgQ; /* Any message waiting in the message queue? */
p_void = OS_MsgQGet(p_msg_q,
p_msg_size,
p_ts,
p_err);
if (*p_err == OS_ERR_NONE) {
#if (OS_CFG_TASK_PROFILE_EN > 0u)
#if (OS_CFG_TS_EN > 0u)
if (p_ts != (CPU_TS *)0) {
OSTCBCurPtr->MsgQPendTime = OS_TS_GET() - *p_ts;
if (OSTCBCurPtr->MsgQPendTimeMax < OSTCBCurPtr->MsgQPendTime) {
OSTCBCurPtr->MsgQPendTimeMax = OSTCBCurPtr->MsgQPendTime;
}
}
#endif
#endif
OS_TRACE_TASK_MSG_Q_PEND(p_msg_q);
CPU_CRITICAL_EXIT();
OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_NONE);
return (p_void); /* Yes, Return oldest message received */
}
if ((opt & OS_OPT_PEND_NON_BLOCKING) != 0u) { /* Caller wants to block if not available? */
*p_err = OS_ERR_PEND_WOULD_BLOCK; /* No */
CPU_CRITICAL_EXIT();
OS_TRACE_TASK_MSG_Q_PEND_FAILED(p_msg_q);
OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_PEND_WOULD_BLOCK);
return ((void *)0);
} else { /* Yes */
if (OSSchedLockNestingCtr > 0u) { /* Can't block when the scheduler is locked */
CPU_CRITICAL_EXIT();
OS_TRACE_TASK_MSG_Q_PEND_FAILED(p_msg_q);
OS_TRACE_TASK_MSG_Q_PEND_EXIT(OS_ERR_SCHED_LOCKED);
*p_err = OS_ERR_SCHED_LOCKED;
return ((void *)0);
}
}
OS_Pend((OS_PEND_OBJ *)0, /* Block task pending on Message */
OSTCBCurPtr,
OS_TASK_PEND_ON_TASK_Q,
timeout);
CPU_CRITICAL_EXIT();
OS_TRACE_TASK_MSG_Q_PEND_BLOCK(p_msg_q);
OSSched(); /* Find the next highest priority task ready to run */
CPU_CRITICAL_ENTER();
switch (OSTCBCurPtr->PendStatus) {
case OS_STATUS_PEND_OK: /* Extract message from TCB (Put there by Post) */
p_void = OSTCBCurPtr->MsgPtr;
*p_msg_size = OSTCBCurPtr->MsgSize;
#if (OS_CFG_TS_EN > 0u)
if (p_ts != (CPU_TS *)0) {
*p_ts = OSTCBCurPtr->TS;
#if (OS_CFG_TASK_PROFILE_EN > 0u)
OSTCBCurPtr->MsgQPendTime = OS_TS_GET() - OSTCBCurPtr->TS;
if (OSTCBCurPtr->MsgQPendTimeMax < OSTCBCurPtr->MsgQPendTime) {
OSTCBCurPtr->MsgQPendTimeMax = OSTCBCurPtr->MsgQPendTime;
}
#endif
}
#endif
OS_TRACE_TASK_MSG_Q_PEND(p_msg_q);
*p_err = OS_ERR_NONE;
break;
case OS_STATUS_PEND_ABORT: /* Indicate that we aborted */
p_void = (void *)0;
*p_msg_size = 0u;
if (p_ts != (CPU_TS *)0) {
*p_ts = 0u;
}
OS_TRACE_TASK_MSG_Q_PEND_FAILED(p_msg_q);
*p_err = OS_ERR_PEND_ABORT;
break;
case OS_STATUS_PEND_TIMEOUT: /* Indicate that we didn't get event within TO */
default:
p_void = (void *)0;
*p_msg_size = 0u;
#if (OS_CFG_TS_EN > 0u)
if (p_ts != (CPU_TS *)0) {
*p_ts = OSTCBCurPtr->TS;
}
#endif
OS_TRACE_TASK_MSG_Q_PEND_FAILED(p_msg_q);
*p_err = OS_ERR_TIMEOUT;
break;
}
CPU_CRITICAL_EXIT();
OS_TRACE_TASK_MSG_Q_PEND_EXIT(*p_err);
return (p_void); /* Return received message */
}
#endif
/*
************************************************************************************************************************
* ABORT WAITING FOR A MESSAGE
*
* Description: This function aborts & readies the task specified. This function should be used to fault-abort the wait
* for a message, rather than to normally post the message to the task via OSTaskQPost().
*
* Arguments : p_tcb is a pointer to the task to pend abort
*
* opt provides options for this function:
*
* OS_OPT_POST_NONE No option specified
* OS_OPT_POST_NO_SCHED Indicates that the scheduler will not be called.
*
* p_err is a pointer to a variable that will contain an error code returned by this function.
*
* OS_ERR_NONE If the task was readied and informed of the aborted wait
* OS_ERR_OPT_INVALID If you specified an invalid option
* OS_ERR_OS_NOT_RUNNING If uC/OS-III is not running yet
* OS_ERR_PEND_ABORT_ISR If you called this function from an ISR
* OS_ERR_PEND_ABORT_NONE If task was not pending on a message and thus there is nothing to
* abort
* OS_ERR_PEND_ABORT_SELF If you passed a NULL pointer for 'p_tcb'
*
* Returns : == OS_FALSE if task was not waiting for a message, or upon error.
* == OS_TRUE if task was waiting for a message and was readied and informed.
*
* Note(s) : none
************************************************************************************************************************
*/
#if (OS_CFG_TASK_Q_EN > 0u) && (OS_CFG_TASK_Q_PEND_ABORT_EN > 0u)
CPU_BOOLEAN OSTaskQPendAbort (OS_TCB *p_tcb,
OS_OPT opt,
OS_ERR *p_err)
{
CPU_TS ts;
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return (OS_FALSE);
}
#endif
#if (OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u)
if (OSIntNestingCtr > 0u) { /* See if called from ISR ... */
*p_err = OS_ERR_PEND_ABORT_ISR; /* ... can't Pend Abort from an ISR */
return (OS_FALSE);
}
#endif
#if (OS_CFG_INVALID_OS_CALLS_CHK_EN > 0u)
if (OSRunning != OS_STATE_OS_RUNNING) { /* Is the kernel running? */
*p_err = OS_ERR_OS_NOT_RUNNING;
return (OS_FALSE);
}
#endif
#if (OS_CFG_ARG_CHK_EN > 0u) /* ---------------- VALIDATE ARGUMENTS ---------------- */
switch (opt) { /* User must supply a valid option */
case OS_OPT_POST_NONE:
case OS_OPT_POST_NO_SCHED:
break;
default:
*p_err = OS_ERR_OPT_INVALID;
return (OS_FALSE);
}
#endif
CPU_CRITICAL_ENTER();
#if (OS_CFG_ARG_CHK_EN > 0u)
if ((p_tcb == (OS_TCB *)0) || /* Pend abort self? */
(p_tcb == OSTCBCurPtr)) {
CPU_CRITICAL_EXIT();
*p_err = OS_ERR_PEND_ABORT_SELF; /* ... doesn't make sense */
return (OS_FALSE);
}
#endif