diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h index 3298959a48..4646ba5cef 100644 --- a/include/nuttx/tls.h +++ b/include/nuttx/tls.h @@ -223,6 +223,7 @@ struct tls_info_s uint16_t tl_size; /* Actual size with alignments */ int tl_errno; /* Per-thread error number */ + pid_t tl_tid; /* Thread ID */ }; /**************************************************************************** diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h index ea66bc0a87..63bfb57b1c 100644 --- a/include/sys/syscall_lookup.h +++ b/include/sys/syscall_lookup.h @@ -29,7 +29,6 @@ SYSCALL_LOOKUP1(_exit, 1) SYSCALL_LOOKUP(_assert, 4) SYSCALL_LOOKUP(getpid, 0) -SYSCALL_LOOKUP(gettid, 0) SYSCALL_LOOKUP(prctl, 2) #ifdef CONFIG_SCHED_HAVE_PARENT diff --git a/libs/libc/sched/CMakeLists.txt b/libs/libc/sched/CMakeLists.txt index 6f1fa76bac..c447e575f2 100644 --- a/libs/libc/sched/CMakeLists.txt +++ b/libs/libc/sched/CMakeLists.txt @@ -28,7 +28,8 @@ set(SRCS task_cancelpt.c task_setcancelstate.c task_setcanceltype.c - task_testcancel.c) + task_testcancel.c + task_gettid.c) if(CONFIG_SMP) list(APPEND SRCS sched_cpucount.c) diff --git a/libs/libc/sched/Make.defs b/libs/libc/sched/Make.defs index da8588d1fa..aa164356dd 100644 --- a/libs/libc/sched/Make.defs +++ b/libs/libc/sched/Make.defs @@ -25,7 +25,7 @@ CSRCS += sched_getprioritymax.c sched_getprioritymin.c CSRCS += clock_getcpuclockid.c clock_getres.c CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c -CSRCS += task_testcancel.c +CSRCS += task_testcancel.c task_gettid.c ifeq ($(CONFIG_SMP),y) CSRCS += sched_cpucount.c diff --git a/libs/libc/sched/task_gettid.c b/libs/libc/sched/task_gettid.c new file mode 100644 index 0000000000..33abbcc2e3 --- /dev/null +++ b/libs/libc/sched/task_gettid.c @@ -0,0 +1,56 @@ +/**************************************************************************** + * libs/libc/sched/task_gettid.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: gettid + * + * Description: + * Get the thread ID of the currently executing thread. + * + * Input parameters: + * None + * + * Returned Value: + * On success, returns the thread ID of the calling process. + * + ****************************************************************************/ + +pid_t gettid(void) +{ + FAR struct tls_info_s *tls = tls_get_info(); + return tls->tl_tid; +} diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c index 80d74fec50..227266cba7 100644 --- a/sched/pthread/pthread_create.c +++ b/sched/pthread/pthread_create.c @@ -281,15 +281,6 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread, } #endif - /* Initialize thread local storage */ - - ret = tls_init_info(&ptcb->cmn); - if (ret != OK) - { - errcode = -ret; - goto errout_with_tcb; - } - /* Should we use the priority and scheduler specified in the pthread * attributes? Or should we use the current thread's priority and * scheduler? @@ -397,6 +388,15 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread, goto errout_with_tcb; } + /* Initialize thread local storage */ + + ret = tls_init_info(&ptcb->cmn); + if (ret != OK) + { + errcode = -ret; + goto errout_with_tcb; + } + #ifdef CONFIG_SMP /* pthread_setup_scheduler() will set the affinity mask by inheriting the * setting from the parent task. We need to override this setting diff --git a/sched/task/task_gettid.c b/sched/task/task_gettid.c index ff1fb663b9..3787de2649 100644 --- a/sched/task/task_gettid.c +++ b/sched/task/task_gettid.c @@ -91,22 +91,3 @@ pid_t nxsched_gettid(void) return 0; } - -/**************************************************************************** - * Name: gettid - * - * Description: - * Get the thread ID of the currently executing thread. - * - * Input parameters: - * None - * - * Returned Value: - * On success, returns the thread ID of the calling process. - * - ****************************************************************************/ - -pid_t gettid(void) -{ - return nxsched_gettid(); -} diff --git a/sched/task/task_init.c b/sched/task/task_init.c index 7d338f53b2..0fa96f9972 100644 --- a/sched/task/task_init.c +++ b/sched/task/task_init.c @@ -168,18 +168,18 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority, goto errout_with_group; } - /* Initialize thread local storage */ + /* Initialize the task control block */ - ret = tls_init_info(&tcb->cmn); + ret = nxtask_setup_scheduler(tcb, priority, nxtask_start, + entry, ttype); if (ret < OK) { goto errout_with_group; } - /* Initialize the task control block */ + /* Initialize thread local storage */ - ret = nxtask_setup_scheduler(tcb, priority, nxtask_start, - entry, ttype); + ret = tls_init_info(&tcb->cmn); if (ret < OK) { goto errout_with_group; diff --git a/sched/tls/tls_initinfo.c b/sched/tls/tls_initinfo.c index 62345e6bd2..bf7227fd1a 100644 --- a/sched/tls/tls_initinfo.c +++ b/sched/tls/tls_initinfo.c @@ -73,5 +73,10 @@ int tls_init_info(FAR struct tcb_s *tcb) /* Attach per-task info in group to TLS */ info->tl_task = tcb->group->tg_info; + + /* Thread ID */ + + info->tl_tid = tcb->pid; + return OK; } diff --git a/syscall/syscall.csv b/syscall/syscall.csv index 91fae362c8..2e3444c132 100644 --- a/syscall/syscall.csv +++ b/syscall/syscall.csv @@ -48,7 +48,6 @@ "getppid","unistd.h","defined(CONFIG_SCHED_HAVE_PARENT)","pid_t" "getsockname","sys/socket.h","defined(CONFIG_NET)","int","int","FAR struct sockaddr *","FAR socklen_t *" "getsockopt","sys/socket.h","defined(CONFIG_NET)","int","int","int","int","FAR void *","FAR socklen_t *" -"gettid","unistd.h","","pid_t" "gettimeofday","sys/time.h","","int","FAR struct timeval *","FAR struct timezone *" "getuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","uid_t" "inotify_add_watch","sys/inotify.h","defined(CONFIG_FS_NOTIFY)","int","int","FAR const char *","uint32_t"