| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (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 |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/synchronization/semaphore.h" |
| 16 |
| 17 #if defined(OS_POSIX) |
| 18 #include <pthread.h> |
| 19 #endif // OS_POSIX |
| 20 |
| 21 #include "gtest/gtest.h" |
| 22 |
| 23 namespace crashpad { |
| 24 namespace test { |
| 25 namespace { |
| 26 |
| 27 TEST(Semaphore, Simple) { |
| 28 Semaphore semaphore(1); |
| 29 semaphore.Wait(); |
| 30 semaphore.Signal(); |
| 31 } |
| 32 |
| 33 TEST(Semaphore, TimedWait) { |
| 34 Semaphore semaphore(0); |
| 35 semaphore.Signal(); |
| 36 EXPECT_TRUE(semaphore.TimedWait(0.01)); // 10ms |
| 37 } |
| 38 |
| 39 TEST(Semaphore, TimedWaitTimeout) { |
| 40 Semaphore semaphore(0); |
| 41 EXPECT_FALSE(semaphore.TimedWait(0.01)); // 10ms |
| 42 } |
| 43 |
| 44 struct ThreadMainInfo { |
| 45 #if defined(OS_POSIX) |
| 46 pthread_t pthread; |
| 47 #elif defined(OS_WIN) |
| 48 HANDLE thread; |
| 49 #endif |
| 50 Semaphore* semaphore; |
| 51 size_t iterations; |
| 52 }; |
| 53 |
| 54 #if defined(OS_POSIX) |
| 55 void* |
| 56 #elif defined(OS_WIN) |
| 57 DWORD WINAPI |
| 58 #endif // OS_POSIX |
| 59 ThreadMain(void* argument) { |
| 60 ThreadMainInfo* info = reinterpret_cast<ThreadMainInfo*>(argument); |
| 61 for (size_t iteration = 0; iteration < info->iterations; ++iteration) { |
| 62 info->semaphore->Wait(); |
| 63 } |
| 64 #if defined(OS_POSIX) |
| 65 return nullptr; |
| 66 #elif defined(OS_WIN) |
| 67 return 0; |
| 68 #endif // OS_POSIX |
| 69 } |
| 70 |
| 71 void StartThread(ThreadMainInfo* info) { |
| 72 #if defined(OS_POSIX) |
| 73 int rv = pthread_create(&info->pthread, nullptr, ThreadMain, info); |
| 74 ASSERT_EQ(0, rv) << "pthread_create"; |
| 75 #elif defined(OS_WIN) |
| 76 info->thread = CreateThread(nullptr, 0, ThreadMain, info, 0, nullptr); |
| 77 ASSERT_NE(nullptr, info->thread) << "CreateThread"; |
| 78 #endif // OS_POSIX |
| 79 } |
| 80 |
| 81 void JoinThread(ThreadMainInfo* info) { |
| 82 #if defined(OS_POSIX) |
| 83 int rv = pthread_join(info->pthread, nullptr); |
| 84 EXPECT_EQ(0, rv) << "pthread_join"; |
| 85 #elif defined(OS_WIN) |
| 86 DWORD result = WaitForSingleObject(info->thread, INFINITE); |
| 87 EXPECT_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject"; |
| 88 #endif // OS_POSIX |
| 89 } |
| 90 |
| 91 TEST(Semaphore, Threaded) { |
| 92 Semaphore semaphore(0); |
| 93 ThreadMainInfo info; |
| 94 info.semaphore = &semaphore; |
| 95 info.iterations = 1; |
| 96 |
| 97 ASSERT_NO_FATAL_FAILURE(StartThread(&info)); |
| 98 |
| 99 semaphore.Signal(); |
| 100 |
| 101 JoinThread(&info); |
| 102 } |
| 103 |
| 104 TEST(Semaphore, TenThreaded) { |
| 105 // This test has a smaller initial value (5) than threads contending for these |
| 106 // resources (10), and the threads each try to obtain the resource a different |
| 107 // number of times. |
| 108 Semaphore semaphore(5); |
| 109 const size_t kThreads = 10; |
| 110 ThreadMainInfo info[kThreads]; |
| 111 size_t iterations = 0; |
| 112 for (size_t index = 0; index < kThreads; ++index) { |
| 113 info[index].semaphore = &semaphore; |
| 114 info[index].iterations = index; |
| 115 iterations += info[index].iterations; |
| 116 |
| 117 ASSERT_NO_FATAL_FAILURE(StartThread(&info[index])); |
| 118 } |
| 119 |
| 120 for (size_t iteration = 0; iteration < iterations; ++iteration) { |
| 121 semaphore.Signal(); |
| 122 } |
| 123 |
| 124 for (size_t index = 0; index < kThreads; ++index) { |
| 125 JoinThread(&info[index]); |
| 126 } |
| 127 } |
| 128 |
| 129 } // namespace |
| 130 } // namespace test |
| 131 } // namespace crashpad |
| OLD | NEW |