【发布时间】:2014-07-04 05:37:57
【问题描述】:
简短版问题:如果我想为我正在创建的线程分配一个新的 TLS 区域,我需要将什么参数传递给 x86_64 Linux 系统上的clone 系统调用。
加长版:
我正在做一个研究项目,对于我正在试验的东西,我想使用clone 系统调用而不是pthread_create 创建线程。但是,我也希望能够使用线程本地存储。我现在不打算创建很多线程,所以我可以为使用 clone 系统调用创建的每个线程创建一个新的 TLS 区域。
我正在查看 clone 的手册页,其中包含有关 TLS 参数标志的以下信息:
CLONE_SETTLS (since Linux 2.5.32)
The newtls argument is the new TLS (Thread Local Storage) descriptor.
(See set_thread_area(2).)
所以我查看了set_thread_area 的手册页,并注意到以下内容看起来很有希望:
When set_thread_area() is passed an entry_number of -1, it uses a
free TLS entry. If set_thread_area() finds a free TLS entry, the value of
u_info->entry_number is set upon return to show which entry was changed.
但是,在尝试了一些之后,我的系统中似乎没有实现set_thread_area(x86_64 平台上的 Ubunut 10.04)。当我运行以下代码时,我收到一条错误消息:set_thread_area() failed: Function not implemented
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <linux/unistd.h>
#include <asm/ldt.h>
int main()
{
struct user_desc u_info;
u_info.entry_number = -1;
int rc = syscall(SYS_set_thread_area,&u_info);
if(rc < 0) {
perror("set_thread_area() failed");
exit(-1);
}
printf("entry_number is %d",u_info.entry_number);
}
我还看到,当我使用 strace 时,看看调用 pthread_create 时会发生什么,我没有看到对 set_thread_area 的任何调用。我也一直在查看 nptl pthread 源代码,试图了解它们在创建线程时做了什么。但我还没有完全理解它,我认为它比我想要做的更复杂,因为我不需要在 pthread 实现中那么健壮的东西。我假设set_thread_area 系统调用适用于x86,并且x86_64 使用了不同的机制。但目前我无法弄清楚它是什么,所以我希望这个问题能帮助我对我需要查看的内容有所了解。
【问题讨论】:
标签: linux pthreads system-calls