Threading.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "Threading.h"
  27. #include <wtf/OwnPtr.h>
  28. #include <wtf/PassOwnPtr.h>
  29. #include <string.h>
  30. namespace WTF {
  31. struct NewThreadContext {
  32. WTF_MAKE_FAST_ALLOCATED;
  33. public:
  34. NewThreadContext(ThreadFunction entryPoint, void* data, const char* name)
  35. : entryPoint(entryPoint)
  36. , data(data)
  37. , name(name)
  38. {
  39. }
  40. ThreadFunction entryPoint;
  41. void* data;
  42. const char* name;
  43. Mutex creationMutex;
  44. };
  45. static void threadEntryPoint(void* contextData)
  46. {
  47. NewThreadContext* context = reinterpret_cast<NewThreadContext*>(contextData);
  48. // Block until our creating thread has completed any extra setup work, including
  49. // establishing ThreadIdentifier.
  50. {
  51. MutexLocker locker(context->creationMutex);
  52. }
  53. initializeCurrentThreadInternal(context->name);
  54. // Grab the info that we need out of the context, then deallocate it.
  55. ThreadFunction entryPoint = context->entryPoint;
  56. void* data = context->data;
  57. delete context;
  58. entryPoint(data);
  59. }
  60. ThreadIdentifier createThread(ThreadFunction entryPoint, void* data, const char* name)
  61. {
  62. // Visual Studio has a 31-character limit on thread names. Longer names will
  63. // be truncated silently, but we'd like callers to know about the limit.
  64. #if !LOG_DISABLED && PLATFORM(WIN)
  65. if (name && strlen(name) > 31)
  66. LOG_ERROR("Thread name \"%s\" is longer than 31 characters and will be truncated by Visual Studio", name);
  67. #endif
  68. NewThreadContext* context = new NewThreadContext(entryPoint, data, name);
  69. // Prevent the thread body from executing until we've established the thread identifier.
  70. MutexLocker locker(context->creationMutex);
  71. return createThreadInternal(threadEntryPoint, context, name);
  72. }
  73. #if PLATFORM(MAC) || PLATFORM(WIN)
  74. // For ABI compatibility with Safari on Mac / Windows: Safari uses the private
  75. // createThread() and waitForThreadCompletion() functions directly and we need
  76. // to keep the old ABI compatibility until it's been rebuilt.
  77. typedef void* (*ThreadFunctionWithReturnValue)(void* argument);
  78. WTF_EXPORT_PRIVATE ThreadIdentifier createThread(ThreadFunctionWithReturnValue entryPoint, void* data, const char* name);
  79. struct ThreadFunctionWithReturnValueInvocation {
  80. ThreadFunctionWithReturnValueInvocation(ThreadFunctionWithReturnValue function, void* data)
  81. : function(function)
  82. , data(data)
  83. {
  84. }
  85. ThreadFunctionWithReturnValue function;
  86. void* data;
  87. };
  88. static void compatEntryPoint(void* param)
  89. {
  90. // Balanced by .leakPtr() in createThread.
  91. OwnPtr<ThreadFunctionWithReturnValueInvocation> invocation = adoptPtr(static_cast<ThreadFunctionWithReturnValueInvocation*>(param));
  92. invocation->function(invocation->data);
  93. }
  94. ThreadIdentifier createThread(ThreadFunctionWithReturnValue entryPoint, void* data, const char* name)
  95. {
  96. OwnPtr<ThreadFunctionWithReturnValueInvocation> invocation = adoptPtr(new ThreadFunctionWithReturnValueInvocation(entryPoint, data));
  97. // Balanced by adoptPtr() in compatEntryPoint.
  98. return createThread(compatEntryPoint, invocation.leakPtr(), name);
  99. }
  100. WTF_EXPORT_PRIVATE int waitForThreadCompletion(ThreadIdentifier, void**);
  101. int waitForThreadCompletion(ThreadIdentifier threadID, void**)
  102. {
  103. return waitForThreadCompletion(threadID);
  104. }
  105. // This function is deprecated but needs to be kept around for backward
  106. // compatibility. Use the 3-argument version of createThread above.
  107. WTF_EXPORT_PRIVATE ThreadIdentifier createThread(ThreadFunctionWithReturnValue entryPoint, void* data);
  108. ThreadIdentifier createThread(ThreadFunctionWithReturnValue entryPoint, void* data)
  109. {
  110. OwnPtr<ThreadFunctionWithReturnValueInvocation> invocation = adoptPtr(new ThreadFunctionWithReturnValueInvocation(entryPoint, data));
  111. // Balanced by adoptPtr() in compatEntryPoint.
  112. return createThread(compatEntryPoint, invocation.leakPtr(), 0);
  113. }
  114. #endif
  115. } // namespace WTF