diff options
author | Vijay Pai <vpai@google.com> | 2018-02-16 22:59:03 -0800 |
---|---|---|
committer | Vijay Pai <vpai@google.com> | 2018-02-19 22:39:58 -0800 |
commit | da69355f30ca6863cc07c0aebffc5a14900de265 (patch) | |
tree | 83c866fcf54b2c058d452312109175cbb2107865 /test/core/gpr/sync_test.cc | |
parent | 5e1298f0a826777b0e5b844328b81216e9c37476 (diff) |
C++ize gpr_thread as grpc_core::Thread, make it 2-phase init (construct/Start)
Diffstat (limited to 'test/core/gpr/sync_test.cc')
-rw-r--r-- | test/core/gpr/sync_test.cc | 66 |
1 files changed, 36 insertions, 30 deletions
diff --git a/test/core/gpr/sync_test.cc b/test/core/gpr/sync_test.cc index d2d1f41775..2cdf061c71 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 <new> #include <stdio.h> #include <stdlib.h> -#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,8 +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 */ - gpr_thd_id* thread_ids; + int nthreads; /* number of threads */ + grpc_core::Thread* threads; int64_t iterations; /* number of iterations per thread */ int64_t counter; @@ -158,15 +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->thread_ids = - static_cast<gpr_thd_id*>(gpr_malloc(sizeof(*m->thread_ids) * 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); @@ -174,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; } @@ -185,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->thread_ids); + 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)) { int i; - for (i = 0; i != m->threads; i++) { - GPR_ASSERT(gpr_thd_new(&m->thread_ids[i], "grpc_create_threads", body, m)); + for (i = 0; i != m->nthreads; i++) { + new (&m->threads[i]) grpc_core::Thread("grpc_create_threads", body, m); + m->threads[i].Start(); } } @@ -204,12 +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->threads; i++) { - gpr_thd_join(m->thread_ids[i]); + 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); @@ -251,19 +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); - gpr_thd_id extra_id; + grpc_core::Thread extra_thd; if (extra != nullptr) { - GPR_ASSERT(gpr_thd_new(&extra_id, name, extra, m)); + 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 (extra != nullptr) { - gpr_thd_join(extra_id); + extra_thd.Join(); } - if (m->counter != m->threads * m->iterations * m->incr_step) { + 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); @@ -305,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); @@ -313,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++; @@ -378,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++) { @@ -433,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); |