cpu_thread_test.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2017 The LibYuv Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <gtest/gtest.h>
  11. #include "libyuv/cpu_id.h"
  12. #if defined(__clang__) && !defined(__wasm__)
  13. #if __has_include(<pthread.h>)
  14. #define LIBYUV_HAVE_PTHREAD 1
  15. #endif
  16. #elif defined(__linux__)
  17. #define LIBYUV_HAVE_PTHREAD 1
  18. #endif
  19. #ifdef LIBYUV_HAVE_PTHREAD
  20. #include <pthread.h>
  21. #endif
  22. namespace libyuv {
  23. #ifdef LIBYUV_HAVE_PTHREAD
  24. void* ThreadMain(void* arg) {
  25. int* flags = static_cast<int*>(arg);
  26. *flags = TestCpuFlag(kCpuInitialized);
  27. return nullptr;
  28. }
  29. #endif // LIBYUV_HAVE_PTHREAD
  30. // Call TestCpuFlag() from two threads. ThreadSanitizer should not report any
  31. // data race.
  32. TEST(LibYUVCpuThreadTest, TestCpuFlagMultipleThreads) {
  33. #ifdef LIBYUV_HAVE_PTHREAD
  34. int cpu_flags1;
  35. int cpu_flags2;
  36. int ret;
  37. pthread_t thread1;
  38. pthread_t thread2;
  39. MaskCpuFlags(0); // Reset to 0 to allow auto detect.
  40. ret = pthread_create(&thread1, nullptr, ThreadMain, &cpu_flags1);
  41. ASSERT_EQ(ret, 0);
  42. ret = pthread_create(&thread2, nullptr, ThreadMain, &cpu_flags2);
  43. ASSERT_EQ(ret, 0);
  44. ret = pthread_join(thread1, nullptr);
  45. EXPECT_EQ(ret, 0);
  46. ret = pthread_join(thread2, nullptr);
  47. EXPECT_EQ(ret, 0);
  48. EXPECT_EQ(cpu_flags1, cpu_flags2);
  49. #else
  50. printf("pthread unavailable; Test skipped.");
  51. #endif // LIBYUV_HAVE_PTHREAD
  52. }
  53. } // namespace libyuv