logd: single std::mutex for locking log buffers and tracking readers

There are only three places where the log buffer lock is not already
held when the reader lock is taken:
1) In LogReader, when a new reader connects
2) In LogReader, when a misbehaving reader disconnects
3) LogReaderThread::ThreadFunction()

1) and 2) happen sufficiently rarely that there's no impact if they
additionally held a global lock.
3) is refactored in this CL.  Previously, it would do the below in a loop
  1) Lock the reader lock then wait on a condition variable
  2) Unlock the reader lock
  3) Lock the log buffer lock in LogBuffer::FlushTo()
  4) In each iteration in the LogBuffer::FlushTo() loop
    1) Lock then unlock the reader lock in FilterSecondPass()
    2) Unlock the log buffer lock to send the message, then re-lock it
  5) Unlock the log buffer lock when leaving LogBuffer::FlushTo()
If these locks are collapsed into a single lock, then this simplifies to:
  1) Lock the single lock then wait on a condition variable
  2) In each iteration in the LogBuffer::FlushTo() loop
    1) Unlock the single lock to send the message, then re-lock it

Collapsing both these locks into a single lock simplifes the code and
removes the overhead of acquiring the second lock, in the majority of
use cases where the first lock is already held.

Secondly, this lock will be a plain std::mutex instead of a RwLock.
RwLock's are appropriate when there is a substantial imbalance between
readers and writers and high contention, neither are true for logd.

Bug: 169736426
Test: logging unit tests
Change-Id: Ia511506f2d0935a5321c1b2f65569066f91ecb06
diff --git a/logd/LogReaderThread.h b/logd/LogReaderThread.h
index f1b673f..96137b3 100644
--- a/logd/LogReaderThread.h
+++ b/logd/LogReaderThread.h
@@ -26,10 +26,12 @@
 #include <list>
 #include <memory>
 
+#include <android-base/thread_annotations.h>
 #include <log/log.h>
 
 #include "LogBuffer.h"
 #include "LogWriter.h"
+#include "LogdLock.h"
 
 class LogReaderList;
 
@@ -39,50 +41,54 @@
                     std::unique_ptr<LogWriter> writer, bool non_block, unsigned long tail,
                     LogMask log_mask, pid_t pid, log_time start_time, uint64_t sequence,
                     std::chrono::steady_clock::time_point deadline);
-    void triggerReader_Locked() { thread_triggered_condition_.notify_all(); }
+    void TriggerReader() REQUIRES(logd_lock) { thread_triggered_condition_.notify_all(); }
 
-    void triggerSkip_Locked(log_id_t id, unsigned int skip) { skip_ahead_[id] = skip; }
-    void cleanSkip_Locked();
+    void TriggerSkip(log_id_t id, unsigned int skip) REQUIRES(logd_lock) { skip_ahead_[id] = skip; }
+    void CleanSkip() REQUIRES(logd_lock) { memset(skip_ahead_, 0, sizeof(skip_ahead_)); }
 
-    void release_Locked() {
+    void Release() REQUIRES(logd_lock) {
         // gracefully shut down the socket.
         writer_->Shutdown();
         release_ = true;
         thread_triggered_condition_.notify_all();
     }
 
-    bool IsWatching(log_id_t id) const { return flush_to_state_->log_mask() & (1 << id); }
-    bool IsWatchingMultiple(LogMask log_mask) const {
-        return flush_to_state_ && flush_to_state_->log_mask() & log_mask;
+    bool IsWatching(log_id_t id) const REQUIRES(logd_lock) {
+        return flush_to_state_->log_mask() & (1 << id);
+    }
+    bool IsWatchingMultiple(LogMask log_mask) const REQUIRES(logd_lock) {
+        return flush_to_state_->log_mask() & log_mask;
     }
 
-    std::string name() const { return writer_->name(); }
-    uint64_t start() const { return flush_to_state_->start(); }
-    std::chrono::steady_clock::time_point deadline() const { return deadline_; }
-    FlushToState& flush_to_state() { return *flush_to_state_; }
+    std::string name() const REQUIRES(logd_lock) { return writer_->name(); }
+    uint64_t start() const REQUIRES(logd_lock) { return flush_to_state_->start(); }
+    std::chrono::steady_clock::time_point deadline() const REQUIRES(logd_lock) { return deadline_; }
+    FlushToState& flush_to_state() REQUIRES(logd_lock) { return *flush_to_state_; }
 
   private:
     void ThreadFunction();
     // flushTo filter callbacks
-    FilterResult FilterFirstPass(log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime);
-    FilterResult FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime);
+    FilterResult FilterFirstPass(log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime)
+            REQUIRES(logd_lock);
+    FilterResult FilterSecondPass(log_id_t log_id, pid_t pid, uint64_t sequence, log_time realtime)
+            REQUIRES(logd_lock);
 
     std::condition_variable thread_triggered_condition_;
     LogBuffer* log_buffer_;
     LogReaderList* reader_list_;
-    std::unique_ptr<LogWriter> writer_;
+    std::unique_ptr<LogWriter> writer_ GUARDED_BY(logd_lock);
 
     // Set to true to cause the thread to end and the LogReaderThread to delete itself.
-    bool release_ = false;
+    bool release_ GUARDED_BY(logd_lock) = false;
 
     // If set to non-zero, only pids equal to this are read by the reader.
     const pid_t pid_;
     // When a reader is referencing (via start_) old elements in the log buffer, and the log
     // buffer's size grows past its memory limit, the log buffer may request the reader to skip
     // ahead a specified number of logs.
-    unsigned int skip_ahead_[LOG_ID_MAX];
+    unsigned int skip_ahead_[LOG_ID_MAX] GUARDED_BY(logd_lock);
     // LogBuffer::FlushTo() needs to store state across subsequent calls.
-    std::unique_ptr<FlushToState> flush_to_state_;
+    std::unique_ptr<FlushToState> flush_to_state_ GUARDED_BY(logd_lock);
 
     // These next three variables are used for reading only the most recent lines aka `adb logcat
     // -t` / `adb logcat -T`.
@@ -100,7 +106,7 @@
     log_time start_time_;
     // CLOCK_MONOTONIC based deadline used for log wrapping.  If this deadline expires before logs
     // wrap, then wake up and send the logs to the reader anyway.
-    std::chrono::steady_clock::time_point deadline_;
+    std::chrono::steady_clock::time_point deadline_ GUARDED_BY(logd_lock);
     // If this reader is 'dumpAndClose' and will disconnect once it has read its intended logs.
     const bool non_block_;
 };