diff options
author | Elliott Hughes <enh@google.com> | 2012-09-07 16:47:54 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2012-09-07 16:47:54 -0700 |
commit | 4d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8 (patch) | |
tree | c64212fa81690ddccfcf6d78db03e1af81e263b4 /tests/pthread_test.cpp | |
parent | d10309c36b93176a473baeeddef6f3fba8bf0678 (diff) | |
download | android_bionic-4d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8.tar.gz android_bionic-4d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8.tar.bz2 android_bionic-4d014e15b44d3e8d1b0189bc9bb7b0d0685e5af8.zip |
Add more pthreads tests.
Someone reported a bug if pthread_detach is called while a pthread_join is
already in progress, but I'm unable to reproduce it. Keep the tests I wrote,
though.
Change-Id: I3d71450bbbb5345f2cb213dc56310ec020d528cc
Diffstat (limited to 'tests/pthread_test.cpp')
-rw-r--r-- | tests/pthread_test.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp index 04975df2f..9a474c0a5 100644 --- a/tests/pthread_test.cpp +++ b/tests/pthread_test.cpp @@ -18,6 +18,7 @@ #include <errno.h> #include <pthread.h> +#include <unistd.h> TEST(pthread, pthread_key_create) { pthread_key_t key; @@ -26,3 +27,56 @@ TEST(pthread, pthread_key_create) { // Can't delete a key that's already been deleted. ASSERT_EQ(EINVAL, pthread_key_delete(key)); } + +static void* IdFn(void* arg) { + return arg; +} + +static void* SleepFn(void* arg) { + sleep(reinterpret_cast<unsigned int>(arg)); + return NULL; +} + +static void* JoinFn(void* arg) { + return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL)); +} + +TEST(pthread, pthread_create) { + void* expected_result = reinterpret_cast<void*>(123); + // Can we create a thread? + pthread_t t; + ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result)); + // If we join, do we get the expected value back? + void* result; + ASSERT_EQ(0, pthread_join(t, &result)); + ASSERT_EQ(expected_result, result); +} + +TEST(pthread, pthread_no_join_after_detach) { + pthread_t t1; + ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5))); + + // After a pthread_detach... + ASSERT_EQ(0, pthread_detach(t1)); + + // ...pthread_join should fail. + void* result; + ASSERT_EQ(EINVAL, pthread_join(t1, &result)); +} + +TEST(pthread, pthread_no_op_detach_after_join) { + pthread_t t1; + ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(1))); + + // If thread 2 is already waiting to join thread 1... + pthread_t t2; + ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1))); + + // ...a call to pthread_detach on thread 1 will "succeed"... + ASSERT_EQ(0, pthread_detach(t1)); + + // ...but the join still goes ahead. + void* join_result; + ASSERT_EQ(0, pthread_join(t2, &join_result)); + ASSERT_EQ(EINVAL, reinterpret_cast<int>(join_result)); +} |