1 #include "pthread_impl.h"
2 #include <semaphore.h>
3 #include <string.h>
4
dummy_0(void)5 static void dummy_0(void)
6 {
7 }
8
9 weak_alias(dummy_0, __tl_lock);
10 weak_alias(dummy_0, __tl_unlock);
11
12 static int target_tid;
13 static void (*callback)(void *), *context;
14 static sem_t target_sem, caller_sem;
15
dummy(void * p)16 static void dummy(void *p)
17 {
18 }
19
dummy_get_tl_lock_caller_count(void)20 static struct call_tl_lock *dummy_get_tl_lock_caller_count(void)
21 {
22 return NULL;
23 }
24
25 weak_alias(dummy_get_tl_lock_caller_count, get_tl_lock_caller_count);
26
handler(int sig)27 static void handler(int sig)
28 {
29 if (__pthread_self()->tid != target_tid) return;
30
31 int old_errno = errno;
32
33 /* Inform caller we have received signal and wait for
34 * the caller to let us make the callback. */
35 sem_post(&caller_sem);
36 sem_wait(&target_sem);
37
38 callback(context);
39
40 /* Inform caller we've complered the callback and wait
41 * for the caller to release us to return. */
42 sem_post(&caller_sem);
43 sem_wait(&target_sem);
44
45 /* Inform caller we are returning and state is destroyable. */
46 sem_post(&caller_sem);
47
48 errno = old_errno;
49 }
50
__synccall(void (* func)(void *),void * ctx)51 void __synccall(void (*func)(void *), void *ctx)
52 {
53 sigset_t oldmask;
54 int cs, i, r;
55 struct sigaction sa = { .sa_flags = SA_RESTART | SA_ONSTACK, .sa_handler = handler };
56 pthread_t self = __pthread_self(), td;
57 int count = 0;
58
59 /* Blocking signals in two steps, first only app-level signals
60 * before taking the lock, then all signals after taking the lock,
61 * is necessary to achieve AS-safety. Blocking them all first would
62 * deadlock if multiple threads called __synccall. Waiting to block
63 * any until after the lock would allow re-entry in the same thread
64 * with the lock already held. */
65 __block_app_sigs(&oldmask);
66 __tl_lock();
67 if (get_tl_lock_caller_count()) {
68 get_tl_lock_caller_count()->__synccall_tl_lock++;
69 }
70 __block_all_sigs(0);
71 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
72
73 sem_init(&target_sem, 0, 0);
74 sem_init(&caller_sem, 0, 0);
75
76 if (!libc.threads_minus_1 || __syscall(SYS_gettid) != self->tid)
77 goto single_threaded;
78
79 callback = func;
80 context = ctx;
81
82 /* Block even implementation-internal signals, so that nothing
83 * interrupts the SIGSYNCCALL handlers. The main possible source
84 * of trouble is asynchronous cancellation. */
85 memset(&sa.sa_mask, -1, sizeof sa.sa_mask);
86 __libc_sigaction(SIGSYNCCALL, &sa, 0);
87
88
89 for (td=self->next; td!=self; td=td->next) {
90 target_tid = td->tid;
91 while ((r = -__syscall(SYS_tkill, td->tid, SIGSYNCCALL)) == EAGAIN);
92 if (r) {
93 /* If we failed to signal any thread, nop out the
94 * callback to abort the synccall and just release
95 * any threads already caught. */
96 callback = func = dummy;
97 break;
98 }
99 sem_wait(&caller_sem);
100 count++;
101 }
102 target_tid = 0;
103
104 /* Serialize execution of callback in caught threads, or just
105 * release them all if synccall is being aborted. */
106 for (i=0; i<count; i++) {
107 sem_post(&target_sem);
108 sem_wait(&caller_sem);
109 }
110
111 sa.sa_handler = SIG_IGN;
112 __libc_sigaction(SIGSYNCCALL, &sa, 0);
113
114 single_threaded:
115 func(ctx);
116
117 /* Only release the caught threads once all threads, including the
118 * caller, have returned from the callback function. */
119 for (i=0; i<count; i++)
120 sem_post(&target_sem);
121 for (i=0; i<count; i++)
122 sem_wait(&caller_sem);
123
124 sem_destroy(&caller_sem);
125 sem_destroy(&target_sem);
126
127 pthread_setcancelstate(cs, 0);
128 if (get_tl_lock_caller_count()) {
129 get_tl_lock_caller_count()->__synccall_tl_lock--;
130 }
131 __tl_unlock();
132 __restore_sigs(&oldmask);
133 }
134