pthread_self.c 876 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "pthread_impl.h"
  2. static struct pthread main_thread;
  3. /* pthread_key_create.c overrides this */
  4. static const void *dummy[1] = { 0 };
  5. weak_alias(dummy, __pthread_tsd_main);
  6. static int *errno_location()
  7. {
  8. return __pthread_self()->errno_ptr;
  9. }
  10. static int init_main_thread()
  11. {
  12. if (__set_thread_area(&main_thread) < 0) return -1;
  13. main_thread.canceldisable = libc.canceldisable;
  14. main_thread.tsd = (void **)__pthread_tsd_main;
  15. main_thread.self = libc.main_thread = &main_thread;
  16. main_thread.errno_ptr = __errno_location();
  17. libc.errno_location = errno_location;
  18. main_thread.tid = main_thread.pid =
  19. __syscall(SYS_set_tid_address, &main_thread.tid);
  20. return 0;
  21. }
  22. pthread_t pthread_self()
  23. {
  24. static int init, failed;
  25. if (!init) {
  26. if (failed) return 0;
  27. if (init_main_thread() < 0) failed = 1;
  28. if (failed) return 0;
  29. init = 1;
  30. }
  31. return __pthread_self();
  32. }