JavaScriptThreading.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
  3. * (C) 2007 Graham Dennis (graham.dennis@gmail.com)
  4. * (C) 2007 Eric Seidel <eric@webkit.org>
  5. * (C) 2012 Patrick Ganstere <paroga@paroga.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  17. * its contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "JavaScriptThreading.h"
  33. #include <JavaScriptCore/JavaScriptCore.h>
  34. #include <stdlib.h>
  35. #include <wtf/Assertions.h>
  36. #include <wtf/HashSet.h>
  37. #include <wtf/Vector.h>
  38. static const size_t javaScriptThreadsCount = 4;
  39. static bool javaScriptThreadsShouldTerminate;
  40. static JSContextGroupRef javaScriptThreadsGroup;
  41. static Mutex& javaScriptThreadsMutex()
  42. {
  43. DEFINE_STATIC_LOCAL(Mutex, staticMutex, ());
  44. return staticMutex;
  45. }
  46. typedef HashSet<ThreadIdentifier> ThreadSet;
  47. static ThreadSet& javaScriptThreads()
  48. {
  49. DEFINE_STATIC_LOCAL(ThreadSet, staticJavaScriptThreads, ());
  50. ASSERT(!javaScriptThreadsMutex().tryLock());
  51. return staticJavaScriptThreads;
  52. }
  53. // This function exercises JSC in a loop until javaScriptThreadsShouldTerminate
  54. // becomes true or it probabilistically decides to spawn a replacement thread and exit.
  55. void runJavaScriptThread(void*)
  56. {
  57. static const char* const script =
  58. "var array = [];"
  59. "for (var i = 0; i < 1024; i++) {"
  60. " array.push(String(i));"
  61. "}";
  62. JSGlobalContextRef ctx;
  63. {
  64. MutexLocker locker(javaScriptThreadsMutex());
  65. ctx = JSGlobalContextCreateInGroup(javaScriptThreadsGroup, 0);
  66. }
  67. JSStringRef scriptRef;
  68. {
  69. MutexLocker locker(javaScriptThreadsMutex());
  70. scriptRef = JSStringCreateWithUTF8CString(script);
  71. }
  72. while (true) {
  73. {
  74. MutexLocker locker(javaScriptThreadsMutex());
  75. JSValueRef exception = 0;
  76. JSEvaluateScript(ctx, scriptRef, 0, 0, 1, &exception);
  77. ASSERT(!exception);
  78. }
  79. {
  80. MutexLocker locker(javaScriptThreadsMutex());
  81. const size_t valuesCount = 1024;
  82. JSValueRef values[valuesCount];
  83. for (size_t i = 0; i < valuesCount; ++i)
  84. values[i] = JSObjectMake(ctx, 0, 0);
  85. }
  86. {
  87. MutexLocker locker(javaScriptThreadsMutex());
  88. if (javaScriptThreadsShouldTerminate)
  89. break;
  90. }
  91. // Respawn probabilistically.
  92. if (rand() % 5)
  93. continue;
  94. MutexLocker locker(javaScriptThreadsMutex());
  95. ThreadIdentifier thread = currentThread();
  96. detachThread(thread);
  97. javaScriptThreads().remove(thread);
  98. javaScriptThreads().add(createThread(&runJavaScriptThread, 0, 0));
  99. break;
  100. }
  101. MutexLocker locker(javaScriptThreadsMutex());
  102. JSStringRelease(scriptRef);
  103. JSGarbageCollect(ctx);
  104. JSGlobalContextRelease(ctx);
  105. }
  106. void startJavaScriptThreads()
  107. {
  108. javaScriptThreadsGroup = JSContextGroupCreate();
  109. MutexLocker locker(javaScriptThreadsMutex());
  110. for (size_t i = 0; i < javaScriptThreadsCount; ++i)
  111. javaScriptThreads().add(createThread(&runJavaScriptThread, 0, 0));
  112. }
  113. void stopJavaScriptThreads()
  114. {
  115. {
  116. MutexLocker locker(javaScriptThreadsMutex());
  117. javaScriptThreadsShouldTerminate = true;
  118. }
  119. Vector<ThreadIdentifier, javaScriptThreadsCount> threads;
  120. {
  121. MutexLocker locker(javaScriptThreadsMutex());
  122. copyToVector(javaScriptThreads(), threads);
  123. ASSERT(threads.size() == javaScriptThreadsCount);
  124. }
  125. for (size_t i = 0; i < javaScriptThreadsCount; ++i)
  126. waitForThreadCompletion(threads[i]);
  127. javaScriptThreads().clear();
  128. JSContextGroupRelease(javaScriptThreadsGroup);
  129. }