| OLD | NEW |
| 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, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 pthread_t pthread; | 32 pthread_t pthread; |
| 33 Semaphore* semaphore; | 33 Semaphore* semaphore; |
| 34 size_t iterations; | 34 size_t iterations; |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 void* ThreadMain(void* argument) { | 37 void* ThreadMain(void* argument) { |
| 38 ThreadMainInfo* info = reinterpret_cast<ThreadMainInfo*>(argument); | 38 ThreadMainInfo* info = reinterpret_cast<ThreadMainInfo*>(argument); |
| 39 for (size_t iteration = 0; iteration < info->iterations; ++iteration) { | 39 for (size_t iteration = 0; iteration < info->iterations; ++iteration) { |
| 40 info->semaphore->Wait(); | 40 info->semaphore->Wait(); |
| 41 } | 41 } |
| 42 return NULL; | 42 return nullptr; |
| 43 } | 43 } |
| 44 | 44 |
| 45 TEST(Semaphore, Threaded) { | 45 TEST(Semaphore, Threaded) { |
| 46 Semaphore semaphore(0); | 46 Semaphore semaphore(0); |
| 47 ThreadMainInfo info; | 47 ThreadMainInfo info; |
| 48 info.semaphore = &semaphore; | 48 info.semaphore = &semaphore; |
| 49 info.iterations = 1; | 49 info.iterations = 1; |
| 50 | 50 |
| 51 int rv = pthread_create(&info.pthread, NULL, ThreadMain, &info); | 51 int rv = pthread_create(&info.pthread, nullptr, ThreadMain, &info); |
| 52 ASSERT_EQ(0, rv) << "pthread_create"; | 52 ASSERT_EQ(0, rv) << "pthread_create"; |
| 53 | 53 |
| 54 semaphore.Signal(); | 54 semaphore.Signal(); |
| 55 | 55 |
| 56 rv = pthread_join(info.pthread, NULL); | 56 rv = pthread_join(info.pthread, nullptr); |
| 57 ASSERT_EQ(0, rv) << "pthread_join"; | 57 ASSERT_EQ(0, rv) << "pthread_join"; |
| 58 } | 58 } |
| 59 | 59 |
| 60 TEST(Semaphore, TenThreaded) { | 60 TEST(Semaphore, TenThreaded) { |
| 61 // This test has a smaller initial value (5) than threads contending for these | 61 // 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 | 62 // resources (10), and the threads each try to obtain the resource a different |
| 63 // number of times. | 63 // number of times. |
| 64 Semaphore semaphore(5); | 64 Semaphore semaphore(5); |
| 65 const size_t kThreads = 10; | 65 const size_t kThreads = 10; |
| 66 ThreadMainInfo info[kThreads]; | 66 ThreadMainInfo info[kThreads]; |
| 67 size_t iterations = 0; | 67 size_t iterations = 0; |
| 68 int rv; | 68 int rv; |
| 69 for (size_t index = 0; index < kThreads; ++index) { | 69 for (size_t index = 0; index < kThreads; ++index) { |
| 70 info[index].semaphore = &semaphore; | 70 info[index].semaphore = &semaphore; |
| 71 info[index].iterations = index; | 71 info[index].iterations = index; |
| 72 iterations += info[index].iterations; | 72 iterations += info[index].iterations; |
| 73 | 73 |
| 74 rv = pthread_create(&info[index].pthread, NULL, ThreadMain, &info[index]); | 74 rv = |
| 75 pthread_create(&info[index].pthread, nullptr, ThreadMain, &info[index]); |
| 75 ASSERT_EQ(0, rv) << "pthread_create"; | 76 ASSERT_EQ(0, rv) << "pthread_create"; |
| 76 } | 77 } |
| 77 | 78 |
| 78 for (size_t iteration = 0; iteration < iterations; ++iteration) { | 79 for (size_t iteration = 0; iteration < iterations; ++iteration) { |
| 79 semaphore.Signal(); | 80 semaphore.Signal(); |
| 80 } | 81 } |
| 81 | 82 |
| 82 for (size_t index = 0; index < kThreads; ++index) { | 83 for (size_t index = 0; index < kThreads; ++index) { |
| 83 rv = pthread_join(info[index].pthread, NULL); | 84 rv = pthread_join(info[index].pthread, nullptr); |
| 84 ASSERT_EQ(0, rv) << "pthread_join"; | 85 ASSERT_EQ(0, rv) << "pthread_join"; |
| 85 } | 86 } |
| 86 } | 87 } |
| 87 | 88 |
| 88 } // namespace | 89 } // namespace |
| 89 } // namespace test | 90 } // namespace test |
| 90 } // namespace crashpad | 91 } // namespace crashpad |
| OLD | NEW |