crashpad/util/synchronization/semaphore_test.cc
Mark Mentovai 5d74f120fc Convert NULL to nullptr.
This change was generated mechanically by running:

  find . \( -name \*.cc -or -name \*.mm -or -name \*.h \) \
      -and -not -path ./third_party/\* -and -not -path ./out/\* \
      -exec sed -i '' -E -e 's/(^|[^_])NULL/\1nullptr/g' {} +

Further manual fix-ups were applied to remove casts of nullptr to other
pointer types where possible, to preserve the intentional use of NULL
(as a short form of MACH_PORT_NULL) in exception_port_tool, and to fix
80-column violations.

https://groups.google.com/a/chromium.org/d/topic/chromium-dev/4mijeJHzxLg/discussion

TEST=*_test
R=rsesek@chromium.org

Review URL: https://codereview.chromium.org/656703002
2014-10-14 11:10:45 -04:00

92 lines
2.4 KiB
C++

// Copyright 2014 The Crashpad Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "util/synchronization/semaphore.h"
#include <pthread.h>
#include "gtest/gtest.h"
namespace crashpad {
namespace test {
namespace {
TEST(Semaphore, Simple) {
Semaphore semaphore(1);
semaphore.Wait();
semaphore.Signal();
}
struct ThreadMainInfo {
pthread_t pthread;
Semaphore* semaphore;
size_t iterations;
};
void* ThreadMain(void* argument) {
ThreadMainInfo* info = reinterpret_cast<ThreadMainInfo*>(argument);
for (size_t iteration = 0; iteration < info->iterations; ++iteration) {
info->semaphore->Wait();
}
return nullptr;
}
TEST(Semaphore, Threaded) {
Semaphore semaphore(0);
ThreadMainInfo info;
info.semaphore = &semaphore;
info.iterations = 1;
int rv = pthread_create(&info.pthread, nullptr, ThreadMain, &info);
ASSERT_EQ(0, rv) << "pthread_create";
semaphore.Signal();
rv = pthread_join(info.pthread, nullptr);
ASSERT_EQ(0, rv) << "pthread_join";
}
TEST(Semaphore, TenThreaded) {
// This test has a smaller initial value (5) than threads contending for these
// resources (10), and the threads each try to obtain the resource a different
// number of times.
Semaphore semaphore(5);
const size_t kThreads = 10;
ThreadMainInfo info[kThreads];
size_t iterations = 0;
int rv;
for (size_t index = 0; index < kThreads; ++index) {
info[index].semaphore = &semaphore;
info[index].iterations = index;
iterations += info[index].iterations;
rv =
pthread_create(&info[index].pthread, nullptr, ThreadMain, &info[index]);
ASSERT_EQ(0, rv) << "pthread_create";
}
for (size_t iteration = 0; iteration < iterations; ++iteration) {
semaphore.Signal();
}
for (size_t index = 0; index < kThreads; ++index) {
rv = pthread_join(info[index].pthread, nullptr);
ASSERT_EQ(0, rv) << "pthread_join";
}
}
} // namespace
} // namespace test
} // namespace crashpad