Fix: compat_futex.c: *uaddr should be read as volatile
[urcu.git] / compat_futex.c
1 /*
2 * compat_futex.c
3 *
4 * Userspace RCU library - sys_futex compatibility code
5 *
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdio.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <poll.h>
29 #include <stdint.h>
30
31 #include <urcu/arch.h>
32 #include <urcu/futex.h>
33 #include <urcu/system.h>
34
35 /*
36 * Using attribute "weak" for __urcu_compat_futex_lock and
37 * __urcu_compat_futex_cond. Those are globally visible by the entire
38 * program, even though many shared objects may have their own version.
39 * The first version that gets loaded will be used by the entire program
40 * (executable and all shared objects).
41 */
42
43 __attribute__((weak))
44 pthread_mutex_t __urcu_compat_futex_lock = PTHREAD_MUTEX_INITIALIZER;
45 __attribute__((weak))
46 pthread_cond_t __urcu_compat_futex_cond = PTHREAD_COND_INITIALIZER;
47
48 /*
49 * _NOT SIGNAL-SAFE_. pthread_cond is not signal-safe anyway. Though.
50 * For now, timeout, uaddr2 and val3 are unused.
51 * Waiter will relinquish the CPU until woken up.
52 */
53
54 int compat_futex_noasync(int32_t *uaddr, int op, int32_t val,
55 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
56 {
57 int ret, gret = 0;
58
59 /*
60 * Check if NULL. Don't let users expect that they are taken into
61 * account.
62 */
63 assert(!timeout);
64 assert(!uaddr2);
65 assert(!val3);
66
67 /*
68 * memory barriers to serialize with the previous uaddr modification.
69 */
70 cmm_smp_mb();
71
72 ret = pthread_mutex_lock(&__urcu_compat_futex_lock);
73 assert(!ret);
74 switch (op) {
75 case FUTEX_WAIT:
76 /*
77 * Wait until *uaddr is changed to something else than "val".
78 * Comparing *uaddr content against val figures out which
79 * thread has been awakened.
80 */
81 while (CMM_LOAD_SHARED(*uaddr) == val)
82 pthread_cond_wait(&__urcu_compat_futex_cond,
83 &__urcu_compat_futex_lock);
84 break;
85 case FUTEX_WAKE:
86 /*
87 * Each wake is sending a broadcast, thus attempting wakeup of
88 * all awaiting threads, independently of their respective
89 * uaddr.
90 */
91 pthread_cond_broadcast(&__urcu_compat_futex_cond);
92 break;
93 default:
94 gret = -EINVAL;
95 }
96 ret = pthread_mutex_unlock(&__urcu_compat_futex_lock);
97 assert(!ret);
98 return gret;
99 }
100
101 /*
102 * _ASYNC SIGNAL-SAFE_.
103 * For now, timeout, uaddr2 and val3 are unused.
104 * Waiter will busy-loop trying to read the condition.
105 */
106
107 int compat_futex_async(int32_t *uaddr, int op, int32_t val,
108 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
109 {
110 /*
111 * Check if NULL. Don't let users expect that they are taken into
112 * account.
113 */
114 assert(!timeout);
115 assert(!uaddr2);
116 assert(!val3);
117
118 /*
119 * Ensure previous memory operations on uaddr have completed.
120 */
121 cmm_smp_mb();
122
123 switch (op) {
124 case FUTEX_WAIT:
125 while (CMM_LOAD_SHARED(*uaddr) == val)
126 poll(NULL, 0, 10);
127 break;
128 case FUTEX_WAKE:
129 break;
130 default:
131 return -EINVAL;
132 }
133 return 0;
134 }
This page took 0.030694 seconds and 4 git commands to generate.