-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtn_tasks.c
1040 lines (808 loc) · 26.7 KB
/
tn_tasks.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
/*
SATNKernel real-time kernel for the Sega Saturn
Based on TNKernel version 2.7
Copyright © 2004, 2013 Yuri Tiomkin
Saturn version modifications copyright © 2013 Anders Montonen
All rights reserved.
Permission to use, copy, modify, and distribute this software in source
and binary forms and its documentation for any purpose and without fee
is hereby granted, provided that the above copyright notice appear
in all copies and that both that copyright notice and this permission
notice appear in supporting documentation.
THIS SOFTWARE IS PROVIDED BY THE YURI TIOMKIN AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL YURI TIOMKIN OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "tn.h"
#include "tn_utils.h"
//-----------------------------------------------------------------------------
int tn_task_create(TN_TCB * task, //-- task TCB
void (*task_func)(void *param), //-- task function
int priority, //-- task priority
unsigned int * task_stack_start, //-- task stack first addr in memory (bottom)
int task_stack_size, //-- task stack size (in sizeof(void*),not bytes)
void * param, //-- task function parameter
int option) //-- Creation option
{
TN_INTSAVE_DATA
int rc;
unsigned int * ptr_stack;
int i;
TN_KERN_CTX * kctx;
//-- Light weight checking of system tasks recreation
if ((priority == 0 && ((option & TN_TASK_TIMER) == 0)) ||
(priority == TN_NUM_PRIORITY-1 && (option & TN_TASK_IDLE) == 0))
return TERR_WRONG_PARAM;
if ((priority < 0 || priority > TN_NUM_PRIORITY-1) ||
task_stack_size < TN_MIN_STACK_SIZE ||
task_func == NULL || task == NULL || task_stack_start == NULL ||
task->id_task != 0) //-- recreation
return TERR_WRONG_PARAM;
rc = TERR_NO_ERR;
TN_CHECK_NON_INT_CONTEXT
kctx = tn_kern_ctx_ptr();
if (kctx->tn_system_state == TN_ST_STATE_RUNNING)
tn_disable_interrupt();
//--- Init task TCB
task->task_func_addr = (void*)task_func;
task->task_func_param = param;
task->stk_start = (unsigned int*)task_stack_start; //-- Base address of task stack space
task->stk_size = task_stack_size; //-- Task stack size (in bytes)
task->base_priority = priority; //-- Task base priority
task->activate_count = 0; //-- Activation request count
task->id_task = TN_ID_TASK;
//-- Fill all task stack space by TN_FILL_STACK_VAL - only inside create_task
for (ptr_stack = task->stk_start, i = 0; i < task->stk_size; i++)
*ptr_stack-- = TN_FILL_STACK_VAL;
task_set_dormant_state(task);
//--- Init task stack
ptr_stack = tn_stack_init(task->task_func_addr,
task->stk_start,
task->task_func_param);
task->task_stk = ptr_stack; //-- Pointer to task top of stack,
//-- when not running
//-- Add task to created task queue
queue_add_tail(&kctx->tn_create_queue, &(task->create_queue));
kctx->tn_created_tasks_qty++;
if((option & TN_TASK_START_ON_CREATION) != 0)
task_to_runnable(task);
if(kctx->tn_system_state == TN_ST_STATE_RUNNING)
tn_enable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
// If the task is runnable, it is moved to the SUSPENDED state. If the task
// is in the WAITING state, it is moved to the WAITINGSUSPENDED state.
//----------------------------------------------------------------------------
int tn_task_suspend(TN_TCB * task)
{
TN_INTSAVE_DATA
int rc;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
#endif
TN_CHECK_NON_INT_CONTEXT
tn_disable_interrupt();
if (task->task_state & TSK_STATE_SUSPEND)
rc = TERR_OVERFLOW;
else
{
if (task->task_state == TSK_STATE_DORMANT)
rc = TERR_WSTATE;
else
{
if (task->task_state == TSK_STATE_RUNNABLE)
{
task->task_state = TSK_STATE_SUSPEND;
task_to_non_runnable(task);
tn_enable_interrupt();
tn_switch_context();
return TERR_NO_ERR;
}
else
{
task->task_state |= TSK_STATE_SUSPEND;
rc = TERR_NO_ERR;
}
}
}
tn_enable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
int tn_task_resume(TN_TCB * task)
{
TN_INTSAVE_DATA
int rc;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
#endif
TN_CHECK_NON_INT_CONTEXT
tn_disable_interrupt();
if (!(task->task_state & TSK_STATE_SUSPEND))
rc = TERR_WSTATE;
else
{
if (!(task->task_state & TSK_STATE_WAIT)) //- The task is not in the WAIT-SUSPEND state
{
task_to_runnable(task);
tn_enable_interrupt();
tn_switch_context();
return TERR_NO_ERR;
}
else //-- Just remove TSK_STATE_SUSPEND from the task state
{
task->task_state &= ~TSK_STATE_SUSPEND;
rc = TERR_NO_ERR;
}
}
tn_enable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
int tn_task_sleep(unsigned long timeout)
{
TN_INTSAVE_DATA
int rc;
TN_TCB * curr_run_task;
if (timeout == 0)
return TERR_WRONG_PARAM;
TN_CHECK_NON_INT_CONTEXT
tn_disable_interrupt();
curr_run_task = tn_kern_ctx_ptr()->tn_curr_run_task;
if (curr_run_task->wakeup_count > 0)
{
curr_run_task->wakeup_count--;
rc = TERR_NO_ERR;
}
else
{
task_curr_to_wait_action(NULL, TSK_WAIT_REASON_SLEEP, timeout);
tn_enable_interrupt();
tn_switch_context();
return TERR_NO_ERR;
}
tn_enable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
int tn_task_wakeup(TN_TCB * task)
{
TN_INTSAVE_DATA
int rc;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
#endif
TN_CHECK_NON_INT_CONTEXT
tn_disable_interrupt();
if (task->task_state == TSK_STATE_DORMANT)
{
rc = TERR_WCONTEXT;
}
else
{
if ((task->task_state & TSK_STATE_WAIT) &&
task->task_wait_reason == TSK_WAIT_REASON_SLEEP)
{
if (task_wait_complete(task))
{
tn_enable_interrupt();
tn_switch_context();
return TERR_NO_ERR;
}
rc = TERR_NO_ERR;
}
else
{ // v.2.7 - Thanks to Eugene Scopal
//-- Check for 0 - case max wakeup_count value is 1
if (task->wakeup_count == 0) //-- if here - the task is
{ //-- not in the SLEEP mode
task->wakeup_count++;
rc = TERR_NO_ERR;
}
else
rc = TERR_OVERFLOW;
}
}
tn_enable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
int tn_task_iwakeup(TN_TCB * task)
{
TN_INTSAVE_DATA_INT
int rc;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
#endif
TN_CHECK_INT_CONTEXT
tn_idisable_interrupt();
if (task->task_state == TSK_STATE_DORMANT)
{
rc = TERR_WCONTEXT;
}
else
{
if ((task->task_state & TSK_STATE_WAIT) &&
task->task_wait_reason == TSK_WAIT_REASON_SLEEP)
{
if (task_wait_complete(task))
{
tn_ienable_interrupt();
return TERR_NO_ERR;
}
rc = TERR_NO_ERR;
}
else
{ // v.2.7 - Thanks to Eugene Scopal
if (task->wakeup_count == 0) //-- if here - the task is
{ //-- not in the SLEEP mode
task->wakeup_count++;
rc = TERR_NO_ERR;
}
else
rc = TERR_OVERFLOW;
}
}
tn_ienable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
int tn_task_activate(TN_TCB * task)
{
TN_INTSAVE_DATA
int rc;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
#endif
TN_CHECK_NON_INT_CONTEXT
tn_disable_interrupt();
if (task->task_state == TSK_STATE_DORMANT)
{
task_to_runnable(task);
tn_enable_interrupt();
tn_switch_context();
return TERR_NO_ERR;
}
else
{
if (task->activate_count == 0)
{
task->activate_count++;
rc = TERR_NO_ERR;
}
else
rc = TERR_OVERFLOW;
}
tn_enable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
int tn_task_iactivate(TN_TCB * task)
{
TN_INTSAVE_DATA_INT
int rc;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
#endif
TN_CHECK_INT_CONTEXT
tn_idisable_interrupt();
if (task->task_state == TSK_STATE_DORMANT)
{
task_to_runnable(task);
tn_ienable_interrupt();
return TERR_NO_ERR;
}
else
{
if (task->activate_count == 0)
{
task->activate_count++;
rc = TERR_NO_ERR;
}
else
rc = TERR_OVERFLOW;
}
tn_ienable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
int tn_task_release_wait(TN_TCB * task)
{
TN_INTSAVE_DATA
int rc;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
#endif
TN_CHECK_NON_INT_CONTEXT
tn_disable_interrupt();
if ((task->task_state & TSK_STATE_WAIT) == 0)
{
rc = TERR_WCONTEXT;
}
else
{
queue_remove_entry(&(task->task_queue));
if (task_wait_complete(task))
{
tn_enable_interrupt();
tn_switch_context();
return TERR_NO_ERR;
}
rc = TERR_NO_ERR;
}
tn_enable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
int tn_task_irelease_wait(TN_TCB * task)
{
TN_INTSAVE_DATA_INT
int rc;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
#endif
TN_CHECK_INT_CONTEXT
tn_idisable_interrupt();
if ((task->task_state & TSK_STATE_WAIT) == 0)
rc = TERR_WCONTEXT;
else
{
queue_remove_entry(&(task->task_queue));
task_wait_complete(task);
rc = TERR_NO_ERR;
}
tn_ienable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void tn_task_exit(int attr)
{
/*
The structure is used to force GCC compiler properly locate and use
'stack_exp' - thanks to Angelo R. Di Filippo
*/
struct // v.2.7
{
#ifdef USE_MUTEXES
CDLL_QUEUE * que;
TN_MUTEX * mutex;
#endif
TN_TCB * task;
TN_KERN_CTX * kctx;
volatile int stack_exp[TN_PORT_STACK_EXPAND_AT_EXIT];
}data;
TN_CHECK_NON_INT_CONTEXT_NORETVAL
data.kctx = tn_kern_ctx_ptr();
tn_cpu_save_sr(); // disable interrupts without saving status
//--------------------------------------------------
//-- Unlock all mutexes, locked by the task
#ifdef USE_MUTEXES
while(!is_queue_empty(&(data.kctx->tn_curr_run_task->mutex_queue)))
{
data.que = queue_remove_head(&(data.kctx->tn_curr_run_task->mutex_queue));
data.mutex = get_mutex_by_mutex_queque(data.que);
do_unlock_mutex(data.mutex);
}
#endif
data.task = data.kctx->tn_curr_run_task;
task_to_non_runnable(data.kctx->tn_curr_run_task);
task_set_dormant_state(data.task);
//-- Pointer to task top of stack,when not running
data.task->task_stk = tn_stack_init(data.task->task_func_addr,
data.task->stk_start,
data.task->task_func_param);
if (data.task->activate_count > 0) //-- Cannot exit
{
data.task->activate_count--;
task_to_runnable(data.task);
}
else // V 2.6 Thanks to Alex Borisov
{
if (attr == TN_EXIT_AND_DELETE_TASK)
{
queue_remove_entry(&(data.task->create_queue));
data.kctx->tn_created_tasks_qty--;
data.task->id_task = 0;
}
}
tn_switch_context_exit(); // interrupts will be enabled inside tn_switch_context_exit()
}
//-----------------------------------------------------------------------------
int tn_task_terminate(TN_TCB * task)
{
TN_INTSAVE_DATA
int rc;
#ifdef USE_MUTEXES
CDLL_QUEUE * que;
TN_MUTEX * mutex;
#endif
TN_KERN_CTX * kctx;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
#endif
TN_CHECK_NON_INT_CONTEXT
kctx = tn_kern_ctx_ptr();
tn_disable_interrupt();
//--------------------------------------------------
rc = TERR_NO_ERR;
if (task->task_state == TSK_STATE_DORMANT || kctx->tn_curr_run_task == task)
rc = TERR_WCONTEXT; //-- Cannot terminate running task
else
{
if (task->task_state == TSK_STATE_RUNNABLE)
task_to_non_runnable(task);
else if (task->task_state & TSK_STATE_WAIT)
{
//-- Free all queues, involved in the 'waiting'
queue_remove_entry(&(task->task_queue));
//-----------------------------------------
if(task->tick_count != TN_WAIT_INFINITE)
queue_remove_entry(&(task->timer_queue));
}
//-- Unlock all mutexes, locked by the task
#ifdef USE_MUTEXES
while (!is_queue_empty(&(task->mutex_queue)))
{
que = queue_remove_head(&(task->mutex_queue));
mutex = get_mutex_by_mutex_queque(que);
do_unlock_mutex(mutex);
}
#endif
task_set_dormant_state(task);
//-- Pointer to task top of the stack when not running
task->task_stk = tn_stack_init(task->task_func_addr,
task->stk_start,
task->task_func_param);
if (task->activate_count > 0) //-- Cannot terminate
{
task->activate_count--;
task_to_runnable(task);
tn_enable_interrupt();
tn_switch_context();
return TERR_NO_ERR;
}
}
tn_enable_interrupt();
return rc;
}
//-----------------------------------------------------------------------------
int tn_task_delete(TN_TCB * task)
{
TN_INTSAVE_DATA
int rc;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
#endif
TN_CHECK_NON_INT_CONTEXT
tn_disable_interrupt();
rc = TERR_NO_ERR;
if (task->task_state != TSK_STATE_DORMANT)
rc = TERR_WCONTEXT; //-- Cannot delete not-terminated task
else
{
queue_remove_entry(&(task->create_queue));
tn_kern_ctx_ptr()->tn_created_tasks_qty--;
task->id_task = 0;
}
tn_enable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
int tn_task_change_priority(TN_TCB * task, int new_priority)
{
TN_INTSAVE_DATA
int rc;
#if TN_CHECK_PARAM
if (task == NULL)
return TERR_WRONG_PARAM;
if (task->id_task != TN_ID_TASK)
return TERR_NOEXS;
if (new_priority < 0 || new_priority > TN_NUM_PRIORITY - 2) //-- try to set pri
return TERR_WRONG_PARAM; // reserved by sys
#endif
TN_CHECK_NON_INT_CONTEXT
tn_disable_interrupt();
if (new_priority == 0)
new_priority = task->base_priority;
rc = TERR_NO_ERR;
if (task->task_state == TSK_STATE_DORMANT)
rc = TERR_WCONTEXT;
else if (task->task_state == TSK_STATE_RUNNABLE)
{
if (change_running_task_priority(task, new_priority))
{
tn_enable_interrupt();
tn_switch_context();
return TERR_NO_ERR;
}
}
else
{
task->priority = new_priority;
}
tn_enable_interrupt();
return rc;
}
//----------------------------------------------------------------------------
// Utilities
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void find_next_task_to_run(void)
{
int tmp;
#ifndef USE_ASM_FFS
int i;
unsigned int mask;
#endif
TN_KERN_CTX * kctx = tn_kern_ctx_ptr();
#ifdef USE_ASM_FFS
tmp = ffs_asm(kctx->tn_ready_to_run_bmp);
tmp--;
#else
mask = 1;
tmp = 0;
for (i = 0; i < TN_BITS_IN_INT; i++) //-- for each bit in bmp
{
if (kctx->tn_ready_to_run_bmp & mask)
{
tmp = i;
break;
}
mask = mask<<1;
}
#endif
kctx->tn_next_task_to_run = get_task_by_tsk_queue(kctx->tn_ready_list[tmp].next);
}
//-----------------------------------------------------------------------------
void task_to_non_runnable(TN_TCB * task)
{
int priority;
CDLL_QUEUE * que;
TN_KERN_CTX * kctx;
kctx = tn_kern_ctx_ptr();
priority = task->priority;
que = &(kctx->tn_ready_list[priority]);
//-- remove the curr task from any queue (now - from ready queue)
queue_remove_entry(&(task->task_queue));
if (is_queue_empty(que)) //-- No ready tasks for the curr priority
{
//-- remove 'ready to run' from the curr priority
kctx->tn_ready_to_run_bmp &= ~(1<<priority);
//-- Find highest priority ready to run -
//-- at least, MSB bit must be set for the idle task
find_next_task_to_run(); //-- v.2.6
}
else //-- There are 'ready to run' task(s) for the curr priority
{
if (kctx->tn_next_task_to_run == task)
kctx->tn_next_task_to_run = get_task_by_tsk_queue(que->next);
}
}
//----------------------------------------------------------------------------
void task_to_runnable(TN_TCB * task)
{
int priority;
TN_KERN_CTX * kctx;
priority = task->priority;
task->task_state = TSK_STATE_RUNNABLE;
task->pwait_queue = NULL;
kctx = tn_kern_ctx_ptr();
//-- Add the task to the end of 'ready queue' for the current priority
queue_add_tail(&(kctx->tn_ready_list[priority]), &(task->task_queue));
kctx->tn_ready_to_run_bmp |= 1 << priority;
//-- less value - greater priority, so '<' operation is used here
if (priority < kctx->tn_next_task_to_run->priority)
kctx->tn_next_task_to_run = task;
}
//----------------------------------------------------------------------------
int task_wait_complete(TN_TCB * task) //-- v. 2.6
{
#ifdef USE_MUTEXES
int fmutex;
int curr_priority;
TN_MUTEX * mutex;
TN_TCB * mt_holder_task;
CDLL_QUEUE * t_que;
#endif
int rc = FALSE;
if (task == NULL)
return 0;
#ifdef USE_MUTEXES
t_que = NULL;
if (task->task_wait_reason == TSK_WAIT_REASON_MUTEX_I ||
task->task_wait_reason == TSK_WAIT_REASON_MUTEX_C)
{
fmutex = TRUE;
t_que = task->pwait_queue;
}
else
fmutex = FALSE;
#endif
task->pwait_queue = NULL;
task->task_wait_rc = TERR_NO_ERR;
if (task->tick_count != TN_WAIT_INFINITE)
queue_remove_entry(&(task->timer_queue));
task->tick_count = TN_WAIT_INFINITE;
if (!(task->task_state & TSK_STATE_SUSPEND))
{
task_to_runnable(task);
rc = TRUE;
}
else //-- remove WAIT state
task->task_state = TSK_STATE_SUSPEND;
#ifdef USE_MUTEXES
if (fmutex)
{
mutex = get_mutex_by_wait_queque(t_que);
mt_holder_task = mutex->holder;
if (mt_holder_task != NULL)
{
//-- if task was blocked by another task and its pri was changed
//-- - recalc current priority
if (mt_holder_task->priority != mt_holder_task->base_priority &&
mt_holder_task->priority == task->priority)
{
curr_priority = find_max_blocked_priority(mutex,
mt_holder_task->base_priority);
set_current_priority(mt_holder_task, curr_priority);
rc = TRUE;
}
}
}
#endif
task->task_wait_reason = 0; //-- Clear wait reason
return rc;
}
//----------------------------------------------------------------------------
void task_curr_to_wait_action(CDLL_QUEUE * wait_que,
int wait_reason,
unsigned long timeout)
{
TN_KERN_CTX * kctx;
kctx = tn_kern_ctx_ptr();
task_to_non_runnable(kctx->tn_curr_run_task);
kctx->tn_curr_run_task->task_state = TSK_STATE_WAIT;
kctx->tn_curr_run_task->task_wait_reason = wait_reason;
kctx->tn_curr_run_task->tick_count = timeout;
//--- Add to the wait queue - FIFO
if (wait_que == NULL) //-- Thanks to Vavilov D.O.
{
queue_reset(&(kctx->tn_curr_run_task->task_queue));
}
else
{
queue_add_tail(wait_que, &(kctx->tn_curr_run_task->task_queue));
kctx->tn_curr_run_task->pwait_queue = wait_que;
}
//--- Add to the timers queue
if (timeout != TN_WAIT_INFINITE)
queue_add_tail(&kctx->tn_wait_timeout_list, &(kctx->tn_curr_run_task->timer_queue));
}
//----------------------------------------------------------------------------
int change_running_task_priority(TN_TCB * task, int new_priority)
{
int old_priority;
TN_KERN_CTX * kctx;
old_priority = task->priority;
//-- remove curr task from any (wait/ready) queue
queue_remove_entry(&(task->task_queue));
//-- If there are no ready tasks for the old priority
//-- clear ready bit for old priority
kctx = tn_kern_ctx_ptr();
if (is_queue_empty(&(kctx->tn_ready_list[old_priority])))
kctx->tn_ready_to_run_bmp &= ~(1<<old_priority);
task->priority = new_priority;
//-- Add task to the end of ready queue for current priority
queue_add_tail(&(kctx->tn_ready_list[new_priority]), &(task->task_queue));
kctx->tn_ready_to_run_bmp |= 1 << new_priority;
find_next_task_to_run();
return TRUE;
}
#ifdef USE_MUTEXES
//----------------------------------------------------------------------------
void set_current_priority(TN_TCB * task, int priority)
{
TN_MUTEX * mutex;
//-- transitive priority changing
// if we have a task A that is blocked by the task B and we changed priority
// of task A, priority of task B also have to be changed. I.e, if we have
// the task A that is waiting for the mutex M1 and we changed priority
// of this task, a task B that holds a mutex M1 now, also needs priority's
// changing. Then, if a task B now is waiting for the mutex M2, the same
// procedure have to be applied to the task C that hold a mutex M2 now
// and so on.
//-- This function in ver 2.6 is more "lightweight".
//-- The code is derived from Vyacheslav Ovsiyenko version
for (;;)
{
if (task->priority <= priority)
return;
if (task->task_state == TSK_STATE_RUNNABLE)
{
change_running_task_priority(task, priority);
return;
}
if (task->task_state & TSK_STATE_WAIT)
{
if (task->task_wait_reason == TSK_WAIT_REASON_MUTEX_I)
{
task->priority = priority;
mutex = get_mutex_by_wait_queque(task->pwait_queue);
task = mutex->holder;
continue;
}
}
task->priority = priority;
return;
}
}
#endif
//----------------------------------------------------------------------------