@@ -31,6 +31,7 @@ static kcb_t kernel_state = {
31
31
.ticks = 0 ,
32
32
.preemptive = true, /* Default to preemptive mode */
33
33
.last_ready_hint = NULL ,
34
+ .kcb_lock = SPINLOCK_INITIALIZER ,
34
35
};
35
36
kcb_t * kcb = & kernel_state ;
36
37
@@ -48,7 +49,6 @@ static struct {
48
49
} task_cache [TASK_CACHE_SIZE ];
49
50
static uint8_t cache_index = 0 ;
50
51
51
- static spinlock_t task_lock = SPINLOCK_INITIALIZER ;
52
52
static uint32_t task_flags = 0 ;
53
53
54
54
static inline bool is_valid_task (tcb_t * task )
@@ -387,12 +387,12 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
387
387
}
388
388
389
389
/* Minimize critical section duration */
390
- spin_lock_irqsave (& task_lock , & task_flags );
390
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
391
391
392
392
if (!kcb -> tasks ) {
393
393
kcb -> tasks = list_create ();
394
394
if (!kcb -> tasks ) {
395
- spin_unlock_irqrestore (& task_lock , task_flags );
395
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
396
396
free (tcb -> stack );
397
397
free (tcb );
398
398
panic (ERR_KCB_ALLOC );
@@ -401,7 +401,7 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
401
401
402
402
list_node_t * node = list_pushback (kcb -> tasks , tcb );
403
403
if (!node ) {
404
- spin_unlock_irqrestore (& task_lock , task_flags );
404
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
405
405
free (tcb -> stack );
406
406
free (tcb );
407
407
panic (ERR_TCB_ALLOC );
@@ -414,7 +414,7 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
414
414
if (!kcb -> task_current )
415
415
kcb -> task_current = node ;
416
416
417
- spin_unlock_irqrestore (& task_lock , task_flags );
417
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
418
418
419
419
/* Initialize execution context outside critical section */
420
420
hal_context_init (& tcb -> context , (size_t ) tcb -> stack , new_stack_size ,
@@ -434,16 +434,16 @@ int32_t mo_task_cancel(uint16_t id)
434
434
if (id == 0 || id == mo_task_id ())
435
435
return ERR_TASK_CANT_REMOVE ;
436
436
437
- spin_lock_irqsave (& task_lock , & task_flags );
437
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
438
438
list_node_t * node = find_task_node_by_id (id );
439
439
if (!node ) {
440
- spin_unlock_irqrestore (& task_lock , task_flags );
440
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
441
441
return ERR_TASK_NOT_FOUND ;
442
442
}
443
443
444
444
tcb_t * tcb = node -> data ;
445
445
if (!tcb || tcb -> state == TASK_RUNNING ) {
446
- spin_unlock_irqrestore (& task_lock , task_flags );
446
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
447
447
return ERR_TASK_CANT_REMOVE ;
448
448
}
449
449
@@ -463,7 +463,7 @@ int32_t mo_task_cancel(uint16_t id)
463
463
if (kcb -> last_ready_hint == node )
464
464
kcb -> last_ready_hint = NULL ;
465
465
466
- spin_unlock_irqrestore (& task_lock , task_flags );
466
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
467
467
468
468
/* Free memory outside critical section */
469
469
free (tcb -> stack );
@@ -482,16 +482,16 @@ void mo_task_delay(uint16_t ticks)
482
482
if (!ticks )
483
483
return ;
484
484
485
- spin_lock_irqsave (& task_lock , & task_flags );
485
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
486
486
if (unlikely (!kcb || !kcb -> task_current || !kcb -> task_current -> data )) {
487
- spin_unlock_irqrestore (& task_lock , task_flags );
487
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
488
488
return ;
489
489
}
490
490
491
491
tcb_t * self = kcb -> task_current -> data ;
492
492
self -> delay = ticks ;
493
493
self -> state = TASK_BLOCKED ;
494
- spin_unlock_irqrestore (& task_lock , task_flags );
494
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
495
495
496
496
mo_task_yield ();
497
497
}
@@ -501,17 +501,17 @@ int32_t mo_task_suspend(uint16_t id)
501
501
if (id == 0 )
502
502
return ERR_TASK_NOT_FOUND ;
503
503
504
- spin_lock_irqsave (& task_lock , & task_flags );
504
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
505
505
list_node_t * node = find_task_node_by_id (id );
506
506
if (!node ) {
507
- spin_unlock_irqrestore (& task_lock , task_flags );
507
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
508
508
return ERR_TASK_NOT_FOUND ;
509
509
}
510
510
511
511
tcb_t * task = node -> data ;
512
512
if (!task || (task -> state != TASK_READY && task -> state != TASK_RUNNING &&
513
513
task -> state != TASK_BLOCKED )) {
514
- spin_unlock_irqrestore (& task_lock , task_flags );
514
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
515
515
return ERR_TASK_CANT_SUSPEND ;
516
516
}
517
517
@@ -522,7 +522,7 @@ int32_t mo_task_suspend(uint16_t id)
522
522
if (kcb -> last_ready_hint == node )
523
523
kcb -> last_ready_hint = NULL ;
524
524
525
- spin_unlock_irqrestore (& task_lock , task_flags );
525
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
526
526
527
527
if (is_current )
528
528
mo_task_yield ();
@@ -535,21 +535,21 @@ int32_t mo_task_resume(uint16_t id)
535
535
if (id == 0 )
536
536
return ERR_TASK_NOT_FOUND ;
537
537
538
- spin_lock_irqsave (& task_lock , & task_flags );
538
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
539
539
list_node_t * node = find_task_node_by_id (id );
540
540
if (!node ) {
541
- spin_unlock_irqrestore (& task_lock , task_flags );
541
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
542
542
return ERR_TASK_NOT_FOUND ;
543
543
}
544
544
545
545
tcb_t * task = node -> data ;
546
546
if (!task || task -> state != TASK_SUSPENDED ) {
547
- spin_unlock_irqrestore (& task_lock , task_flags );
547
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
548
548
return ERR_TASK_CANT_RESUME ;
549
549
}
550
550
551
551
task -> state = TASK_READY ;
552
- spin_unlock_irqrestore (& task_lock , task_flags );
552
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
553
553
return ERR_OK ;
554
554
}
555
555
@@ -558,22 +558,22 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
558
558
if (id == 0 || !is_valid_priority (priority ))
559
559
return ERR_TASK_INVALID_PRIO ;
560
560
561
- spin_lock_irqsave (& task_lock , & task_flags );
561
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
562
562
list_node_t * node = find_task_node_by_id (id );
563
563
if (!node ) {
564
- spin_unlock_irqrestore (& task_lock , task_flags );
564
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
565
565
return ERR_TASK_NOT_FOUND ;
566
566
}
567
567
568
568
tcb_t * task = node -> data ;
569
569
if (!task ) {
570
- spin_unlock_irqrestore (& task_lock , task_flags );
570
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
571
571
return ERR_TASK_NOT_FOUND ;
572
572
}
573
573
574
574
uint8_t base = (uint8_t ) (priority >> 8 );
575
575
task -> prio = ((uint16_t ) base << 8 ) | base ;
576
- spin_unlock_irqrestore (& task_lock , task_flags );
576
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
577
577
578
578
return ERR_OK ;
579
579
}
@@ -583,21 +583,21 @@ int32_t mo_task_rt_priority(uint16_t id, void *priority)
583
583
if (id == 0 )
584
584
return ERR_TASK_NOT_FOUND ;
585
585
586
- spin_lock_irqsave (& task_lock , & task_flags );
586
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
587
587
list_node_t * node = find_task_node_by_id (id );
588
588
if (!node ) {
589
- spin_unlock_irqrestore (& task_lock , task_flags );
589
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
590
590
return ERR_TASK_NOT_FOUND ;
591
591
}
592
592
593
593
tcb_t * task = node -> data ;
594
594
if (!task ) {
595
- spin_unlock_irqrestore (& task_lock , task_flags );
595
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
596
596
return ERR_TASK_NOT_FOUND ;
597
597
}
598
598
599
599
task -> rt_prio = priority ;
600
- spin_unlock_irqrestore (& task_lock , task_flags );
600
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
601
601
return ERR_OK ;
602
602
}
603
603
@@ -613,9 +613,9 @@ int32_t mo_task_idref(void *task_entry)
613
613
if (!task_entry || !kcb -> tasks )
614
614
return ERR_TASK_NOT_FOUND ;
615
615
616
- spin_lock_irqsave (& task_lock , & task_flags );
616
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
617
617
list_node_t * node = list_foreach (kcb -> tasks , refcmp , task_entry );
618
- spin_unlock_irqrestore (& task_lock , task_flags );
618
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
619
619
620
620
return node ? ((tcb_t * ) node -> data )-> id : ERR_TASK_NOT_FOUND ;
621
621
}
0 commit comments