Ignore:
Timestamp:
2/11/2010 2:15:09 AM (2 years ago)
Author:
lowjoel
Message:
  • Move the contents of the OpenHandle::ResolveHandlePath? function to the ResolveHandleName? function in native code (we would have p/invoked quite a bit in that function)
  • Move the global name resolution thread and it's task queue to native code and in a anonymous namespace
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser6/Eraser.Util.Native/OpenHandle.NameResolver.cpp

    r1802 r1812  
    2323#include "OpenHandle.h" 
    2424 
    25 #pragma managed(push) 
    2625#pragma unmanaged 
    2726 
    2827namespace { 
     28    HANDLE NameResolutionThread; 
     29    NameResolutionThreadParams NameResolutionThreadParam; 
     30 
    2931    DWORD __stdcall nameResolutionThread(void* data) 
    3032    { 
     
    8688        return 0; 
    8789    } 
     90 
     91    void CreateNameThread(HANDLE& handle, NameResolutionThreadParams& params) 
     92    { 
     93        //If the handle is valid terminate the thread 
     94        if (handle) 
     95        { 
     96            TerminateThread(handle, 1); 
     97            CloseHandle(handle); 
     98        } 
     99 
     100        //Create the thread 
     101        handle = CreateThread(NULL, 0, nameResolutionThread, &params, 0, NULL); 
     102    } 
     103 
    88104} 
    89105 
    90 void CreateNameThread(HANDLE& handle, NameResolutionThreadParams& params) 
     106std::wstring ResolveHandleName(HANDLE handle, int pid) 
    91107{ 
    92     //If the handle is valid terminate the thread 
    93     if (handle) 
     108    //Start a name resolution thread (in case one entry hangs) 
     109    if (NameResolutionThread == NULL) 
     110        CreateNameThread(NameResolutionThread, NameResolutionThreadParam); 
     111 
     112    //Create a duplicate handle 
     113    HANDLE localHandle; 
     114    HANDLE processHandle = OpenProcess(PROCESS_DUP_HANDLE, false, pid); 
     115    DuplicateHandle(processHandle, static_cast<void*>(handle), GetCurrentProcess(), 
     116        &localHandle, 0, false, DUPLICATE_SAME_ACCESS); 
     117    CloseHandle(processHandle); 
     118 
     119    //We need a handle 
     120    if (!localHandle) 
     121        return std::wstring(); 
     122 
     123    //Send the handle to the secondary thread for name resolution 
     124    NameResult result(localHandle); 
     125    NameResolutionThreadParam.Input.push_back(&result); 
     126    ReleaseSemaphore(NameResolutionThreadParam.Semaphore, 1, NULL); 
     127 
     128    //Wait for the result 
     129    if (WaitForSingleObject(result.Event, 50) != WAIT_OBJECT_0) 
    94130    { 
    95         TerminateThread(handle, 1); 
    96         CloseHandle(handle); 
     131        //The wait failed. Terminate the thread and recreate another. 
     132        CreateNameThread(NameResolutionThread, NameResolutionThreadParam); 
    97133    } 
    98134 
    99     //Create the thread 
    100     handle = CreateThread(NULL, 0, nameResolutionThread, &params, 0, NULL); 
     135    //Close the handle which we duplicated 
     136    CloseHandle(localHandle); 
     137 
     138    //Return the result 
     139    return result.Name; 
    101140} 
Note: See TracChangeset for help on using the changeset viewer.