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 
11 #include <gtest/gtest.h>
12 
13 #include "libyuv/cpu_id.h"
14 
15 #if defined(__clang__)
16 #if __has_include(<pthread.h>)
17 #define LIBYUV_HAVE_PTHREAD 1
18 #endif
19 #elif defined(__linux__)
20 #define LIBYUV_HAVE_PTHREAD 1
21 #endif
22 
23 #ifdef LIBYUV_HAVE_PTHREAD
24 #include <pthread.h>
25 #endif
26 
27 namespace libyuv {
28 
29 #ifdef LIBYUV_HAVE_PTHREAD
ThreadMain(void * arg)30 void* ThreadMain(void* arg) {
31   int* flags = static_cast<int*>(arg);
32 
33   *flags = TestCpuFlag(kCpuHasSSSE3);
34   return nullptr;
35 }
36 #endif  // LIBYUV_HAVE_PTHREAD
37 
38 // Call TestCpuFlag() from two threads. ThreadSanitizer should not report any
39 // data race.
TEST(LibYUVCpuThreadTest,TestCpuFlagMultipleThreads)40 TEST(LibYUVCpuThreadTest, TestCpuFlagMultipleThreads) {
41 #ifdef LIBYUV_HAVE_PTHREAD
42   int cpu_flags1;
43   int cpu_flags2;
44   int ret;
45   pthread_t thread1;
46   pthread_t thread2;
47 
48   MaskCpuFlags(0);  // Reset to 0 to allow auto detect.
49   ret = pthread_create(&thread1, nullptr, ThreadMain, &cpu_flags1);
50   ASSERT_EQ(ret, 0);
51   ret = pthread_create(&thread2, nullptr, ThreadMain, &cpu_flags2);
52   ASSERT_EQ(ret, 0);
53   ret = pthread_join(thread1, nullptr);
54   EXPECT_EQ(ret, 0);
55   ret = pthread_join(thread2, nullptr);
56   EXPECT_EQ(ret, 0);
57   EXPECT_EQ(cpu_flags1, cpu_flags2);
58 #else
59   printf("pthread unavailable; Test skipped.");
60 #endif  // LIBYUV_HAVE_PTHREAD
61 }
62 
63 }  // namespace libyuv
64