RuntimeThreadData.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "stdafx.h"
  6. void RuntimeThreadLocalData::Initialize(RuntimeThreadData* threadData)
  7. {
  8. this->threadData = threadData;
  9. }
  10. void RuntimeThreadLocalData::Uninitialize()
  11. {
  12. }
  13. THREAD_LOCAL RuntimeThreadLocalData threadLocalData;
  14. RuntimeThreadLocalData& GetRuntimeThreadLocalData()
  15. {
  16. return threadLocalData;
  17. }
  18. RuntimeThreadData::RuntimeThreadData() :
  19. hSemaphore(nullptr),
  20. hThread(nullptr),
  21. sharedContent(nullptr),
  22. receiveBroadcastCallbackFunc(nullptr),
  23. runtime(nullptr),
  24. context(nullptr),
  25. parent(nullptr),
  26. leaving(false)
  27. {
  28. this->hevntInitialScriptCompleted = CreateEvent(NULL, TRUE, FALSE, NULL);
  29. this->hevntReceivedBroadcast = CreateEvent(NULL, FALSE, FALSE, NULL);
  30. this->hevntShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
  31. InitializeCriticalSection(&csReportQ);
  32. }
  33. RuntimeThreadData::~RuntimeThreadData()
  34. {
  35. CloseHandle(this->hevntInitialScriptCompleted);
  36. CloseHandle(this->hevntReceivedBroadcast);
  37. CloseHandle(this->hevntShutdown);
  38. CloseHandle(this->hThread);
  39. DeleteCriticalSection(&csReportQ);
  40. }
  41. DWORD RuntimeThreadData::ThreadProc()
  42. {
  43. JsValueRef scriptSource;
  44. JsValueRef fname;
  45. const char* fullPath = "agent source";
  46. HRESULT hr = S_OK;
  47. threadLocalData.Initialize(this);
  48. IfJsErrorFailLog(ChakraRTInterface::JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime));
  49. IfJsErrorFailLog(ChakraRTInterface::JsCreateContext(runtime, &context));
  50. IfJsErrorFailLog(ChakraRTInterface::JsSetCurrentContext(context));
  51. if (!WScriptJsrt::Initialize())
  52. {
  53. IfFailGo(E_FAIL);
  54. }
  55. IfJsErrorFailLog(ChakraRTInterface::JsCreateExternalArrayBuffer((void*)this->initialSource.c_str(),
  56. (unsigned int)this->initialSource.size(), nullptr, nullptr, &scriptSource));
  57. ChakraRTInterface::JsCreateString(fullPath, strlen(fullPath), &fname);
  58. ChakraRTInterface::JsRun(scriptSource, WScriptJsrt::GetNextSourceContext(), fname, JsParseScriptAttributeNone, nullptr);
  59. SetEvent(this->parent->hevntInitialScriptCompleted);
  60. // loop waiting for work;
  61. while (true)
  62. {
  63. HANDLE handles[] = { this->hevntReceivedBroadcast, this->hevntShutdown };
  64. DWORD waitRet = WaitForMultipleObjects(_countof(handles), handles, false, INFINITE);
  65. if (waitRet == WAIT_OBJECT_0)
  66. {
  67. JsValueRef args[3];
  68. ChakraRTInterface::JsGetGlobalObject(&args[0]);
  69. ChakraRTInterface::JsCreateSharedArrayBufferWithSharedContent(this->parent->sharedContent, &args[1]);
  70. ChakraRTInterface::JsDoubleToNumber(1, &args[2]);
  71. // notify the parent we received the data
  72. ReleaseSemaphore(this->parent->hSemaphore, 1, NULL);
  73. if (this->receiveBroadcastCallbackFunc)
  74. {
  75. ChakraRTInterface::JsCallFunction(this->receiveBroadcastCallbackFunc, args, 3, nullptr);
  76. }
  77. }
  78. if (waitRet == WAIT_OBJECT_0 + 1 || this->leaving)
  79. {
  80. WScriptJsrt::Uninitialize();
  81. if (this->receiveBroadcastCallbackFunc)
  82. {
  83. ChakraRTInterface::JsRelease(this->receiveBroadcastCallbackFunc, nullptr);
  84. }
  85. ChakraRTInterface::JsSetCurrentContext(nullptr);
  86. ChakraRTInterface::JsDisposeRuntime(runtime);
  87. threadLocalData.Uninitialize();
  88. return 0;
  89. }
  90. else if (waitRet != WAIT_OBJECT_0)
  91. {
  92. Assert(false);
  93. break;
  94. }
  95. }
  96. Error:
  97. ChakraRTInterface::JsSetCurrentContext(nullptr);
  98. ChakraRTInterface::JsDisposeRuntime(runtime);
  99. threadLocalData.Uninitialize();
  100. return 0;
  101. }