1
0

membarrier.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <sys/membarrier.h>
  2. #include <semaphore.h>
  3. #include <signal.h>
  4. #include <string.h>
  5. #include "pthread_impl.h"
  6. #include "syscall.h"
  7. static void dummy_0(void)
  8. {
  9. }
  10. weak_alias(dummy_0, __tl_lock);
  11. weak_alias(dummy_0, __tl_unlock);
  12. static sem_t barrier_sem;
  13. static void bcast_barrier(int s)
  14. {
  15. sem_post(&barrier_sem);
  16. }
  17. int __membarrier(int cmd, int flags)
  18. {
  19. int r = __syscall(SYS_membarrier, cmd, flags);
  20. /* Emulate the private expedited command, which is needed by the
  21. * dynamic linker for installation of dynamic TLS, for older
  22. * kernels that lack the syscall. Unlike the syscall, this only
  23. * synchronizes with threads of the process, not other processes
  24. * sharing the VM, but such sharing is not a supported usage
  25. * anyway. */
  26. if (r && cmd == MEMBARRIER_CMD_PRIVATE_EXPEDITED && !flags) {
  27. pthread_t self=__pthread_self(), td;
  28. sigset_t set;
  29. __block_app_sigs(&set);
  30. __tl_lock();
  31. sem_init(&barrier_sem, 0, 0);
  32. struct sigaction sa = {
  33. .sa_flags = SA_RESTART,
  34. .sa_handler = bcast_barrier
  35. };
  36. memset(&sa.sa_mask, -1, sizeof sa.sa_mask);
  37. if (!__libc_sigaction(SIGSYNCCALL, &sa, 0)) {
  38. for (td=self->next; td!=self; td=td->next)
  39. __syscall(SYS_tkill, td->tid, SIGSYNCCALL);
  40. for (td=self->next; td!=self; td=td->next)
  41. sem_wait(&barrier_sem);
  42. r = 0;
  43. sa.sa_handler = SIG_IGN;
  44. __libc_sigaction(SIGSYNCCALL, &sa, 0);
  45. }
  46. sem_destroy(&barrier_sem);
  47. __tl_unlock();
  48. __restore_sigs(&set);
  49. }
  50. return __syscall_ret(r);
  51. }
  52. void __membarrier_init(void)
  53. {
  54. /* If membarrier is linked, attempt to pre-register to be able to use
  55. * the private expedited command before the process becomes multi-
  56. * threaded, since registering later has bad, potentially unbounded
  57. * latency. This syscall should be essentially free, and it's arguably
  58. * a mistake in the API design that registration was even required.
  59. * For other commands, registration may impose some cost, so it's left
  60. * to the application to do so if desired. Unfortunately this means
  61. * library code initialized after the process becomes multi-threaded
  62. * cannot use these features without accepting registration latency. */
  63. __syscall(SYS_membarrier, MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0);
  64. }
  65. weak_alias(__membarrier, membarrier);