File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -278,8 +278,10 @@ int sys_task_spawn(void *task, int stack_size)
278278
279279static int _tcancel (int id )
280280{
281- if (unlikely (id <= 0 ))
281+ if (unlikely (( unsigned int ) id - 1 >= UINT16_MAX ))
282282 return - EINVAL ;
283+ if ((uint16_t ) id != mo_task_id ())
284+ return - EPERM ;
283285
284286 return mo_task_cancel (id );
285287}
@@ -316,8 +318,10 @@ int sys_tdelay(int ticks)
316318
317319static int _tsuspend (int id )
318320{
319- if (unlikely (id <= 0 ))
321+ if (unlikely (( unsigned int ) id - 1 >= UINT16_MAX ))
320322 return - EINVAL ;
323+ if ((uint16_t ) id != mo_task_id ())
324+ return - EPERM ;
321325
322326 return mo_task_suspend (id );
323327}
@@ -329,10 +333,9 @@ int sys_tsuspend(int id)
329333
330334static int _tresume (int id )
331335{
332- if (unlikely (id <= 0 ))
333- return - EINVAL ;
334-
335- return mo_task_resume (id );
336+ (void ) id ;
337+ /* U-mode cannot resume any task; suspended task cannot call syscall */
338+ return - EPERM ;
336339}
337340
338341int sys_tresume (int id )
@@ -342,8 +345,10 @@ int sys_tresume(int id)
342345
343346static int _tpriority (int id , int priority )
344347{
345- if (unlikely (id <= 0 ))
348+ if (unlikely (( unsigned int ) id - 1 >= UINT16_MAX ))
346349 return - EINVAL ;
350+ if ((uint16_t ) id != mo_task_id ())
351+ return - EPERM ;
347352
348353 return mo_task_priority (id , priority );
349354}
Original file line number Diff line number Diff line change @@ -395,7 +395,7 @@ static void task_cleanup_zombies(void)
395395 list_node_t * next = list_next (node );
396396 tcb_t * tcb = node -> data ;
397397
398- if (tcb && tcb -> state == TASK_ZOMBIE ) {
398+ if (tcb && tcb -> state == TASK_ZOMBIE && node != kcb -> task_current ) {
399399 /* Remove from task list */
400400 list_remove (kcb -> tasks , node );
401401 kcb -> task_count -- ;
@@ -992,9 +992,22 @@ int32_t mo_task_spawn_user(void *task_entry, uint16_t stack_size)
992992
993993int32_t mo_task_cancel (uint16_t id )
994994{
995- if (id == 0 || id == mo_task_id () )
995+ if (id == 0 )
996996 return ERR_TASK_CANT_REMOVE ;
997997
998+ /* Self-termination marks the task as zombie and yields to the scheduler.
999+ * The dispatcher will reclaim resources after context switch completes.
1000+ */
1001+ if (id == mo_task_id ()) {
1002+ tcb_t * self = kcb -> task_current -> data ;
1003+ CRITICAL_ENTER ();
1004+ self -> state = TASK_ZOMBIE ;
1005+ CRITICAL_LEAVE ();
1006+ _yield ();
1007+ while (1 )
1008+ ;
1009+ }
1010+
9981011 CRITICAL_ENTER ();
9991012 list_node_t * node = find_task_node_by_id (id );
10001013 if (!node ) {
You can’t perform that action at this time.
0 commit comments