thd_test.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /* Test of gpr thread support. */
  19. #include "src/core/lib/gprpp/thd.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <grpc/support/log.h>
  23. #include <grpc/support/sync.h>
  24. #include <grpc/support/time.h>
  25. #include "test/core/util/test_config.h"
  26. #define NUM_THREADS 100
  27. struct test {
  28. gpr_mu mu;
  29. int n;
  30. int is_done;
  31. gpr_cv done_cv;
  32. };
  33. /* A Thread body. Decrement t->n, and if is becomes zero, set t->done. */
  34. static void thd_body1(void* v) {
  35. struct test* t = static_cast<struct test*>(v);
  36. gpr_mu_lock(&t->mu);
  37. t->n--;
  38. if (t->n == 0) {
  39. t->is_done = 1;
  40. gpr_cv_signal(&t->done_cv);
  41. }
  42. gpr_mu_unlock(&t->mu);
  43. }
  44. /* Test that we can create a number of threads, wait for them, and join them. */
  45. static void test1(void) {
  46. grpc_core::Thread thds[NUM_THREADS];
  47. struct test t;
  48. gpr_mu_init(&t.mu);
  49. gpr_cv_init(&t.done_cv);
  50. t.n = NUM_THREADS;
  51. t.is_done = 0;
  52. for (auto& th : thds) {
  53. th = grpc_core::Thread("grpc_thread_body1_test", &thd_body1, &t);
  54. th.Start();
  55. }
  56. gpr_mu_lock(&t.mu);
  57. while (!t.is_done) {
  58. gpr_cv_wait(&t.done_cv, &t.mu, gpr_inf_future(GPR_CLOCK_REALTIME));
  59. }
  60. gpr_mu_unlock(&t.mu);
  61. for (auto& th : thds) {
  62. th.Join();
  63. }
  64. GPR_ASSERT(t.n == 0);
  65. gpr_mu_destroy(&t.mu);
  66. gpr_cv_destroy(&t.done_cv);
  67. }
  68. static void thd_body2(void* /*v*/) {}
  69. /* Test that we can create a number of threads and join them. */
  70. static void test2(void) {
  71. grpc_core::Thread thds[NUM_THREADS];
  72. for (auto& th : thds) {
  73. bool ok;
  74. th = grpc_core::Thread("grpc_thread_body2_test", &thd_body2, nullptr, &ok);
  75. GPR_ASSERT(ok);
  76. th.Start();
  77. }
  78. for (auto& th : thds) {
  79. th.Join();
  80. }
  81. }
  82. /* ------------------------------------------------- */
  83. int main(int argc, char* argv[]) {
  84. grpc::testing::TestEnvironment env(argc, argv);
  85. test1();
  86. test2();
  87. return 0;
  88. }