@@ -31,6 +31,7 @@ static kcb_t kernel_state = {
3131 .ticks = 0 ,
3232 .preemptive = true, /* Default to preemptive mode */
3333 .last_ready_hint = NULL ,
34+ .kcb_lock = SPINLOCK_INITIALIZER ,
3435};
3536kcb_t * kcb = & kernel_state ;
3637
@@ -48,7 +49,6 @@ static struct {
4849} task_cache [TASK_CACHE_SIZE ];
4950static uint8_t cache_index = 0 ;
5051
51- static spinlock_t task_lock = SPINLOCK_INITIALIZER ;
5252static uint32_t task_flags = 0 ;
5353
5454static 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)
387387 }
388388
389389 /* Minimize critical section duration */
390- spin_lock_irqsave (& task_lock , & task_flags );
390+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
391391
392392 if (!kcb -> tasks ) {
393393 kcb -> tasks = list_create ();
394394 if (!kcb -> tasks ) {
395- spin_unlock_irqrestore (& task_lock , task_flags );
395+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
396396 free (tcb -> stack );
397397 free (tcb );
398398 panic (ERR_KCB_ALLOC );
@@ -401,7 +401,7 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
401401
402402 list_node_t * node = list_pushback (kcb -> tasks , tcb );
403403 if (!node ) {
404- spin_unlock_irqrestore (& task_lock , task_flags );
404+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
405405 free (tcb -> stack );
406406 free (tcb );
407407 panic (ERR_TCB_ALLOC );
@@ -414,7 +414,7 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
414414 if (!kcb -> task_current )
415415 kcb -> task_current = node ;
416416
417- spin_unlock_irqrestore (& task_lock , task_flags );
417+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
418418
419419 /* Initialize execution context outside critical section */
420420 hal_context_init (& tcb -> context , (size_t ) tcb -> stack , new_stack_size ,
@@ -434,16 +434,16 @@ int32_t mo_task_cancel(uint16_t id)
434434 if (id == 0 || id == mo_task_id ())
435435 return ERR_TASK_CANT_REMOVE ;
436436
437- spin_lock_irqsave (& task_lock , & task_flags );
437+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
438438 list_node_t * node = find_task_node_by_id (id );
439439 if (!node ) {
440- spin_unlock_irqrestore (& task_lock , task_flags );
440+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
441441 return ERR_TASK_NOT_FOUND ;
442442 }
443443
444444 tcb_t * tcb = node -> data ;
445445 if (!tcb || tcb -> state == TASK_RUNNING ) {
446- spin_unlock_irqrestore (& task_lock , task_flags );
446+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
447447 return ERR_TASK_CANT_REMOVE ;
448448 }
449449
@@ -463,7 +463,7 @@ int32_t mo_task_cancel(uint16_t id)
463463 if (kcb -> last_ready_hint == node )
464464 kcb -> last_ready_hint = NULL ;
465465
466- spin_unlock_irqrestore (& task_lock , task_flags );
466+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
467467
468468 /* Free memory outside critical section */
469469 free (tcb -> stack );
@@ -482,16 +482,16 @@ void mo_task_delay(uint16_t ticks)
482482 if (!ticks )
483483 return ;
484484
485- spin_lock_irqsave (& task_lock , & task_flags );
485+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
486486 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 );
488488 return ;
489489 }
490490
491491 tcb_t * self = kcb -> task_current -> data ;
492492 self -> delay = ticks ;
493493 self -> state = TASK_BLOCKED ;
494- spin_unlock_irqrestore (& task_lock , task_flags );
494+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
495495
496496 mo_task_yield ();
497497}
@@ -501,17 +501,17 @@ int32_t mo_task_suspend(uint16_t id)
501501 if (id == 0 )
502502 return ERR_TASK_NOT_FOUND ;
503503
504- spin_lock_irqsave (& task_lock , & task_flags );
504+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
505505 list_node_t * node = find_task_node_by_id (id );
506506 if (!node ) {
507- spin_unlock_irqrestore (& task_lock , task_flags );
507+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
508508 return ERR_TASK_NOT_FOUND ;
509509 }
510510
511511 tcb_t * task = node -> data ;
512512 if (!task || (task -> state != TASK_READY && task -> state != TASK_RUNNING &&
513513 task -> state != TASK_BLOCKED )) {
514- spin_unlock_irqrestore (& task_lock , task_flags );
514+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
515515 return ERR_TASK_CANT_SUSPEND ;
516516 }
517517
@@ -522,7 +522,7 @@ int32_t mo_task_suspend(uint16_t id)
522522 if (kcb -> last_ready_hint == node )
523523 kcb -> last_ready_hint = NULL ;
524524
525- spin_unlock_irqrestore (& task_lock , task_flags );
525+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
526526
527527 if (is_current )
528528 mo_task_yield ();
@@ -535,21 +535,21 @@ int32_t mo_task_resume(uint16_t id)
535535 if (id == 0 )
536536 return ERR_TASK_NOT_FOUND ;
537537
538- spin_lock_irqsave (& task_lock , & task_flags );
538+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
539539 list_node_t * node = find_task_node_by_id (id );
540540 if (!node ) {
541- spin_unlock_irqrestore (& task_lock , task_flags );
541+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
542542 return ERR_TASK_NOT_FOUND ;
543543 }
544544
545545 tcb_t * task = node -> data ;
546546 if (!task || task -> state != TASK_SUSPENDED ) {
547- spin_unlock_irqrestore (& task_lock , task_flags );
547+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
548548 return ERR_TASK_CANT_RESUME ;
549549 }
550550
551551 task -> state = TASK_READY ;
552- spin_unlock_irqrestore (& task_lock , task_flags );
552+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
553553 return ERR_OK ;
554554}
555555
@@ -558,22 +558,22 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
558558 if (id == 0 || !is_valid_priority (priority ))
559559 return ERR_TASK_INVALID_PRIO ;
560560
561- spin_lock_irqsave (& task_lock , & task_flags );
561+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
562562 list_node_t * node = find_task_node_by_id (id );
563563 if (!node ) {
564- spin_unlock_irqrestore (& task_lock , task_flags );
564+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
565565 return ERR_TASK_NOT_FOUND ;
566566 }
567567
568568 tcb_t * task = node -> data ;
569569 if (!task ) {
570- spin_unlock_irqrestore (& task_lock , task_flags );
570+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
571571 return ERR_TASK_NOT_FOUND ;
572572 }
573573
574574 uint8_t base = (uint8_t ) (priority >> 8 );
575575 task -> prio = ((uint16_t ) base << 8 ) | base ;
576- spin_unlock_irqrestore (& task_lock , task_flags );
576+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
577577
578578 return ERR_OK ;
579579}
@@ -583,21 +583,21 @@ int32_t mo_task_rt_priority(uint16_t id, void *priority)
583583 if (id == 0 )
584584 return ERR_TASK_NOT_FOUND ;
585585
586- spin_lock_irqsave (& task_lock , & task_flags );
586+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
587587 list_node_t * node = find_task_node_by_id (id );
588588 if (!node ) {
589- spin_unlock_irqrestore (& task_lock , task_flags );
589+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
590590 return ERR_TASK_NOT_FOUND ;
591591 }
592592
593593 tcb_t * task = node -> data ;
594594 if (!task ) {
595- spin_unlock_irqrestore (& task_lock , task_flags );
595+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
596596 return ERR_TASK_NOT_FOUND ;
597597 }
598598
599599 task -> rt_prio = priority ;
600- spin_unlock_irqrestore (& task_lock , task_flags );
600+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
601601 return ERR_OK ;
602602}
603603
@@ -613,9 +613,9 @@ int32_t mo_task_idref(void *task_entry)
613613 if (!task_entry || !kcb -> tasks )
614614 return ERR_TASK_NOT_FOUND ;
615615
616- spin_lock_irqsave (& task_lock , & task_flags );
616+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
617617 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 );
619619
620620 return node ? ((tcb_t * ) node -> data )-> id : ERR_TASK_NOT_FOUND ;
621621}
0 commit comments