1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/compiler_specific.h"
6 #include "base/threading/platform_thread.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 // Trivial tests that thread runs and doesn't crash on create and join ---------
14 class TrivialThread
: public PlatformThread::Delegate
{
16 TrivialThread() : did_run_(false) {}
18 void ThreadMain() override
{ did_run_
= true; }
20 bool did_run() const { return did_run_
; }
25 DISALLOW_COPY_AND_ASSIGN(TrivialThread
);
28 TEST(PlatformThreadTest
, Trivial
) {
30 PlatformThreadHandle handle
;
32 ASSERT_FALSE(thread
.did_run());
33 ASSERT_TRUE(PlatformThread::Create(0, &thread
, &handle
));
34 PlatformThread::Join(handle
);
35 ASSERT_TRUE(thread
.did_run());
38 TEST(PlatformThreadTest
, TrivialTimesTen
) {
39 TrivialThread thread
[10];
40 PlatformThreadHandle handle
[arraysize(thread
)];
42 for (size_t n
= 0; n
< arraysize(thread
); n
++)
43 ASSERT_FALSE(thread
[n
].did_run());
44 for (size_t n
= 0; n
< arraysize(thread
); n
++)
45 ASSERT_TRUE(PlatformThread::Create(0, &thread
[n
], &handle
[n
]));
46 for (size_t n
= 0; n
< arraysize(thread
); n
++)
47 PlatformThread::Join(handle
[n
]);
48 for (size_t n
= 0; n
< arraysize(thread
); n
++)
49 ASSERT_TRUE(thread
[n
].did_run());
52 // Tests of basic thread functions ---------------------------------------------
54 class FunctionTestThread
: public TrivialThread
{
56 FunctionTestThread() : thread_id_(0) {}
58 void ThreadMain() override
{
59 thread_id_
= PlatformThread::CurrentId();
60 PlatformThread::YieldCurrentThread();
61 PlatformThread::Sleep(TimeDelta::FromMilliseconds(50));
63 // Make sure that the thread ID is the same across calls.
64 EXPECT_EQ(thread_id_
, PlatformThread::CurrentId());
66 TrivialThread::ThreadMain();
69 PlatformThreadId
thread_id() const { return thread_id_
; }
72 PlatformThreadId thread_id_
;
74 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread
);
77 TEST(PlatformThreadTest
, Function
) {
78 PlatformThreadId main_thread_id
= PlatformThread::CurrentId();
80 FunctionTestThread thread
;
81 PlatformThreadHandle handle
;
83 ASSERT_FALSE(thread
.did_run());
84 ASSERT_TRUE(PlatformThread::Create(0, &thread
, &handle
));
85 PlatformThread::Join(handle
);
86 ASSERT_TRUE(thread
.did_run());
87 EXPECT_NE(thread
.thread_id(), main_thread_id
);
89 // Make sure that the thread ID is the same across calls.
90 EXPECT_EQ(main_thread_id
, PlatformThread::CurrentId());
93 TEST(PlatformThreadTest
, FunctionTimesTen
) {
94 PlatformThreadId main_thread_id
= PlatformThread::CurrentId();
96 FunctionTestThread thread
[10];
97 PlatformThreadHandle handle
[arraysize(thread
)];
99 for (size_t n
= 0; n
< arraysize(thread
); n
++)
100 ASSERT_FALSE(thread
[n
].did_run());
101 for (size_t n
= 0; n
< arraysize(thread
); n
++)
102 ASSERT_TRUE(PlatformThread::Create(0, &thread
[n
], &handle
[n
]));
103 for (size_t n
= 0; n
< arraysize(thread
); n
++)
104 PlatformThread::Join(handle
[n
]);
105 for (size_t n
= 0; n
< arraysize(thread
); n
++) {
106 ASSERT_TRUE(thread
[n
].did_run());
107 EXPECT_NE(thread
[n
].thread_id(), main_thread_id
);
109 // Make sure no two threads get the same ID.
110 for (size_t i
= 0; i
< n
; ++i
) {
111 EXPECT_NE(thread
[i
].thread_id(), thread
[n
].thread_id());
115 // Make sure that the thread ID is the same across calls.
116 EXPECT_EQ(main_thread_id
, PlatformThread::CurrentId());