Bug 1615921 - Suspend late write checks in Windows vprintf_stderr r=froydnj

Differential Revision: https://phabricator.services.mozilla.com/D63216

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Doug Thayer 2020-02-21 18:51:37 +00:00
parent 0223215a5b
commit ef59845575
4 changed files with 52 additions and 0 deletions

View File

@ -10,6 +10,7 @@
#include "mozilla/ClearOnShutdown.h" #include "mozilla/ClearOnShutdown.h"
#include "mozilla/FileUtils.h" #include "mozilla/FileUtils.h"
#include "mozilla/LateWriteChecks.h"
#include "mozilla/Mutex.h" #include "mozilla/Mutex.h"
#include "mozilla/StaticPtr.h" #include "mozilla/StaticPtr.h"
#include "mozilla/Printf.h" #include "mozilla/Printf.h"
@ -390,6 +391,7 @@ class LogModuleManager {
void Print(const char* aName, LogLevel aLevel, const TimeStamp* aStart, void Print(const char* aName, LogLevel aLevel, const TimeStamp* aStart,
const char* aFmt, va_list aArgs) MOZ_FORMAT_PRINTF(5, 0) { const char* aFmt, va_list aArgs) MOZ_FORMAT_PRINTF(5, 0) {
AutoSuspendLateWriteChecks suspendLateWriteChecks;
long pid = static_cast<long>(base::GetCurrentProcId()); long pid = static_cast<long>(base::GetCurrentProcId());
const size_t kBuffSize = 1024; const size_t kBuffSize = 1024;
char buff[kBuffSize]; char buff[kBuffSize];

View File

@ -19,6 +19,7 @@
#ifdef XP_WIN #ifdef XP_WIN
# include <io.h> # include <io.h>
# include <windows.h> # include <windows.h>
# include "mozilla/LateWriteChecks.h"
# include "mozilla/UniquePtr.h" # include "mozilla/UniquePtr.h"
#endif #endif
@ -259,6 +260,7 @@ void vprintf_stderr(const char* aFmt, va_list aArgs) {
vfprintf(fp, aFmt, aArgs); vfprintf(fp, aFmt, aArgs);
AutoSuspendLateWriteChecks suspend;
fclose(fp); fclose(fp);
} }

View File

@ -38,6 +38,15 @@
/*************************** Auxiliary Declarations ***************************/ /*************************** Auxiliary Declarations ***************************/
static MOZ_THREAD_LOCAL(int) tlsSuspendLateWriteChecks;
bool SuspendingLateWriteChecksForCurrentThread() {
if (!tlsSuspendLateWriteChecks.init()) {
return true;
}
return tlsSuspendLateWriteChecks.get() > 0;
}
// This a wrapper over a file descriptor that provides a Printf method and // This a wrapper over a file descriptor that provides a Printf method and
// computes the sha1 of the data that passes through it. // computes the sha1 of the data that passes through it.
class SHA1Stream { class SHA1Stream {
@ -103,6 +112,10 @@ class LateWriteObserver final : public mozilla::IOInterposeObserver {
void LateWriteObserver::Observe( void LateWriteObserver::Observe(
mozilla::IOInterposeObserver::Observation& aOb) { mozilla::IOInterposeObserver::Observation& aOb) {
if (SuspendingLateWriteChecksForCurrentThread()) {
return;
}
#ifdef DEBUG #ifdef DEBUG
MOZ_CRASH(); MOZ_CRASH();
#endif #endif
@ -233,4 +246,20 @@ void StopLateWriteChecks() {
} }
} }
void PushSuspendLateWriteChecks() {
if (!tlsSuspendLateWriteChecks.init()) {
return;
}
tlsSuspendLateWriteChecks.set(tlsSuspendLateWriteChecks.get() + 1);
}
void PopSuspendLateWriteChecks() {
if (!tlsSuspendLateWriteChecks.init()) {
return;
}
int current = tlsSuspendLateWriteChecks.get();
MOZ_ASSERT(current > 0);
tlsSuspendLateWriteChecks.set(current - 1);
}
} // namespace mozilla } // namespace mozilla

View File

@ -54,6 +54,25 @@ void BeginLateWriteChecks();
*/ */
void StopLateWriteChecks(); void StopLateWriteChecks();
/**
* Temporarily suspend late write checks for the current thread. This is useful
* if you're about to perform a write, but it would be fine if this write were
* interrupted or skipped during a fast shutdown.
*/
void PushSuspendLateWriteChecks();
/**
* Resume late write checks for the current thread, assuming an ancestor in the
* call stack hasn't also pushed a suspension.
*/
void PopSuspendLateWriteChecks();
class MOZ_RAII AutoSuspendLateWriteChecks {
public:
AutoSuspendLateWriteChecks() { PushSuspendLateWriteChecks(); }
~AutoSuspendLateWriteChecks() { PopSuspendLateWriteChecks(); }
};
} // namespace mozilla } // namespace mozilla
#endif // mozilla_LateWriteChecks_h #endif // mozilla_LateWriteChecks_h