pthread_self.c 1.1 KB

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