Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Multi-Thread Issues

By definition, each executing thread should have its own call stack. This means every call of myHookMalloc can access the per-thread call-stack data structure freely without any problem. Thread Local Storage (TLS) comes to assistance here. For each thread, we store the pointer to its corresponding call-stack root node and another pointer to the current node using the win32 API TlsSetValue. This data will be retrieved by myHookedMalloc using TlsGetValue. Care has to be taken in implementing myHookedFree because it may operate on a call-stack node that doesn’t belong to the current thread.

To protect the deallocation routine from race conditions, each thread’s root node is assigned a critical section, and all its ancestor nodes will keep a reference to it. When myHookedMalloc or myHookedFree is invoked, the critical section of that node will be locked before any statistic information update. Such a design keeps the number of critical sections to a minimum while preserving a very low lock contention level; moreover, it does not create a locking hierarchy, so it should be free from dead-lock.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint