OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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. | |
4 | |
5 // Tests of AtomicFlag class. | |
6 | |
7 #include "base/synchronization/atomic_flag.h" | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/threading/thread.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace base { | |
15 | |
16 namespace { | |
17 | |
18 void CancelHelper(AtomicFlag* flag) { | |
danakj
2016/07/20 19:21:01
rename to SetFlag or something?
fdoray
2016/07/21 13:08:33
Done.
| |
19 ASSERT_TRUE(flag); | |
20 #if GTEST_HAS_DEATH_TEST | |
21 ASSERT_DEBUG_DEATH(flag->Set(), ""); | |
fdoray
2016/07/20 16:24:11
TODO: Use EXPECT_DCHECK_DEATH once https://coderev
| |
22 #endif | |
23 } | |
24 | |
25 void ExpectIsNotSet(AtomicFlag* flag) { | |
26 ASSERT_TRUE(flag); | |
27 EXPECT_FALSE(flag->IsSet()); | |
28 } | |
29 | |
30 void ExpectIsSet(AtomicFlag* flag) { | |
31 ASSERT_TRUE(flag); | |
32 EXPECT_TRUE(flag->IsSet()); | |
33 } | |
34 | |
35 } // namespace | |
36 | |
37 TEST(AtomicFlagTest, SimpleSingleThreadedTest) { | |
38 AtomicFlag flag; | |
39 ASSERT_FALSE(flag.IsSet()); | |
40 flag.Set(); | |
41 ASSERT_TRUE(flag.IsSet()); | |
42 } | |
43 | |
44 TEST(AtomicFlagTest, DoubleSetTest) { | |
45 AtomicFlag flag; | |
46 ASSERT_FALSE(flag.IsSet()); | |
47 flag.Set(); | |
48 ASSERT_TRUE(flag.IsSet()); | |
49 flag.Set(); | |
50 ASSERT_TRUE(flag.IsSet()); | |
51 } | |
52 | |
53 TEST(AtomicFlagTest, ReadFromDifferentThread) { | |
54 AtomicFlag flag; | |
55 | |
56 { | |
57 Thread t("AtomicFlagTest.ReadFromDifferentThread.ExpectIsNotSetThread"); | |
58 ASSERT_TRUE(t.Start()); | |
59 t.task_runner()->PostTask(FROM_HERE, Bind(&ExpectIsNotSet, &flag)); | |
60 } | |
61 | |
62 flag.Set(); | |
63 | |
64 { | |
65 Thread t("AtomicFlagTest.ReadFromDifferentThread.ExpectIsSetThread"); | |
66 ASSERT_TRUE(t.Start()); | |
67 t.task_runner()->PostTask(FROM_HERE, Bind(&ExpectIsSet, &flag)); | |
danakj
2016/07/20 19:21:01
post task is a memory barrier so this test is kind
fdoray
2016/07/21 13:08:33
sgtm
gab
2016/07/21 13:27:12
True, then maybe sleep here for say 10ms before ca
fdoray
2016/07/21 14:05:30
sgtm.
I'm still a little bit worried that Sleep,
gab
2016/07/21 14:16:04
I doubt Sleep and Yield result in memory barriers.
fdoray
2016/07/21 15:50:02
Shouldn't the memory be synchronized when the thre
gab
2016/07/21 15:56:42
I don't think memory *has* to be synchronized when
| |
68 } | |
69 } | |
70 | |
71 TEST(AtomicFlagTest, SetOnDifferentThreadDeathTest) { | |
72 // Checks that Set() can't be called from any other thread. | |
73 // AtomicFlag should die on a DCHECK if Set() is called from | |
74 // other thread. | |
75 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | |
76 Thread t("AtomicFlagTest.SetOnDifferentThreadDeathTest"); | |
77 ASSERT_TRUE(t.Start()); | |
78 | |
79 AtomicFlag flag; | |
80 t.task_runner()->PostTask(FROM_HERE, Bind(&CancelHelper, &flag)); | |
81 } | |
82 | |
83 } // namespace base | |
OLD | NEW |