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