locked
Must ICorProfilerInfo calls be synchronized?

    Question

  • Must ICorProfilerInfo (and other profiling api) calls be synchronized or is the profiling api thread safe?

    Thanks,

    Tom

    Wednesday, May 02, 2012 5:35 PM

Answers

All replies

  • Hi Tom,

    Welcome to the MSDN Forum.

    According to this documentation: http://msdn.microsoft.com/en-us/library/ms233177.aspx 

    The methods of the ICorProfilerInfo interface are implemented by the CLR using the free-threaded model. Each method returns an HRESULT to indicate success or failure. See CorError.h for a list of possible return codes.

    So ICorProfilerInfo calls can be not synchronized.

    Here is more about free-threaded modelhttp://msdn.microsoft.com/en-us/library/windows/desktop/ms693421(v=vs.85).aspx 

    In a multithreaded apartment model, all the threads in the process that have been initialized as free-threaded reside in a single apartment. Therefore, there is no need to marshal between threads. The threads need not retrieve and dispatch messages because COM does not use window messages in this model.

    Following are some important considerations regarding synchronization for multithreaded apartments:

    • COM provides call synchronization for single-threaded apartments only.
    • Multithreaded apartments do not receive calls while making calls (on the same thread).
    • Multithreaded apartments cannot make input-synchronized calls.
    • Asynchronous calls are converted to synchronous calls in multithreaded apartments.
    • The message filter is not called for any thread in a multithreaded apartment.

    And this: http://www.microsoft.com/msj/0897/free.aspx 

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ICorThomas Friday, May 04, 2012 1:32 PM
    • Unmarked as answer by ICorThomas Thursday, May 10, 2012 8:39 AM
    Thursday, May 03, 2012 11:59 AM
    Moderator
  • Ok, so my profiler can access ICorProfilerInfo instances concurrently from multiple threads without synchronization.

    What about the meta data interfaces like IMetaDataImport for example?

    Thanks,

    Tom

    Thursday, May 03, 2012 4:02 PM
  • Hi Tom,

    Based on the documentation: http://msdn.microsoft.com/en-us/library/ms230172.aspx 

    There is no such descriptions as the same as ICorProfilerInfo, so it is not sure all the method is thread-safe or not. Actually, a method in an interface is thread-safe or not, is more related to how it is implemented. 

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 04, 2012 5:32 AM
    Moderator
  • Hi, Tom.

    The metadata interfaces are free-thread as well, as far as I know.  But that doesn't mean you don't have to use synchronization when using the profiling or metadata interfaces.  IDs passed between your profiler and the CLR can get unloaded, become invalid, and then cause the CLR to crash if you continue to specify them in calls to the profiling API.  Some kind of cross-thread synchronization inside your profiler is required to ensure your profiler never passes a bad ID to the CLR.  Some references:

    http://blogs.msdn.com/b/davbr/archive/2011/10/17/metadata-tokens-run-time-ids-and-type-loading.aspx

    http://msdn.microsoft.com/en-us/library/bb384619(v=vs.110).aspx

    Thanks,
    Dave

    • Marked as answer by ICorThomas Thursday, May 10, 2012 8:39 AM
    Wednesday, May 09, 2012 7:06 PM
    Owner