JSLock.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2005, 2008, 2009 Apple Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. *
  19. */
  20. #ifndef JSLock_h
  21. #define JSLock_h
  22. #include <wtf/Assertions.h>
  23. #include <wtf/Noncopyable.h>
  24. #include <wtf/RefPtr.h>
  25. #include <wtf/TCSpinLock.h>
  26. #include <wtf/Threading.h>
  27. namespace JSC {
  28. // To make it safe to use JavaScript on multiple threads, it is
  29. // important to lock before doing anything that allocates a
  30. // JavaScript data structure or that interacts with shared state
  31. // such as the protect count hash table. The simplest way to lock
  32. // is to create a local JSLockHolder object in the scope where the lock
  33. // must be held and pass it the context that requires protection.
  34. // The lock is recursive so nesting is ok. The JSLock
  35. // object also acts as a convenience short-hand for running important
  36. // initialization routines.
  37. // To avoid deadlock, sometimes it is necessary to temporarily
  38. // release the lock. Since it is recursive you actually have to
  39. // release all locks held by your thread. This is safe to do if
  40. // you are executing code that doesn't require the lock, and you
  41. // reacquire the right number of locks at the end. You can do this
  42. // by constructing a locally scoped JSLock::DropAllLocks object. The
  43. // DropAllLocks object takes care to release the JSLock only if your
  44. // thread acquired it to begin with.
  45. class ExecState;
  46. class VM;
  47. // This class is used to protect the initialization of the legacy single
  48. // shared VM.
  49. class GlobalJSLock {
  50. WTF_MAKE_NONCOPYABLE(GlobalJSLock);
  51. public:
  52. JS_EXPORT_PRIVATE GlobalJSLock();
  53. JS_EXPORT_PRIVATE ~GlobalJSLock();
  54. static void initialize();
  55. private:
  56. static Mutex* s_sharedInstanceLock;
  57. };
  58. class JSLockHolder {
  59. public:
  60. JS_EXPORT_PRIVATE JSLockHolder(VM*);
  61. JS_EXPORT_PRIVATE JSLockHolder(VM&);
  62. JS_EXPORT_PRIVATE JSLockHolder(ExecState*);
  63. JS_EXPORT_PRIVATE ~JSLockHolder();
  64. private:
  65. void init();
  66. RefPtr<VM> m_vm;
  67. };
  68. class JSLock : public ThreadSafeRefCounted<JSLock> {
  69. WTF_MAKE_NONCOPYABLE(JSLock);
  70. public:
  71. JSLock(VM*);
  72. JS_EXPORT_PRIVATE ~JSLock();
  73. JS_EXPORT_PRIVATE void lock();
  74. JS_EXPORT_PRIVATE void unlock();
  75. static void lock(ExecState*);
  76. static void unlock(ExecState*);
  77. static void lock(VM&);
  78. static void unlock(VM&);
  79. VM* vm() { return m_vm; }
  80. JS_EXPORT_PRIVATE bool currentThreadIsHoldingLock();
  81. unsigned dropAllLocks();
  82. unsigned dropAllLocksUnconditionally();
  83. void grabAllLocks(unsigned lockCount);
  84. void willDestroyVM(VM*);
  85. class DropAllLocks {
  86. WTF_MAKE_NONCOPYABLE(DropAllLocks);
  87. public:
  88. JS_EXPORT_PRIVATE DropAllLocks(ExecState* exec);
  89. JS_EXPORT_PRIVATE DropAllLocks(VM*);
  90. JS_EXPORT_PRIVATE ~DropAllLocks();
  91. private:
  92. intptr_t m_lockCount;
  93. RefPtr<VM> m_vm;
  94. };
  95. private:
  96. SpinLock m_spinLock;
  97. Mutex m_lock;
  98. ThreadIdentifier m_ownerThread;
  99. intptr_t m_lockCount;
  100. unsigned m_lockDropDepth;
  101. VM* m_vm;
  102. };
  103. } // namespace
  104. #endif // JSLock_h