crashpad/util/synchronization/semaphore_test.cc
Mark Mentovai 50ed179e9a Use BUILDFLAG for OS checking
Use BUILDFLAG(IS_*) instead of defined(OS_*).

This was generated mostly mechnically by performing the following steps:
 - sed -i '' -E -e 's/defined\(OS_/BUILDFLAG(IS_/g' \
                -e 's%([ !])OS_([A-Z]+)%\1BUILDFLAG(IS_\2)%g' \
       $(git grep -l 'OS_'
         '**/*.c' '**/*.cc' '**/*.h' '**/*.m' '**/*.mm')
 - sed -i '' -e 's/#ifdef BUILDFLAG(/#if BUILDFLAG(/' \
       $(git grep -l '#ifdef BUILDFLAG('
         '**/*.c' '**/*.cc' '**/*.h' '**/*.m' '**/*.mm')
 - gsed -i -z -E -e \
       's%(.*)#include "%\1#include "build/buildflag.h"\n#include "%' \
       $(git grep -l 'BUILDFLAG(IS_'
         '**/*.c' '**/*.cc' '**/*.h' '**/*.m' '**/*.mm')
 - Spot checks to move #include "build/buildflag.h" to the correct parts
   of files.
 - sed -i '' -E -e \
       's%^(#include "build/buildflag.h")$%#include "build/build_config.h"\n\1%' \
       $(grep -L '^#include "build/build_config.h"$'
         $(git grep -l 'BUILDFLAG(IS_'
           '**/*.c' '**/*.cc' '**/*.h' '**/*.m' '**/*.mm'))
 - Add “clang-format off” around tool usage messages.
 - git cl format
 - Update mini_chromium to 85ba51f98278 (intermediate step).
   TESTING ONLY).
 - for f in $(git grep -l '^#include "build/buildflag.h"$'
              '**/*.c' '**/*.cc' '**/*.h' '**/*.m' '**/*.mm'); do \
       grep -v '^#include "build/buildflag.h"$' "${f}" > /tmp/z; \
       cp /tmp/z "${f}"; done
 - git cl format
 - Update mini_chromium to 735143774c5f (intermediate step).
 - Update mini_chromium to f41420eb45fa (as checked in).
 - Update mini_chromium to 6e2f204b4ae1 (as checked in).

For ease of review and inspection, each of these steps is uploaded as a
new patch set in a review series.

This includes an update of mini_chromium to 6e2f204b4ae1:

f41420eb45fa Use BUILDFLAG for OS checking
6e2f204b4ae1 Include what you use: string_util.h uses build_config.h

Bug: chromium:1234043
Change-Id: Ieef86186f094c64e59b853729737e36982f8cf69
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/3400258
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
Commit-Queue: Mark Mentovai <mark@chromium.org>
2022-01-19 20:21:19 +00:00

150 lines
3.9 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 <sys/types.h>
#include "base/cxx17_backports.h"
#include "build/build_config.h"
#include "gtest/gtest.h"
#if BUILDFLAG(IS_POSIX)
#include <pthread.h>
#endif // BUILDFLAG(IS_POSIX)
namespace crashpad {
namespace test {
namespace {
TEST(Semaphore, Simple) {
Semaphore semaphore(1);
semaphore.Wait();
semaphore.Signal();
}
TEST(Semaphore, TimedWait) {
Semaphore semaphore(0);
semaphore.Signal();
EXPECT_TRUE(semaphore.TimedWait(0.01)); // 10ms
}
TEST(Semaphore, TimedWaitTimeout) {
Semaphore semaphore(0);
semaphore.Signal();
constexpr double kTenMs = 0.01;
EXPECT_TRUE(semaphore.TimedWait(kTenMs));
EXPECT_FALSE(semaphore.TimedWait(kTenMs));
}
TEST(Semaphore, TimedWaitInfinite_0) {
Semaphore semaphore(0);
semaphore.Signal();
EXPECT_TRUE(semaphore.TimedWait(std::numeric_limits<double>::infinity()));
}
TEST(Semaphore, TimedWaitInfinite_1) {
Semaphore semaphore(1);
EXPECT_TRUE(semaphore.TimedWait(std::numeric_limits<double>::infinity()));
semaphore.Signal();
}
struct ThreadMainInfo {
#if BUILDFLAG(IS_POSIX)
pthread_t pthread;
#elif BUILDFLAG(IS_WIN)
HANDLE thread;
#endif
Semaphore* semaphore;
size_t iterations;
};
#if BUILDFLAG(IS_POSIX)
void*
#elif BUILDFLAG(IS_WIN)
DWORD WINAPI
#endif // BUILDFLAG(IS_POSIX)
ThreadMain(void* argument) {
ThreadMainInfo* info = reinterpret_cast<ThreadMainInfo*>(argument);
for (size_t iteration = 0; iteration < info->iterations; ++iteration) {
info->semaphore->Wait();
}
#if BUILDFLAG(IS_POSIX)
return nullptr;
#elif BUILDFLAG(IS_WIN)
return 0;
#endif // BUILDFLAG(IS_POSIX)
}
void StartThread(ThreadMainInfo* info) {
#if BUILDFLAG(IS_POSIX)
int rv = pthread_create(&info->pthread, nullptr, ThreadMain, info);
ASSERT_EQ(rv, 0) << "pthread_create";
#elif BUILDFLAG(IS_WIN)
info->thread = CreateThread(nullptr, 0, ThreadMain, info, 0, nullptr);
ASSERT_NE(info->thread, nullptr) << "CreateThread";
#endif // BUILDFLAG(IS_POSIX)
}
void JoinThread(ThreadMainInfo* info) {
#if BUILDFLAG(IS_POSIX)
int rv = pthread_join(info->pthread, nullptr);
EXPECT_EQ(rv, 0) << "pthread_join";
#elif BUILDFLAG(IS_WIN)
DWORD result = WaitForSingleObject(info->thread, INFINITE);
EXPECT_EQ(result, WAIT_OBJECT_0) << "WaitForSingleObject";
#endif // BUILDFLAG(IS_POSIX)
}
TEST(Semaphore, Threaded) {
Semaphore semaphore(0);
ThreadMainInfo info;
info.semaphore = &semaphore;
info.iterations = 1;
ASSERT_NO_FATAL_FAILURE(StartThread(&info));
semaphore.Signal();
JoinThread(&info);
}
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);
ThreadMainInfo info[10];
size_t iterations = 0;
for (size_t index = 0; index < base::size(info); ++index) {
info[index].semaphore = &semaphore;
info[index].iterations = index;
iterations += info[index].iterations;
ASSERT_NO_FATAL_FAILURE(StartThread(&info[index]));
}
for (size_t iteration = 0; iteration < iterations; ++iteration) {
semaphore.Signal();
}
for (size_t index = 0; index < base::size(info); ++index) {
JoinThread(&info[index]);
}
}
} // namespace
} // namespace test
} // namespace crashpad