summaryrefslogtreecommitdiffstats
path: root/tests/pthread_test.cpp
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2014-12-01 17:41:04 -0800
committerYabin Cui <yabinc@google.com>2014-12-08 21:52:43 -0800
commit634816055f51c536d24dea30dfe930b7fe2fa603 (patch)
tree5d3739b116df16774776010538254c10007c0a76 /tests/pthread_test.cpp
parentf64c43ba6c9244c50e904961dc432f04b1dfcfd9 (diff)
downloadbionic-634816055f51c536d24dea30dfe930b7fe2fa603.zip
bionic-634816055f51c536d24dea30dfe930b7fe2fa603.tar.gz
bionic-634816055f51c536d24dea30dfe930b7fe2fa603.tar.bz2
support _POSIX_REALTIME_SIGNALS
Bug: 18489947 Change-Id: I2e834d68bc10ca5fc7ebde047b517a3074179475
Diffstat (limited to 'tests/pthread_test.cpp')
-rw-r--r--tests/pthread_test.cpp59
1 files changed, 41 insertions, 18 deletions
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<uintptr_t>(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<volatile bool*>(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<void*>(pthread_join(reinterpret_cast<pthread_t>(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<void*>(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<void*>(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<void*>(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<void*>(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;