Make futex compat internal to liblttng-ust
[lttng-ust.git] / src / lib / lttng-ust / futex.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-or-later
3 *
4 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Userspace RCU library - sys_futex compatibility code
7 */
8
9 #include <stdio.h>
10 #include <pthread.h>
11 #include <signal.h>
12 #include <assert.h>
13 #include <errno.h>
14 #include <poll.h>
15 #include <stdint.h>
16
17 #include <urcu/arch.h>
18 #include <urcu/system.h>
19
20 #include "lib/lttng-ust/futex.h"
21
22 /*
23 * This compat header originated in userspace RCU where it's used across
24 * multiple shared objects hence the need to have the mutexes as public weak
25 * symbols, in our case here, it's only used internally by liblttng-ust so we
26 * can hide them. If we end up using this compat header in another library in
27 * this project we will have to use the same scheme, but in the meantime, don't
28 * expose those symbols in the ABI.
29 */
30
31 /*
32 * This comment will apply if we start using this compat header in multiple
33 * libraires.
34 *
35 * Using attribute "weak" for __lttng_ust_compat_futex_lock and
36 * __lttng_ust_compat_futex_cond. Those are globally visible by the entire
37 * program, even though many shared objects may have their own version.
38 * The first version that gets loaded will be used by the entire program
39 * (executable and all shared objects).
40 */
41
42 static pthread_mutex_t __lttng_ust_compat_futex_lock = PTHREAD_MUTEX_INITIALIZER;
43 static pthread_cond_t __lttng_ust_compat_futex_cond = PTHREAD_COND_INITIALIZER;
44
45 /*
46 * _NOT SIGNAL-SAFE_. pthread_cond is not signal-safe anyway. Though.
47 * For now, timeout, uaddr2 and val3 are unused.
48 * Waiter will relinquish the CPU until woken up.
49 */
50
51 int lttng_ust_compat_futex_noasync(int32_t *uaddr, int op, int32_t val,
52 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
53 {
54 int ret = 0, lockret;
55
56 /*
57 * Check if NULL. Don't let users expect that they are taken into
58 * account.
59 */
60 assert(!timeout);
61 assert(!uaddr2);
62 assert(!val3);
63
64 /*
65 * memory barriers to serialize with the previous uaddr modification.
66 */
67 cmm_smp_mb();
68
69 lockret = pthread_mutex_lock(&__lttng_ust_compat_futex_lock);
70 if (lockret) {
71 errno = lockret;
72 ret = -1;
73 goto end;
74 }
75 switch (op) {
76 case FUTEX_WAIT:
77 /*
78 * Wait until *uaddr is changed to something else than "val".
79 * Comparing *uaddr content against val figures out which
80 * thread has been awakened.
81 */
82 while (CMM_LOAD_SHARED(*uaddr) == val)
83 pthread_cond_wait(&__lttng_ust_compat_futex_cond,
84 &__lttng_ust_compat_futex_lock);
85 break;
86 case FUTEX_WAKE:
87 /*
88 * Each wake is sending a broadcast, thus attempting wakeup of
89 * all awaiting threads, independently of their respective
90 * uaddr.
91 */
92 pthread_cond_broadcast(&__lttng_ust_compat_futex_cond);
93 break;
94 default:
95 errno = EINVAL;
96 ret = -1;
97 }
98 lockret = pthread_mutex_unlock(&__lttng_ust_compat_futex_lock);
99 if (lockret) {
100 errno = lockret;
101 ret = -1;
102 }
103 end:
104 return ret;
105 }
106
107 /*
108 * _ASYNC SIGNAL-SAFE_.
109 * For now, timeout, uaddr2 and val3 are unused.
110 * Waiter will busy-loop trying to read the condition.
111 * It is OK to use compat_futex_async() on a futex address on which
112 * futex() WAKE operations are also performed.
113 */
114
115 int lttng_ust_compat_futex_async(int32_t *uaddr, int op, int32_t val,
116 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
117 {
118 int ret = 0;
119
120 /*
121 * Check if NULL. Don't let users expect that they are taken into
122 * account.
123 */
124 assert(!timeout);
125 assert(!uaddr2);
126 assert(!val3);
127
128 /*
129 * Ensure previous memory operations on uaddr have completed.
130 */
131 cmm_smp_mb();
132
133 switch (op) {
134 case FUTEX_WAIT:
135 while (CMM_LOAD_SHARED(*uaddr) == val) {
136 if (poll(NULL, 0, 10) < 0) {
137 ret = -1;
138 /* Keep poll errno. Caller handles EINTR. */
139 goto end;
140 }
141 }
142 break;
143 case FUTEX_WAKE:
144 break;
145 default:
146 errno = EINVAL;
147 ret = -1;
148 }
149 end:
150 return ret;
151 }
This page took 0.031724 seconds and 4 git commands to generate.