DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * 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/. */

/*
 * GC-internal classes for acquiring and releasing the GC lock.
 */

#ifndef gc_GCLock_h
#define gc_GCLock_h

#include "vm/Runtime.h"

namespace js {

class AutoUnlockGC;

/*
 * RAII class that takes the GC lock while it is live.
 *
 * Usually functions will pass const references of this class.  However
 * non-const references can be used to either temporarily release the lock by
 * use of AutoUnlockGC or to start background allocation when the lock is
 * released.
 */
class MOZ_RAII AutoLockGC {
 public:
  explicit AutoLockGC(JSRuntime* rt MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
      : runtime_(rt) {
    MOZ_GUARD_OBJECT_NOTIFIER_INIT;
    lock();
  }

  ~AutoLockGC() { lockGuard_.reset(); }

  void lock() {
    MOZ_ASSERT(lockGuard_.isNothing());
    lockGuard_.emplace(runtime_->gc.lock);
  }

  void unlock() {
    MOZ_ASSERT(lockGuard_.isSome());
    lockGuard_.reset();
  }

 protected:
  js::LockGuard<js::Mutex>& guard() { return lockGuard_.ref(); }

  JSRuntime* runtime() const { return runtime_; }

 private:
  JSRuntime* runtime_;
  mozilla::Maybe<js::LockGuard<js::Mutex>> lockGuard_;
  MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER

  AutoLockGC(const AutoLockGC&) = delete;
  AutoLockGC& operator=(const AutoLockGC&) = delete;
};

/*
 * Same as AutoLockGC except it can optionally start a background chunk
 * allocation task when the lock is released.
 */
class MOZ_RAII AutoLockGCBgAlloc : public AutoLockGC {
 public:
  explicit AutoLockGCBgAlloc(JSRuntime* rt)
      : AutoLockGC(rt), startBgAlloc(false) {}

  ~AutoLockGCBgAlloc() {
    unlock();

    /*
     * We have to do this after releasing the lock because it may acquire
     * the helper lock which could cause lock inversion if we still held
     * the GC lock.
     */
    if (startBgAlloc) {
      runtime()->gc.startBackgroundAllocTaskIfIdle();  // Ignore failure.
    }
  }

  /*
   * This can be used to start a background allocation task (if one isn't
   * already running) that allocates chunks and makes them available in the
   * free chunks list.  This happens after the lock is released in order to
   * avoid lock inversion.
   */
  void tryToStartBackgroundAllocation() { startBgAlloc = true; }

 private:
  // true if we should start a background chunk allocation task after the
  // lock is released.
  bool startBgAlloc;
};

class MOZ_RAII AutoUnlockGC {
 public:
  explicit AutoUnlockGC(AutoLockGC& lock MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
      : lock(lock) {
    MOZ_GUARD_OBJECT_NOTIFIER_INIT;
    lock.unlock();
  }

  ~AutoUnlockGC() { lock.lock(); }

 private:
  AutoLockGC& lock;
  MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER

  AutoUnlockGC(const AutoUnlockGC&) = delete;
  AutoUnlockGC& operator=(const AutoUnlockGC&) = delete;
};

}  // namespace js

#endif /* gc_GCLock_h */