From 6c8a3f7007383a73e0eccd9df12ba86de1575953 Mon Sep 17 00:00:00 2001 From: Mark Mentovai Date: Fri, 24 Mar 2017 16:31:41 -0400 Subject: [PATCH] win: Use a semaphore for synchronization in ProcessSnapshotTest Bug: crashpad:160 Test: crashpad_snapshot_test ProcessSnapshotTest.CrashpadInfoChild* Change-Id: Id9bd1ee6ec71c57ecab2ccba6106d41446d66902 Reviewed-on: https://chromium-review.googlesource.com/458879 Reviewed-by: Scott Graham Commit-Queue: Mark Mentovai --- .../crashpad_snapshot_test_image_reader.cc | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/snapshot/win/crashpad_snapshot_test_image_reader.cc b/snapshot/win/crashpad_snapshot_test_image_reader.cc index 4496a8f8..6f4203d6 100644 --- a/snapshot/win/crashpad_snapshot_test_image_reader.cc +++ b/snapshot/win/crashpad_snapshot_test_image_reader.cc @@ -17,12 +17,14 @@ #include "base/logging.h" #include "client/crashpad_info.h" #include "util/file/file_io.h" +#include "util/synchronization/semaphore.h" #include "util/win/scoped_handle.h" namespace { DWORD WINAPI LotsOfReferencesThreadProc(void* param) { - LONG* count = reinterpret_cast(param); + crashpad::Semaphore* semaphore = + reinterpret_cast(param); // Allocate a bunch of pointers to things on the stack. int* pointers[1000]; @@ -30,7 +32,7 @@ DWORD WINAPI LotsOfReferencesThreadProc(void* param) { pointers[i] = new int[2048]; } - InterlockedIncrement(count); + semaphore->Signal(); Sleep(INFINITE); return 0; } @@ -48,13 +50,13 @@ int wmain(int argc, wchar_t* argv[]) { // Create threads with lots of stack pointers to memory. This is used to // verify the cap on pointed-to memory. - LONG thread_ready_count = 0; + crashpad::Semaphore semaphore(0); crashpad::ScopedKernelHANDLE threads[100]; for (int i = 0; i < arraysize(threads); ++i) { threads[i].reset(CreateThread(nullptr, 0, &LotsOfReferencesThreadProc, - reinterpret_cast(&thread_ready_count), + reinterpret_cast(&semaphore), 0, nullptr)); if (!threads[i].is_valid()) { @@ -63,14 +65,8 @@ int wmain(int argc, wchar_t* argv[]) { } } - for (;;) { - if (InterlockedCompareExchange(&thread_ready_count, - thread_ready_count, - arraysize(threads)) == arraysize(threads)) { - // All threads have allocated their references. - break; - } - Sleep(10); + for (int i = 0; i < arraysize(threads); ++i) { + semaphore.Wait(); } crashpad::CrashpadInfo* crashpad_info =