Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arch/arm/src/arm_a_r/arm_userfault.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
xiaoxiang781216 marked this conversation as resolved.
Resolved

regs[REG_PC] = (uint32_t)_exit;
regs[REG_R0] = SIGSEGV;
Expand Down
2 changes: 1 addition & 1 deletion arch/arm64/src/common/arm64_fatal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion arch/risc-v/src/common/riscv_exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
2 changes: 1 addition & 1 deletion arch/x86_64/src/intel64/intel64_irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions fs/procfs/fs_procfsproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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");
Expand Down
46 changes: 29 additions & 17 deletions include/nuttx/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions sched/group/group_killchildren.c
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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 */

Expand Down
3 changes: 2 additions & 1 deletion sched/group/group_pgrp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion sched/init/nx_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions sched/irq/irq_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 3 additions & 2 deletions sched/misc/assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sched/pthread/pthread_cancel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions sched/pthread/pthread_completejoin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

Expand Down
9 changes: 4 additions & 5 deletions sched/pthread/pthread_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions sched/pthread/pthread_detach.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion sched/pthread/pthread_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions sched/pthread/pthread_join.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions sched/sched/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions sched/sched/sched_addreadytorun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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);

Expand Down
Loading
Loading