]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
log: add Log::is_inside_log_lock()
authorSage Weil <sage@redhat.com>
Fri, 12 Sep 2014 22:24:50 +0000 (15:24 -0700)
committerSage Weil <sage@redhat.com>
Fri, 12 Sep 2014 22:24:50 +0000 (15:24 -0700)
Signed-off-by: Sage Weil <sage@redhat.com>
src/log/Log.cc
src/log/Log.h

index 39fd8c4074401563a3b7284f08031527a5e5815f..8da77ef8b77ed3be8ad348b4e73917713d58240f 100644 (file)
@@ -37,6 +37,8 @@ static void log_on_exit(void *p)
 Log::Log(SubsystemMap *s)
   : m_indirect_this(NULL),
     m_subs(s),
+    m_queue_mutex_holder(0),
+    m_flush_mutex_holder(0),
     m_new(), m_recent(),
     m_fd(-1),
     m_syslog_log(-2), m_syslog_crash(-2),
@@ -141,6 +143,7 @@ void Log::set_stderr_level(int log, int crash)
 void Log::submit_entry(Entry *e)
 {
   pthread_mutex_lock(&m_queue_mutex);
+  m_queue_mutex_holder = pthread_self();
 
   // wait for flush to catch up
   while (m_new.m_len > m_max_new)
@@ -148,6 +151,7 @@ void Log::submit_entry(Entry *e)
 
   m_new.enqueue(e);
   pthread_cond_signal(&m_cond_flusher);
+  m_queue_mutex_holder = 0;
   pthread_mutex_unlock(&m_queue_mutex);
 }
 
@@ -171,10 +175,13 @@ Entry *Log::create_entry(int level, int subsys)
 void Log::flush()
 {
   pthread_mutex_lock(&m_flush_mutex);
+  m_flush_mutex_holder = pthread_self();
   pthread_mutex_lock(&m_queue_mutex);
+  m_queue_mutex_holder = pthread_self();
   EntryQueue t;
   t.swap(m_new);
   pthread_cond_broadcast(&m_cond_loggers);
+  m_queue_mutex_holder = 0;
   pthread_mutex_unlock(&m_queue_mutex);
   _flush(&t, &m_recent, false);
 
@@ -183,6 +190,7 @@ void Log::flush()
     delete m_recent.dequeue();
   }
 
+  m_flush_mutex_holder = 0;
   pthread_mutex_unlock(&m_flush_mutex);
 }
 
@@ -254,10 +262,15 @@ void Log::_log_message(const char *s, bool crash)
 void Log::dump_recent()
 {
   pthread_mutex_lock(&m_flush_mutex);
+  m_flush_mutex_holder = pthread_self();
 
   pthread_mutex_lock(&m_queue_mutex);
+  m_queue_mutex_holder = pthread_self();
+
   EntryQueue t;
   t.swap(m_new);
+
+  m_queue_mutex_holder = 0;
   pthread_mutex_unlock(&m_queue_mutex);
   _flush(&t, &m_recent, false);
 
@@ -287,6 +300,7 @@ void Log::dump_recent()
 
   _log_message("--- end dump of recent events ---", true);
 
+  m_flush_mutex_holder = 0;
   pthread_mutex_unlock(&m_flush_mutex);
 }
 
@@ -313,20 +327,31 @@ void Log::stop()
 void *Log::entry()
 {
   pthread_mutex_lock(&m_queue_mutex);
+  m_queue_mutex_holder = pthread_self();
   while (!m_stop) {
     if (!m_new.empty()) {
+      m_queue_mutex_holder = 0;
       pthread_mutex_unlock(&m_queue_mutex);
       flush();
       pthread_mutex_lock(&m_queue_mutex);
+      m_queue_mutex_holder = pthread_self();
       continue;
     }
 
     pthread_cond_wait(&m_cond_flusher, &m_queue_mutex);
   }
+  m_queue_mutex_holder = 0;
   pthread_mutex_unlock(&m_queue_mutex);
   flush();
   return NULL;
 }
 
+bool Log::is_inside_log_lock()
+{
+  return
+    pthread_self() == m_queue_mutex_holder ||
+    pthread_self() == m_flush_mutex_holder;
+}
+
 } // ceph::log::
 } // ceph::
index b5e16fdde793c11a103aa4128bb41e9e2030243d..b77be87e82e431f25e1d1448db0a4a525fa2520f 100644 (file)
@@ -26,6 +26,9 @@ class Log : private Thread
   pthread_cond_t m_cond_loggers;
   pthread_cond_t m_cond_flusher;
 
+  pthread_t m_queue_mutex_holder;
+  pthread_t m_flush_mutex_holder;
+
   EntryQueue m_new;    ///< new entries
   EntryQueue m_recent; ///< recent (less new) entries we've already written at low detail
 
@@ -68,6 +71,9 @@ public:
 
   void start();
   void stop();
+
+  /// true if the log lock is held by our thread
+  bool is_inside_log_lock();
 };
 
 }