示例#1
0
文件: sxs.cpp 项目: Afshintm/coreclr
PAL_ERROR
AllocatePalThread(CPalThread **ppThread)
{
    CPalThread *pThread = NULL;

    PAL_ERROR palError = CreateThreadData(&pThread);
    if (NO_ERROR != palError)
    {
        goto exit;
    }

    HANDLE hThread;
    palError = CreateThreadObject(pThread, pThread, &hThread);
    if (NO_ERROR != palError)
    {
        pthread_setspecific(thObjKey, NULL);
        pThread->ReleaseThreadReference();
        goto exit;
    }
    
    // Like CreateInitialProcessAndThreadObjects, we do not need this 
    // thread handle, since we're not returning it to anyone who will 
    // possibly release it.
    (void)g_pObjectManager->RevokeHandle(pThread, hThread);

    PROCAddThread(pThread, pThread);

exit:
    *ppThread = pThread;
    return palError;
}
示例#2
0
文件: sxs.cpp 项目: Dykam/coreclr
/*++
Function:
  PAL_Enter

Abstract:
  This function needs to be called on a thread when it enters
  a region of code that depends on this instance of the PAL
  in the process, and the current thread may or may not be
  known to the PAL.  This function can fail (for something else
  than an internal error) if this is the first time that the
  current thread entered this PAL.  Note that PAL_Initialize
  implies a call to this function.

  NOTE: This function must not modify LastError.
--*/
PAL_ERROR
PALAPI
PAL_Enter(PAL_Boundary boundary)
{
    ENTRY_EXTERNAL("PAL_Enter(boundary=%u)\n", boundary);

    PAL_ERROR palError = ERROR_SUCCESS;
    CPalThread *pThread = InternalGetCurrentThread();
    if (pThread != NULL)
    {
        palError = pThread->Enter(boundary);
    }
    else
    {
        // If this assert fires, we'll have to pipe this information so that 
        // CPalThread's RunPostCreateInitializers call to SEHEnable 
        // can know what direction.
        _ASSERT_MSG(PAL_BoundaryTop == boundary, "How are we entering a PAL "
            "thread for the first time not from the top? (boundary=%u)", boundary);
            
        palError = CreateThreadData(&pThread);
        if (NO_ERROR != palError)
        {
            ERROR("Unable to create thread data: error %d\n", palError);
            goto EXIT;
        }

        HANDLE hThread;
        palError = CreateThreadObject(pThread, pThread, &hThread);
        if (NO_ERROR != palError)
        {
            ERROR("Unable to create thread object: error %d\n", palError);
            pthread_setspecific(thObjKey, NULL);
            pThread->ReleaseThreadReference();
            goto EXIT;
        }
        
        // Like CreateInitialProcessAndThreadObjects, we do not need this 
        // thread handle, since we're not returning it to anyone who will 
        // possibly release it.
        (void)g_pObjectManager->RevokeHandle(pThread, hThread);

        PROCAddThread(pThread, pThread);
    }

EXIT:
    LOGEXIT("PAL_Enter returns %d\n", palError);
    return palError;
}
示例#3
0
PAL_ERROR
AllocatePalThread(CPalThread **ppThread)
{
    CPalThread *pThread = NULL;
    PAL_ERROR palError;

#if !HAVE_MACH_EXCEPTIONS
    // Ensure alternate stack for SIGSEGV handling. Our SIGSEGV handler is set to
    // run on an alternate stack and the stack needs to be allocated per thread.
    if (!EnsureSignalAlternateStack())
    {
        ERROR("Cannot allocate alternate stack for SIGSEGV handler!\n");
        palError = ERROR_NOT_ENOUGH_MEMORY;
        goto exit;
    }
#endif // !HAVE_MACH_EXCEPTIONS

    palError = CreateThreadData(&pThread);
    if (NO_ERROR != palError)
    {
        goto exit;
    }

    HANDLE hThread;
    palError = CreateThreadObject(pThread, pThread, &hThread);
    if (NO_ERROR != palError)
    {
        pthread_setspecific(thObjKey, NULL);
        pThread->ReleaseThreadReference();
        goto exit;
    }
    
    // Like CreateInitialProcessAndThreadObjects, we do not need this 
    // thread handle, since we're not returning it to anyone who will 
    // possibly release it.
    (void)g_pObjectManager->RevokeHandle(pThread, hThread);

    PROCAddThread(pThread, pThread);

exit:
    *ppThread = pThread;
    return palError;
}
示例#4
0
/*++
Function:
  PALCommonCleanup

Utility function to free any resource used by the PAL. 

Parameters :
    step: selects the desired cleanup step
    full_cleanup:  FALSE: cleanup only what's needed and leave the rest 
                          to the OS process cleanup
                   TRUE:  full cleanup 
--*/
void 
PALCommonCleanup(PALCLEANUP_STEP step, BOOL full_cleanup)
{
    CPalThread *pThread = InternalGetCurrentThread();
    static int step_done[PALCLEANUP_STEP_INVALID] = { 0 };

    switch (step)
    {
    case PALCLEANUP_ALL_STEPS:
    case PALCLEANUP_STEP_ONE:
        /* Note: in order to work correctly, this step should be executed with 
           init_count > 0
         */
        if (!step_done[PALCLEANUP_STEP_ONE])
        {
            step_done[PALCLEANUP_STEP_ONE] = 1;

            PALSetShutdownIntent();

            //
            // Let the synchronization manager know we're about to shutdown
            //

            CPalSynchMgrController::PrepareForShutdown();

#ifdef _DEBUG
            PROCDumpThreadList();
#endif

            TRACE("About to suspend every other thread\n");

            /* prevent other threads from acquiring signaled objects */
            PROCCondemnOtherThreads();
            /* prevent other threads from using services we're shutting down */
            PROCSuspendOtherThreads();

            TRACE("Every other thread suspended until exit\n");
        }

        /* Fall down for PALCLEANUP_ALL_STEPS */
        if (PALCLEANUP_ALL_STEPS != step)
            break;

    case PALCLEANUP_STEP_TWO:
        if (!step_done[PALCLEANUP_STEP_TWO])
        {
            step_done[PALCLEANUP_STEP_TWO] = 1;

            /* LOADFreeeModules needs to be called before unitializing the rest
               of the PAL since it could result in calling DllMain for loaded
               libraries. For the user DllMain, all PAL APIs should still be
               functional. */
            LOADFreeModules(FALSE);

#ifdef PAL_PERF
            PERFDisableProcessProfile();
            PERFDisableThreadProfile(FALSE);
            PERFTerminate();  
#endif

            if (full_cleanup)
            {
                /* close primary handles of standard file objects */
                FILECleanupStdHandles();
                /* This unloads the palrt so, during its unloading, they
                   can call any number of APIs, so we have to be active for it to work. */
                FMTMSG_FormatMessageCleanUp();
                VIRTUALCleanup();
                /* SEH requires information from the process structure to work;
                   LOADFreeModules requires SEH to be functional when calling DllMain.
                   Therefore SEHCleanup must go between LOADFreeModules and
                   PROCCleanupInitialProcess */
                SEHCleanup();
                PROCCleanupInitialProcess();
            }

            // Object manager shutdown may cause all CPalThread objects
            // to be deleted. Since the CPalThread of the shutdown thread
            // needs to be available for reference by the thread suspension unsafe
            // operations, the reference of CPalThread is incremented here
            // to keep it alive until PAL finishes cleanup.
            pThread->AddThreadReference();

            //
            // Shutdown object manager -- this needs to happen before the
            // synch manager shutdown since it will call into the synch
            // manager to free object synch data
            // 
            static_cast<CSharedMemoryObjectManager*>(g_pObjectManager)->Shutdown(pThread);

            //
            // Final synch manager shutdown
            //
            CPalSynchMgrController::Shutdown(pThread, full_cleanup);

            if (full_cleanup)
            {
                /* It needs to be done after stopping the handle manager, because
                   the cleanup will delete the critical section which is used
                   when closing the handle of a file mapping */
                MAPCleanup();
                // MutexCleanup();

                MiscCleanup();

                TLSCleanup();
            }

            // The thread object will no longer be available after the shutdown thread
            // releases the thread reference.
            g_fThreadDataAvailable = FALSE;
            pThread->ReleaseThreadReference();
            pthread_setspecific(thObjKey, NULL); // Make sure any TLS entry is removed.

            // Since thread object is no longer available here,
            // the code path from here should stop using any functions
            // that reference thread object.
            SHMCleanup();

            TRACE("PAL Terminated.\n");
        }
        break;

    default:
        ASSERT("Unknown final cleanup step %d", step);
        break;
    }
}