Changeset 1812


Ignore:
Timestamp:
2/11/2010 2:15:09 AM (5 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
Location:
trunk/eraser6/Eraser.Util.Native
Files:
4 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} 
  • trunk/eraser6/Eraser.Util.Native/OpenHandle.NameResolver.h

    r1802 r1812  
    8181}; 
    8282 
    83 void CreateNameThread(HANDLE& handle, NameResolutionThreadParams& params); 
     83std::wstring ResolveHandleName(HANDLE handle, int pid); 
  • trunk/eraser6/Eraser.Util.Native/OpenHandle.cpp

    r1802 r1812  
    7373    String^ OpenHandle::ResolveHandlePath(IntPtr handle, int pid) 
    7474    { 
    75         //Start a name resolution thread (in case one entry hangs) 
    76         if (NameResolutionThread == NULL) 
    77         { 
    78             NameResolutionThread = new HANDLE(0); 
    79             NameResolutionThreadParam = new NameResolutionThreadParams; 
    80             CreateNameThread(*NameResolutionThread, *NameResolutionThreadParam); 
    81         } 
    82  
    83         //Create a duplicate handle 
    84         HANDLE localHandle(NULL); 
    85         HANDLE processHandle = OpenProcess(PROCESS_DUP_HANDLE, false, pid); 
    86         DuplicateHandle(processHandle, static_cast<void*>(handle), GetCurrentProcess(), 
    87             &localHandle, 0, false, DUPLICATE_SAME_ACCESS); 
    88         CloseHandle(processHandle); 
    89  
    90         //We need a handle 
    91         if (!localHandle) 
    92             return nullptr; 
    93  
    94         //Send the handle to the secondary thread for name resolution 
    95         NameResult result(localHandle); 
    96         NameResolutionThreadParam->Input.push_back(&result); 
    97         ReleaseSemaphore(NameResolutionThreadParam->Semaphore, 1, NULL); 
    98  
    99         //Wait for the result 
    100         if (WaitForSingleObject(result.Event, 50) != WAIT_OBJECT_0) 
    101         { 
    102             //The wait failed. Terminate the thread and recreate another. 
    103             CreateNameThread(*NameResolutionThread, *NameResolutionThreadParam); 
    104         } 
    105  
    106         //Close the handle which we duplicated 
    107         CloseHandle(localHandle); 
    108  
    109         //Return the result 
    110         if (result.Name.empty()) 
    111             return nullptr; 
    112         else 
    113             return gcnew String(result.Name.c_str(), 0, 
    114                 static_cast<int>(result.Name.length())); 
     75        std::wstring result(ResolveHandleName(static_cast<void*>(handle), pid)); 
     76        return result.empty() ? nullptr : 
     77            gcnew String(result.c_str(), 0, static_cast<int>(result.length())); 
    11578    } 
    11679 
  • trunk/eraser6/Eraser.Util.Native/OpenHandle.h

    r1802 r1812  
    9393 
    9494    private: 
    95         static HANDLE* NameResolutionThread; 
    96         static NameResolutionThreadParams* NameResolutionThreadParam; 
    97  
    9895        IntPtr handle; 
    9996        String^ path; 
Note: See TracChangeset for help on using the changeset viewer.