3.4 线程id获取
来源:互联网 发布:python 周期性任务 编辑:程序博客网 时间:2024/05/15 10:03
在debugtool里面会创建收发包线程,然后再把线程号注册到底层驱动。以前一直没弄太明白这里面tid的计算方法,今天研究了一下,终于弄明白了。
1、在我们的程序中,获取线程tid的地方有两个:
- a、一个是在创建线程时,返回一个pthread_t结构的数据。
int pthread_create (pthread_t *newthread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
- b、二是在收发线程内部,调用pthread_self返回一个pthread_t结构的数据。
pthread_t pthread_self (void);
2、但是线程号并不是保存在pthread_t结构里面的,在/usr/include/bits/pthreadtypes.h有pthread_t的定义: typedef unsigned long int pthread_t;
debugtool代码里面对pthread_t和tid之间的转换,用了一下的一个方法:
void *Sendpid_addr;MTP_UINT32 Sendpid;/* 计算PID_T */Sendpid_addr = (void *)pthread_self();printf("send thread func Sendpid_addr = %d\n\r", (MTP_UINT32)Sendpid_addr);Sendpid = (MTP_UINT32)(*(MTP_UINT32*)((unsigned long *)Sendpid_addr + 0x12)); ------开始一直不太理解这里,这个加0x12就得到tid,比较雷。printf("Sendpid = %d\n",Sendpid);
3、经过查资料、研究,问题的本质是这样的:
pthread_t返回的并不是的tid,真正的tid是保存在一个类型为pthread的结构当中的,pthread结构的定义在glibc源代码nptl/descr.h当中,pthread_t返回的只是这个pthread结构的指针。
可能是glibc不想把pthread这个数据结构的内部暴露给用户,所以在用户态程序使用glibc时,并不能包含descr.h,所以只能手工的通过结构体成员的偏移量计算出来。所以才有了0x12这个值的存在。
struct pthread ------------------- pthread_t返回的是这个pthread类型数据结构的基地址。{ union {#if !TLS_DTV_AT_TP /* This overlaps the TCB as used for TLS without threads (see tls.h). */ tcbhead_t header;#else struct { int multiple_threads; } header;#endif /* This extra padding has no special purpose, and this structure layout is private and subject to change without affecting the official ABI. We just have it here in case it might be convenient for some implementation-specific instrumentation hack or suchlike. */ void *__padding[16]; }; /* This descriptor's link on the `stack_used' or `__stack_user' list. */ list_t list; /* Thread ID - which is also a 'is this thread descriptor (and therefore stack) used' flag. */ pid_t tid; -------------------------- 这个成员才是我们真正想要的tid. /* Process ID - thread group ID in kernel speak. */ pid_t pid; /* List of cleanup buffers. */ struct _pthread_cleanup_buffer *cleanup; /* Unwind information. */ struct pthread_unwind_buf *cleanup_jmp_buf;#define HAVE_CLEANUP_JMP_BUF /* Flags determining processing of cancellation. */ int cancelhandling; /* Bit set if cancellation is disabled. */#define CANCELSTATE_BIT 0#define CANCELSTATE_BITMASK 0x01 /* Bit set if asynchronous cancellation mode is selected. */#define CANCELTYPE_BIT 1#define CANCELTYPE_BITMASK 0x02 /* Bit set if canceling has been initiated. */#define CANCELING_BIT 2#define CANCELING_BITMASK 0x04 /* Bit set if canceled. */#define CANCELED_BIT 3#define CANCELED_BITMASK 0x08 /* Bit set if thread is exiting. */#define EXITING_BIT 4#define EXITING_BITMASK 0x10 /* Bit set if thread terminated and TCB is freed. */#define TERMINATED_BIT 5#define TERMINATED_BITMASK 0x20 /* Mask for the rest. Helps the compiler to optimize. */#define CANCEL_RESTMASK 0xffffffc0#define CANCEL_ENABLED_AND_CANCELED(value) \ (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK \ | CANCEL_RESTMASK | TERMINATED_BITMASK)) == CANCELED_BITMASK)#define CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS(value) \ (((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK \ | EXITING_BITMASK | CANCEL_RESTMASK | TERMINATED_BITMASK)) \ == (CANCELTYPE_BITMASK | CANCELED_BITMASK)) /* We allocate one block of references here. This should be enough to avoid allocating any memory dynamically for most applications. */ struct pthread_key_data { /* Sequence number. We use uintptr_t to not require padding on 32- and 64-bit machines. On 64-bit machines it helps to avoid wrapping, too. */ uintptr_t seq; /* Data pointer. */ void *data; } specific_1stblock[PTHREAD_KEY_2NDLEVEL_SIZE]; /* Flag which is set when specific data is set. */ bool specific_used; /* Two-level array for the thread-specific data. */ struct pthread_key_data *specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* True if events must be reported. */ bool report_events; /* True if the user provided the stack. */ bool user_stack; /* True if thread must stop at startup time. */ bool stopped_start; /* Lock to synchronize access to the descriptor. */ lll_lock_t lock;#if HP_TIMING_AVAIL /* Offset of the CPU clock at start thread start time. */ hp_timing_t cpuclock_offset;#endif /* If the thread waits to join another one the ID of the latter is stored here. In case a thread is detached this field contains a pointer of the TCB if the thread itself. This is something which cannot happen in normal operation. */ struct pthread *joinid; /* Check whether a thread is detached. */#define IS_DETACHED(pd) ((pd)->joinid == (pd)) /* Flags. Including those copied from the thread attribute. */ int flags; /* The result of the thread function. */ void *result; /* Scheduling parameters for the new thread. */ struct sched_param schedparam; int schedpolicy; /* Start position of the code to be executed and the argument passed to the function. */ void *(*start_routine) (void *); void *arg; /* Debug state. */ td_eventbuf_t eventbuf; /* Next descriptor with a pending event. */ struct pthread *nextevent;#ifdef HAVE_FORCED_UNWIND /* Machine-specific unwind info. */ struct _Unwind_Exception exc;#endif /* If nonzero pointer to area allocated for the stack and its size. */ void *stackblock; size_t stackblock_size; /* Size of the included guard area. */ size_t guardsize; /* This is what the user specified and what we will report. */ size_t reported_guardsize; /* Resolver state. */ struct __res_state res;}
阅读全文
0 0
- 3.4 线程id获取
- Linux获取线程ID
- 获取线程ID
- 获取线程ID
- 获取线程ID
- Liunx获取线程Id
- linux获取线程ID
- linux下获取线程id
- linux 下获取线程ID
- linux多线程,获取线程ID
- 获取当前进程ID和子线程的线程ID
- gettid()获取线程ID测试程序
- linux 获取线程ID的方法
- 获取进程和线程id函数简介
- linux 获取线程ID的方法
- 获取当前线程的id号 gettid()
- 根据线程ID获取其Handle
- 获取线程id的另一种方法
- Java设计模式_(行为型)_观察者模式
- 【codevs 2451】互不侵犯
- bitset+暴力——校门外的树(增强版)
- shell
- python里面random的使用
- 3.4 线程id获取
- LeetCode 96. Unique Binary Search Trees Python Solution, Catalan数 结点数为n的不同形态的二叉树一共有多少种
- 沪江日语百度云视频 0-n1百度云网盘视频的swf需要特定的播放器才能播放
- [POJ 3321]Apple Tree dfs序+树状数组
- 中文分词之jieba简单应用
- html 中的 header-作用与使用
- 学会这20个正则表达式,少些几千行代码!
- poj2112 二分+网络流
- [BZOJ]4950 二分图 + 最大匹配