diff options
Diffstat (limited to 'test/core/gpr')
-rw-r--r-- | test/core/gpr/BUILD | 10 | ||||
-rw-r--r-- | test/core/gpr/arena_test.cc | 21 | ||||
-rw-r--r-- | test/core/gpr/cpu_test.cc | 28 | ||||
-rw-r--r-- | test/core/gpr/mpscq_test.cc | 40 | ||||
-rw-r--r-- | test/core/gpr/spinlock_test.cc | 24 | ||||
-rw-r--r-- | test/core/gpr/sync_test.cc | 63 | ||||
-rw-r--r-- | test/core/gpr/thd_test.cc | 104 | ||||
-rw-r--r-- | test/core/gpr/time_test.cc | 1 | ||||
-rw-r--r-- | test/core/gpr/tls_test.cc | 26 |
9 files changed, 114 insertions, 203 deletions
diff --git a/test/core/gpr/BUILD b/test/core/gpr/BUILD index 4032664b59..5308ea0934 100644 --- a/test/core/gpr/BUILD +++ b/test/core/gpr/BUILD @@ -129,16 +129,6 @@ grpc_cc_test( ) grpc_cc_test( - name = "thd_test", - srcs = ["thd_test.cc"], - language = "C++", - deps = [ - "//:gpr", - "//test/core/util:gpr_test_util", - ], -) - -grpc_cc_test( name = "time_test", srcs = ["time_test.cc"], language = "C++", diff --git a/test/core/gpr/arena_test.cc b/test/core/gpr/arena_test.cc index 9eaf57b631..b00c014cea 100644 --- a/test/core/gpr/arena_test.cc +++ b/test/core/gpr/arena_test.cc @@ -18,16 +18,18 @@ #include "src/core/lib/gpr/arena.h" +#include <inttypes.h> +#include <string.h> +#include <new> + #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/string_util.h> #include <grpc/support/sync.h> -#include <inttypes.h> -#include <string.h> #include "src/core/lib/gpr/string.h" -#include "src/core/lib/gpr/thd.h" #include "src/core/lib/gpr/useful.h" +#include "src/core/lib/gprpp/thd.h" #include "test/core/util/test_config.h" static void test_noop(void) { gpr_arena_destroy(gpr_arena_create(1)); } @@ -97,19 +99,18 @@ static void concurrent_test(void) { gpr_event_init(&args.ev_start); args.arena = gpr_arena_create(1024); - gpr_thd_id thds[CONCURRENT_TEST_THREADS]; + grpc_core::Thread 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); + new (&thds[i]) + grpc_core::Thread("grpc_concurrent_test", concurrent_test_body, &args); + thds[i].Start(); } gpr_event_set(&args.ev_start, (void*)1); - for (int i = 0; i < CONCURRENT_TEST_THREADS; i++) { - gpr_thd_join(thds[i]); + for (auto& th : thds) { + th.Join(); } gpr_arena_destroy(args.arena); diff --git a/test/core/gpr/cpu_test.cc b/test/core/gpr/cpu_test.cc index 9f2c3f1923..279e6e6f5a 100644 --- a/test/core/gpr/cpu_test.cc +++ b/test/core/gpr/cpu_test.cc @@ -21,15 +21,18 @@ gpr_cpu_current_cpu() */ -#include <grpc/support/alloc.h> #include <grpc/support/cpu.h> + +#include <stdio.h> +#include <string.h> +#include <new> + +#include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/sync.h> #include <grpc/support/time.h> -#include <stdio.h> -#include <string.h> -#include "src/core/lib/gpr/thd.h" +#include "src/core/lib/gprpp/thd.h" #include "test/core/util/test_config.h" /* Test structure is essentially: @@ -101,7 +104,6 @@ static void cpu_test(void) { uint32_t i; int cores_seen = 0; struct cpu_test ct; - 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 +112,25 @@ 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; + grpc_core::Thread* thd = + static_cast<grpc_core::Thread*>(gpr_malloc(sizeof(*thd) * nthreads)); + + for (i = 0; i < nthreads; i++) { + new (&thd[i]) grpc_core::Thread("grpc_cpu_test", &worker_thread, &ct); + thd[i].Start(); } 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++) { + thd[i].Join(); + thd[i].~Thread(); + } + 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..33f93878e0 100644 --- a/test/core/gpr/mpscq_test.cc +++ b/test/core/gpr/mpscq_test.cc @@ -19,13 +19,14 @@ #include "src/core/lib/gpr/mpscq.h" #include <stdlib.h> +#include <new> #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/sync.h> -#include "src/core/lib/gpr/thd.h" #include "src/core/lib/gpr/useful.h" +#include "src/core/lib/gprpp/thd.h" #include "test/core/util/test_config.h" typedef struct test_node { @@ -76,18 +77,16 @@ static void test_mt(void) { gpr_log(GPR_DEBUG, "test_mt"); gpr_event start; gpr_event_init(&start); - gpr_thd_id thds[100]; + grpc_core::Thread thds[100]; thd_args ta[GPR_ARRAY_SIZE(thds)]; 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)); + new (&thds[i]) grpc_core::Thread("grpc_mt_test", test_thread, &ta[i]); + thds[i].Start(); } size_t num_done = 0; size_t spins = 0; @@ -104,8 +103,8 @@ static void test_mt(void) { gpr_free(tn); } gpr_log(GPR_DEBUG, "spins: %" PRIdPTR, spins); - for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) { - gpr_thd_join(thds[i]); + for (auto& th : thds) { + th.Join(); } gpr_mpscq_destroy(&q); } @@ -147,19 +146,17 @@ static void test_mt_multipop(void) { gpr_log(GPR_DEBUG, "test_mt_multipop"); gpr_event start; gpr_event_init(&start); - gpr_thd_id thds[100]; - gpr_thd_id pull_thds[100]; + grpc_core::Thread thds[100]; + grpc_core::Thread pull_thds[100]; thd_args ta[GPR_ARRAY_SIZE(thds)]; 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)); + new (&thds[i]) grpc_core::Thread("grpc_multipop_test", test_thread, &ta[i]); + thds[i].Start(); } pull_args pa; pa.ta = ta; @@ -170,18 +167,17 @@ 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)); + new (&pull_thds[i]) + grpc_core::Thread("grpc_multipop_pull", pull_thread, &pa); + pull_thds[i].Start(); } gpr_event_set(&start, (void*)1); - for (size_t i = 0; i < GPR_ARRAY_SIZE(pull_thds); i++) { - gpr_thd_join(pull_thds[i]); + for (auto& pth : pull_thds) { + pth.Join(); } gpr_log(GPR_DEBUG, "spins: %" PRIdPTR, pa.spins); - for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) { - gpr_thd_join(thds[i]); + for (auto& th : thds) { + th.Join(); } gpr_mpscq_destroy(&q); } diff --git a/test/core/gpr/spinlock_test.cc b/test/core/gpr/spinlock_test.cc index 9f182bc154..ac9f70f301 100644 --- a/test/core/gpr/spinlock_test.cc +++ b/test/core/gpr/spinlock_test.cc @@ -16,24 +16,27 @@ * */ -/* Test of gpr synchronization support. */ +/* Test of gpr spin-lock support. */ #include "src/core/lib/gpr/spinlock.h" + +#include <stdio.h> +#include <stdlib.h> +#include <new> + #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/sync.h> #include <grpc/support/time.h> -#include <stdio.h> -#include <stdlib.h> -#include "src/core/lib/gpr/thd.h" +#include "src/core/lib/gprpp/thd.h" #include "test/core/util/test_config.h" /* ------------------------------------------------- */ /* Tests for gpr_spinlock. */ struct test { int thread_count; /* number of threads */ - gpr_thd_id* threads; + grpc_core::Thread* threads; int64_t iterations; /* number of iterations per thread */ int64_t counter; @@ -46,7 +49,7 @@ 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->thread_count = threads; - m->threads = static_cast<gpr_thd_id*>( + m->threads = static_cast<grpc_core::Thread*>( gpr_malloc(sizeof(*m->threads) * static_cast<size_t>(threads))); m->iterations = iterations; m->counter = 0; @@ -66,10 +69,8 @@ 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)); + new (&m->threads[i]) grpc_core::Thread("grpc_create_threads", body, m); + m->threads[i].Start(); } } @@ -77,7 +78,8 @@ static void test_create_threads(struct test* m, void (*body)(void* arg)) { static void test_wait(struct test* m) { int i; for (i = 0; i != m->thread_count; i++) { - gpr_thd_join(m->threads[i]); + m->threads[i].Join(); + m->threads[i].~Thread(); } } diff --git a/test/core/gpr/sync_test.cc b/test/core/gpr/sync_test.cc index bafd91020b..487f394b14 100644 --- a/test/core/gpr/sync_test.cc +++ b/test/core/gpr/sync_test.cc @@ -18,14 +18,17 @@ /* Test of gpr synchronization support. */ -#include <grpc/support/alloc.h> -#include <grpc/support/log.h> #include <grpc/support/sync.h> -#include <grpc/support/time.h> + #include <stdio.h> #include <stdlib.h> +#include <new> -#include "src/core/lib/gpr/thd.h" +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> +#include <grpc/support/time.h> + +#include "src/core/lib/gprpp/thd.h" #include "test/core/util/test_config.h" /* ==================Example use of interface=================== @@ -133,7 +136,8 @@ 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 */ + int nthreads; /* number of threads */ + grpc_core::Thread* threads; int64_t iterations; /* number of iterations per thread */ int64_t counter; @@ -157,13 +161,15 @@ struct test { }; /* Return pointer to a new struct test. */ -static struct test* test_new(int threads, int64_t iterations, int incr_step) { +static struct test* test_new(int nthreads, int64_t iterations, int incr_step) { struct test* m = static_cast<struct test*>(gpr_malloc(sizeof(*m))); - m->threads = threads; + m->nthreads = nthreads; + m->threads = static_cast<grpc_core::Thread*>( + gpr_malloc(sizeof(*m->threads) * nthreads)); m->iterations = iterations; m->counter = 0; m->thread_count = 0; - m->done = threads; + m->done = nthreads; m->incr_step = incr_step; gpr_mu_init(&m->mu); gpr_cv_init(&m->cv); @@ -171,7 +177,7 @@ static struct test* test_new(int threads, int64_t iterations, int incr_step) { queue_init(&m->q); gpr_stats_init(&m->stats_counter, 0); gpr_ref_init(&m->refcount, 0); - gpr_ref_init(&m->thread_refcount, threads); + gpr_ref_init(&m->thread_refcount, nthreads); gpr_event_init(&m->event); return m; } @@ -182,15 +188,16 @@ 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->threads); gpr_free(m); } -/* Create m->threads threads, each running (*body)(m) */ +/* Create m->nthreads 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)); + for (i = 0; i != m->nthreads; i++) { + new (&m->threads[i]) grpc_core::Thread("grpc_create_threads", body, m); + m->threads[i].Start(); } } @@ -201,9 +208,13 @@ 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->nthreads; i++) { + m->threads[i].Join(); + m->threads[i].~Thread(); + } } -/* Get an integer thread id in the raneg 0..threads-1 */ +/* Get an integer thread id in the raneg 0..nthreads-1 */ static int thread_id(struct test* m) { int id; gpr_mu_lock(&m->mu); @@ -245,16 +256,20 @@ 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); + grpc_core::Thread extra_thd; if (extra != nullptr) { - gpr_thd_id id; - GPR_ASSERT(gpr_thd_new(&id, name, extra, m, nullptr)); + new (&extra_thd) grpc_core::Thread(name, extra, m); + extra_thd.Start(); m->done++; /* one more thread to wait for */ } test_create_threads(m, body); test_wait(m); - if (m->counter != m->threads * m->iterations * m->incr_step) { + if (extra != nullptr) { + extra_thd.Join(); + } + if (m->counter != m->nthreads * m->iterations * m->incr_step) { fprintf(stderr, "counter %ld threads %d iterations %ld\n", - static_cast<long>(m->counter), m->threads, + static_cast<long>(m->counter), m->nthreads, static_cast<long>(m->iterations)); fflush(stderr); GPR_ASSERT(0); @@ -296,7 +311,7 @@ static void inctry(void* v /*=m*/) { mark_thread_done(m); } -/* Increment counter only when (m->counter%m->threads)==m->thread_id; then mark +/* Increment counter only when (m->counter%m->nthreads)==m->thread_id; then mark thread as done. */ static void inc_by_turns(void* v /*=m*/) { struct test* m = static_cast<struct test*>(v); @@ -304,7 +319,7 @@ static void inc_by_turns(void* v /*=m*/) { int id = thread_id(m); for (i = 0; i != m->iterations; i++) { gpr_mu_lock(&m->mu); - while ((m->counter % m->threads) != id) { + while ((m->counter % m->nthreads) != id) { gpr_cv_wait(&m->cv, &m->mu, gpr_inf_future(GPR_CLOCK_MONOTONIC)); } m->counter++; @@ -369,12 +384,12 @@ static void many_producers(void* v /*=m*/) { mark_thread_done(m); } -/* Consume elements from m->q until m->threads*m->iterations are seen, +/* Consume elements from m->q until m->nthreads*m->iterations are seen, wait an extra second to confirm that no more elements are arriving, then mark thread as done. */ static void consumer(void* v /*=m*/) { struct test* m = static_cast<struct test*>(v); - int64_t n = m->iterations * m->threads; + int64_t n = m->iterations * m->nthreads; int64_t i; int value; for (i = 0; i != n; i++) { @@ -424,11 +439,11 @@ static void refinc(void* v /*=m*/) { } /* Wait until m->event is set to (void *)1, then decrement m->refcount by 1 - (m->threads * m->iterations * m->incr_step) times, and ensure that the last + (m->nthreads * m->iterations * m->incr_step) times, and ensure that the last decrement caused the counter to reach zero, then mark thread as done. */ static void refcheck(void* v /*=m*/) { struct test* m = static_cast<struct test*>(v); - int64_t n = m->iterations * m->threads * m->incr_step; + int64_t n = m->iterations * m->nthreads * m->incr_step; int64_t i; GPR_ASSERT(gpr_event_wait(&m->event, gpr_inf_future(GPR_CLOCK_REALTIME)) == (void*)1); diff --git a/test/core/gpr/thd_test.cc b/test/core/gpr/thd_test.cc deleted file mode 100644 index 18bbaae6c9..0000000000 --- a/test/core/gpr/thd_test.cc +++ /dev/null @@ -1,104 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -/* Test of gpr thread support. */ - -#include "src/core/lib/gpr/thd.h" - -#include <grpc/support/log.h> -#include <grpc/support/sync.h> -#include <grpc/support/time.h> -#include <stdio.h> -#include <stdlib.h> - -#include "test/core/util/test_config.h" - -#define NUM_THREADS 300 - -struct test { - gpr_mu mu; - int n; - int is_done; - gpr_cv done_cv; -}; - -/* A Thread body. Decrement t->n, and if is becomes zero, set t->done. */ -static void thd_body(void* v) { - struct test* t = static_cast<struct test*>(v); - gpr_mu_lock(&t->mu); - t->n--; - if (t->n == 0) { - t->is_done = 1; - gpr_cv_signal(&t->done_cv); - } - 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) { - 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_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); - GPR_ASSERT(t.n == 0); - gpr_thd_options_set_joinable(&options); - for (i = 0; i < NUM_THREADS; i++) { - GPR_ASSERT(gpr_thd_new(&thds[i], "grpc_joinable_thread_test", - &thd_body_joinable, nullptr, &options)); - } - for (i = 0; i < NUM_THREADS; i++) { - gpr_thd_join(thds[i]); - } -} - -/* ------------------------------------------------- */ - -int main(int argc, char* argv[]) { - grpc_test_init(argc, argv); - test_options(); - test(); - return 0; -} diff --git a/test/core/gpr/time_test.cc b/test/core/gpr/time_test.cc index e6bcc1247d..c80aac649d 100644 --- a/test/core/gpr/time_test.cc +++ b/test/core/gpr/time_test.cc @@ -26,7 +26,6 @@ #include <stdlib.h> #include <string.h> -#include "src/core/lib/gpr/thd.h" #include "test/core/util/test_config.h" static void to_fp(void* arg, const char* buf, size_t len) { diff --git a/test/core/gpr/tls_test.cc b/test/core/gpr/tls_test.cc index 1e4534dc5a..a060cd47f1 100644 --- a/test/core/gpr/tls_test.cc +++ b/test/core/gpr/tls_test.cc @@ -18,13 +18,16 @@ /* Test of gpr thread local storage support. */ -#include <grpc/support/log.h> -#include <grpc/support/sync.h> +#include "src/core/lib/gpr/tls.h" + #include <stdio.h> #include <stdlib.h> +#include <new> -#include "src/core/lib/gpr/thd.h" -#include "src/core/lib/gpr/tls.h" +#include <grpc/support/log.h> +#include <grpc/support/sync.h> + +#include "src/core/lib/gprpp/thd.h" #include "test/core/util/test_config.h" #define NUM_THREADS 100 @@ -46,21 +49,18 @@ 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]; + grpc_core::Thread threads[NUM_THREADS]; grpc_test_init(argc, 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); + for (auto& th : threads) { + new (&th) grpc_core::Thread("grpc_tls_test", thd_body, nullptr); + th.Start(); } - for (i = 0; i < NUM_THREADS; i++) { - gpr_thd_join(threads[i]); + for (auto& th : threads) { + th.Join(); } gpr_tls_destroy(&test_var); |