diff --git a/arch/arm/src/arm_a_r/arm_userfault.c b/arch/arm/src/arm_a_r/arm_userfault.c index 34fe5a1372456..0c3b15974d5f7 100644 --- a/arch/arm/src/arm_a_r/arm_userfault.c +++ b/arch/arm/src/arm_a_r/arm_userfault.c @@ -76,7 +76,7 @@ bool arm_fault_kill(uint32_t *regs) ptcb != NULL ? get_task_name(ptcb) : "", tcb->pid, get_task_name(tcb)); - tcb->flags |= TCB_FLAG_FORCED_CANCEL; + atomic_or(&tcb->shared_flags, TCB_FLAG_FORCED_CANCEL); regs[REG_PC] = (uint32_t)_exit; regs[REG_R0] = SIGSEGV; diff --git a/arch/arm64/src/common/arm64_fatal.c b/arch/arm64/src/common/arm64_fatal.c index aabeafc1fedee..a16fe15100f1d 100644 --- a/arch/arm64/src/common/arm64_fatal.c +++ b/arch/arm64/src/common/arm64_fatal.c @@ -682,7 +682,7 @@ uint64_t *arm64_fatal_handler(uint64_t *regs) tcb->pid, get_task_name(tcb)); _alert("Reason: %s - %s\n", reason, desc); - tcb->flags |= TCB_FLAG_FORCED_CANCEL; + atomic_or(&tcb->shared_flags, TCB_FLAG_FORCED_CANCEL); regs[REG_ELR] = (uint64_t)_exit; regs[REG_X0] = SIGSEGV; diff --git a/arch/risc-v/src/common/riscv_exception.c b/arch/risc-v/src/common/riscv_exception.c index 8316fc580a3d7..1277a7015cce6 100644 --- a/arch/risc-v/src/common/riscv_exception.c +++ b/arch/risc-v/src/common/riscv_exception.c @@ -122,7 +122,7 @@ static void riscv_fault_handler(uintreg_t cause, void *regs) sched_dumpstack(tcb->pid); #endif - tcb->flags |= TCB_FLAG_FORCED_CANCEL; + atomic_or(&tcb->shared_flags, TCB_FLAG_FORCED_CANCEL); /* Return to _exit function in privileged mode with argument SIGSEGV */ diff --git a/arch/x86_64/src/intel64/intel64_irq.c b/arch/x86_64/src/intel64/intel64_irq.c index 52791fce5c69a..f45b7c260b206 100644 --- a/arch/x86_64/src/intel64/intel64_irq.c +++ b/arch/x86_64/src/intel64/intel64_irq.c @@ -578,7 +578,7 @@ static int x86_64_fault_user_isr(int irq, void *c, void *arg) sched_dumpstack(tcb->pid); #endif - tcb->flags |= TCB_FLAG_FORCED_CANCEL; + atomic_or(&tcb->shared_flags, TCB_FLAG_FORCED_CANCEL); /* Return to _exit(SIGSEGV) in ring 0 instead of to the instruction that * faulted, which would only fault again. The frame this rewrites is the diff --git a/fs/procfs/fs_procfsproc.c b/fs/procfs/fs_procfsproc.c index 2887ee72a34f7..1d1378708297e 100644 --- a/fs/procfs/fs_procfsproc.c +++ b/fs/procfs/fs_procfsproc.c @@ -619,7 +619,8 @@ static ssize_t proc_status(FAR struct proc_file_s *procfile, linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%c\n", "Flags:", - tcb->flags & TCB_FLAG_EXIT_PROCESSING ? 'P' : '-'); + atomic_read(&tcb->shared_flags) & + TCB_FLAG_EXIT_PROCESSING ? 'P' : '-'); copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining, &offset); @@ -657,7 +658,8 @@ static ssize_t proc_status(FAR struct proc_file_s *procfile, /* Show the scheduler policy */ - policy = g_policy[(tcb->flags & TCB_FLAG_POLICY_MASK) >> + policy = g_policy[(atomic_read(&tcb->shared_flags) & + TCB_FLAG_POLICY_MASK) >> TCB_FLAG_POLICY_SHIFT]; linesize = procfs_snprintf(procfile->line, STATUS_LINELEN, "%-12s%s\n", "Scheduler:", policy); @@ -1051,7 +1053,7 @@ static ssize_t proc_heapcheck(FAR struct proc_file_s *procfile, size_t totalsize = 0; size_t heapcheck = 0; - if (tcb->flags & TCB_FLAG_HEAP_CHECK) + if (atomic_read(&tcb->shared_flags) & TCB_FLAG_HEAP_CHECK) { heapcheck = 1; } @@ -1073,10 +1075,10 @@ static ssize_t proc_heapcheck_write(FAR struct proc_file_s *procfile, switch (buffer[0]) { case '0': - tcb->flags &= ~TCB_FLAG_HEAP_CHECK; + atomic_and(&tcb->shared_flags, ~TCB_FLAG_HEAP_CHECK); break; case '1': - tcb->flags |= TCB_FLAG_HEAP_CHECK; + atomic_or(&tcb->shared_flags, TCB_FLAG_HEAP_CHECK); break; default: ferr("ERROR: invalid argument\n"); diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index d298cacbd17ba..e55b618cdc1fe 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -88,33 +88,44 @@ #define MAX_LOCK_COUNT 127 -/* Values for the struct tcb_s flags bits */ +/* Values for the struct tcb_s flags bits. The creator of the TCB sets + * the thread type and TCB_FLAG_FREE_TCB before the task runs, + * TCB_FLAG_FREE_STACK and TCB_FLAG_EXIT_COUNTED change only while the task + * does not run or when it exits, and only the task itself changes the other + * bits. So they need no atomic access. + */ #define TCB_FLAG_TTYPE_SHIFT (0) /* Bits 0-1: thread type */ #define TCB_FLAG_TTYPE_MASK (3 << TCB_FLAG_TTYPE_SHIFT) # define TCB_FLAG_TTYPE_TASK (0 << TCB_FLAG_TTYPE_SHIFT) /* Normal user task */ # define TCB_FLAG_TTYPE_PTHREAD (1 << TCB_FLAG_TTYPE_SHIFT) /* User pthread */ # define TCB_FLAG_TTYPE_KERNEL (2 << TCB_FLAG_TTYPE_SHIFT) /* Kernel thread */ -#define TCB_FLAG_POLICY_SHIFT (3) /* Bit 3-4: Scheduling policy */ +#define TCB_FLAG_SIGNAL_ACTION (1 << 2) /* Bit 2: In a signal handler */ +#define TCB_FLAG_SYSCALL (1 << 3) /* Bit 3: In a system call */ +#define TCB_FLAG_FREE_STACK (1 << 4) /* Bit 4: Free stack after exit */ +#define TCB_FLAG_FREE_TCB (1 << 5) /* Bit 5: Free tcb after exit */ +#define TCB_FLAG_PREEMPT_SCHED (1 << 6) /* Bit 6: tcb is PREEMPT_SCHED */ +#define TCB_FLAG_SIGMASK_SAVED (1 << 7) /* Bit 7: sigsavedmask to restore */ +#define TCB_FLAG_UNINTERRUPTIBLE (1 << 8) /* Bit 8: Blocking wait ignores signals */ +#define TCB_FLAG_EXIT_COUNTED (1 << 9) /* Bit 9: Counted as exited in group */ + +/* Values for the struct tcb_s shared_flags bits. Other tasks and CPUs + * change them, so they are accessed with atomic operations. They are + * used only with shared_flags, never with flags. + */ + +#define TCB_FLAG_POLICY_SHIFT (0) /* Bits 0-1: Scheduling policy */ #define TCB_FLAG_POLICY_MASK (3 << TCB_FLAG_POLICY_SHIFT) # define TCB_FLAG_SCHED_FIFO (0 << TCB_FLAG_POLICY_SHIFT) /* FIFO scheding policy */ # define TCB_FLAG_SCHED_RR (1 << TCB_FLAG_POLICY_SHIFT) /* Round robin scheding policy */ # define TCB_FLAG_SCHED_SPORADIC (2 << TCB_FLAG_POLICY_SHIFT) /* Sporadic scheding policy */ # define TCB_FLAG_SCHED_OTHER (3 << TCB_FLAG_POLICY_SHIFT) /* Other scheduling policy */ -#define TCB_FLAG_CPU_LOCKED (1 << 5) /* Bit 5: Locked to this CPU */ -#define TCB_FLAG_SIGNAL_ACTION (1 << 6) /* Bit 6: In a signal handler */ -#define TCB_FLAG_SYSCALL (1 << 7) /* Bit 7: In a system call */ -#define TCB_FLAG_EXIT_PROCESSING (1 << 8) /* Bit 8: Exiting */ -#define TCB_FLAG_FREE_STACK (1 << 9) /* Bit 9: Free stack after exit */ -#define TCB_FLAG_HEAP_CHECK (1 << 10) /* Bit 10: Heap check */ -#define TCB_FLAG_DETACHED (1 << 12) /* Bit 12: Pthread detached */ -#define TCB_FLAG_FORCED_CANCEL (1 << 13) /* Bit 13: Pthread cancel is forced */ -#define TCB_FLAG_JOIN_COMPLETED (1 << 14) /* Bit 14: Pthread join completed */ -#define TCB_FLAG_FREE_TCB (1 << 15) /* Bit 15: Free tcb after exit */ -#define TCB_FLAG_PREEMPT_SCHED (1 << 16) /* Bit 16: tcb is PREEMPT_SCHED */ -#define TCB_FLAG_SIGMASK_SAVED (1 << 17) /* Bit 17: sigsavedmask to restore */ -#define TCB_FLAG_UNINTERRUPTIBLE (1 << 18) /* Bit 18: Blocking wait ignores signals */ -#define TCB_FLAG_EXIT_COUNTED (1 << 19) /* Bit 19: Counted as exited in group */ +#define TCB_FLAG_CPU_LOCKED (1 << 2) /* Bit 2: Locked to this CPU */ +#define TCB_FLAG_EXIT_PROCESSING (1 << 3) /* Bit 3: Exiting */ +#define TCB_FLAG_HEAP_CHECK (1 << 4) /* Bit 4: Heap check */ +#define TCB_FLAG_DETACHED (1 << 5) /* Bit 5: Pthread detached */ +#define TCB_FLAG_FORCED_CANCEL (1 << 6) /* Bit 6: Pthread cancel is forced */ +#define TCB_FLAG_JOIN_COMPLETED (1 << 7) /* Bit 7: Pthread join completed */ /* Values for struct task_group tg_flags */ @@ -671,6 +682,7 @@ struct tcb_s pid_t pid; /* This is the ID of the thread */ uint8_t sched_priority; /* Current priority of the thread */ uint8_t init_priority; /* Initial priority of the thread */ + uint16_t flags; /* Misc. general status flags */ start_t start; /* Thread start function */ entry_t entry; /* Entry Point into the thread */ @@ -687,7 +699,7 @@ struct tcb_s uint8_t cpu; /* CPU index if running/assigned */ cpu_set_t affinity; /* Bit set of permitted CPUs */ #endif - uint32_t flags; /* Misc. general status flags */ + atomic_t shared_flags; /* Flags written by other tasks */ int16_t lockcount; /* 0=preemptible (not-locked) */ #ifdef CONFIG_IRQCOUNT int16_t irqcount; /* 0=Not in critical section */ diff --git a/sched/group/group_killchildren.c b/sched/group/group_killchildren.c index 5a7345fdfeda6..49f2662d2d87f 100644 --- a/sched/group/group_killchildren.c +++ b/sched/group/group_killchildren.c @@ -150,7 +150,7 @@ static int group_cancel_children_handler(pid_t pid, FAR void *arg) { /* Cancel this thread. This is a forced cancellation. */ - rtcb->flags |= TCB_FLAG_FORCED_CANCEL; + atomic_or(&rtcb->shared_flags, TCB_FLAG_FORCED_CANCEL); /* 'pid' could refer to the main task of the thread. That pid * will appear in the group member list as well! @@ -219,7 +219,7 @@ int group_kill_children(FAR struct tcb_s *tcb) #if defined(CONFIG_GROUP_KILL_CHILDREN_TIMEOUT_MS) && \ CONFIG_GROUP_KILL_CHILDREN_TIMEOUT_MS != 0 - if ((tcb->flags & TCB_FLAG_FORCED_CANCEL) == 0) + if ((atomic_read(&tcb->shared_flags) & TCB_FLAG_FORCED_CANCEL) == 0) { /* Send SIGTERM for each first */ diff --git a/sched/group/group_pgrp.c b/sched/group/group_pgrp.c index ab6ab152ddd9e..2b07c5caf7c55 100644 --- a/sched/group/group_pgrp.c +++ b/sched/group/group_pgrp.c @@ -109,7 +109,8 @@ bool group_in_pgrp(FAR struct tcb_s *tcb, pid_t pgid) return pgid > 0 && group != NULL && tcb->pid == group->tg_pid && group->tg_pgid == pgid && - (tcb->flags & TCB_FLAG_EXIT_PROCESSING) == 0 && + (atomic_read(&tcb->shared_flags) & + TCB_FLAG_EXIT_PROCESSING) == 0 && (group->tg_flags & GROUP_FLAG_EXITING) == 0; } diff --git a/sched/init/nx_start.c b/sched/init/nx_start.c index 3abae8bf71157..40cffacbe9058 100644 --- a/sched/init/nx_start.c +++ b/sched/init/nx_start.c @@ -339,7 +339,8 @@ static void idle_task_initialize(void) */ #ifdef CONFIG_SMP - tcb->flags = (TCB_FLAG_TTYPE_KERNEL | TCB_FLAG_CPU_LOCKED); + tcb->flags = TCB_FLAG_TTYPE_KERNEL; + atomic_set(&tcb->shared_flags, TCB_FLAG_CPU_LOCKED); tcb->cpu = i; /* Set the affinity mask to allow the thread to run on all CPUs. No, diff --git a/sched/irq/irq_dispatch.c b/sched/irq/irq_dispatch.c index 713fee3f40da5..5d5acf126cee5 100644 --- a/sched/irq/irq_dispatch.c +++ b/sched/irq/irq_dispatch.c @@ -150,8 +150,8 @@ void irq_dispatch(int irq, FAR void *context) #endif #ifdef CONFIG_DEBUG_MM - if ((rtcb->flags & TCB_FLAG_HEAP_CHECK) || - (this_task()->flags & TCB_FLAG_HEAP_CHECK)) + if ((atomic_read(&rtcb->shared_flags) & TCB_FLAG_HEAP_CHECK) || + (atomic_read(&this_task()->shared_flags) & TCB_FLAG_HEAP_CHECK)) { kmm_checkcorruption(); } diff --git a/sched/misc/assert.c b/sched/misc/assert.c index f81fa47f83483..e26d4a82ec6c8 100644 --- a/sched/misc/assert.c +++ b/sched/misc/assert.c @@ -451,11 +451,12 @@ static void dump_task(FAR struct tcb_s *tcb, FAR void *arg) , tcb->cpu #endif , tcb->sched_priority - , g_policy[(tcb->flags & TCB_FLAG_POLICY_MASK) >> + , g_policy[nxsched_get_policy(tcb) >> TCB_FLAG_POLICY_SHIFT] , g_ttypenames[(tcb->flags & TCB_FLAG_TTYPE_MASK) >> TCB_FLAG_TTYPE_SHIFT] - , tcb->flags & TCB_FLAG_EXIT_PROCESSING ? 'P' : '-' + , atomic_read(&tcb->shared_flags) & + TCB_FLAG_EXIT_PROCESSING ? 'P' : '-' , state #ifndef CONFIG_DISABLE_ALL_SIGNALS , SIGSET_ELEM(&tcb->sigprocmask) diff --git a/sched/pthread/pthread_cancel.c b/sched/pthread/pthread_cancel.c index abe2b81b126a9..c9cfe005a7119 100644 --- a/sched/pthread/pthread_cancel.c +++ b/sched/pthread/pthread_cancel.c @@ -74,7 +74,7 @@ int pthread_cancel(pthread_t thread) /* Return ESRCH when thread was in exit processing */ - if ((tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + if ((atomic_read(&tcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0) { return ESRCH; } diff --git a/sched/pthread/pthread_completejoin.c b/sched/pthread/pthread_completejoin.c index 1a31f8ba5a34b..f73bd3c90f076 100644 --- a/sched/pthread/pthread_completejoin.c +++ b/sched/pthread/pthread_completejoin.c @@ -98,7 +98,7 @@ int pthread_completejoin(pid_t pid, FAR void *exit_value) } } else if (!sq_is_singular(&tcb->group->tg_members) && - (tcb->flags & TCB_FLAG_DETACHED) == 0) + (atomic_read(&tcb->shared_flags) & TCB_FLAG_DETACHED) == 0) { ret = pthread_findjoininfo(tcb->group, pid, &join, true); if (ret == OK) @@ -107,7 +107,7 @@ int pthread_completejoin(pid_t pid, FAR void *exit_value) } } - tcb->flags |= TCB_FLAG_JOIN_COMPLETED; + atomic_or(&tcb->shared_flags, TCB_FLAG_JOIN_COMPLETED); nxrmutex_unlock(&group->tg_mutex); diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c index c06d3c104a23b..d03f1d3b84a92 100644 --- a/sched/pthread/pthread_create.c +++ b/sched/pthread/pthread_create.c @@ -269,7 +269,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread, if (attr->detachstate == PTHREAD_CREATE_DETACHED) { - ptcb->flags |= TCB_FLAG_DETACHED; + atomic_or(&ptcb->shared_flags, TCB_FLAG_DETACHED); } if (attr->stackaddr) @@ -447,25 +447,24 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread, /* Set the appropriate scheduling policy in the TCB */ - ptcb->flags &= ~TCB_FLAG_POLICY_MASK; switch (policy) { default: case SCHED_FIFO: - ptcb->flags |= TCB_FLAG_SCHED_FIFO; + nxsched_set_policy(ptcb, TCB_FLAG_SCHED_FIFO); break; #if CONFIG_RR_INTERVAL > 0 case SCHED_OTHER: case SCHED_RR: - ptcb->flags |= TCB_FLAG_SCHED_RR; + nxsched_set_policy(ptcb, TCB_FLAG_SCHED_RR); ptcb->timeslice = MSEC2TICK(CONFIG_RR_INTERVAL); break; #endif #ifdef CONFIG_SCHED_SPORADIC case SCHED_SPORADIC: - ptcb->flags |= TCB_FLAG_SCHED_SPORADIC; + nxsched_set_policy(ptcb, TCB_FLAG_SCHED_SPORADIC); break; #endif } diff --git a/sched/pthread/pthread_detach.c b/sched/pthread/pthread_detach.c index 4a3d3a7048ebc..200798c216507 100644 --- a/sched/pthread/pthread_detach.c +++ b/sched/pthread/pthread_detach.c @@ -73,7 +73,8 @@ int pthread_detach(pthread_t thread) nxrmutex_lock(&group->tg_mutex); tcb = nxsched_get_tcb((pid_t)thread); - if (tcb == NULL || (tcb->flags & TCB_FLAG_JOIN_COMPLETED) != 0) + if (tcb == NULL || (atomic_read(&tcb->shared_flags) & + TCB_FLAG_JOIN_COMPLETED) != 0) { /* Destroy the join information */ @@ -91,13 +92,13 @@ int pthread_detach(pthread_t thread) } if ((group != tcb->group) || - (tcb->flags & TCB_FLAG_DETACHED) != 0) + (atomic_read(&tcb->shared_flags) & TCB_FLAG_DETACHED) != 0) { ret = EINVAL; } else { - tcb->flags |= TCB_FLAG_DETACHED; + atomic_or(&tcb->shared_flags, TCB_FLAG_DETACHED); ret = OK; } diff --git a/sched/pthread/pthread_exit.c b/sched/pthread/pthread_exit.c index 9a25815997d3d..18fe92b159b75 100644 --- a/sched/pthread/pthread_exit.c +++ b/sched/pthread/pthread_exit.c @@ -74,7 +74,7 @@ void nx_pthread_exit(FAR void *exit_value) DEBUGASSERT(tcb != NULL); - tcb->flags |= TCB_FLAG_EXIT_PROCESSING; + atomic_or(&tcb->shared_flags, TCB_FLAG_EXIT_PROCESSING); /* Block any signal actions that would awaken us while were * are performing the JOIN handshake. diff --git a/sched/pthread/pthread_join.c b/sched/pthread/pthread_join.c index d578bd7d0dd51..d8983d5e7cfc2 100644 --- a/sched/pthread/pthread_join.c +++ b/sched/pthread/pthread_join.c @@ -225,7 +225,8 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) nxrmutex_lock(&group->tg_mutex); tcb = nxsched_get_tcb((pid_t)thread); - if (tcb == NULL || (tcb->flags & TCB_FLAG_JOIN_COMPLETED) != 0) + if (tcb == NULL || (atomic_read(&tcb->shared_flags) & + TCB_FLAG_JOIN_COMPLETED) != 0) { ret = pthread_findjoininfo(group, (pid_t)thread, &join, false); if (ret == OK) @@ -260,7 +261,7 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) /* Task was detached or not a pthread, return EINVAL */ if ((tcb->group != group) || - (tcb->flags & TCB_FLAG_DETACHED) != 0) + (atomic_read(&tcb->shared_flags) & TCB_FLAG_DETACHED) != 0) { ret = EINVAL; goto errout; diff --git a/sched/sched/sched.h b/sched/sched/sched.h index 8a2e38ca9cb58..92865f671f263 100644 --- a/sched/sched/sched.h +++ b/sched/sched/sched.h @@ -395,6 +395,20 @@ int nxsched_waitid(idtype_t idtype, pid_t id, FAR siginfo_t *info, int options); #endif +static inline_function int32_t nxsched_get_policy(FAR struct tcb_s *tcb) +{ + return atomic_read(&tcb->shared_flags) & TCB_FLAG_POLICY_MASK; +} + +static inline_function void nxsched_set_policy(FAR struct tcb_s *tcb, + int32_t policy) +{ + int32_t old = atomic_read(&tcb->shared_flags); + + while (!atomic_try_cmpxchg(&tcb->shared_flags, &old, + (old & ~TCB_FLAG_POLICY_MASK) | policy)); +} + #if defined(up_this_task) # define this_task() up_this_task() #elif !defined(CONFIG_SMP) diff --git a/sched/sched/sched_addreadytorun.c b/sched/sched/sched_addreadytorun.c index 2724a1bd5acb7..3be9f9dffd1ed 100644 --- a/sched/sched/sched_addreadytorun.c +++ b/sched/sched/sched_addreadytorun.c @@ -184,7 +184,8 @@ bool nxsched_switch_running(int cpu, bool switch_equal) */ if (CPU_ISSET(cpu, &btcb->affinity) && - ((btcb->flags & TCB_FLAG_CPU_LOCKED) == 0 || btcb->cpu == cpu)) + ((atomic_read(&btcb->shared_flags) & + TCB_FLAG_CPU_LOCKED) == 0 || btcb->cpu == cpu)) { /* Found a task, remove it from ready-to-run list */ @@ -253,7 +254,8 @@ bool nxsched_switch_running(int cpu, bool switch_equal) bool nxsched_add_readytorun(FAR struct tcb_s *btcb) { bool doswitch = false; - int target_cpu = btcb->flags & TCB_FLAG_CPU_LOCKED ? btcb->cpu : + int target_cpu = atomic_read(&btcb->shared_flags) & + TCB_FLAG_CPU_LOCKED ? btcb->cpu : nxsched_select_cpu(btcb->affinity); FAR struct tcb_s *tcb = current_task(target_cpu); diff --git a/sched/sched/sched_backtrace.c b/sched/sched/sched_backtrace.c index c6deec98212c8..e4a4c3a49d1d7 100644 --- a/sched/sched/sched_backtrace.c +++ b/sched/sched/sched_backtrace.c @@ -86,7 +86,7 @@ static int sched_backtrace_handler(FAR void *cookie) tcb = nxsched_get_tcb(arg->pid); if (!tcb || tcb->task_state == TSTATE_TASK_INVALID || - (tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + (atomic_read(&tcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0) { /* There is no TCB with this pid or, if there is, it is not a task. */ @@ -96,7 +96,7 @@ static int sched_backtrace_handler(FAR void *cookie) if (arg->need_restore) { - tcb->flags &= ~TCB_FLAG_CPU_LOCKED; + atomic_and(&tcb->shared_flags, ~TCB_FLAG_CPU_LOCKED); } leave_critical_section(flags); @@ -143,7 +143,8 @@ int sched_backtrace(pid_t tid, FAR void **buffer, int size, int skip) if (tcb != NULL) { if (tcb->task_state == TSTATE_TASK_INVALID || - (tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + (atomic_read(&tcb->shared_flags) & + TCB_FLAG_EXIT_PROCESSING) != 0) { leave_critical_section(flags); return 0; @@ -159,14 +160,14 @@ int sched_backtrace(pid_t tid, FAR void **buffer, int size, int skip) FAR void *scratch[BACKTRACE_SCRATCH_DEPTH]; #endif - if ((tcb->flags & TCB_FLAG_CPU_LOCKED) != 0) + if ((atomic_or(&tcb->shared_flags, TCB_FLAG_CPU_LOCKED) & + TCB_FLAG_CPU_LOCKED) != 0) { need_restore = false; } else { need_restore = true; - tcb->flags |= TCB_FLAG_CPU_LOCKED; } arg.pid = tcb->pid; diff --git a/sched/sched/sched_getparam.c b/sched/sched/sched_getparam.c index 9bf826f7d48f2..32de0f4d750e7 100644 --- a/sched/sched/sched_getparam.c +++ b/sched/sched/sched_getparam.c @@ -54,7 +54,7 @@ static void nxsched_get_sporadic_param(FAR struct tcb_s *tcb, FAR struct sched_param *param) { - if ((tcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC) + if (nxsched_get_policy(tcb) == TCB_FLAG_SCHED_SPORADIC) { FAR struct sporadic_s *sporadic = tcb->sporadic; diff --git a/sched/sched/sched_getscheduler.c b/sched/sched/sched_getscheduler.c index 86d5cb00b3a4d..9d20fba8226b2 100644 --- a/sched/sched/sched_getscheduler.c +++ b/sched/sched/sched_getscheduler.c @@ -88,7 +88,7 @@ int nxsched_get_scheduler(pid_t pid) { /* Return the scheduling policy from the TCB */ - ret = (tcb->flags & TCB_FLAG_POLICY_MASK) >> TCB_FLAG_POLICY_SHIFT; + ret = nxsched_get_policy(tcb) >> TCB_FLAG_POLICY_SHIFT; } return ret; diff --git a/sched/sched/sched_process_delivered.c b/sched/sched/sched_process_delivered.c index 36effcd028d4b..1319f15b65c8e 100644 --- a/sched/sched/sched_process_delivered.c +++ b/sched/sched/sched_process_delivered.c @@ -101,10 +101,13 @@ void nxsched_process_delivered(int cpu) */ FAR struct tcb_s *tcb = (FAR struct tcb_s *)dq_peek(list_readytorun()); + if (tcb) { - int target_cpu = tcb->flags & TCB_FLAG_CPU_LOCKED ? + int target_cpu = atomic_read(&tcb->shared_flags) & + TCB_FLAG_CPU_LOCKED ? tcb->cpu : nxsched_select_cpu(tcb->affinity); + if (target_cpu != cpu && current_task(target_cpu)->sched_priority < tcb->sched_priority) { diff --git a/sched/sched/sched_processtick.c b/sched/sched/sched_processtick.c index 44d3d169a54f8..5ada01f80460d 100644 --- a/sched/sched/sched_processtick.c +++ b/sched/sched/sched_processtick.c @@ -74,7 +74,7 @@ static inline void nxsched_cpu_scheduler(int cpu) #if CONFIG_RR_INTERVAL > 0 /* Check if the currently executing task uses round robin scheduling. */ - if ((rtcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_RR) + if (nxsched_get_policy(rtcb) == TCB_FLAG_SCHED_RR) { /* Yes, check if the currently executing task has exceeded its * timeslice. @@ -87,7 +87,7 @@ static inline void nxsched_cpu_scheduler(int cpu) #ifdef CONFIG_SCHED_SPORADIC /* Check if the currently executing task uses sporadic scheduling. */ - if ((rtcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC) + if (nxsched_get_policy(rtcb) == TCB_FLAG_SCHED_SPORADIC) { /* Yes, check if the currently executing task has exceeded its * budget. diff --git a/sched/sched/sched_processtickless.c b/sched/sched/sched_processtickless.c index c91eda9aa5fae..565bc7fa760ee 100644 --- a/sched/sched/sched_processtickless.c +++ b/sched/sched/sched_processtickless.c @@ -199,7 +199,7 @@ static clock_t nxsched_cpu_scheduler(int cpu, clock_t ticks, #if CONFIG_RR_INTERVAL > 0 /* Check if the currently executing task uses round robin scheduling. */ - if ((rtcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_RR) + if (nxsched_get_policy(rtcb) == TCB_FLAG_SCHED_RR) { /* Yes, check if the currently executing task has exceeded its * timeslice. @@ -213,7 +213,7 @@ static clock_t nxsched_cpu_scheduler(int cpu, clock_t ticks, #ifdef CONFIG_SCHED_SPORADIC /* Check if the currently executing task uses sporadic scheduling. */ - if ((rtcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC) + if (nxsched_get_policy(rtcb) == TCB_FLAG_SCHED_SPORADIC) { FAR struct sporadic_s *sporadic = rtcb->sporadic; diff --git a/sched/sched/sched_roundrobin.c b/sched/sched/sched_roundrobin.c index 8400b40055772..64230d952a38d 100644 --- a/sched/sched/sched_roundrobin.c +++ b/sched/sched/sched_roundrobin.c @@ -72,7 +72,7 @@ static int nxsched_roundrobin_handler(FAR void *cookie) tcb = nxsched_get_tcb(pid); if (!tcb || tcb->task_state == TSTATE_TASK_INVALID || - (tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + (atomic_read(&tcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0) { /* There is no TCB with this pid or, if there is, it is not a task. */ diff --git a/sched/sched/sched_rrgetinterval.c b/sched/sched/sched_rrgetinterval.c index d8c6a1642a38e..a64f50e32f06d 100644 --- a/sched/sched/sched_rrgetinterval.c +++ b/sched/sched/sched_rrgetinterval.c @@ -96,8 +96,7 @@ int sched_rr_get_interval(pid_t pid, struct timespec *interval) * configured for round-robin scheduling. */ - if ((rrtcb->flags & TCB_FLAG_POLICY_MASK) == - TCB_FLAG_SCHED_RR) + if (nxsched_get_policy(rrtcb) == TCB_FLAG_SCHED_RR) { /* Convert the timeslice value from ticks to a timespec */ diff --git a/sched/sched/sched_setaffinity.c b/sched/sched/sched_setaffinity.c index a488914524733..65048114ead0e 100644 --- a/sched/sched/sched_setaffinity.c +++ b/sched/sched/sched_setaffinity.c @@ -109,7 +109,7 @@ int nxsched_set_affinity(pid_t pid, size_t cpusetsize, */ flags = enter_critical_section(); - if ((tcb->flags & TCB_FLAG_CPU_LOCKED) != 0) + if ((atomic_read(&tcb->shared_flags) & TCB_FLAG_CPU_LOCKED) != 0) { ret = -EINVAL; goto errout_with_csection; diff --git a/sched/sched/sched_setparam.c b/sched/sched/sched_setparam.c index 407d0a42a2f42..e5e0357fcaab9 100644 --- a/sched/sched/sched_setparam.c +++ b/sched/sched/sched_setparam.c @@ -54,7 +54,7 @@ int set_sporadic_param(FAR const struct sched_param *param, /* Update parameters associated with SCHED_SPORADIC */ - if ((tcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC) + if (nxsched_get_policy(tcb) == TCB_FLAG_SCHED_SPORADIC) { ret = nxsched_validate_sporadic(param, &repl_ticks, &budget_ticks); if (ret < 0) diff --git a/sched/sched/sched_setscheduler.c b/sched/sched/sched_setscheduler.c index 2aee700e5733e..c1ffe6f4b4103 100644 --- a/sched/sched/sched_setscheduler.c +++ b/sched/sched/sched_setscheduler.c @@ -60,7 +60,7 @@ int process_sporadic(FAR struct tcb_s *tcb, /* Initialize or reset current sporadic scheduling */ - if ((tcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC) + if (nxsched_get_policy(tcb) == TCB_FLAG_SCHED_SPORADIC) { ret = nxsched_reset_sporadic(tcb); } @@ -73,8 +73,7 @@ int process_sporadic(FAR struct tcb_s *tcb, if (ret >= 0) { - tcb->flags &= ~TCB_FLAG_POLICY_MASK; - tcb->flags |= TCB_FLAG_SCHED_SPORADIC; + nxsched_set_policy(tcb, TCB_FLAG_SCHED_SPORADIC); tcb->timeslice = budget_ticks; sporadic = tcb->sporadic; @@ -202,8 +201,7 @@ int nxsched_set_scheduler(pid_t pid, int policy, #ifdef CONFIG_SCHED_SPORADIC /* Cancel any on-going sporadic scheduling */ - if ((tcb->flags & TCB_FLAG_POLICY_MASK) == - TCB_FLAG_SCHED_SPORADIC) + if (nxsched_get_policy(tcb) == TCB_FLAG_SCHED_SPORADIC) { DEBUGVERIFY(nxsched_stop_sporadic(tcb)); } @@ -211,8 +209,7 @@ int nxsched_set_scheduler(pid_t pid, int policy, /* Set the new policy flag */ - tcb->flags &= ~TCB_FLAG_POLICY_MASK; - tcb->flags |= policy << TCB_FLAG_POLICY_SHIFT; + nxsched_set_policy(tcb, policy << TCB_FLAG_POLICY_SHIFT); /* Configure timeslice based on policy */ #if CONFIG_RR_INTERVAL > 0 || defined(CONFIG_SCHED_SPORADIC) diff --git a/sched/sched/sched_sporadic.c b/sched/sched/sched_sporadic.c index 86b0b13957447..ab4d35d14de32 100644 --- a/sched/sched/sched_sporadic.c +++ b/sched/sched/sched_sporadic.c @@ -960,7 +960,7 @@ int nxsched_stop_sporadic(FAR struct tcb_s *tcb) * context switch on thread exit would dereference it. */ - tcb->flags &= ~TCB_FLAG_POLICY_MASK; + atomic_and(&tcb->shared_flags, ~TCB_FLAG_POLICY_MASK); return OK; } diff --git a/sched/sched/sched_suspend.c b/sched/sched/sched_suspend.c index 6fb2bb635bf8e..e4470878b72e5 100644 --- a/sched/sched/sched_suspend.c +++ b/sched/sched/sched_suspend.c @@ -63,7 +63,7 @@ static int nxsched_suspend_handler(FAR void *cookie) tcb = nxsched_get_tcb(arg->pid); if (!tcb || tcb->task_state == TSTATE_TASK_INVALID || - (tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + (atomic_read(&tcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0) { /* There is no TCB with this pid or, if there is, it is not a task. */ @@ -73,7 +73,7 @@ static int nxsched_suspend_handler(FAR void *cookie) if (arg->need_restore) { - tcb->flags &= ~TCB_FLAG_CPU_LOCKED; + atomic_and(&tcb->shared_flags, ~TCB_FLAG_CPU_LOCKED); } nxsched_remove_readytorun(tcb); @@ -170,16 +170,15 @@ void nxsched_suspend(FAR struct tcb_s *tcb) { struct suspend_arg_s arg; - if ((tcb->flags & TCB_FLAG_CPU_LOCKED) != 0) + arg.pid = tcb->pid; + if (atomic_or(&tcb->shared_flags, TCB_FLAG_CPU_LOCKED) & + TCB_FLAG_CPU_LOCKED) { - arg.pid = tcb->pid; arg.need_restore = false; } else { - arg.pid = tcb->pid; arg.need_restore = true; - tcb->flags |= TCB_FLAG_CPU_LOCKED; } nxsched_smp_call_single(tcb->cpu, nxsched_suspend_handler, &arg); diff --git a/sched/sched/sched_switchcontext.c b/sched/sched/sched_switchcontext.c index 1402897e84fbc..a078c22cfd241 100644 --- a/sched/sched/sched_switchcontext.c +++ b/sched/sched/sched_switchcontext.c @@ -60,7 +60,7 @@ void nxsched_switch_context(FAR struct tcb_s *from, FAR struct tcb_s *to) * for the time it has consumed from its timeslice. */ - if ((from->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_RR) + if (nxsched_get_policy(from) == TCB_FLAG_SCHED_RR) { nxsched_suspend_roundrobin(from); } @@ -73,12 +73,12 @@ void nxsched_switch_context(FAR struct tcb_s *from, FAR struct tcb_s *to) #ifdef CONFIG_SCHED_SPORADIC /* Perform sporadic schedule operations */ - if ((from->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC) + if (nxsched_get_policy(from) == TCB_FLAG_SCHED_SPORADIC) { DEBUGVERIFY(nxsched_suspend_sporadic(from)); } - if ((to->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC) + if (nxsched_get_policy(to) == TCB_FLAG_SCHED_SPORADIC) { DEBUGVERIFY(nxsched_resume_sporadic(to)); } @@ -89,7 +89,7 @@ void nxsched_switch_context(FAR struct tcb_s *from, FAR struct tcb_s *to) * the timer for its remaining timeslice. */ - if ((to->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_RR) + if (nxsched_get_policy(to) == TCB_FLAG_SCHED_RR) { nxsched_resume_roundrobin(to); } diff --git a/sched/sched/sched_unlock.c b/sched/sched/sched_unlock.c index 9c16c0be6c36b..13317587a22a7 100644 --- a/sched/sched/sched_unlock.c +++ b/sched/sched/sched_unlock.c @@ -109,7 +109,7 @@ void sched_unlock(void) * timer for the next time slice. */ - if ((rtcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_RR && + if (nxsched_get_policy(rtcb) == TCB_FLAG_SCHED_RR && rtcb->timeslice == 0) { /* Yes.. that is the situation. But one more thing. The call @@ -145,7 +145,7 @@ void sched_unlock(void) * for the next time slice. */ - if ((rtcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC + if (nxsched_get_policy(rtcb) == TCB_FLAG_SCHED_SPORADIC && rtcb->timeslice < 0) { /* Yes.. that is the situation. Force the low-priority state diff --git a/sched/sched/sched_waitid.c b/sched/sched/sched_waitid.c index 80edc3f8c1c13..15e72bbc65406 100644 --- a/sched/sched/sched_waitid.c +++ b/sched/sched/sched_waitid.c @@ -138,7 +138,7 @@ static void child_handler(FAR struct tcb_s *tcb, FAR void *arg) #ifdef CONFIG_SCHED_CHILD_STATUS /* A child that sent SIGCHLD can still be found during its exit */ - if ((tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + if ((atomic_read(&tcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0) { return; } @@ -278,7 +278,8 @@ static int check_children(FAR struct tcb_s *rtcb, idtype_t idtype, pid_t id) ctcb = nxsched_get_tcb(id); if (ctcb == NULL || ctcb->group == NULL || ctcb->group->tg_ppid != rtcb->group->tg_pid || - (ctcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + (atomic_read(&ctcb->shared_flags) & + TCB_FLAG_EXIT_PROCESSING) != 0) { return -ECHILD; } diff --git a/sched/sched/sched_waitpid.c b/sched/sched/sched_waitpid.c index ca94aee142e0b..3d224534914d6 100644 --- a/sched/sched/sched_waitpid.c +++ b/sched/sched/sched_waitpid.c @@ -177,7 +177,8 @@ pid_t nxsched_waitpid(pid_t pid, FAR int *stat_loc, int options) if (ctcb != NULL) { if ((group = ctcb->group) != NULL && - (ctcb->flags & TCB_FLAG_EXIT_PROCESSING) == 0) + (atomic_read(&ctcb->shared_flags) & + TCB_FLAG_EXIT_PROCESSING) == 0) { /* Lock this group so that it cannot be * deleted until the wait completes diff --git a/sched/signal/sig_default.c b/sched/signal/sig_default.c index d93eaccd87491..028c633f82b41 100644 --- a/sched/signal/sig_default.c +++ b/sched/signal/sig_default.c @@ -233,11 +233,11 @@ static void nxsig_abnormal_termination(int signo) * fatal exception: a thread can block or catch SIGTERM. */ - rtcb->flags |= TCB_FLAG_FORCED_CANCEL; + atomic_or(&rtcb->shared_flags, TCB_FLAG_FORCED_CANCEL); group_kill_children(rtcb); #endif - if ((rtcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + if ((atomic_read(&rtcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0) { /* If we are already in the process of terminating, then we should * not be here. We should have already exited. diff --git a/sched/signal/sig_dispatch.c b/sched/signal/sig_dispatch.c index 40cc3da280b98..ad2d6ddb26e4f 100644 --- a/sched/signal/sig_dispatch.c +++ b/sched/signal/sig_dispatch.c @@ -75,7 +75,7 @@ static int sig_handler(FAR void *cookie) tcb = nxsched_get_tcb(arg->pid); if (!tcb || tcb->task_state == TSTATE_TASK_INVALID || - (tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + (atomic_read(&tcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0) { /* There is no TCB with this pid or, if there is, it is not a task. */ @@ -85,7 +85,7 @@ static int sig_handler(FAR void *cookie) if (arg->need_restore) { - tcb->flags &= ~TCB_FLAG_CPU_LOCKED; + atomic_and(&tcb->shared_flags, ~TCB_FLAG_CPU_LOCKED); } if (tcb->sigdeliver) @@ -176,17 +176,17 @@ static int nxsig_queue_action(FAR struct tcb_s *stcb, { struct sig_arg_s arg; - if ((stcb->flags & TCB_FLAG_CPU_LOCKED) != 0) + arg.pid = stcb->pid; + if (atomic_or(&stcb->shared_flags, TCB_FLAG_CPU_LOCKED) & + TCB_FLAG_CPU_LOCKED) { arg.need_restore = false; } else { arg.need_restore = true; - stcb->flags |= TCB_FLAG_CPU_LOCKED; } - arg.pid = stcb->pid; nxsched_smp_call_single(stcb->cpu, sig_handler, &arg); } else @@ -676,7 +676,7 @@ int nxsig_tcbdispatch(FAR struct tcb_s *stcb, FAR siginfo_t *info, /* Return ESRCH when thread was in exit processing */ - if ((stcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + if ((atomic_read(&stcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0) { return -ESRCH; } @@ -710,7 +710,7 @@ int nxsig_tcbdispatch(FAR struct tcb_s *stcb, FAR siginfo_t *info, * signals pending for the thread. */ - if ((stcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + if ((atomic_read(&stcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0) { leave_critical_section(flags); return -ESRCH; diff --git a/sched/task/exit.c b/sched/task/exit.c index aba4bc097e453..7d20fd1c443c5 100644 --- a/sched/task/exit.c +++ b/sched/task/exit.c @@ -92,7 +92,7 @@ void _exit(int status) * once, or does something very naughty. */ - tcb->flags |= TCB_FLAG_EXIT_PROCESSING; + atomic_or(&tcb->shared_flags, TCB_FLAG_EXIT_PROCESSING); nxtask_exithook(tcb, status); diff --git a/sched/task/task_cancelpt.c b/sched/task/task_cancelpt.c index 7cd16dc073015..feb744233321d 100644 --- a/sched/task/task_cancelpt.c +++ b/sched/task/task_cancelpt.c @@ -109,7 +109,7 @@ bool nxnotify_cancellation(FAR struct tcb_s *tcb) /* Check to see if this task has the non-cancelable bit set. */ - if ((tcb->flags & TCB_FLAG_FORCED_CANCEL) == 0 && + if ((atomic_read(&tcb->shared_flags) & TCB_FLAG_FORCED_CANCEL) == 0 && (tls->tl_cpstate & CANCEL_FLAG_NONCANCELABLE) != 0u) { /* Then we cannot cancel the thread now. Here is how this is diff --git a/sched/task/task_exithook.c b/sched/task/task_exithook.c index 0f70bc3922929..dc38057c4eb51 100644 --- a/sched/task/task_exithook.c +++ b/sched/task/task_exithook.c @@ -512,7 +512,8 @@ void nxtask_exithook(FAR struct tcb_s *tcb, int status) * called. If that bit is set, then just exit doing nothing more.. */ - DEBUGASSERT((tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0); + DEBUGASSERT((atomic_read(&tcb->shared_flags) & + TCB_FLAG_EXIT_PROCESSING) != 0); nxsched_dumponexit(); diff --git a/sched/task/task_recover.c b/sched/task/task_recover.c index 58415db7f4188..8286c095c8765 100644 --- a/sched/task/task_recover.c +++ b/sched/task/task_recover.c @@ -103,7 +103,7 @@ void nxtask_recover(FAR struct tcb_s *tcb) #endif #ifdef CONFIG_SCHED_SPORADIC - if ((tcb->flags & TCB_FLAG_POLICY_MASK) == TCB_FLAG_SCHED_SPORADIC) + if (nxsched_get_policy(tcb) == TCB_FLAG_SCHED_SPORADIC) { /* Stop current sporadic scheduling */ diff --git a/sched/task/task_restart.c b/sched/task/task_restart.c index dc10c5c695c53..29dd9c9f1475e 100644 --- a/sched/task/task_restart.c +++ b/sched/task/task_restart.c @@ -67,7 +67,7 @@ static int restart_handler(FAR void *cookie) tcb = nxsched_get_tcb(arg->pid); if (!tcb || tcb->task_state == TSTATE_TASK_INVALID || - (tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + (atomic_read(&tcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0) { /* There is no TCB with this pid or, if there is, it is not a task. */ @@ -77,7 +77,7 @@ static int restart_handler(FAR void *cookie) if (arg->need_restore) { - tcb->flags &= ~TCB_FLAG_CPU_LOCKED; + atomic_and(&tcb->shared_flags, ~TCB_FLAG_CPU_LOCKED); } nxsched_remove_readytorun(tcb); @@ -233,23 +233,23 @@ static int nxtask_restart(pid_t pid) { struct restart_arg_s arg; - if ((tcb->flags & TCB_FLAG_CPU_LOCKED) != 0) + arg.pid = tcb->pid; + if (atomic_or(&tcb->shared_flags, TCB_FLAG_CPU_LOCKED) & + TCB_FLAG_CPU_LOCKED) { - arg.pid = tcb->pid; arg.need_restore = false; } else { - arg.pid = tcb->pid; arg.need_restore = true; - tcb->flags |= TCB_FLAG_CPU_LOCKED; } nxsched_smp_call_single(tcb->cpu, restart_handler, &arg); tcb = nxsched_get_tcb(pid); if (!tcb || tcb->task_state != TSTATE_TASK_INVALID || - (tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0) + (atomic_read(&tcb->shared_flags) & + TCB_FLAG_EXIT_PROCESSING) != 0) { ret = -ESRCH; goto errout_with_lock; diff --git a/sched/task/task_setup.c b/sched/task/task_setup.c index 0203ad2ad4c79..000b92872d04d 100644 --- a/sched/task/task_setup.c +++ b/sched/task/task_setup.c @@ -434,12 +434,11 @@ static int nxthread_setup_scheduler(FAR struct tcb_s *tcb, int priority, /* Set the appropriate scheduling policy in the TCB */ - tcb->flags &= ~TCB_FLAG_POLICY_MASK; #if CONFIG_RR_INTERVAL > 0 - tcb->flags |= TCB_FLAG_SCHED_RR; + nxsched_set_policy(tcb, TCB_FLAG_SCHED_RR); tcb->timeslice = MSEC2TICK(CONFIG_RR_INTERVAL); #else - tcb->flags |= TCB_FLAG_SCHED_FIFO; + nxsched_set_policy(tcb, TCB_FLAG_SCHED_FIFO); #endif /* Save the task ID of the parent task in the TCB and allocate diff --git a/sched/task/task_terminate.c b/sched/task/task_terminate.c index 16e1b6a69e62c..724d1d96ed583 100644 --- a/sched/task/task_terminate.c +++ b/sched/task/task_terminate.c @@ -122,14 +122,13 @@ int nxtask_terminate(pid_t pid) /* Find for the TCB associated with matching PID */ dtcb = nxsched_get_tcb(pid); - if (!dtcb || dtcb->flags & TCB_FLAG_EXIT_PROCESSING) + if (!dtcb || (atomic_or(&dtcb->shared_flags, TCB_FLAG_EXIT_PROCESSING) & + TCB_FLAG_EXIT_PROCESSING) != 0) { leave_critical_section(flags); return -ESRCH; } - dtcb->flags |= TCB_FLAG_EXIT_PROCESSING; - /* Remove dtcb from tasklist, let remove_readtorun() do the job */ task_state = dtcb->task_state; @@ -137,11 +136,10 @@ int nxtask_terminate(pid_t pid) if (task_state == TSTATE_TASK_RUNNING && dtcb->cpu != this_cpu()) { - uint16_t tcb_flags; + int32_t tcb_flags; int ret; - tcb_flags = dtcb->flags; - dtcb->flags |= TCB_FLAG_CPU_LOCKED; + tcb_flags = atomic_or(&dtcb->shared_flags, TCB_FLAG_CPU_LOCKED); ret = nxsched_smp_call_single(dtcb->cpu, terminat_handler, (FAR void *)(uintptr_t)pid); @@ -154,7 +152,10 @@ int nxtask_terminate(pid_t pid) return ret; } - dtcb->flags = tcb_flags; + if ((tcb_flags & TCB_FLAG_CPU_LOCKED) == 0) + { + atomic_and(&dtcb->shared_flags, ~TCB_FLAG_CPU_LOCKED); + } } else #endif diff --git a/tools/pynuttx/nxgdb/thread.py b/tools/pynuttx/nxgdb/thread.py index d7ca0a8221b48..d5e6be469db4f 100644 --- a/tools/pynuttx/nxgdb/thread.py +++ b/tools/pynuttx/nxgdb/thread.py @@ -471,7 +471,7 @@ def cast2ptr(x, t): policy = eval2str( TaskSchedPolicy, - (tcb["flags"] & get_macro("TCB_FLAG_POLICY_MASK")) + (tcb["shared_flags"] & get_macro("TCB_FLAG_POLICY_MASK")) >> get_macro("TCB_FLAG_POLICY_SHIFT"), ) @@ -481,7 +481,11 @@ def cast2ptr(x, t): >> get_macro("TCB_FLAG_TTYPE_SHIFT"), ) - npx = "P" if (tcb["flags"] & get_macro("TCB_FLAG_EXIT_PROCESSING")) else "-" + npx = ( + "P" + if (tcb["shared_flags"] & get_macro("TCB_FLAG_EXIT_PROCESSING")) + else "-" + ) waiter = ( str(int(cast2ptr(tcb["waitobj"], "mutex_t")["holder"]))