1// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2// UNSUPPORTED: tvos, watchos
3// XFAIL: ios && !iossim
4#include "sanitizer_common/sanitizer_ucontext.h"
5#include "test.h"
6
7char stack[64 * 1024] __attribute__((aligned(16)));
8
9ucontext_t uc, orig_uc[2];
10void *fiber, *orig_fiber[2];
11
12const unsigned N = 1000;
13
14__attribute__((noinline))
15void switch0() {
16 __tsan_switch_to_fiber(fiber: orig_fiber[0], flags: 0);
17 swapcontext(&uc, &orig_uc[0]);
18}
19
20void func() {
21 for (;;) {
22 switch0();
23 __tsan_switch_to_fiber(fiber: orig_fiber[1], flags: 0);
24 swapcontext(&uc, &orig_uc[1]);
25 }
26}
27
28void *Thread(void *x) {
29 orig_fiber[1] = __tsan_get_current_fiber();
30 for (unsigned i = 0; i < N; i++) {
31 barrier_wait(barrier: &barrier);
32 __tsan_switch_to_fiber(fiber, flags: 0);
33 swapcontext(&orig_uc[1], &uc);
34 barrier_wait(barrier: &barrier);
35 }
36 return 0;
37}
38
39int main() {
40 fiber = __tsan_create_fiber(flags: 0);
41 barrier_init(barrier: &barrier, count: 2);
42 pthread_t t;
43 pthread_create(newthread: &t, attr: 0, start_routine: Thread, arg: 0);
44 orig_fiber[0] = __tsan_get_current_fiber();
45 getcontext(&uc);
46 uc.uc_stack.ss_sp = stack;
47 uc.uc_stack.ss_size = sizeof(stack);
48 uc.uc_link = 0;
49 makecontext(&uc, func, 0);
50 for (unsigned i = 0; i < N; i++) {
51 __tsan_switch_to_fiber(fiber, flags: 0);
52 swapcontext(&orig_uc[0], &uc);
53 barrier_wait(barrier: &barrier);
54 barrier_wait(barrier: &barrier);
55 }
56 pthread_join(th: t, thread_return: 0);
57 __tsan_destroy_fiber(fiber);
58 fprintf(stderr, format: "PASS\n");
59 return 0;
60}
61
62// CHECK-NOT: WARNING: ThreadSanitizer:
63// CHECK: PASS
64

source code of compiler-rt/test/tsan/fiber_two_threads.cpp