diff options
Diffstat (limited to 'test/core/gpr')
-rw-r--r-- | test/core/gpr/arena_test.cc | 5 | ||||
-rw-r--r-- | test/core/gpr/cpu_test.cc | 15 | ||||
-rw-r--r-- | test/core/gpr/mpscq_test.cc | 17 | ||||
-rw-r--r-- | test/core/gpr/spinlock_test.cc | 5 | ||||
-rw-r--r-- | test/core/gpr/sync_test.cc | 17 | ||||
-rw-r--r-- | test/core/gpr/thd_test.cc | 45 | ||||
-rw-r--r-- | test/core/gpr/tls_test.cc | 5 |
7 files changed, 51 insertions, 58 deletions
diff --git a/test/core/gpr/arena_test.cc b/test/core/gpr/arena_test.cc index 9eaf57b631..1cfaefa686 100644 --- a/test/core/gpr/arena_test.cc +++ b/test/core/gpr/arena_test.cc @@ -100,10 +100,7 @@ static void concurrent_test(void) { gpr_thd_id thds[CONCURRENT_TEST_THREADS]; for (int i = 0; i < CONCURRENT_TEST_THREADS; i++) { - gpr_thd_options opt = gpr_thd_options_default(); - gpr_thd_options_set_joinable(&opt); - gpr_thd_new(&thds[i], "grpc_concurrent_test", concurrent_test_body, &args, - &opt); + gpr_thd_new(&thds[i], "grpc_concurrent_test", concurrent_test_body, &args); } gpr_event_set(&args.ev_start, (void*)1); diff --git a/test/core/gpr/cpu_test.cc b/test/core/gpr/cpu_test.cc index 9f2c3f1923..c97facef7d 100644 --- a/test/core/gpr/cpu_test.cc +++ b/test/core/gpr/cpu_test.cc @@ -101,7 +101,7 @@ static void cpu_test(void) { uint32_t i; int cores_seen = 0; struct cpu_test ct; - gpr_thd_id thd; + gpr_thd_id* thd; ct.ncores = gpr_cpu_num_cores(); GPR_ASSERT(ct.ncores > 0); ct.nthreads = static_cast<int>(ct.ncores) * 3; @@ -110,15 +110,22 @@ static void cpu_test(void) { gpr_mu_init(&ct.mu); gpr_cv_init(&ct.done_cv); ct.is_done = 0; - for (i = 0; i < ct.ncores * 3; i++) { - GPR_ASSERT( - gpr_thd_new(&thd, "grpc_cpu_test", &worker_thread, &ct, nullptr)); + + uint32_t nthreads = ct.ncores * 3; + thd = static_cast<gpr_thd_id*>(gpr_malloc(sizeof(thd[0]) * nthreads)); + + for (i = 0; i < nthreads; i++) { + GPR_ASSERT(gpr_thd_new(&thd[i], "grpc_cpu_test", &worker_thread, &ct)); } gpr_mu_lock(&ct.mu); while (!ct.is_done) { gpr_cv_wait(&ct.done_cv, &ct.mu, gpr_inf_future(GPR_CLOCK_MONOTONIC)); } gpr_mu_unlock(&ct.mu); + for (i = 0; i < nthreads; i++) { + gpr_thd_join(thd[i]); + } + gpr_free(thd); fprintf(stderr, "Saw cores ["); fflush(stderr); for (i = 0; i < ct.ncores; i++) { diff --git a/test/core/gpr/mpscq_test.cc b/test/core/gpr/mpscq_test.cc index 96813466c9..55998445f6 100644 --- a/test/core/gpr/mpscq_test.cc +++ b/test/core/gpr/mpscq_test.cc @@ -81,13 +81,10 @@ static void test_mt(void) { gpr_mpscq q; gpr_mpscq_init(&q); for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) { - gpr_thd_options options = gpr_thd_options_default(); - gpr_thd_options_set_joinable(&options); ta[i].ctr = 0; ta[i].q = &q; ta[i].start = &start; - GPR_ASSERT( - gpr_thd_new(&thds[i], "grpc_mt_test", test_thread, &ta[i], &options)); + GPR_ASSERT(gpr_thd_new(&thds[i], "grpc_mt_test", test_thread, &ta[i])); } size_t num_done = 0; size_t spins = 0; @@ -153,13 +150,11 @@ static void test_mt_multipop(void) { gpr_mpscq q; gpr_mpscq_init(&q); for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) { - gpr_thd_options options = gpr_thd_options_default(); - gpr_thd_options_set_joinable(&options); ta[i].ctr = 0; ta[i].q = &q; ta[i].start = &start; - GPR_ASSERT(gpr_thd_new(&thds[i], "grpc_multipop_test", test_thread, &ta[i], - &options)); + GPR_ASSERT( + gpr_thd_new(&thds[i], "grpc_multipop_test", test_thread, &ta[i])); } pull_args pa; pa.ta = ta; @@ -170,10 +165,8 @@ static void test_mt_multipop(void) { pa.start = &start; gpr_mu_init(&pa.mu); for (size_t i = 0; i < GPR_ARRAY_SIZE(pull_thds); i++) { - gpr_thd_options options = gpr_thd_options_default(); - gpr_thd_options_set_joinable(&options); - GPR_ASSERT(gpr_thd_new(&pull_thds[i], "grpc_multipop_pull", pull_thread, - &pa, &options)); + GPR_ASSERT( + gpr_thd_new(&pull_thds[i], "grpc_multipop_pull", pull_thread, &pa)); } gpr_event_set(&start, (void*)1); for (size_t i = 0; i < GPR_ARRAY_SIZE(pull_thds); i++) { diff --git a/test/core/gpr/spinlock_test.cc b/test/core/gpr/spinlock_test.cc index 9f182bc154..1392cff7ad 100644 --- a/test/core/gpr/spinlock_test.cc +++ b/test/core/gpr/spinlock_test.cc @@ -66,10 +66,7 @@ static void test_destroy(struct test* m) { static void test_create_threads(struct test* m, void (*body)(void* arg)) { int i; for (i = 0; i != m->thread_count; i++) { - gpr_thd_options opt = gpr_thd_options_default(); - gpr_thd_options_set_joinable(&opt); - GPR_ASSERT( - gpr_thd_new(&m->threads[i], "grpc_create_threads", body, m, &opt)); + GPR_ASSERT(gpr_thd_new(&m->threads[i], "grpc_create_threads", body, m)); } } diff --git a/test/core/gpr/sync_test.cc b/test/core/gpr/sync_test.cc index bafd91020b..d2d1f41775 100644 --- a/test/core/gpr/sync_test.cc +++ b/test/core/gpr/sync_test.cc @@ -134,6 +134,7 @@ int queue_remove(queue* q, int* head, gpr_timespec abs_deadline) { /* Tests for gpr_mu and gpr_cv, and the queue example. */ struct test { int threads; /* number of threads */ + gpr_thd_id* thread_ids; int64_t iterations; /* number of iterations per thread */ int64_t counter; @@ -160,6 +161,8 @@ struct test { static struct test* test_new(int threads, int64_t iterations, int incr_step) { struct test* m = static_cast<struct test*>(gpr_malloc(sizeof(*m))); m->threads = threads; + m->thread_ids = + static_cast<gpr_thd_id*>(gpr_malloc(sizeof(*m->thread_ids) * threads)); m->iterations = iterations; m->counter = 0; m->thread_count = 0; @@ -182,15 +185,15 @@ static void test_destroy(struct test* m) { gpr_cv_destroy(&m->cv); gpr_cv_destroy(&m->done_cv); queue_destroy(&m->q); + gpr_free(m->thread_ids); gpr_free(m); } /* Create m->threads threads, each running (*body)(m) */ static void test_create_threads(struct test* m, void (*body)(void* arg)) { - gpr_thd_id id; int i; for (i = 0; i != m->threads; i++) { - GPR_ASSERT(gpr_thd_new(&id, "grpc_create_threads", body, m, nullptr)); + GPR_ASSERT(gpr_thd_new(&m->thread_ids[i], "grpc_create_threads", body, m)); } } @@ -201,6 +204,9 @@ static void test_wait(struct test* m) { gpr_cv_wait(&m->done_cv, &m->mu, gpr_inf_future(GPR_CLOCK_MONOTONIC)); } gpr_mu_unlock(&m->mu); + for (int i = 0; i != m->threads; i++) { + gpr_thd_join(m->thread_ids[i]); + } } /* Get an integer thread id in the raneg 0..threads-1 */ @@ -245,13 +251,16 @@ static void test(const char* name, void (*body)(void* m), fprintf(stderr, " %ld", static_cast<long>(iterations)); fflush(stderr); m = test_new(10, iterations, incr_step); + gpr_thd_id extra_id; if (extra != nullptr) { - gpr_thd_id id; - GPR_ASSERT(gpr_thd_new(&id, name, extra, m, nullptr)); + GPR_ASSERT(gpr_thd_new(&extra_id, name, extra, m)); m->done++; /* one more thread to wait for */ } test_create_threads(m, body); test_wait(m); + if (extra != nullptr) { + gpr_thd_join(extra_id); + } if (m->counter != m->threads * m->iterations * m->incr_step) { fprintf(stderr, "counter %ld threads %d iterations %ld\n", static_cast<long>(m->counter), m->threads, diff --git a/test/core/gpr/thd_test.cc b/test/core/gpr/thd_test.cc index 18bbaae6c9..47e9a22fd8 100644 --- a/test/core/gpr/thd_test.cc +++ b/test/core/gpr/thd_test.cc @@ -38,7 +38,7 @@ struct test { }; /* A Thread body. Decrement t->n, and if is becomes zero, set t->done. */ -static void thd_body(void* v) { +static void thd_body1(void* v) { struct test* t = static_cast<struct test*>(v); gpr_mu_lock(&t->mu); t->n--; @@ -49,45 +49,38 @@ static void thd_body(void* v) { gpr_mu_unlock(&t->mu); } -static void thd_body_joinable(void* v) {} - -/* Test thread options work as expected */ -static void test_options(void) { - gpr_thd_options options = gpr_thd_options_default(); - GPR_ASSERT(!gpr_thd_options_is_joinable(&options)); - GPR_ASSERT(gpr_thd_options_is_detached(&options)); - gpr_thd_options_set_joinable(&options); - GPR_ASSERT(gpr_thd_options_is_joinable(&options)); - GPR_ASSERT(!gpr_thd_options_is_detached(&options)); - gpr_thd_options_set_detached(&options); - GPR_ASSERT(!gpr_thd_options_is_joinable(&options)); - GPR_ASSERT(gpr_thd_options_is_detached(&options)); -} - -/* Test that we can create a number of threads and wait for them. */ -static void test(void) { +/* Test that we can create a number of threads, wait for them, and join them. */ +static void test1(void) { int i; - gpr_thd_id thd; gpr_thd_id thds[NUM_THREADS]; struct test t; - gpr_thd_options options = gpr_thd_options_default(); gpr_mu_init(&t.mu); gpr_cv_init(&t.done_cv); t.n = NUM_THREADS; t.is_done = 0; for (i = 0; i < NUM_THREADS; i++) { - GPR_ASSERT(gpr_thd_new(&thd, "grpc_thread_test", &thd_body, &t, nullptr)); + GPR_ASSERT(gpr_thd_new(&thds[i], "grpc_thread_body1_test", &thd_body1, &t)); } gpr_mu_lock(&t.mu); while (!t.is_done) { gpr_cv_wait(&t.done_cv, &t.mu, gpr_inf_future(GPR_CLOCK_REALTIME)); } gpr_mu_unlock(&t.mu); + for (i = 0; i < NUM_THREADS; i++) { + gpr_thd_join(thds[i]); + } GPR_ASSERT(t.n == 0); - gpr_thd_options_set_joinable(&options); +} + +static void thd_body2(void* v) {} + +/* Test that we can create a number of threads and join them. */ +static void test2(void) { + int i; + gpr_thd_id thds[NUM_THREADS]; for (i = 0; i < NUM_THREADS; i++) { - GPR_ASSERT(gpr_thd_new(&thds[i], "grpc_joinable_thread_test", - &thd_body_joinable, nullptr, &options)); + GPR_ASSERT( + gpr_thd_new(&thds[i], "grpc_thread_body2_test", &thd_body2, nullptr)); } for (i = 0; i < NUM_THREADS; i++) { gpr_thd_join(thds[i]); @@ -98,7 +91,7 @@ static void test(void) { int main(int argc, char* argv[]) { grpc_test_init(argc, argv); - test_options(); - test(); + test1(); + test2(); return 0; } diff --git a/test/core/gpr/tls_test.cc b/test/core/gpr/tls_test.cc index 1e4534dc5a..f3f0864d3d 100644 --- a/test/core/gpr/tls_test.cc +++ b/test/core/gpr/tls_test.cc @@ -46,7 +46,6 @@ static void thd_body(void* arg) { /* ------------------------------------------------- */ int main(int argc, char* argv[]) { - gpr_thd_options opt = gpr_thd_options_default(); int i; gpr_thd_id threads[NUM_THREADS]; @@ -54,10 +53,8 @@ int main(int argc, char* argv[]) { gpr_tls_init(&test_var); - gpr_thd_options_set_joinable(&opt); - for (i = 0; i < NUM_THREADS; i++) { - gpr_thd_new(&threads[i], "grpc_tls_test", thd_body, nullptr, &opt); + gpr_thd_new(&threads[i], "grpc_tls_test", thd_body, nullptr); } for (i = 0; i < NUM_THREADS; i++) { gpr_thd_join(threads[i]); |