Bug 1323100 - Create nsThreadPoolNaming::GetNextThreadName. r?froydnj
MozReview-Commit-ID: F0ZFFa5VkAW
--- 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: