Watchdog: disable fatal timers if Watchdog is not enabled

Currently the fatal timers trigger regardless of the watchdog
being enabled (which happens only on traced/traced_probes).
This causes task runners to fail in execution contexts where
the shouldn't (e.g. trace processor).
This change makes CreateFatalTimer a no-op when the watchdog
is not started.

Bug: 117199418
Change-Id: Ic2ed93ef4325154609bae6ee5f4e0baabe80776a
diff --git a/src/base/watchdog_posix.cc b/src/base/watchdog_posix.cc
index 85cbb96..82cd6ac 100644
--- a/src/base/watchdog_posix.cc
+++ b/src/base/watchdog_posix.cc
@@ -65,15 +65,11 @@
 
 Watchdog::~Watchdog() {
   if (!thread_.joinable()) {
-    PERFETTO_DCHECK(quit_);
+    PERFETTO_DCHECK(!enabled_);
     return;
   }
-
-  {
-    std::lock_guard<std::mutex> guard(mutex_);
-    PERFETTO_DCHECK(!quit_);
-    quit_ = true;
-  }
+  PERFETTO_DCHECK(enabled_);
+  enabled_ = false;
   exit_signal_.notify_one();
   thread_.join();
 }
@@ -84,20 +80,23 @@
 }
 
 Watchdog::Timer Watchdog::CreateFatalTimer(uint32_t ms) {
+  if (!enabled_.load(std::memory_order_relaxed))
+    return Watchdog::Timer(0);
+
   return Watchdog::Timer(ms);
 }
 
 void Watchdog::Start() {
   std::lock_guard<std::mutex> guard(mutex_);
   if (thread_.joinable()) {
-    PERFETTO_DCHECK(!quit_);
+    PERFETTO_DCHECK(enabled_);
   } else {
-    PERFETTO_DCHECK(quit_);
+    PERFETTO_DCHECK(!enabled_);
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
     // Kick the thread to start running but only on Android or Linux.
-    quit_ = false;
+    enabled_ = true;
     thread_ = std::thread(&Watchdog::ThreadMain, this);
 #endif
   }
@@ -137,7 +136,7 @@
   for (;;) {
     exit_signal_.wait_for(guard,
                           std::chrono::milliseconds(polling_interval_ms_));
-    if (quit_)
+    if (!enabled_)
       return;
 
     lseek(stat_fd.get(), 0, SEEK_SET);
@@ -240,6 +239,9 @@
 }
 
 Watchdog::Timer::Timer(uint32_t ms) {
+  if (!ms)
+    return;  // No-op timer created when the watchdog is disabled.
+
   struct sigevent sev = {};
   sev.sigev_notify = SIGEV_SIGNAL;
   sev.sigev_signo = SIGABRT;