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 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/time.h>
42 : #include "test/core/util/test_config.h"
43 :
44 : struct test {
45 : gpr_mu mu;
46 : int n;
47 : int is_done;
48 : gpr_cv done_cv;
49 : };
50 :
51 : /* A Thread body. Decrement t->n, and if is becomes zero, set t->done. */
52 1000 : static void thd_body(void *v) {
53 1000 : struct test *t = v;
54 1000 : gpr_mu_lock(&t->mu);
55 1000 : t->n--;
56 1000 : if (t->n == 0) {
57 1 : t->is_done = 1;
58 1 : gpr_cv_signal(&t->done_cv);
59 : }
60 1000 : gpr_mu_unlock(&t->mu);
61 1000 : }
62 :
63 1000 : static void thd_body_joinable(void *v) {}
64 :
65 : /* Test that we can create a number of threads and wait for them. */
66 1 : static void test(void) {
67 : int i;
68 : gpr_thd_id thd;
69 : gpr_thd_id thds[1000];
70 : struct test t;
71 1 : int n = 1000;
72 1 : gpr_thd_options options = gpr_thd_options_default();
73 1 : gpr_mu_init(&t.mu);
74 1 : gpr_cv_init(&t.done_cv);
75 1 : t.n = n;
76 1 : t.is_done = 0;
77 1001 : for (i = 0; i != n; i++) {
78 1000 : GPR_ASSERT(gpr_thd_new(&thd, &thd_body, &t, NULL));
79 : }
80 1 : gpr_mu_lock(&t.mu);
81 3 : while (!t.is_done) {
82 1 : gpr_cv_wait(&t.done_cv, &t.mu, gpr_inf_future(GPR_CLOCK_REALTIME));
83 : }
84 1 : gpr_mu_unlock(&t.mu);
85 1 : GPR_ASSERT(t.n == 0);
86 1 : gpr_thd_options_set_joinable(&options);
87 1001 : for (i = 0; i < n; i++) {
88 1000 : GPR_ASSERT(gpr_thd_new(&thds[i], &thd_body_joinable, NULL, &options));
89 : }
90 1001 : for (i = 0; i < n; i++) {
91 1000 : gpr_thd_join(thds[i]);
92 : }
93 1 : }
94 :
95 : /* ------------------------------------------------- */
96 :
97 1 : int main(int argc, char *argv[]) {
98 1 : grpc_test_init(argc, argv);
99 1 : test();
100 1 : return 0;
101 : }
|