pthread_tls.cc 609 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "tls.h"
  2. #include <pthread.h>
  3. // Pthread
  4. class PthreadTLS : public TLS
  5. {
  6. public:
  7. PthreadTLS()
  8. {
  9. pthread_key_create(&m_key, NULL);
  10. }
  11. ~PthreadTLS()
  12. {
  13. pthread_key_delete(m_key);
  14. }
  15. void* get(int thread_id = -1)
  16. {
  17. return pthread_getspecific(m_key);
  18. }
  19. const void* get(int thread_id = -1) const
  20. {
  21. return pthread_getspecific(m_key);
  22. }
  23. void set(void *vp)
  24. {
  25. pthread_setspecific(m_key, vp);
  26. }
  27. private:
  28. pthread_key_t m_key;
  29. };
  30. __attribute__((weak)) TLS* TLS::create()
  31. {
  32. return new PthreadTLS();
  33. }