pin_tls.cc 900 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "tls.h"
  2. #include "log.h"
  3. #include <pin.H>
  4. class PinTLS : public TLS
  5. {
  6. public:
  7. PinTLS()
  8. {
  9. m_key = PIN_CreateThreadDataKey(NULL);
  10. }
  11. ~PinTLS()
  12. {
  13. PIN_DeleteThreadDataKey(m_key);
  14. }
  15. void* get(int thread_id)
  16. {
  17. if (thread_id == -1)
  18. return PIN_GetThreadData(m_key, PIN_ThreadId());
  19. else
  20. return PIN_GetThreadData(m_key, thread_id);
  21. }
  22. const void* get(int thread_id) const
  23. {
  24. return ((PinTLS*)this)->get(thread_id);
  25. }
  26. void set(void *vp)
  27. {
  28. LOG_PRINT("%p->set(%p)", this, vp);
  29. __attribute__((unused)) BOOL res = PIN_SetThreadData(m_key, vp, PIN_ThreadId());
  30. LOG_ASSERT_ERROR(res, "Error setting TLS -- pin tid = %d", PIN_ThreadId());
  31. }
  32. private:
  33. TLS_KEY m_key;
  34. };
  35. #if 1
  36. // override PthreadTLS
  37. TLS* TLS::create()
  38. {
  39. return new PinTLS();
  40. }
  41. #endif