From 634816055f51c536d24dea30dfe930b7fe2fa603 Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Mon, 1 Dec 2014 17:41:04 -0800 Subject: support _POSIX_REALTIME_SIGNALS Bug: 18489947 Change-Id: I2e834d68bc10ca5fc7ebde047b517a3074179475 --- tests/pthread_test.cpp | 59 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 18 deletions(-) (limited to 'tests/pthread_test.cpp') diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp index 797468e..f63d1ee 100644 --- a/tests/pthread_test.cpp +++ b/tests/pthread_test.cpp @@ -178,17 +178,34 @@ static void* IdFn(void* arg) { return arg; } -static void* SleepFn(void* arg) { - sleep(reinterpret_cast(arg)); - return NULL; -} +class SpinFunctionHelper { + public: + SpinFunctionHelper() { + SpinFunctionHelper::spin_flag_ = true; + } + ~SpinFunctionHelper() { + UnSpin(); + } + auto GetFunction() -> void* (*)(void*) { + return SpinFunctionHelper::SpinFn; + } -static void* SpinFn(void* arg) { - volatile bool* b = reinterpret_cast(arg); - while (!*b) { + void UnSpin() { + SpinFunctionHelper::spin_flag_ = false; } - return NULL; -} + + private: + static void* SpinFn(void*) { + while (spin_flag_) {} + return NULL; + } + static volatile bool spin_flag_; +}; + +// It doesn't matter if spin_flag_ is used in several tests, +// because it is always set to false after each test. Each thread +// loops on spin_flag_ can find it becomes false at some time. +volatile bool SpinFunctionHelper::spin_flag_ = false; static void* JoinFn(void* arg) { return reinterpret_cast(pthread_join(reinterpret_cast(arg), NULL)); @@ -229,8 +246,10 @@ TEST(pthread, pthread_create_EAGAIN) { } TEST(pthread, pthread_no_join_after_detach) { + SpinFunctionHelper spinhelper; + pthread_t t1; - ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast(5))); + ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); // After a pthread_detach... ASSERT_EQ(0, pthread_detach(t1)); @@ -241,10 +260,10 @@ TEST(pthread, pthread_no_join_after_detach) { } TEST(pthread, pthread_no_op_detach_after_join) { - bool done = false; + SpinFunctionHelper spinhelper; pthread_t t1; - ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); + ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); // If thread 2 is already waiting to join thread 1... pthread_t t2; @@ -256,7 +275,7 @@ TEST(pthread, pthread_no_op_detach_after_join) { ASSERT_EQ(0, pthread_detach(t1)); AssertDetached(t1, false); - done = true; + spinhelper.UnSpin(); // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). void* join_result; @@ -369,8 +388,10 @@ TEST(pthread, pthread_setname_np__self) { } TEST(pthread, pthread_setname_np__other) { + SpinFunctionHelper spinhelper; + pthread_t t1; - ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast(5))); + ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); ASSERT_EQ(0, pthread_setname_np(t1, "short 2")); } @@ -451,8 +472,10 @@ TEST(pthread, pthread_detach__leak) { } TEST(pthread, pthread_getcpuclockid__clock_gettime) { + SpinFunctionHelper spinhelper; + pthread_t t; - ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast(5))); + ASSERT_EQ(0, pthread_create(&t, NULL, spinhelper.GetFunction(), NULL)); clockid_t c; ASSERT_EQ(0, pthread_getcpuclockid(t, &c)); @@ -501,10 +524,10 @@ TEST(pthread, pthread_kill__no_such_thread) { } TEST(pthread, pthread_join__multijoin) { - bool done = false; + SpinFunctionHelper spinhelper; pthread_t t1; - ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done)); + ASSERT_EQ(0, pthread_create(&t1, NULL, spinhelper.GetFunction(), NULL)); pthread_t t2; ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast(t1))); @@ -514,7 +537,7 @@ TEST(pthread, pthread_join__multijoin) { // Multiple joins to the same thread should fail. ASSERT_EQ(EINVAL, pthread_join(t1, NULL)); - done = true; + spinhelper.UnSpin(); // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes). void* join_result; -- cgit v1.1