Bug 757933 - Switch nsRegionAllocator to ThreadLocal and avoid extra library call. r=BenWa
authorOleg Romashin <oleg.romashin@nokia.com>
Thu, 24 May 2012 18:36:18 -0700
changeset 95054 34c3bcd58f3b6a1d5332bd2d0d1801410360323f
parent 95053 ca31a899ce4ea86b7c1eb6e1d597ba5f98d91993
child 95055 a1a4961783efd104bd386e5199c6befb62a69177
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersBenWa
bugs757933
milestone15.0a1
Bug 757933 - Switch nsRegionAllocator to ThreadLocal and avoid extra library call. r=BenWa
gfx/src/nsRegion.cpp
--- a/gfx/src/nsRegion.cpp
+++ b/gfx/src/nsRegion.cpp
@@ -1,15 +1,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsRegion.h"
 #include "nsISupportsImpl.h"
 #include "nsTArray.h"
+#include "mozilla/ThreadLocal.h"
 
 /*
  * The SENTINEL values below guaranties that a < or >
  * comparison with it will be false for all values of the
  * underlying nscoord type.  E.g. this is always false:
  *   aCoord > NS_COORD_GREATER_SENTINEL
  * Setting the mRectListHead dummy rectangle to these values
  * allows us to loop without checking for the list end.
@@ -178,57 +179,53 @@ void RgnRectMemoryAllocator::Free (nsReg
   mFreeEntries++;
   aRect->next = mFreeListHead;
   mFreeListHead = aRect;
   Unlock ();
 }
 
 
 // Global pool for nsRegion::RgnRect allocation
-static PRUintn gRectPoolTlsIndex;
+mozilla::ThreadLocal<RgnRectMemoryAllocator*> gRectPoolTlsIndex;
 
 void RgnRectMemoryAllocatorDTOR(void *priv)
 {
-  RgnRectMemoryAllocator* allocator = (static_cast<RgnRectMemoryAllocator*>(
-                                       PR_GetThreadPrivate(gRectPoolTlsIndex)));
+  RgnRectMemoryAllocator* allocator = gRectPoolTlsIndex.get();
   delete allocator;
 }
 
 nsresult nsRegion::InitStatic()
 {
-  return PR_NewThreadPrivateIndex(&gRectPoolTlsIndex, RgnRectMemoryAllocatorDTOR);
+  return gRectPoolTlsIndex.init() ? NS_OK : NS_ERROR_FAILURE;
 }
 
 void nsRegion::ShutdownStatic()
 {
-  RgnRectMemoryAllocator* allocator = (static_cast<RgnRectMemoryAllocator*>(
-                                       PR_GetThreadPrivate(gRectPoolTlsIndex)));
+  RgnRectMemoryAllocator* allocator = gRectPoolTlsIndex.get();
   if (!allocator)
     return;
 
   delete allocator;
 
-  PR_SetThreadPrivate(gRectPoolTlsIndex, nsnull);
+  gRectPoolTlsIndex.set(nsnull);
 }
 
 void* nsRegion::RgnRect::operator new (size_t) CPP_THROW_NEW
 {
-  RgnRectMemoryAllocator* allocator = (static_cast<RgnRectMemoryAllocator*>(
-                                       PR_GetThreadPrivate(gRectPoolTlsIndex)));
+  RgnRectMemoryAllocator* allocator = gRectPoolTlsIndex.get();
   if (!allocator) {
     allocator = new RgnRectMemoryAllocator(INIT_MEM_CHUNK_ENTRIES);
-    PR_SetThreadPrivate(gRectPoolTlsIndex, allocator);
+    gRectPoolTlsIndex.set(allocator);
   }
   return allocator->Alloc ();
 }
 
 void nsRegion::RgnRect::operator delete (void* aRect, size_t)
 {
-  RgnRectMemoryAllocator* allocator = (static_cast<RgnRectMemoryAllocator*>(
-                                       PR_GetThreadPrivate(gRectPoolTlsIndex)));
+  RgnRectMemoryAllocator* allocator = gRectPoolTlsIndex.get();
   if (!allocator) {
     NS_ERROR("Invalid nsRegion::RgnRect delete");
     return;
   }
   allocator->Free (static_cast<RgnRect*>(aRect));
 }