ThreadTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <gtest/gtest.h>
  17. #include <pthread.h>
  18. #include <unistd.h>
  19. #include <utility>
  20. #include "Action.h"
  21. #include "Pointers.h"
  22. #include "Thread.h"
  23. typedef std::pair<Thread*, volatile bool*> thread_data_t;
  24. TEST(ThreadTest, ready) {
  25. Thread thread;
  26. // A thread should be ready immediately. If not, this will hang forever.
  27. thread.WaitForReady();
  28. }
  29. void* ThreadWaitForReady(void* data) {
  30. thread_data_t* thread_data = reinterpret_cast<thread_data_t*>(data);
  31. Thread* thread = thread_data->first;
  32. volatile bool* finish = thread_data->second;
  33. thread->WaitForReady();
  34. *finish = true;
  35. return nullptr;
  36. }
  37. TEST(ThreadTest, ready_thread) {
  38. Thread thread;
  39. volatile bool finish = false;
  40. thread_data_t thread_data = std::make_pair(&thread, &finish);
  41. thread.SetPending();
  42. pthread_t thread_id;
  43. ASSERT_TRUE(pthread_create(&thread_id, nullptr, ThreadWaitForReady, &thread_data) == 0);
  44. ASSERT_FALSE(finish);
  45. sleep(1);
  46. ASSERT_FALSE(finish);
  47. thread.ClearPending();
  48. ASSERT_TRUE(pthread_join(thread_id, nullptr) == 0);
  49. ASSERT_TRUE(finish);
  50. }
  51. void* ThreadWaitForPending(void* data) {
  52. thread_data_t* thread_data = reinterpret_cast<thread_data_t*>(data);
  53. Thread* thread = thread_data->first;
  54. volatile bool* finish = thread_data->second;
  55. thread->WaitForPending();
  56. *finish = true;
  57. return nullptr;
  58. }
  59. TEST(ThreadTest, pending) {
  60. Thread thread;
  61. volatile bool finish = false;
  62. thread_data_t thread_data = std::make_pair(&thread, &finish);
  63. pthread_t thread_id;
  64. ASSERT_TRUE(pthread_create(&thread_id, nullptr, ThreadWaitForPending, &thread_data) == 0);
  65. ASSERT_FALSE(finish);
  66. sleep(1);
  67. ASSERT_FALSE(finish);
  68. thread.SetPending();
  69. ASSERT_TRUE(pthread_join(thread_id, nullptr) == 0);
  70. ASSERT_TRUE(finish);
  71. }
  72. TEST(ThreadTest, pointers) {
  73. Pointers pointers(2);
  74. Thread thread;
  75. ASSERT_TRUE(thread.pointers() == nullptr);
  76. thread.set_pointers(&pointers);
  77. ASSERT_TRUE(thread.pointers() == &pointers);
  78. }
  79. TEST(ThreadTest, action) {
  80. Thread thread;
  81. Action* action = thread.CreateAction(0x1234, "thread_done", "");
  82. ASSERT_EQ(action, thread.GetAction());
  83. // Verify the action object is not garbage.
  84. action->Execute(nullptr);
  85. ASSERT_TRUE(action->EndThread());
  86. ASSERT_FALSE(action->DoesFree());
  87. }