记录一下最近看android bionic pthread相关源码的理解
这里以android kikat bionic为例
先看一下pthread_create函数源码
int pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
void* (*start_routine)(void*), void* arg) {
ErrnoRestorer errno_restorer;
// Inform the rest of the C library that at least one thread
// was created. This will enforce certain functions to acquire/release
// locks (e.g. atexit()) to protect shared global structures.
// This works because pthread_create() is not called by the C library
// initialization routine that sets up the main thread's data structures.
__isthreaded = 1;
pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(calloc(sizeof(*thread), 1)); 使用calloc创建thread结构体,初始化为0
if (thread == NULL) {
__libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: couldn't allocate thread");
return EAGAIN;
}
thread->allocated_on_heap = true; 标记thread结构体是在heap上创建的,也提示了之后不使用的时候要用free去销毁
if (attr == NULL) {
pthread_attr_init(&thread->attr); 如果创建线程没有指定attr则使用default的attr
} else {
thread->attr = *attr; 如果有指定attr,则使用指定的attr
attr = NULL; // Prevent misuse below.
}
// Make sure the stack size and guard size are multiples of PAGE_SIZE.
thread->attr.stack_size = (thread->attr.stack_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
thread->attr.guard_size = (thread->attr.guard_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
if (thread->attr.stack_base == NULL) {
// The caller didn't provide a stack, so allocate one.
thread->attr.stack_base = __create_thread_stack(thread); 如果没有指定attr的stack_base,那么自己创建一个
if (thread->attr.stack_base == NULL) {
free(thread);
return EAGAIN;
}
} else {
// The caller did provide a stack, so remember we're not supposed to free it.
thread->attr.flags |= PTHREAD_ATTR_FLAG_USER_STACK; 指定一个flag,之后由user自己释放这块地址
}
// Make room for the TLS area.
// The child stack is the same address, just growing in the opposite direction.
// At offsets >= 0, we have the TLS slots.
// At offsets < 0, we have the child stack.
void** tls = (void**)((uint8_t*)(thread->attr.stack_base) + thread->attr.stack_size - BIONIC_TLS_SLOTS * sizeof(void*));
void* child_stack = tls;
// Create a mutex for the thread in TLS_SLOT_SELF to wait on once it starts so we can keep
// it from doing anything until after we notify the debugger about it
//
// This also provides the memory barrier we need to ensure that all
// memory accesses previously performed by this thread are visible to
// the new thread.
pthread_mutex_t* start_mutex = (pthread_mutex_t*) &tls[TLS_SLOT_SELF];
pthread_mutex_init(start_mutex, NULL);
ScopedPthreadMutexLocker start_locker(start_mutex);
tls[TLS_SLOT_THREAD_ID] = thread; TLS数组的TLS_SLOT_THREAD_ID index保存thread结构体,后面其他线程函数可以通过从TLS拿到当前线程thread结构体
int flags = CLONE_FILES | CLONE_FS | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM;
int tid = __pthread_clone(start_routine, child_stack, flags, arg); 使用clone系统调用,指定stack的起始地址为TLS之后
if (tid < 0) {
int clone_errno = errno;
if ((thread->attr.flags & PTHREAD_ATTR_FLAG_USER_STACK) == 0) {
munmap(thread->attr.stack_base, thread->attr.stack_size);
}
free(thread);
__libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: clone failed: %s", strerror(errno));
return clone_errno;
}
thread->tid = tid;
int init_errno = _init_thread(thread, true);
if (init_errno != 0) {
// Mark the thread detached and let its __thread_entry run to
// completion. (It'll just exit immediately, cleaning up its resources.)
thread->internal_flags |= kPthreadInitFailed;
thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED;
return init_errno;
}
// Notify any debuggers about the new thread.
{
ScopedPthreadMutexLocker debugger_locker(&gDebuggerNotificationLock);
_thread_created_hook(thread->tid);
}
// Publish the pthread_t and let the thread run.
*thread_out = (pthread_t) thread;
return 0;
}
看一下pthread_attr_init函数指定的default值
#define DEFAULT_THREAD_STACK_SIZE ((1 * 1024 * 1024) - SIGSTKSZ)
int pthread_attr_init(pthread_attr_t* attr) {
attr->flags = 0;
attr->stack_base = NULL;
attr->stack_size = DEFAULT_THREAD_STACK_SIZE;
attr->guard_size = PAGE_SIZE;
attr->sched_policy = SCHED_NORMAL;
attr->sched_priority = 0;
return 0;
}
再看一下__create_thread_stack函数实现
static void* __create_thread_stack(pthread_internal_t* thread) {
ScopedPthreadMutexLocker lock(&gPthreadStackCreationLock);
// Create a new private anonymous map.
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
void* stack = mmap(NULL, thread->attr.stack_size, prot, flags, -1, 0); 使用mmap映射一块大小为stack_size的虚拟地址,这块内存是可读可写的,他是匿名映射,也就是不对应具体的文件,他是私有的,也就是每个进程/线程独有的
if (stack == MAP_FAILED) {
__libc_format_log(ANDROID_LOG_WARN,
"libc",
"pthread_create failed: couldn't allocate %zd-byte stack: %s",
thread->attr.stack_size, strerror(errno));
return NULL;
}
// Set the guard region at the end of the stack to PROT_NONE.
if (mprotect(stack, thread->attr.guard_size, PROT_NONE) == -1) {
__libc_format_log(ANDROID_LOG_WARN, "libc",
"pthread_create failed: couldn't mprotect PROT_NONE %zd-byte stack guard region: %s",
thread->attr.guard_size, strerror(errno));
munmap(stack, thread->attr.stack_size);
return NULL;
}
return stack;
}
最后看一下pthread_exit函数
void pthread_exit(void * retval)
{
pthread_internal_t* thread = __get_thread(); 通过TLS的TLS_SLOT_THREAD_ID index获取当前线程的thread结构,大概思路是之前pthread_create mmap匿名创建那一块内存,栈顶之上一部分是TLS区域,所以可以很容易通过栈底的寄存器拿到TLS的首地址
void* stack_base = thread->attr.stack_base;
int stack_size = thread->attr.stack_size;
int user_stack = (thread->attr.flags & PTHREAD_ATTR_FLAG_USER_STACK) != 0;
sigset_t mask;
// call the cleanup handlers first
while (thread->cleanup_stack) { 如果user有指定cleanup_stack的方法则先调用它
__pthread_cleanup_t* c = thread->cleanup_stack;
thread->cleanup_stack = c->__cleanup_prev;
c->__cleanup_routine(c->__cleanup_arg);
}
// call the TLS destructors, it is important to do that before removing this
// thread from the global list. this will ensure that if someone else deletes
// a TLS key, the corresponding value will be set to NULL in this thread's TLS
// space (see pthread_key_delete)
pthread_key_clean_all();
if (thread->alternate_signal_stack != NULL) {
// Tell the kernel to stop using the alternate signal stack.
stack_t ss;
ss.ss_sp = NULL;
ss.ss_flags = SS_DISABLE;
sigaltstack(&ss, NULL);
// Free it.
munmap(thread->alternate_signal_stack, SIGSTKSZ);
thread->alternate_signal_stack = NULL;
}
// if the thread is detached, destroy the pthread_internal_t
// otherwise, keep it in memory and signal any joiners.
pthread_mutex_lock(&gThreadListLock);
if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) { 如果是detached,会将本线程创建在heap上的thread结构体free
_pthread_internal_remove_locked(thread);
} else {
/* make sure that the thread struct doesn't have stale pointers to a stack that
* will be unmapped after the exit call below.
*/
if (!user_stack) {
thread->attr.stack_base = NULL;
thread->attr.stack_size = 0;
thread->tls = NULL;
}
/* Indicate that the thread has exited for joining threads. */
thread->attr.flags |= PTHREAD_ATTR_FLAG_ZOMBIE;
thread->return_value = retval;
/* Signal the joining thread if present. */
if (thread->attr.flags & PTHREAD_ATTR_FLAG_JOINED) { 如果是joined,会通过pthread_cond_signal唤醒监听当前thread的pthread_join函数
pthread_cond_signal(&thread->join_cond); 会通过futex_wake系统调用唤醒睡眠的程序
}
}
pthread_mutex_unlock(&gThreadListLock);
sigfillset(&mask);
sigdelset(&mask, SIGSEGV);
(void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
// destroy the thread stack
if (user_stack) 如果是user自己指定的stack_base,则user自己管理
_exit_thread((int)retval);
else
_exit_with_stack_teardown(stack_base, stack_size, (int)retval); 如果是自己创建的stack,那么调用munmap系统调用取消这块内存的匿名映射
}
那么如何指定detached呢?
int pthread_detach(pthread_t t) {
pthread_accessor thread(t);
if (thread.get() == NULL) {
return ESRCH;
}
if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) {
return EINVAL; // Already detached.
}
if (thread->attr.flags & PTHREAD_ATTR_FLAG_JOINED) {
return 0; // Already being joined; silently do nothing, like glibc.
}
thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED; 指定为detached
return 0;
}
那么如何指定joined呢?
int pthread_join(pthread_t t, void** ret_val) {
if (t == pthread_self()) {
return EDEADLK;
}
pthread_accessor thread(t);
if (thread.get() == NULL) {
return ESRCH;
}
if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) {
return EINVAL;
}
if (thread->attr.flags & PTHREAD_ATTR_FLAG_JOINED) {
return EINVAL;
}
// Signal our intention to join, and wait for the thread to exit.
thread->attr.flags |= PTHREAD_ATTR_FLAG_JOINED; 指定为joined
while ((thread->attr.flags & PTHREAD_ATTR_FLAG_ZOMBIE) == 0) {
pthread_cond_wait(&thread->join_cond, &gThreadListLock); 会通过fetux_wait进入睡眠
}
if (ret_val) {
*ret_val = thread->return_value;
}
_pthread_internal_remove_locked(thread.get()); 销毁当前thread的thread结构体
return 0;
}
本文深入探讨了Android Kikat Bionic的pthread相关源码,主要涉及pthread_create函数、pthread_attr_init函数的默认设置、__create_thread_stack函数的实现以及pthread_exit函数的工作原理。同时,阐述了如何设定线程为detached和joinable状态。

1865

被折叠的 条评论
为什么被折叠?



