aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/gpr
diff options
context:
space:
mode:
authorGravatar Vijay Pai <vpai@google.com>2018-02-16 22:59:03 -0800
committerGravatar Vijay Pai <vpai@google.com>2018-02-19 22:39:58 -0800
commitda69355f30ca6863cc07c0aebffc5a14900de265 (patch)
tree83c866fcf54b2c058d452312109175cbb2107865 /test/core/gpr
parent5e1298f0a826777b0e5b844328b81216e9c37476 (diff)
C++ize gpr_thread as grpc_core::Thread, make it 2-phase init (construct/Start)
Diffstat (limited to 'test/core/gpr')
-rw-r--r--test/core/gpr/arena_test.cc18
-rw-r--r--test/core/gpr/cpu_test.cc21
-rw-r--r--test/core/gpr/mpscq_test.cc33
-rw-r--r--test/core/gpr/spinlock_test.cc21
-rw-r--r--test/core/gpr/sync_test.cc66
-rw-r--r--test/core/gpr/thd_test.cc97
-rw-r--r--test/core/gpr/time_test.cc1
-rw-r--r--test/core/gpr/tls_test.cc23
8 files changed, 104 insertions, 176 deletions
diff --git a/test/core/gpr/arena_test.cc b/test/core/gpr/arena_test.cc
index 1cfaefa686..717052eacd 100644
--- a/test/core/gpr/arena_test.cc
+++ b/test/core/gpr/arena_test.cc
@@ -18,15 +18,17 @@
#include "src/core/lib/gpr/arena.h"
+#include <new>
+#include <inttypes.h>
+#include <string.h>
+
#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/gprpp/thd.h"
#include "src/core/lib/gpr/useful.h"
#include "test/core/util/test_config.h"
@@ -97,16 +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_new(&thds[i], "grpc_concurrent_test", concurrent_test_body, &args);
+ 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 c97facef7d..4575fb643a 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 <new>
+#include <stdio.h>
+#include <string.h>
+
+#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;
@@ -112,10 +114,12 @@ static void cpu_test(void) {
ct.is_done = 0;
uint32_t nthreads = ct.ncores * 3;
- thd = static_cast<gpr_thd_id*>(gpr_malloc(sizeof(thd[0]) * nthreads));
+ grpc_core::Thread* thd =
+ static_cast<grpc_core::Thread*>(gpr_malloc(sizeof(*thd)*nthreads));
for (i = 0; i < nthreads; i++) {
- GPR_ASSERT(gpr_thd_new(&thd[i], "grpc_cpu_test", &worker_thread, &ct));
+ new (&thd[i]) grpc_core::Thread("grpc_cpu_test", &worker_thread, &ct);
+ thd[i].Start();
}
gpr_mu_lock(&ct.mu);
while (!ct.is_done) {
@@ -123,7 +127,8 @@ static void cpu_test(void) {
}
gpr_mu_unlock(&ct.mu);
for (i = 0; i < nthreads; i++) {
- gpr_thd_join(thd[i]);
+ thd[i].Join();
+ thd[i].~Thread();
}
gpr_free(thd);
fprintf(stderr, "Saw cores [");
diff --git a/test/core/gpr/mpscq_test.cc b/test/core/gpr/mpscq_test.cc
index 55998445f6..bf65b2d0b9 100644
--- a/test/core/gpr/mpscq_test.cc
+++ b/test/core/gpr/mpscq_test.cc
@@ -18,13 +18,14 @@
#include "src/core/lib/gpr/mpscq.h"
+#include <new>
#include <stdlib.h>
#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/gprpp/thd.h"
#include "src/core/lib/gpr/useful.h"
#include "test/core/util/test_config.h"
@@ -76,7 +77,7 @@ 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);
@@ -84,7 +85,8 @@ static void test_mt(void) {
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]));
+ 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;
@@ -101,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);
}
@@ -144,8 +146,8 @@ 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);
@@ -153,8 +155,8 @@ static void test_mt_multipop(void) {
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]));
+ new (&thds[i]) grpc_core::Thread("grpc_multipop_test", test_thread, &ta[i]);
+ thds[i].Start();
}
pull_args pa;
pa.ta = ta;
@@ -165,16 +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_ASSERT(
- gpr_thd_new(&pull_thds[i], "grpc_multipop_pull", pull_thread, &pa));
+ 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 1392cff7ad..1a3475b8f2 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 <new>
+#include <stdio.h>
+#include <stdlib.h>
+
#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,7 +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_ASSERT(gpr_thd_new(&m->threads[i], "grpc_create_threads", body, m));
+ new (&m->threads[i]) grpc_core::Thread("grpc_create_threads", body, m);
+ m->threads[i].Start();
}
}
@@ -74,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 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);
diff --git a/test/core/gpr/thd_test.cc b/test/core/gpr/thd_test.cc
deleted file mode 100644
index 47e9a22fd8..0000000000
--- a/test/core/gpr/thd_test.cc
+++ /dev/null
@@ -1,97 +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_body1(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);
-}
-
-/* Test that we can create a number of threads, wait for them, and join them. */
-static void test1(void) {
- int i;
- gpr_thd_id thds[NUM_THREADS];
- struct test t;
- 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(&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);
-}
-
-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_thread_body2_test", &thd_body2, nullptr));
- }
- for (i = 0; i < NUM_THREADS; i++) {
- gpr_thd_join(thds[i]);
- }
-}
-
-/* ------------------------------------------------- */
-
-int main(int argc, char* argv[]) {
- grpc_test_init(argc, argv);
- test1();
- test2();
- 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 f3f0864d3d..15f329f21a 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 <new>
#include <stdio.h>
#include <stdlib.h>
-#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,18 +49,18 @@ static void thd_body(void* arg) {
/* ------------------------------------------------- */
int main(int argc, char* argv[]) {
- int i;
- gpr_thd_id threads[NUM_THREADS];
+ grpc_core::Thread threads[NUM_THREADS];
grpc_test_init(argc, argv);
gpr_tls_init(&test_var);
- for (i = 0; i < NUM_THREADS; i++) {
- gpr_thd_new(&threads[i], "grpc_tls_test", thd_body, nullptr);
+ 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);