Skip to content

Commit

Permalink
pthread: add pthread_self/pthread_gettid_np function
Browse files Browse the repository at this point in the history
explicitly defined functions can support assignment as function pointers

Signed-off-by: zhanghongyu <[email protected]>
  • Loading branch information
zhhyu7 committed Jun 26, 2024
1 parent c3b05bd commit 199f40d
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
4 changes: 2 additions & 2 deletions include/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */

Expand Down
4 changes: 3 additions & 1 deletion libs/libc/pthread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions libs/libc/pthread/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions libs/libc/pthread/pthread_gettid_np.c
Original file line number Diff line number Diff line change
@@ -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 <pthread.h>

/****************************************************************************
* 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;
}
48 changes: 48 additions & 0 deletions libs/libc/pthread/pthread_self.c
Original file line number Diff line number Diff line change
@@ -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 <pthread.h>

/****************************************************************************
* 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();
}

0 comments on commit 199f40d

Please sign in to comment.