Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: util/synchronization/semaphore_test.cc

Issue 840933004: win: port semaphore test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@disable-clock-test-win
Patch Set: fix assertions Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "util/synchronization/semaphore.h" 15 #include "util/synchronization/semaphore.h"
16 16
17 #if defined(OS_POSIX)
17 #include <pthread.h> 18 #include <pthread.h>
19 #endif // OS_POSIX
18 20
19 #include "gtest/gtest.h" 21 #include "gtest/gtest.h"
20 22
21 namespace crashpad { 23 namespace crashpad {
22 namespace test { 24 namespace test {
23 namespace { 25 namespace {
24 26
25 TEST(Semaphore, Simple) { 27 TEST(Semaphore, Simple) {
26 Semaphore semaphore(1); 28 Semaphore semaphore(1);
27 semaphore.Wait(); 29 semaphore.Wait();
28 semaphore.Signal(); 30 semaphore.Signal();
29 } 31 }
30 32
31 struct ThreadMainInfo { 33 struct ThreadMainInfo {
34 #if defined(OS_POSIX)
32 pthread_t pthread; 35 pthread_t pthread;
36 #elif defined(OS_WIN)
37 HANDLE thread;
38 #endif
33 Semaphore* semaphore; 39 Semaphore* semaphore;
34 size_t iterations; 40 size_t iterations;
35 }; 41 };
36 42
37 void* ThreadMain(void* argument) { 43 #if defined(OS_POSIX)
44 void*
45 #elif defined(OS_WIN)
46 DWORD WINAPI
47 #endif // OS_POSIX
48 ThreadMain(void* argument) {
38 ThreadMainInfo* info = reinterpret_cast<ThreadMainInfo*>(argument); 49 ThreadMainInfo* info = reinterpret_cast<ThreadMainInfo*>(argument);
39 for (size_t iteration = 0; iteration < info->iterations; ++iteration) { 50 for (size_t iteration = 0; iteration < info->iterations; ++iteration) {
40 info->semaphore->Wait(); 51 info->semaphore->Wait();
41 } 52 }
53 #if defined(OS_POSIX)
42 return nullptr; 54 return nullptr;
55 #elif defined(OS_WIN)
56 return 0;
57 #endif // OS_POSIX
58 }
59
60 void StartThread(ThreadMainInfo* info) {
61 #if defined(OS_POSIX)
62 int rv = pthread_create(&info->pthread, nullptr, ThreadMain, info);
63 ASSERT_EQ(0, rv) << "pthread_create";
64 #elif defined(OS_WIN)
65 info->thread = CreateThread(nullptr, 0, ThreadMain, info, 0, nullptr);
66 ASSERT_NE(nullptr, info->thread) << "CreateThread";
67 #endif // OS_POSIX
68 }
69
70 void JoinThread(ThreadMainInfo* info) {
71 #if defined(OS_POSIX)
72 int rv = pthread_join(info->pthread, nullptr);
73 EXPECT_EQ(0, rv) << "pthread_join";
74 #elif defined(OS_WIN)
75 DWORD result = WaitForSingleObject(info->thread, INFINITE);
76 EXPECT_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject";
77 #endif // OS_POSIX
43 } 78 }
44 79
45 TEST(Semaphore, Threaded) { 80 TEST(Semaphore, Threaded) {
46 Semaphore semaphore(0); 81 Semaphore semaphore(0);
47 ThreadMainInfo info; 82 ThreadMainInfo info;
48 info.semaphore = &semaphore; 83 info.semaphore = &semaphore;
49 info.iterations = 1; 84 info.iterations = 1;
50 85
51 int rv = pthread_create(&info.pthread, nullptr, ThreadMain, &info); 86 ASSERT_NO_FATAL_FAILURE(StartThread(&info));
52 ASSERT_EQ(0, rv) << "pthread_create";
53 87
54 semaphore.Signal(); 88 semaphore.Signal();
55 89
56 rv = pthread_join(info.pthread, nullptr); 90 JoinThread(&info);
57 ASSERT_EQ(0, rv) << "pthread_join";
58 } 91 }
59 92
60 TEST(Semaphore, TenThreaded) { 93 TEST(Semaphore, TenThreaded) {
61 // This test has a smaller initial value (5) than threads contending for these 94 // This test has a smaller initial value (5) than threads contending for these
62 // resources (10), and the threads each try to obtain the resource a different 95 // resources (10), and the threads each try to obtain the resource a different
63 // number of times. 96 // number of times.
64 Semaphore semaphore(5); 97 Semaphore semaphore(5);
65 const size_t kThreads = 10; 98 const size_t kThreads = 10;
66 ThreadMainInfo info[kThreads]; 99 ThreadMainInfo info[kThreads];
67 size_t iterations = 0; 100 size_t iterations = 0;
68 int rv;
69 for (size_t index = 0; index < kThreads; ++index) { 101 for (size_t index = 0; index < kThreads; ++index) {
70 info[index].semaphore = &semaphore; 102 info[index].semaphore = &semaphore;
71 info[index].iterations = index; 103 info[index].iterations = index;
72 iterations += info[index].iterations; 104 iterations += info[index].iterations;
73 105
74 rv = 106 ASSERT_NO_FATAL_FAILURE(StartThread(&info[index]));
75 pthread_create(&info[index].pthread, nullptr, ThreadMain, &info[index]);
76 ASSERT_EQ(0, rv) << "pthread_create";
77 } 107 }
78 108
79 for (size_t iteration = 0; iteration < iterations; ++iteration) { 109 for (size_t iteration = 0; iteration < iterations; ++iteration) {
80 semaphore.Signal(); 110 semaphore.Signal();
81 } 111 }
82 112
83 for (size_t index = 0; index < kThreads; ++index) { 113 for (size_t index = 0; index < kThreads; ++index) {
84 rv = pthread_join(info[index].pthread, nullptr); 114 JoinThread(&info[index]);
85 ASSERT_EQ(0, rv) << "pthread_join";
86 } 115 }
87 } 116 }
88 117
89 } // namespace 118 } // namespace
90 } // namespace test 119 } // namespace test
91 } // namespace crashpad 120 } // namespace crashpad
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698