This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 1325a419756 sched: split tcb->flags by writer
1325a419756 is described below
commit 1325a419756929a6fc6f7b25c4c835b1e8ff2cdd
Author: zhangyuan29 <[email protected]>
AuthorDate: Tue Nov 24 19:28:12 2026 +0800
sched: split tcb->flags by writer
The flags of one task are written from more than one CPU.
nxsched_suspend(), nxsig_queue_action() and nxtask_restart() set
TCB_FLAG_CPU_LOCKED on a task that runs on another CPU, and
sched_setscheduler() changes the scheduling policy of any task, while
that task changes its own bits without the critical section, for
example TCB_FLAG_SYSCALL on system call entry and exit. A plain
read-modify-write of tcb->flags can then lose one of the two updates.
Split the word by who writes it:
- tcb->flags keeps the bits that have a single writer: the thread type
and TCB_FLAG_FREE_TCB, set by the creator before the task runs,
TCB_FLAG_FREE_STACK and TCB_FLAG_EXIT_COUNTED, which change only
while the task does not run or when it exits, and the bits that only
the task itself changes. It keeps its name and the TCB_FLAG_* names,
the remaining bits are renumbered to bits 0-9 in their original
order, and it is still accessed with plain loads and stores, so the
arch code and the system call, signal delivery and semaphore wait
paths are not changed. It becomes a uint16_t in the hole after
init_priority.
- The scheduling policy and the bits that other tasks change move to
tcb->shared_flags, an atomic_t in the place of the old field, and are
accessed with atomic_or(), atomic_and(), atomic_read() and, for the
policy, a compare-and-exchange loop in nxsched_set_policy(). They
keep their TCB_FLAG_* names, but are used only with shared_flags.
Where a flag was tested and then set in two steps (TCB_FLAG_CPU_LOCKED
in nxsched_suspend(), nxtask_restart(), nxsig_queue_action() and the
backtrace path, TCB_FLAG_EXIT_PROCESSING in nxtask_terminate()), use the
value returned by atomic_or() instead, so that two callers cannot both
see the flag clear.
This also fixes nxtask_terminate(). It saved the flags before the SMP
call that removes a running task from its CPU and wrote the whole word
back afterwards. The task keeps running on the other CPU until that
call is handled, so a bit that it or another task changed in the
meantime, such as TCB_FLAG_FORCED_CANCEL, TCB_FLAG_JOIN_COMPLETED or
TCB_FLAG_DETACHED, was lost. The copy was also a uint16_t, so writing
it back cleared bits 16-19. Now it clears TCB_FLAG_CPU_LOCKED only,
and only if it set it.
Fix the nxstyle errors that the touched files carry.
Signed-off-by: hujun5 <[email protected]>
Signed-off-by: zhangyuan29 <[email protected]>
---
arch/arm/src/arm_a_r/arm_userfault.c | 2 +-
arch/arm64/src/common/arm64_fatal.c | 2 +-
arch/risc-v/src/common/riscv_exception.c | 2 +-
arch/x86_64/src/intel64/intel64_irq.c | 2 +-
fs/procfs/fs_procfsproc.c | 12 +++++----
include/nuttx/sched.h | 46 ++++++++++++++++++++------------
sched/group/group_killchildren.c | 4 +--
sched/group/group_pgrp.c | 3 ++-
sched/init/nx_start.c | 3 ++-
sched/irq/irq_dispatch.c | 4 +--
sched/misc/assert.c | 5 ++--
sched/pthread/pthread_cancel.c | 2 +-
sched/pthread/pthread_completejoin.c | 4 +--
sched/pthread/pthread_create.c | 9 +++----
sched/pthread/pthread_detach.c | 7 ++---
sched/pthread/pthread_exit.c | 2 +-
sched/pthread/pthread_join.c | 5 ++--
sched/sched/sched.h | 14 ++++++++++
sched/sched/sched_addreadytorun.c | 6 +++--
sched/sched/sched_backtrace.c | 11 ++++----
sched/sched/sched_getparam.c | 2 +-
sched/sched/sched_getscheduler.c | 2 +-
sched/sched/sched_process_delivered.c | 5 +++-
sched/sched/sched_processtick.c | 4 +--
sched/sched/sched_processtickless.c | 4 +--
sched/sched/sched_roundrobin.c | 2 +-
sched/sched/sched_rrgetinterval.c | 3 +--
sched/sched/sched_setaffinity.c | 2 +-
sched/sched/sched_setparam.c | 2 +-
sched/sched/sched_setscheduler.c | 11 +++-----
sched/sched/sched_sporadic.c | 2 +-
sched/sched/sched_suspend.c | 11 ++++----
sched/sched/sched_switchcontext.c | 8 +++---
sched/sched/sched_unlock.c | 4 +--
sched/sched/sched_waitid.c | 5 ++--
sched/sched/sched_waitpid.c | 3 ++-
sched/signal/sig_default.c | 4 +--
sched/signal/sig_dispatch.c | 14 +++++-----
sched/task/exit.c | 2 +-
sched/task/task_cancelpt.c | 2 +-
sched/task/task_exithook.c | 3 ++-
sched/task/task_recover.c | 2 +-
sched/task/task_restart.c | 14 +++++-----
sched/task/task_setup.c | 5 ++--
sched/task/task_terminate.c | 15 ++++++-----
tools/pynuttx/nxgdb/thread.py | 8 ++++--
46 files changed, 162 insertions(+), 122 deletions(-)
diff --git a/arch/arm/src/arm_a_r/arm_userfault.c
b/arch/arm/src/arm_a_r/arm_userfault.c
index 34fe5a13724..0c3b15974d5 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) : "<gone>",
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 aabeafc1fed..a16fe15100f 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 8316fc580a3..1277a7015cc 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 52791fce5c6..f45b7c260b2 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 2887ee72a34..1d137870829 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 d298cacbd17..e55b618cdc1 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 5a7345fdfed..49f2662d2d8 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 ab6ab152ddd..2b07c5caf7c 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 3abae8bf711..40cffacbe90 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 713fee3f40d..5d5acf126ce 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 c8f276a715d..f8ee74d18ff 100644
--- a/sched/misc/assert.c
+++ b/sched/misc/assert.c
@@ -457,11 +457,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 abe2b81b126..c9cfe005a71 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 1a31f8ba5a3..f73bd3c90f0 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 ce38c32a3d0..e0468c44abd 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 4a3d3a7048e..200798c2165 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 9a25815997d..18fe92b159b 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 d578bd7d0dd..d8983d5e7cf 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 8a2e38ca9cb..92865f671f2 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 2724a1bd5ac..3be9f9dffd1 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 c6deec98212..e4a4c3a49d1 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 9bf826f7d48..32de0f4d750 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 86d5cb00b3a..9d20fba8226 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 36effcd028d..1319f15b65c 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 44d3d169a54..5ada01f8046 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 c91eda9aa5f..565bc7fa760 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 8400b400557..64230d952a3 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 d8c6a1642a3..a64f50e32f0 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 a4889145247..65048114ead 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 407d0a42a2f..e5e0357fcaa 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 2aee700e573..c1ffe6f4b41 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 86b0b139574..ab4d35d14de 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 6fb2bb635bf..e4470878b72 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 1402897e84f..a078c22cfd2 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 9c16c0be6c3..13317587a22 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 80edc3f8c1c..15e72bbc654 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 ca94aee142e..3d224534914 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 d93eaccd874..028c633f82b 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 40cc3da280b..ad2d6ddb26e 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 aba4bc097e4..7d20fd1c443 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 7cd16dc0730..feb74423332 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 0f70bc39229..dc38057c4eb 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 58415db7f41..8286c095c87 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 dc10c5c695c..29dd9c9f147 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 0203ad2ad4c..000b92872d0 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 16e1b6a69e6..724d1d96ed5 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 d7ca0a8221b..d5e6be469db 100644
--- a/tools/pynuttx/nxgdb/thread.py
+++ b/tools/pynuttx/nxgdb/thread.py
@@ -471,7 +471,7 @@ class Ps(gdb.Command):
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 @@ class Ps(gdb.Command):
>> 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"]))