Bug 1082794 - Use Maybe instead of a separate constructor to maybe construct an AutoLockGC; r=jonco
authorTerrence Cole <terrence@mozilla.com>
Tue, 14 Oct 2014 13:03:23 -0700
changeset 210625 f94b063de92581c9a4e3ef0701450a057fe829a4
parent 210624 6243d2b50e33dbfba24080b2b76f1223f20db3fa
child 210626 3352a380a448d21b0a32d8c99a93d0dfa550a732
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersjonco
bugs1082794
milestone36.0a1
Bug 1082794 - Use Maybe instead of a separate constructor to maybe construct an AutoLockGC; r=jonco
js/src/jsgc.cpp
js/src/vm/Runtime.h
--- a/js/src/jsgc.cpp
+++ b/js/src/jsgc.cpp
@@ -1006,19 +1006,20 @@ Chunk::recycleArena(ArenaHeader *aheader
 
 void
 Chunk::releaseArena(ArenaHeader *aheader)
 {
     MOZ_ASSERT(aheader->allocated());
     MOZ_ASSERT(!aheader->hasDelayedMarking);
     Zone *zone = aheader->zone;
     JSRuntime *rt = zone->runtimeFromAnyThread();
-    AutoLockGC maybeLock;
+
+    Maybe<AutoLockGC> maybeLock;
     if (rt->gc.isBackgroundSweeping())
-        maybeLock.lock(rt);
+        maybeLock.emplace(rt);
 
     if (rt->gc.isBackgroundSweeping())
         zone->threshold.updateForRemovedArena(rt->gc.tunables);
     zone->usage.removeGCArena();
 
     aheader->setAsNotAllocated();
     addArenaToFreeList(rt, aheader);
 
@@ -1919,35 +1920,35 @@ ArenaLists::allocateFromArena(JS::Zone *
     return allocateFromArena(zone, thingKind, maybeStartBackgroundAllocation);
 }
 
 TenuredCell *
 ArenaLists::allocateFromArena(JS::Zone *zone, AllocKind thingKind,
                               AutoMaybeStartBackgroundAllocation &maybeStartBGAlloc)
 {
     JSRuntime *rt = zone->runtimeFromAnyThread();
-    AutoLockGC maybeLock;
+    Maybe<AutoLockGC> maybeLock;
 
     // See if we can proceed without taking the GC lock.
     if (backgroundFinalizeState[thingKind] != BFS_DONE)
-        maybeLock.lock(rt);
+        maybeLock.emplace(rt);
 
     ArenaList &al = arenaLists[thingKind];
     ArenaHeader *aheader = al.takeNextArena();
     if (aheader) {
         // Empty arenas should be immediately freed except in Parallel JS.
         MOZ_ASSERT_IF(aheader->isEmpty(), InParallelSection());
 
         return allocateFromArenaInner<HasFreeThings>(zone, aheader, thingKind);
     }
 
     // Parallel threads have their own ArenaLists, but chunks are shared;
     // if we haven't already, take the GC lock now to avoid racing.
-    if (!maybeLock.locked())
-        maybeLock.lock(rt);
+    if (maybeLock.isNothing())
+        maybeLock.emplace(rt);
 
     Chunk *chunk = rt->gc.pickChunk(zone, maybeStartBGAlloc);
     if (!chunk)
         return nullptr;
 
     // Although our chunk should definitely have enough space for another arena,
     // there are other valid reasons why Chunk::allocateArena() may fail.
     aheader = chunk->allocateArena(zone, thingKind);
--- a/js/src/vm/Runtime.h
+++ b/js/src/vm/Runtime.h
@@ -1491,41 +1491,27 @@ FreeOp::freeLater(void *p)
 
     if (!freeLaterList.append(p))
         CrashAtUnhandlableOOM("FreeOp::freeLater");
 }
 
 class AutoLockGC
 {
   public:
-    explicit AutoLockGC(JSRuntime *rt = nullptr
+    explicit AutoLockGC(JSRuntime *rt
                         MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
       : runtime(rt)
     {
         MOZ_GUARD_OBJECT_NOTIFIER_INIT;
-        // Avoid MSVC warning C4390 for non-threadsafe builds.
-        if (rt)
-            rt->lockGC();
+        rt->lockGC();
     }
 
     ~AutoLockGC()
     {
-        if (runtime)
-            runtime->unlockGC();
-    }
-
-    bool locked() const {
-        return !!runtime;
-    }
-
-    void lock(JSRuntime *rt) {
-        MOZ_ASSERT(rt);
-        MOZ_ASSERT(!runtime);
-        runtime = rt;
-        rt->lockGC();
+        runtime->unlockGC();
     }
 
   private:
     JSRuntime *runtime;
     MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
 };
 
 class AutoUnlockGC