Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : /* Test of gpr thread local storage support. */
35 :
36 : #include <stdio.h>
37 : #include <stdlib.h>
38 : #include <grpc/support/log.h>
39 : #include <grpc/support/sync.h>
40 : #include <grpc/support/thd.h>
41 : #include <grpc/support/tls.h>
42 : #include "test/core/util/test_config.h"
43 :
44 : #define NUM_THREADS 100
45 :
46 : GPR_TLS_DECL(test_var);
47 :
48 34602384 : static void thd_body(void *arg) {
49 : gpr_intptr i;
50 :
51 34602384 : GPR_ASSERT(gpr_tls_get(&test_var) == 0);
52 :
53 206801108 : for (i = 0; i < 10000000; i++) {
54 206801008 : gpr_tls_set(&test_var, i);
55 206801008 : GPR_ASSERT(gpr_tls_get(&test_var) == i);
56 : }
57 100 : gpr_tls_set(&test_var, 0);
58 100 : }
59 :
60 : /* ------------------------------------------------- */
61 :
62 1 : int main(int argc, char *argv[]) {
63 1 : gpr_thd_options opt = gpr_thd_options_default();
64 : int i;
65 : gpr_thd_id threads[NUM_THREADS];
66 :
67 1 : grpc_test_init(argc, argv);
68 :
69 : gpr_tls_init(&test_var);
70 :
71 1 : gpr_thd_options_set_joinable(&opt);
72 :
73 101 : for (i = 0; i < NUM_THREADS; i++) {
74 100 : gpr_thd_new(&threads[i], thd_body, NULL, &opt);
75 : }
76 101 : for (i = 0; i < NUM_THREADS; i++) {
77 100 : gpr_thd_join(threads[i]);
78 : }
79 :
80 : gpr_tls_destroy(&test_var);
81 :
82 1 : return 0;
83 : }
|