From e14ae3e6812e4ddb8fb42cb6e9141f906bb139d8 Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Mon, 17 Jun 2024 17:27:14 +0800 Subject: [PATCH] pthread: add pthread_self/pthread_gettid_np function explicitly defined functions can support assignment as function pointers Signed-off-by: zhanghongyu --- include/pthread.h | 4 +-- libs/libc/libc.csv | 2 ++ libs/libc/pthread/CMakeLists.txt | 4 ++- libs/libc/pthread/Make.defs | 1 + libs/libc/pthread/pthread_gettid_np.c | 48 +++++++++++++++++++++++++++ libs/libc/pthread/pthread_self.c | 48 +++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 libs/libc/pthread/pthread_gettid_np.c create mode 100644 libs/libc/pthread/pthread_self.c diff --git a/include/pthread.h b/include/pthread.h index b9fc93ae0dd1d..99c5f6a21c953 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -563,8 +563,8 @@ void pthread_yield(void); /* A thread may obtain a copy of its own thread handle. */ -#define pthread_self() ((pthread_t)gettid()) -#define pthread_gettid_np(thread) ((pid_t)(thread)) +pthread_t pthread_self(void); +pid_t pthread_gettid_np(pthread_t thread); /* Compare two thread IDs. */ diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv index 3623e7ff62525..097a95e8fe3f8 100644 --- a/libs/libc/libc.csv +++ b/libs/libc/libc.csv @@ -214,6 +214,7 @@ "pthread_create","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_t *","FAR const pthread_attr_t *","pthread_startroutine_t","pthread_addr_t" "pthread_getname_np","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t","char *","size_t" "pthread_getspecific","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && CONFIG_TLS_NELEM > 0","FAR void *","pthread_key_t" +"pthread_gettid_np","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","pid_t","pthread_t" "pthread_key_create","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && CONFIG_TLS_NELEM > 0","int","FAR pthread_key_t *","FAR void (*) (void *)|FAR void *" "pthread_key_delete","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && CONFIG_TLS_NELEM > 0","int","pthread_key_t" "pthread_mutex_lock","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_mutex_t *" @@ -228,6 +229,7 @@ "pthread_rwlock_init","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_rwlock_t *restrict","FAR const pthread_rwlockattr_t *" "pthread_rwlock_rdlock","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_rwlock_t *" "pthread_rwlock_unlock","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_rwlock_t *" +"pthread_self","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","pthread_t","void" "pthread_setname_np","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t","const char *" "pthread_setspecific","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && CONFIG_TLS_NELEM > 0","int","pthread_key_t","FAR const void *" "pthread_yield","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","void" diff --git a/libs/libc/pthread/CMakeLists.txt b/libs/libc/pthread/CMakeLists.txt index c6d8a923318eb..43d5d6babbd36 100644 --- a/libs/libc/pthread/CMakeLists.txt +++ b/libs/libc/pthread/CMakeLists.txt @@ -93,7 +93,9 @@ if(NOT CONFIG_DISABLE_PTHREAD) pthread_setcancelstate.c pthread_setcanceltype.c pthread_testcancel.c - pthread_getcpuclockid.c) + pthread_getcpuclockid.c + pthread_self.c + pthread_gettid_np.c) if(CONFIG_SMP) list(APPEND SRCS pthread_attr_getaffinity.c pthread_attr_setaffinity.c) diff --git a/libs/libc/pthread/Make.defs b/libs/libc/pthread/Make.defs index b996bcdbd6555..5ae7667650e79 100644 --- a/libs/libc/pthread/Make.defs +++ b/libs/libc/pthread/Make.defs @@ -58,6 +58,7 @@ CSRCS += pthread_rwlockattr_getpshared.c pthread_rwlockattr_setpshared.c CSRCS += pthread_rwlock.c pthread_rwlock_rdlock.c pthread_rwlock_wrlock.c CSRCS += pthread_setcancelstate.c pthread_setcanceltype.c CSRCS += pthread_testcancel.c pthread_getcpuclockid.c +CSRCS += pthread_self.c pthread_gettid_np.c ifeq ($(CONFIG_SMP),y) CSRCS += pthread_attr_getaffinity.c pthread_attr_setaffinity.c diff --git a/libs/libc/pthread/pthread_gettid_np.c b/libs/libc/pthread/pthread_gettid_np.c new file mode 100644 index 0000000000000..7199f5112b387 --- /dev/null +++ b/libs/libc/pthread/pthread_gettid_np.c @@ -0,0 +1,48 @@ +/**************************************************************************** + * libs/libc/pthread/pthread_gettid_np.c + * + * 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 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pthread_gettid_np + * + * Description: + * Return the thread ID of the specified thread. + * + * Input Parameters: + * thread - The thread for which to return the thread ID + * + * Returned Value: + * The ID of the thread. + * + ****************************************************************************/ + +pid_t pthread_gettid_np(pthread_t thread) +{ + return (pid_t)thread; +} diff --git a/libs/libc/pthread/pthread_self.c b/libs/libc/pthread/pthread_self.c new file mode 100644 index 0000000000000..3b7c102e09ea1 --- /dev/null +++ b/libs/libc/pthread/pthread_self.c @@ -0,0 +1,48 @@ +/**************************************************************************** + * libs/libc/pthread/pthread_self.c + * + * 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 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pthread_self + * + * Description: + * Return the thread ID of the calling thread. + * + * Input Parameters: + * None + * + * Returned Value: + * ID of the calling thread. + * + ****************************************************************************/ + +pthread_t pthread_self(void) +{ + return (pthread_t)gettid(); +}