HRESULT ServerInitializeThreadContext( /* [in] */ handle_t binding, /* [in] */ __RPC__in ThreadContextDataIDL * threadContextData, /* [out] */ __RPC__out intptr_t *threadContextRoot, /* [out] */ __RPC__out intptr_t *prereservedRegionAddr) { AUTO_NESTED_HANDLED_EXCEPTION_TYPE(static_cast<ExceptionType>(ExceptionType_OutOfMemory | ExceptionType_StackOverflow)); ServerThreadContext * contextInfo = HeapNewNoThrow(ServerThreadContext, threadContextData); if (contextInfo == nullptr) { return E_OUTOFMEMORY; } AutoReleaseContext<ServerThreadContext> autoThreadContext(contextInfo); return ServerCallWrapper(contextInfo, [&]()->HRESULT { ServerContextManager::RegisterThreadContext(contextInfo); *threadContextRoot = (intptr_t)EncodePointer(contextInfo); *prereservedRegionAddr = (intptr_t)contextInfo->GetPreReservedVirtualAllocator()->EnsurePreReservedRegion(); return S_OK; }); }
QueuedFullJitWorkItem *CodeGenWorkItem::EnsureQueuedFullJitWorkItem() { if(queuedFullJitWorkItem) { return queuedFullJitWorkItem; } queuedFullJitWorkItem = HeapNewNoThrow(QueuedFullJitWorkItem, this); return queuedFullJitWorkItem; }
HashTbl * HashTbl::Create(uint cidHash, ErrHandler * perr) { HashTbl * phtbl; if (nullptr == (phtbl = HeapNewNoThrow(HashTbl,perr))) return nullptr; if (!phtbl->Init(cidHash)) { delete phtbl; return nullptr; } return phtbl; }
void BackgroundJobProcessor::InitializeParallelThreadDataForThreadServiceCallBack(AllocationPolicyManager* policyManager) { //thread is provided by service callback, no need to create thread here. Currently only one thread in service callback supported. this->maxThreadCount = 1; this->parallelThreadData = HeapNewArrayZ(ParallelThreadData *, this->maxThreadCount); this->parallelThreadData[0] = HeapNewNoThrow(ParallelThreadData, policyManager); if (this->parallelThreadData[0] == nullptr) { HeapDeleteArray(this->maxThreadCount, this->parallelThreadData); Js::Throw::OutOfMemory(); } this->parallelThreadData[0]->processor = this; this->parallelThreadData[0]->isWaitingForJobs = true; #if DBG_DUMP this->parallelThreadData[0]->backgroundPageAllocator.debugName = _u("BackgroundJobProcessor"); #endif this->threadCount = 1; return; }
void BackgroundJobProcessor::InitializeParallelThreadData(AllocationPolicyManager* policyManager, bool disableParallelThreads) { if (!disableParallelThreads) { InitializeThreadCount(); } else { this->maxThreadCount = 1; } Assert(this->maxThreadCount >= 1); this->parallelThreadData = HeapNewArrayZ(ParallelThreadData*, this->maxThreadCount); for (uint i = 0; i < this->maxThreadCount; i++) { this->parallelThreadData[i] = HeapNewNoThrow(ParallelThreadData, policyManager); if (this->parallelThreadData[i] == nullptr) { if (i == 0) { HeapDeleteArray(this->maxThreadCount, this->parallelThreadData); Js::Throw::OutOfMemory(); } // At least one thread is created, continue break; } this->parallelThreadData[i]->processor = this; // Make sure to create the thread suspended so the thread handle can be assigned before the thread starts running this->parallelThreadData[i]->threadHandle = reinterpret_cast<HANDLE>(PlatformAgnostic::Thread::Create(0, &StaticThreadProc, this->parallelThreadData[i], PlatformAgnostic::Thread::ThreadInitCreateSuspended)); if (!this->parallelThreadData[i]->threadHandle) { HeapDelete(parallelThreadData[i]); parallelThreadData[i] = nullptr; if (i == 0) { Js::Throw::OutOfMemory(); } // At least one thread is created, continue break; } if (ResumeThread(this->parallelThreadData[i]->threadHandle) == static_cast<DWORD>(-1)) { CloseHandle(this->parallelThreadData[i]->threadHandle); HeapDelete(parallelThreadData[i]); this->parallelThreadData[i] = nullptr; if (i == 0) { Js::Throw::OutOfMemory(); } // At least one thread is created, continue break; } this->threadCount++; // Wait for the thread to fully start. This is necessary because Close may be called before the thread starts and if // Close is called while holding the loader lock during DLL_THREAD_DETACH, the thread may be stuck waiting for the // loader lock for DLL_THREAD_ATTACH to start up, and Close would then end up waiting forever, causing a deadlock. WaitWithThreadForThreadStartedOrClosingEvent(this->parallelThreadData[i]); this->parallelThreadData[i]->threadStartedOrClosing.Reset(); // after this, the event will be used to wait for the thread to close #if DBG_DUMP if (i < (sizeof(DebugThreadNames) / sizeof(DebugThreadNames[i]))) { this->parallelThreadData[i]->backgroundPageAllocator.debugName = DebugThreadNames[i]; } else { this->parallelThreadData[i]->backgroundPageAllocator.debugName = _u("BackgroundJobProcessor thread"); } #endif } Assert(this->threadCount >= 1); }
void SharedArrayBuffer::Init(uint32 length, uint32 maxLength) { AssertOrFailFast(!sharedContents && length <= maxLength); BYTE * buffer = nullptr; if (length > MaxSharedArrayBufferLength) { // http://tc39.github.io/ecmascript_sharedmem/shmem.html#DataTypesValues.SpecTypes.DataBlocks.CreateSharedByteDataBlock // Let db be a new Shared Data Block value consisting of size bytes. // If it is impossible to create such a Shared Data Block, throw a RangeError exception. JavascriptError::ThrowRangeError(GetScriptContext(), JSERR_FunctionArgument_Invalid); } SharedContents* localSharedContents = HeapNewNoThrow(SharedContents, nullptr, length, maxLength); if (localSharedContents == nullptr) { JavascriptError::ThrowOutOfMemoryError(GetScriptContext()); } struct AutoCleanupSharedContents { SharedContents* sharedContents; bool allocationCompleted = false; AutoCleanupSharedContents(SharedContents* sharedContents) : sharedContents(sharedContents) {} ~AutoCleanupSharedContents() { if (!allocationCompleted) { HeapDelete(sharedContents); } } } autoCleanupSharedContents(localSharedContents); Recycler* recycler = GetType()->GetLibrary()->GetRecycler(); if (maxLength != 0) { if (recycler->RequestExternalMemoryAllocation(length)) { buffer = this->AllocBuffer(length, maxLength); if (buffer == nullptr) { recycler->CollectNow<CollectOnTypedArrayAllocation>(); buffer = this->AllocBuffer(length, maxLength); if (buffer == nullptr) { recycler->ReportExternalMemoryFailure(length); JavascriptError::ThrowOutOfMemoryError(GetScriptContext()); } } } else { JavascriptError::ThrowOutOfMemoryError(GetScriptContext()); } Assert(buffer != nullptr); ZeroMemory(buffer, length); } localSharedContents->buffer = buffer; #if DBG localSharedContents->AddAgent((DWORD_PTR)GetScriptContext()); #endif sharedContents = localSharedContents; autoCleanupSharedContents.allocationCompleted = true; }