src/os/unix/ngx_thread_id.c - nginx source code

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_thread_pool.h>


  8. #if (NGX_LINUX)

  9. /*
  10. * Linux thread id is a pid of thread created by clone(2),
  11. * glibc does not provide a wrapper for gettid().
  12. */

  13. ngx_tid_t
  14. ngx_thread_tid(void)
  15. {
  16.     return syscall(SYS_gettid);
  17. }

  18. #elif (NGX_FREEBSD) && (__FreeBSD_version >= 900031)

  19. #include <pthread_np.h>

  20. ngx_tid_t
  21. ngx_thread_tid(void)
  22. {
  23.     return pthread_getthreadid_np();
  24. }

  25. #elif (NGX_DARWIN)

  26. /*
  27. * MacOSX thread has two thread ids:
  28. *
  29. * 1) MacOSX 10.6 (Snow Leoprad) has pthread_threadid_np() returning
  30. *    an uint64_t value, which is obtained using the __thread_selfid()
  31. *    syscall.  It is a number above 300,000.
  32. */

  33. ngx_tid_t
  34. ngx_thread_tid(void)
  35. {
  36.     uint64_t  tid;

  37.     (void) pthread_threadid_np(NULL, &tid);
  38.     return tid;
  39. }

  40. /*
  41. * 2) Kernel thread mach_port_t returned by pthread_mach_thread_np().
  42. *    It is a number in range 100-100,000.
  43. *
  44. * return pthread_mach_thread_np(pthread_self());
  45. */

  46. #else

  47. ngx_tid_t
  48. ngx_thread_tid(void)
  49. {
  50.     return (uint64_t) (uintptr_t) pthread_self();
  51. }

  52. #endif