[PATCH] D32782: Add pthread_self function prototype and make it speculatable.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Sat May 20 16:31:30 PDT 2017


Just before you revert, I did some investigation and found out that
when a thread forks the new process will only have one thread.

http://pubs.opengroup.org/onlinepubs/000095399/functions/fork.html

So, given it's an entire replica of the original thread possibly it
also inherits the thread id?
POSIX doesn't seem to specify so.

On Sat, May 20, 2017 at 4:27 PM, Xin Tong <trent.xin.tong at gmail.com> wrote:
> Let me revert that.  thanks for raising the issue. I will take a closer look.
>
> Thanks
> -Xni
>
> On Sun, May 21, 2017 at 8:21 AM, Davide Italiano <dccitaliano at gmail.com> wrote:
>> On Sat, May 20, 2017 at 3:51 PM, Peter Collingbourne <peter at pcc.me.uk> wrote:
>>> Couldn't pthread_self() return a different value if fork() is called in the
>>> body of the loop?
>>>
>>
>> I was worried about a case where this could break but I wasn't able to
>> find a counterexample.
>> I guess yours holds. FWIW, I'm not sure what's the interaction between
>> fork() and pthread_self() but if fork()'ing can actually change the
>> value, GCC miscompiles the following code. Xin, I recommend to revert
>> this one until we get a better understanding.
>>
>> #include <pthread.h>
>> #include <unistd.h>
>>
>> extern void x(pthread_t);
>>
>> int foo(int n) {
>>    for (int i = 0; i < n; i++) {
>>     auto id = pthread_self();
>>     x(id);
>>     fork();
>>   }
>> }
>> foo(int):
>>         test    edi, edi
>>         jle     .L6
>>         push    r12
>>         push    rbp
>>         mov     ebp, edi
>>         push    rbx
>>         xor     ebx, ebx
>>         call    pthread_self
>>         mov     r12, rax
>> .L3:
>>         mov     rdi, r12
>>         add     ebx, 1
>>         call    x(unsigned long)
>>         call    fork
>>         cmp     ebp, ebx
>>         jne     .L3
>>         pop     rbx
>>         pop     rbp
>>         pop     r12
>>         ret
>> .L6:
>>         rep ret
>
>
>
> --
> Software Engineer - Compiler Toolchain
> Employee of Facebook Inc.


More information about the llvm-commits mailing list