1 //===-- Test handling of thread local data --------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "src/__support/threads/thread.h"
10 #include "test/IntegrationTest/test.h"
11
12 static constexpr int INIT_VAL = 100;
13 static constexpr int UPDATE_VAL = 123;
14
15 static thread_local int tlval = INIT_VAL;
16
17 // This function updates the tlval and returns its old value.
func(void *)18 int func(void *) {
19 int old_tlval = tlval;
20 tlval = UPDATE_VAL;
21 return old_tlval;
22 }
23
thread_local_test()24 void thread_local_test() {
25 int retval;
26
27 LIBC_NAMESPACE::Thread th1;
28 th1.run(func, nullptr, nullptr, 0);
29 th1.join(&retval);
30 ASSERT_EQ(retval, INIT_VAL);
31
32 LIBC_NAMESPACE::Thread th2;
33 th2.run(func, nullptr, nullptr, 0);
34 th2.join(&retval);
35 ASSERT_EQ(retval, INIT_VAL);
36 }
37
TEST_MAIN()38 TEST_MAIN() {
39 // From the main thread, we will update the main thread's tlval.
40 // This should not affect the child thread's tlval;
41 ASSERT_EQ(tlval, INIT_VAL);
42 tlval = UPDATE_VAL;
43 ASSERT_EQ(tlval, UPDATE_VAL);
44 thread_local_test();
45 return 0;
46 }
47