提交 7d383b13 编写于 作者: 些猜's avatar 些猜

新增API pthread_gettid()

Signed-off-by: 些猜's avatarcaifuzhou <504631861@qq.com>
上级 93285f22
#include <pthread.h>
#include <string.h>
#include "test.h"
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
static pthread_mutex_t mutex ;
void* pthread_test(void* arg)
{
pthread_mutex_lock(&mutex);
*((pid_t *)arg) = gettid();
pthread_mutex_unlock(&mutex);
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_mutex_init(&mutex, NULL);
TEST(gettid() == pthread_gettid(pthread_self()),"pthread_gettid() is failed\n");
pthread_mutex_lock(&mutex);
pid_t tid;
pthread_t t;
pthread_create(&t,NULL,pthread_test,&tid);
pid_t recv_result = pthread_gettid(t);
TEST(0 == pthread_join(t,NULL),"pthread_join is failed\n");
pthread_mutex_unlock(&mutex);
TEST(tid == recv_result,"the tid of pthread or tid of pthread_gettid is wrong\n");
return 0;
}
\ No newline at end of file
......@@ -23,6 +23,7 @@ functional_list = [
"pthread_cancel-points",
"pthread_cancel",
"pthread_cond",
"pthread_gettid",
"pthread_mutex",
"pthread_mutex_pi",
"pthread_robust",
......
......@@ -96,6 +96,7 @@ int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void
int pthread_detach(pthread_t);
_Noreturn void pthread_exit(void *);
int pthread_join(pthread_t, void **);
pid_t pthread_gettid(pthread_t);
#ifdef __GNUC__
__attribute__((const))
......
......@@ -183,6 +183,7 @@ hidden void __inhibit_ptc(void);
hidden void __tl_lock(void);
hidden void __tl_unlock(void);
hidden void __tl_sync(pthread_t);
hidden struct pthread* __pthread_list_find(pthread_t, const char*);
extern hidden volatile int __thread_list_lock;
......
......@@ -6,6 +6,15 @@
#include <sys/mman.h>
#include <string.h>
#include <stddef.h>
#include <stdarg.h>
void log_print(const char* info,...)
{
va_list ap;
va_start(ap, info);
vfprintf(stdout,info, ap);
va_end(ap);
}
static void dummy_0()
{
......@@ -380,3 +389,33 @@ fail:
weak_alias(__pthread_exit, pthread_exit);
weak_alias(__pthread_create, pthread_create);
struct pthread* __pthread_list_find(pthread_t thread_id, const char* info)
{
struct pthread *thread = (struct pthread *)thread_id;
if (NULL == thread) {
log_print("invalid pthread_t (0) passed to %s\n", info);
return NULL;
}
struct pthread *self = __pthread_self();
if (thread == self) {
return thread;
}
struct pthread *t = self;
t = t->next ;
while (t != self) {
if (t == thread) return thread;
t = t->next ;
}
return NULL;
}
pid_t __pthread_gettid(pthread_t t)
{
__tl_lock();
struct pthread* thread = __pthread_list_find(t, "pthread_gettid");
__tl_unlock();
return thread ? thread->tid : -1;
}
weak_alias(__pthread_gettid, pthread_gettid);
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册