Bug 1347963 - part 2 - make nsHttpRequestHead use RecursiveMutex; r=mcmanus
authorNathan Froyd <froydnj@mozilla.com>
Tue, 18 Apr 2017 11:05:16 -0400
changeset 616049 7232578cc2b11d4348fb6bfa928005c4b264974b
parent 616048 6c1c1d869619ba3b721630a1f6375a4d54697e31
child 616050 a9be4c2f9c2a8e4451e710879bd640fc685af09b
push id70554
push userbmo:mcooper@mozilla.com
push dateWed, 26 Jul 2017 17:18:29 +0000
reviewersmcmanus
bugs1347963
milestone56.0a1
Bug 1347963 - part 2 - make nsHttpRequestHead use RecursiveMutex; r=mcmanus Making nsHttpRequestHead slightly faster with RecursiveMutex is a good thing.
netwerk/protocol/http/nsHttpRequestHead.cpp
netwerk/protocol/http/nsHttpRequestHead.h
--- a/netwerk/protocol/http/nsHttpRequestHead.cpp
+++ b/netwerk/protocol/http/nsHttpRequestHead.cpp
@@ -16,261 +16,261 @@
 namespace mozilla {
 namespace net {
 
 nsHttpRequestHead::nsHttpRequestHead()
     : mMethod(NS_LITERAL_CSTRING("GET"))
     , mVersion(NS_HTTP_VERSION_1_1)
     , mParsedMethod(kMethod_Get)
     , mHTTPS(false)
-    , mReentrantMonitor("nsHttpRequestHead.mReentrantMonitor")
+    , mRecursiveMutex("nsHttpRequestHead.mRecursiveMutex")
     , mInVisitHeaders(false)
 {
     MOZ_COUNT_CTOR(nsHttpRequestHead);
 }
 
 nsHttpRequestHead::~nsHttpRequestHead()
 {
     MOZ_COUNT_DTOR(nsHttpRequestHead);
 }
 
 // Don't use this function. It is only used by HttpChannelParent to avoid
 // copying of request headers!!!
 const nsHttpHeaderArray &
 nsHttpRequestHead::Headers() const
 {
     nsHttpRequestHead &curr = const_cast<nsHttpRequestHead&>(*this);
-    curr.mReentrantMonitor.AssertCurrentThreadIn();
+    curr.mRecursiveMutex.AssertCurrentThreadIn();
     return mHeaders;
 }
 
 void
 nsHttpRequestHead::SetHeaders(const nsHttpHeaderArray& aHeaders)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     mHeaders = aHeaders;
 }
 
 void
 nsHttpRequestHead::SetVersion(nsHttpVersion version)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     mVersion = version;
 }
 
 void
 nsHttpRequestHead::SetRequestURI(const nsACString& s)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     mRequestURI = s;
 }
 
 void
 nsHttpRequestHead::SetPath(const nsACString& s)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     mPath = s;
 }
 
 uint32_t
 nsHttpRequestHead::HeaderCount()
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     return mHeaders.Count();
 }
 
 nsresult
 nsHttpRequestHead::VisitHeaders(nsIHttpHeaderVisitor *visitor,
                                 nsHttpHeaderArray::VisitorFilter filter /* = nsHttpHeaderArray::eFilterAll*/)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     mInVisitHeaders = true;
     nsresult rv = mHeaders.VisitHeaders(visitor, filter);
     mInVisitHeaders = false;
     return rv;
 }
 
 void
 nsHttpRequestHead::Method(nsACString &aMethod)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     aMethod = mMethod;
 }
 
 nsHttpVersion
 nsHttpRequestHead::Version()
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     return mVersion;
 }
 
 void
 nsHttpRequestHead::RequestURI(nsACString &aRequestURI)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     aRequestURI = mRequestURI;
 }
 
 void
 nsHttpRequestHead::Path(nsACString &aPath)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     aPath = mPath.IsEmpty() ? mRequestURI : mPath;
 }
 
 void
 nsHttpRequestHead::SetHTTPS(bool val)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     mHTTPS = val;
 }
 
 void
 nsHttpRequestHead::Origin(nsACString &aOrigin)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     aOrigin = mOrigin;
 }
 
 nsresult
 nsHttpRequestHead::SetHeader(const nsACString &h, const nsACString &v,
                              bool m /*= false*/)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
 
     if (mInVisitHeaders) {
         return NS_ERROR_FAILURE;
     }
 
     return mHeaders.SetHeader(h, v, m,
                               nsHttpHeaderArray::eVarietyRequestOverride);
 }
 
 nsresult
 nsHttpRequestHead::SetHeader(nsHttpAtom h, const nsACString &v,
                              bool m /*= false*/)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
 
     if (mInVisitHeaders) {
         return NS_ERROR_FAILURE;
     }
 
     return mHeaders.SetHeader(h, v, m,
                               nsHttpHeaderArray::eVarietyRequestOverride);
 }
 
 nsresult
 nsHttpRequestHead::SetHeader(nsHttpAtom h, const nsACString &v, bool m,
                              nsHttpHeaderArray::HeaderVariety variety)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
 
     if (mInVisitHeaders) {
         return NS_ERROR_FAILURE;
     }
 
     return mHeaders.SetHeader(h, v, m, variety);
 }
 
 nsresult
 nsHttpRequestHead::SetEmptyHeader(const nsACString &h)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
 
     if (mInVisitHeaders) {
         return NS_ERROR_FAILURE;
     }
 
     return mHeaders.SetEmptyHeader(h,
                                    nsHttpHeaderArray::eVarietyRequestOverride);
 }
 
 nsresult
 nsHttpRequestHead::GetHeader(nsHttpAtom h, nsACString &v)
 {
     v.Truncate();
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     return mHeaders.GetHeader(h, v);
 }
 
 nsresult
 nsHttpRequestHead::ClearHeader(nsHttpAtom h)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
 
     if (mInVisitHeaders) {
         return NS_ERROR_FAILURE;
     }
 
     mHeaders.ClearHeader(h);
     return NS_OK;
 }
 
 void
 nsHttpRequestHead::ClearHeaders()
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
 
     if (mInVisitHeaders) {
         return;
     }
 
     mHeaders.Clear();
 }
 
 bool
 nsHttpRequestHead::HasHeader(nsHttpAtom h)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     return mHeaders.HasHeader(h);
 }
 
 bool
 nsHttpRequestHead::HasHeaderValue(nsHttpAtom h, const char *v)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     return mHeaders.HasHeaderValue(h, v);
 }
 
 nsresult
 nsHttpRequestHead::SetHeaderOnce(nsHttpAtom h, const char *v,
                                  bool merge /*= false */)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
 
     if (mInVisitHeaders) {
         return NS_ERROR_FAILURE;
     }
 
     if (!merge || !mHeaders.HasHeaderValue(h, v)) {
         return mHeaders.SetHeader(h, nsDependentCString(v), merge,
                                   nsHttpHeaderArray::eVarietyRequestOverride);
     }
     return NS_OK;
 }
 
 nsHttpRequestHead::ParsedMethodType
 nsHttpRequestHead::ParsedMethod()
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     return mParsedMethod;
 }
 
 bool
 nsHttpRequestHead::EqualsMethod(ParsedMethodType aType)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     return mParsedMethod == aType;
 }
 
 void
 nsHttpRequestHead::ParseHeaderSet(const char *buffer)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     nsHttpAtom hdr;
     nsAutoCString headerNameOriginal;
     nsAutoCString val;
     while (buffer) {
         const char *eof = strchr(buffer, '\r');
         if (!eof) {
             break;
         }
@@ -291,24 +291,24 @@ nsHttpRequestHead::ParseHeaderSet(const 
             buffer++;
         }
     }
 }
 
 bool
 nsHttpRequestHead::IsHTTPS()
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     return mHTTPS;
 }
 
 void
 nsHttpRequestHead::SetMethod(const nsACString &method)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     mParsedMethod = kMethod_Custom;
     mMethod = method;
     if (!strcmp(mMethod.get(), "GET")) {
         mParsedMethod = kMethod_Get;
     } else if (!strcmp(mMethod.get(), "POST")) {
         mParsedMethod = kMethod_Post;
     } else if (!strcmp(mMethod.get(), "OPTIONS")) {
         mParsedMethod = kMethod_Options;
@@ -322,30 +322,30 @@ nsHttpRequestHead::SetMethod(const nsACS
         mParsedMethod = kMethod_Trace;
     }
 }
 
 void
 nsHttpRequestHead::SetOrigin(const nsACString &scheme, const nsACString &host,
                              int32_t port)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     mOrigin.Assign(scheme);
     mOrigin.Append(NS_LITERAL_CSTRING("://"));
     mOrigin.Append(host);
     if (port >= 0) {
         mOrigin.Append(NS_LITERAL_CSTRING(":"));
         mOrigin.AppendInt(port);
     }
 }
 
 bool
 nsHttpRequestHead::IsSafeMethod()
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     // This code will need to be extended for new safe methods, otherwise
     // they'll default to "not safe".
     if ((mParsedMethod == kMethod_Get) || (mParsedMethod == kMethod_Head) ||
         (mParsedMethod == kMethod_Options) || (mParsedMethod == kMethod_Trace)
        ) {
         return true;
     }
 
