Bug 1323100 - Create nsThreadPoolNaming::GetNextThreadName. r?froydnj draft
authorMarkus Stange <mstange@themasta.com>
Thu, 22 Dec 2016 00:38:41 +0100
changeset 456878 b46e3762c296756eeaa2ed30db27f74260c74dfc
parent 456877 6e6a9d539ce92d734bf8442ab24683c263b6d1a2
child 456879 58e73a3fe700792b823c740503b093df38afe67d
push id40636
push userbmo:mstange@themasta.com
push dateFri, 06 Jan 2017 12:38:48 +0000
reviewersfroydnj
bugs1323100
milestone53.0a1
Bug 1323100 - Create nsThreadPoolNaming::GetNextThreadName. r?froydnj MozReview-Commit-ID: F0ZFFa5VkAW
xpcom/glue/nsThreadUtils.cpp
xpcom/glue/nsThreadUtils.h
--- a/xpcom/glue/nsThreadUtils.cpp
+++ b/xpcom/glue/nsThreadUtils.cpp
@@ -435,23 +435,30 @@ NS_SetThreadName(nsIThread* aThread, con
 nsIThread*
 NS_GetCurrentThread()
 {
   return nsThreadManager::get().GetCurrentThread();
 }
 #endif
 
 // nsThreadPoolNaming
+nsCString
+nsThreadPoolNaming::GetNextThreadName(const nsACString& aPoolName)
+{
+  nsCString name(aPoolName);
+  name.AppendLiteral(" #");
+  name.AppendInt(++mCounter, 10); // The counter is declared as atomic
+  return name;
+}
+ 
 void
 nsThreadPoolNaming::SetThreadPoolName(const nsACString& aPoolName,
                                       nsIThread* aThread)
 {
-  nsCString name(aPoolName);
-  name.AppendLiteral(" #");
-  name.AppendInt(++mCounter, 10); // The counter is declared as volatile
+  nsCString name = GetNextThreadName(aPoolName);
 
   if (aThread) {
     // Set on the target thread
     NS_SetThreadName(aThread, name);
   } else {
     // Set on the current thread
 #ifndef XPCOM_GLUE_AVOID_NSPR
     PR_SetCurrentThreadName(name.BeginReading());
--- a/xpcom/glue/nsThreadUtils.h
+++ b/xpcom/glue/nsThreadUtils.h
@@ -1033,16 +1033,27 @@ private:
  * with incremental numbers.
  */
 class nsThreadPoolNaming
 {
 public:
   nsThreadPoolNaming() : mCounter(0) {}
 
   /**
+   * Returns a thread name as "<aPoolName> #<n>" and increments the counter.
+   */
+  nsCString GetNextThreadName(const nsACString& aPoolName);
+
+  template<size_t LEN>
+  nsCString GetNextThreadName(const char (&aPoolName)[LEN])
+  {
+    return GetNextThreadName(nsDependentCString(aPoolName, LEN - 1));
+  }
+
+  /**
    * Creates and sets next thread name as "<aPoolName> #<n>"
    * on the specified thread.  If no thread is specified (aThread
    * is null) then the name is synchronously set on the current thread.
    */
   void SetThreadPoolName(const nsACString& aPoolName,
                          nsIThread* aThread = nullptr);
 
 private: