• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
19 /* Test of gpr thread local storage support. */
20 
21 #include "src/core/lib/gpr/tls.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 #include <grpc/support/log.h>
27 #include <grpc/support/sync.h>
28 
29 #include "src/core/lib/gprpp/thd.h"
30 #include "test/core/util/test_config.h"
31 
32 #define NUM_THREADS 100
33 
34 GPR_TLS_DECL(test_var);
35 
thd_body(void * arg)36 static void thd_body(void* arg) {
37   intptr_t i;
38 
39   GPR_ASSERT(gpr_tls_get(&test_var) == 0);
40 
41   for (i = 0; i < 100000; i++) {
42     gpr_tls_set(&test_var, i);
43     GPR_ASSERT(gpr_tls_get(&test_var) == i);
44   }
45   gpr_tls_set(&test_var, 0);
46 }
47 
48 /* ------------------------------------------------- */
49 
main(int argc,char * argv[])50 int main(int argc, char* argv[]) {
51   grpc_core::Thread threads[NUM_THREADS];
52 
53   grpc_test_init(argc, argv);
54 
55   gpr_tls_init(&test_var);
56 
57   for (auto& th : threads) {
58     th = grpc_core::Thread("grpc_tls_test", thd_body, nullptr);
59     th.Start();
60   }
61   for (auto& th : threads) {
62     th.Join();
63   }
64 
65   gpr_tls_destroy(&test_var);
66 
67   return 0;
68 }
69