thread.i 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. typedef pthread_t THREADHANDLE;
  11. #endif
  12. #if defined(_freebsd_)
  13. #include <pthread_np.h>
  14. #endif
  15. #include <util/digest/numeric.h>
  16. static inline size_t SystemCurrentThreadIdImpl() noexcept {
  17. #if defined(_unix_)
  18. return (size_t)pthread_self();
  19. #elif defined(_win_)
  20. return (size_t)GetCurrentThreadId();
  21. #else
  22. #error todo
  23. #endif
  24. }
  25. template <class T>
  26. static inline T ThreadIdHashFunction(T t) noexcept {
  27. /*
  28. * we must permute threadid bits, because some strange platforms(such Linux)
  29. * have strange threadid numeric properties
  30. *
  31. * Because they are alligned pointers to pthread_t rather that tid.
  32. * Currently there is no way to get tid without syscall (slightly slower)
  33. * (pthread_getthreadid_np is not implemeted in glibc/musl for some reason).
  34. */
  35. return IntHash(t);
  36. }
  37. static inline size_t SystemCurrentThreadId() noexcept {
  38. return ThreadIdHashFunction(SystemCurrentThreadIdImpl());
  39. }