@@ -356,17 +356,17 @@ nsHttpRequestHead::IsSafeMethod()
     return (!strcmp(mMethod.get(), "PROPFIND") ||
             !strcmp(mMethod.get(), "REPORT") ||
             !strcmp(mMethod.get(), "SEARCH"));
 }
 
 void
 nsHttpRequestHead::Flatten(nsACString &buf, bool pruneProxyHeaders)
 {
-    ReentrantMonitorAutoEnter mon(mReentrantMonitor);
+    RecursiveMutexAutoLock mon(mRecursiveMutex);
     // note: the first append is intentional.
 
     buf.Append(mMethod);
     buf.Append(' ');
     buf.Append(mRequestURI);
     buf.AppendLiteral(" HTTP/");
 
     switch (mVersion) {
--- a/netwerk/protocol/http/nsHttpRequestHead.h
+++ b/netwerk/protocol/http/nsHttpRequestHead.h
@@ -4,17 +4,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsHttpRequestHead_h__
 #define nsHttpRequestHead_h__
 
 #include "nsHttp.h"
 #include "nsHttpHeaderArray.h"
 #include "nsString.h"
-#include "mozilla/ReentrantMonitor.h"
+#include "mozilla/RecursiveMutex.h"
 
 class nsIHttpHeaderVisitor;
 
 namespace mozilla { namespace net {
 
 //-----------------------------------------------------------------------------
 // nsHttpRequestHead represents the request line and headers from an HTTP
 // request.
@@ -25,18 +25,18 @@ class nsHttpRequestHead
 public:
     nsHttpRequestHead();
     ~nsHttpRequestHead();
 
     // The following function is only used in HttpChannelParent to avoid
     // copying headers. If you use it be careful to do it only under
     // nsHttpRequestHead lock!!!
     const nsHttpHeaderArray &Headers() const;
-    void Enter() { mReentrantMonitor.Enter(); }
-    void Exit() { mReentrantMonitor.Exit(); }
+    void Enter() { mRecursiveMutex.Lock(); }
+    void Exit() { mRecursiveMutex.Unlock(); }
 
     void SetHeaders(const nsHttpHeaderArray& aHeaders);
 
     void SetMethod(const nsACString &method);
     void SetVersion(nsHttpVersion version);
     void SetRequestURI(const nsACString& s);
     void SetPath(const nsACString& s);
     uint32_t HeaderCount();
@@ -114,19 +114,19 @@ private:
     // because this is used off the main thread
     nsCString         mRequestURI;
     nsCString         mPath;
 
     nsCString         mOrigin;
     ParsedMethodType  mParsedMethod;
     bool              mHTTPS;
 
-    // We are using ReentrantMonitor instead of a Mutex because VisitHeader
+    // We are using RecursiveMutex instead of a Mutex because VisitHeader
     // function calls nsIHttpHeaderVisitor::VisitHeader while under lock.
-    ReentrantMonitor  mReentrantMonitor;
+    RecursiveMutex  mRecursiveMutex;
 
     // During VisitHeader we sould not allow cal to SetHeader.
     bool mInVisitHeaders;
 };
 
 } // namespace net
 } // namespace mozilla