3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 /* Test of gpr thread local storage support. */
21 #include "src/core/lib/gpr/tls.h"
25 #include <gtest/gtest.h>
27 #include "src/core/lib/gprpp/thd.h"
28 #include "test/core/util/test_config.h"
30 struct BiggerThanMachineWord {
35 static GPR_THREAD_LOCAL(BiggerThanMachineWord) test_var;
36 // Fails to compile: static GPR_THREAD_LOCAL(std::unique_ptr<char>) non_trivial;
39 void thd_body(void*) {
40 for (size_t i = 0; i < 100000; i++) {
41 BiggerThanMachineWord next = {i, i, uint8_t(i)};
43 BiggerThanMachineWord read = test_var;
46 ASSERT_EQ(read.c, uint8_t(i)) << i;
50 TEST(ThreadLocal, ReadWrite) {
51 std::array<grpc_core::Thread, 100> threads;
52 for (grpc_core::Thread& th : threads) {
53 th = grpc_core::Thread("grpc_tls_test", thd_body, nullptr);
56 for (grpc_core::Thread& th : threads) {
63 int main(int argc, char* argv[]) {
64 grpc::testing::TestEnvironment env(argc, argv);
65 ::testing::InitGoogleTest(&argc, argv);
66 return RUN_ALL_TESTS();