<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - pthread with ucontext on mac"
   href="https://llvm.org/bugs/show_bug.cgi?id=23154">23154</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>pthread with ucontext on mac
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>albertnetymk@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I am using ucontext along with pthread. The below program works OK on Linux,
but has failed assertion on Mac.

The problem seems that thread local variables are not correctly accessed after
resuming the context from another thread.

The program creates two threads, A and B. A sets the context ready before B
could resume the context, for it's synced properly.

It's very appreciated if someone could shed some light on this behavior on mac.

ENV:

```
clang version 3.7.0  (<a href="http://llvm.org/git/llvm.git">http://llvm.org/git/llvm.git</a>
8d70064a4ac2ae09b8003173e751cfad9dc15400)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
```
Program:

```
#define _XOPEN_SOURCE 800
#include <ucontext.h>
#include <signal.h>

#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <assert.h>

static int flag = 0;

void swap(ucontext_t *old, ucontext_t *new)
{
    int ret = swapcontext(old, new);
    assert(ret == 0);
}

#define SSIZE MINSIGSTKSZ

static char stack[SSIZE];
static ucontext_t a_ctx[2];
static ucontext_t b_ctx[2];

volatile static __thread int bug = 0;

static void func(int b) { }

static void f1 (void)
{
    assert(bug == 0);
    func(bug);
    swap(&a_ctx[1], &a_ctx[0]);
    assert(bug == 1);
}

void *thread_a(void *arg)
{
    printf("A is %lu\n", pthread_self());

    bug = 0;

    ucontext_t ctx = a_ctx[1];

    getcontext(&ctx);
    ctx.uc_stack.ss_sp = stack;
    ctx.uc_stack.ss_size = sizeof stack;
    makecontext(&ctx, f1, 0);

    swap(&a_ctx[0], &ctx);
    __atomic_store_n(&flag, 1, __ATOMIC_RELAXED);
    sleep(1);

    return NULL;
}

void *thread_b(void *arg)
{
    printf("B is %lu\n", pthread_self());

    bug = 1;
    while(__atomic_load_n(&flag, __ATOMIC_RELAXED) == 0) ;
    swap(&b_ctx[0], &a_ctx[1]);

    return NULL;
}

int main(int argc, char **argv)
{
    pthread_t a, b;
    pthread_create(&b, NULL, &thread_b, NULL);
    pthread_create(&a, NULL, &thread_a, NULL);
    pthread_exit(NULL);
}
```

I posted it on
[SO](<a href="http://stackoverflow.com/questions/29430932/clang-bug-on-pthread-with-ucontext-on-mac">http://stackoverflow.com/questions/29430932/clang-bug-on-pthread-with-ucontext-on-mac</a>)
as well, but no real answers so far.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>