Skip to content

Commit

Permalink
task/pthread_cancelpt: Move cancel point handling to libc, data to TLS
Browse files Browse the repository at this point in the history
This moves task / thread cancel point logic from the NuttX kernel into
libc, while the data needed by the cancel point logic is moved to TLS.

The change is an enabler to move user-space APIs to libc as well, for
a coherent user/kernel separation.
  • Loading branch information
pussuw authored and xiaoxiang781216 committed Nov 15, 2023
1 parent 1e31ec8 commit 0dedbcd
Show file tree
Hide file tree
Showing 24 changed files with 414 additions and 547 deletions.
4 changes: 1 addition & 3 deletions fs/procfs/fs_procfsproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,7 @@ static ssize_t proc_status(FAR struct proc_file_s *procfile,
/* Show task flags */

linesize = procfs_snprintf(procfile->line, STATUS_LINELEN,
"%-12s%c%c%c\n", "Flags:",
tcb->flags & TCB_FLAG_NONCANCELABLE ? 'N' : '-',
tcb->flags & TCB_FLAG_CANCEL_PENDING ? 'P' : '-',
"%-12s%c\n", "Flags:",
tcb->flags & TCB_FLAG_EXIT_PROCESSING ? 'P' : '-');

copysize = procfs_memcpy(procfile->line, linesize, buffer, remaining,
Expand Down
8 changes: 8 additions & 0 deletions include/nuttx/cancelpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@

#include <stdbool.h>

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define CANCEL_FLAG_NONCANCELABLE (1 << 0) /* Pthread is non-cancelable */
#define CANCEL_FLAG_CANCEL_ASYNC (1 << 1) /* Async (vs deferred) cancellation type */
#define CANCEL_FLAG_CANCEL_PENDING (1 << 2) /* Pthread cancel is pending */

/****************************************************************************
* Public Function Prototypes
****************************************************************************/
Expand Down
25 changes: 10 additions & 15 deletions include/nuttx/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,20 @@
# 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_NONCANCELABLE (1 << 2) /* Bit 2: Pthread is non-cancelable */
#define TCB_FLAG_CANCEL_DEFERRED (1 << 3) /* Bit 3: Deferred (vs asynch) cancellation type */
#define TCB_FLAG_CANCEL_PENDING (1 << 4) /* Bit 4: Pthread cancel is pending */
#define TCB_FLAG_POLICY_SHIFT (5) /* Bit 5-6: Scheduling policy */
#define TCB_FLAG_POLICY_SHIFT (3) /* Bit 3-4: 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_CPU_LOCKED (1 << 8) /* Bit 7: Locked to this CPU */
#define TCB_FLAG_SIGNAL_ACTION (1 << 9) /* Bit 8: In a signal handler */
#define TCB_FLAG_SYSCALL (1 << 10) /* Bit 9: In a system call */
#define TCB_FLAG_EXIT_PROCESSING (1 << 11) /* Bit 10: Exitting */
#define TCB_FLAG_FREE_STACK (1 << 12) /* Bit 12: Free stack after exit */
#define TCB_FLAG_HEAP_CHECK (1 << 13) /* Bit 13: Heap check */
#define TCB_FLAG_HEAP_DUMP (1 << 14) /* Bit 14: Heap dump */
#define TCB_FLAG_DETACHED (1 << 15) /* Bit 15: Pthread detached */
#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: Exitting */
#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_HEAP_DUMP (1 << 11) /* Bit 11: Heap dump */
#define TCB_FLAG_DETACHED (1 << 12) /* Bit 12: Pthread detached */
#define TCB_FLAG_FORCED_CANCEL (1 << 13) /* Bit 13: Pthread cancel is forced */

/* Values for struct task_group tg_flags */

Expand Down Expand Up @@ -572,9 +570,6 @@ struct tcb_s
int16_t lockcount; /* 0=preemptible (not-locked) */
#ifdef CONFIG_IRQCOUNT
int16_t irqcount; /* 0=Not in critical section */
#endif
#ifdef CONFIG_CANCELLATION_POINTS
int16_t cpcount; /* Nested cancellation point count */
#endif
int16_t errcode; /* Used to pass error information */

Expand Down
27 changes: 26 additions & 1 deletion include/nuttx/tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ struct tls_info_s
struct pthread_cleanup_s tl_stack[CONFIG_PTHREAD_CLEANUP_STACKSIZE];
#endif

uint8_t tl_cpstate; /* Cancellation state */

#ifdef CONFIG_CANCELLATION_POINTS
int16_t tl_cpcount; /* Nested cancellation point count */
#endif

int tl_errno; /* Per-thread error number */
};

Expand Down Expand Up @@ -310,9 +316,28 @@ uintptr_t task_tls_get_value(int tlsindex);
#elif defined(CONFIG_TLS_ALIGNED) && !defined(__KERNEL__)
# define tls_get_info() TLS_INFO(up_getsp())
#else
FAR struct tls_info_s *tls_get_info(void);
# define tls_get_info() tls_get_info_pid(0)
#endif

/****************************************************************************
* Name: tls_get_info_pid
*
* Description:
* Return a reference to the tls_info_s structure. This is used as part
* of the internal implementation of tls_get/set_elem() and ONLY for the
* where CONFIG_TLS_ALIGNED is *not* defined or __KERNEL__ is defined.
*
* Input Parameters:
* pid - Thread ID to query, set to 0 to query own
*
* Returned Value:
* A reference to the thread-specific tls_info_s structure is return on
* success. NULL would be returned in the event of any failure.
*
****************************************************************************/

FAR struct tls_info_s *tls_get_info_pid(pid_t pid);

/****************************************************************************
* Name: tls_destruct
*
Expand Down
7 changes: 0 additions & 7 deletions include/sys/syscall_lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ SYSCALL_LOOKUP(sem_wait, 1)
SYSCALL_LOOKUP(pgalloc, 2)
#endif

SYSCALL_LOOKUP(task_setcancelstate, 2)

#ifdef CONFIG_CANCELLATION_POINTS
SYSCALL_LOOKUP(task_setcanceltype, 2)
SYSCALL_LOOKUP(task_testcancel, 0)
#endif

/* The following can be individually enabled */

#ifdef CONFIG_ARCH_HAVE_FORK
Expand Down
10 changes: 5 additions & 5 deletions libs/libc/sched/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ set(SRCS
clock_timespec_add.c
clock_timespec_subtract.c
clock_getcpuclockid.c
clock_getres.c)

if(NOT CONFIG_CANCELLATION_POINTS)
list(APPEND SRCS task_setcanceltype.c task_testcancel.c)
endif()
clock_getres.c
task_cancelpt.c
task_setcancelstate.c
task_setcanceltype.c
task_testcancel.c)

if(CONFIG_SMP)
list(APPEND SRCS sched_cpucount.c)
Expand Down
6 changes: 2 additions & 4 deletions libs/libc/sched/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ CSRCS += sched_getprioritymax.c sched_getprioritymin.c
CSRCS += clock_ticks2time.c clock_time2ticks.c
CSRCS += clock_timespec_add.c clock_timespec_subtract.c
CSRCS += clock_getcpuclockid.c clock_getres.c

ifneq ($(CONFIG_CANCELLATION_POINTS),y)
CSRCS += task_setcanceltype.c task_testcancel.c
endif
CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c
CSRCS += task_testcancel.c

ifeq ($(CONFIG_SMP),y)
CSRCS += sched_cpucount.c
Expand Down
Loading

0 comments on commit 0dedbcd

Please sign in to comment.