thread.i 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //do not use directly
  2. #pragma once
  3. #include "platform.h"
  4. #if defined(_win_)
  5. #include "winint.h"
  6. #include <process.h>
  7. typedef HANDLE THREADHANDLE;
  8. #else
  9. #include <pthread.h>
  10. #include <sched.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. typedef pthread_t THREADHANDLE;
  14. #endif
  15. #if defined(_freebsd_)
  16. #include <pthread_np.h>
  17. #elif defined(_linux_)
  18. #include <sys/prctl.h>
  19. #endif
  20. #include <util/digest/numeric.h>
  21. static inline size_t SystemCurrentThreadIdImpl() noexcept {
  22. #if defined(_unix_)
  23. return (size_t)pthread_self();
  24. #elif defined(_win_)
  25. return (size_t)GetCurrentThreadId();
  26. #else
  27. #error todo
  28. #endif
  29. }
  30. template <class T>
  31. static inline T ThreadIdHashFunction(T t) noexcept {
  32. /*
  33. * we must permute threadid bits, because some strange platforms(such Linux)
  34. * have strange threadid numeric properties
  35. *
  36. * Because they are alligned pointers to pthread_t rather that tid.
  37. * Currently there is no way to get tid without syscall (slightly slower)
  38. * (pthread_getthreadid_np is not implemeted in glibc/musl for some reason).
  39. */
  40. return IntHash(t);
  41. }
  42. static inline size_t SystemCurrentThreadId() noexcept {
  43. return ThreadIdHashFunction(SystemCurrentThreadIdImpl());
  44. }