synccall.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "pthread_impl.h"
  2. #include <semaphore.h>
  3. #include <string.h>
  4. static struct chain {
  5. struct chain *next;
  6. sem_t sem, sem2;
  7. } *head, *cur;
  8. static void (*callback)(void *), *context;
  9. static int chainlen;
  10. static sem_t chainlock, chaindone;
  11. static void handler(int sig, siginfo_t *si, void *ctx)
  12. {
  13. struct chain ch;
  14. pthread_t self = __pthread_self();
  15. int old_errno = errno;
  16. if (chainlen == libc.threads_minus_1) return;
  17. sigqueue(self->pid, SIGSYNCCALL, (union sigval){0});
  18. /* Threads which have already decremented themselves from the
  19. * thread count must not act. Block further receipt of signals
  20. * and return. */
  21. if (self->dead) {
  22. memset(&((ucontext_t *)ctx)->uc_sigmask, -1, 8);
  23. errno = old_errno;
  24. return;
  25. }
  26. sem_init(&ch.sem, 0, 0);
  27. sem_init(&ch.sem2, 0, 0);
  28. while (sem_wait(&chainlock));
  29. ch.next = head;
  30. head = &ch;
  31. if (++chainlen == libc.threads_minus_1) sem_post(&chaindone);
  32. sem_post(&chainlock);
  33. while (sem_wait(&ch.sem));
  34. callback(context);
  35. sem_post(&ch.sem2);
  36. while (sem_wait(&ch.sem));
  37. errno = old_errno;
  38. }
  39. void __synccall(void (*func)(void *), void *ctx)
  40. {
  41. pthread_t self;
  42. struct sigaction sa;
  43. struct chain *next;
  44. uint64_t oldmask;
  45. if (!libc.threads_minus_1) {
  46. func(ctx);
  47. return;
  48. }
  49. __inhibit_ptc();
  50. __syscall(SYS_rt_sigprocmask, SIG_BLOCK, SIGALL_SET,
  51. &oldmask, __SYSCALL_SSLEN);
  52. sem_init(&chaindone, 0, 0);
  53. sem_init(&chainlock, 0, 1);
  54. chainlen = 0;
  55. callback = func;
  56. context = ctx;
  57. sa.sa_flags = SA_SIGINFO | SA_RESTART;
  58. sa.sa_sigaction = handler;
  59. sigfillset(&sa.sa_mask);
  60. __libc_sigaction(SIGSYNCCALL, &sa, 0);
  61. self = __pthread_self();
  62. sigqueue(self->pid, SIGSYNCCALL, (union sigval){0});
  63. while (sem_wait(&chaindone));
  64. for (cur=head; cur; cur=cur->next) {
  65. sem_post(&cur->sem);
  66. while (sem_wait(&cur->sem2));
  67. }
  68. func(ctx);
  69. for (cur=head; cur; cur=next) {
  70. next = cur->next;
  71. sem_post(&cur->sem);
  72. }
  73. sa.sa_flags = 0;
  74. sa.sa_handler = SIG_IGN;
  75. __libc_sigaction(SIGSYNCCALL, &sa, 0);
  76. __syscall(SYS_rt_sigprocmask, SIG_SETMASK,
  77. &oldmask, 0, __SYSCALL_SSLEN);
  78. __release_ptc();
  79. }