Commit | Line | Data |
---|---|---|
49617de1 MD |
1 | /* |
2 | * compat_futex.c | |
3 | * | |
4 | * Userspace RCU library - sys_futex compatibility code | |
5 | * | |
6982d6d7 | 6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
49617de1 MD |
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> | |
6d841bc2 | 29 | #include <stdint.h> |
49617de1 MD |
30 | |
31 | #include <urcu/arch.h> | |
41849996 | 32 | #include <urcu/futex.h> |
f4fe9309 | 33 | #include <urcu/system.h> |
49617de1 | 34 | |
5c02e37d MD |
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; | |
49617de1 MD |
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 | ||
6d841bc2 MD |
54 | int compat_futex_noasync(int32_t *uaddr, int op, int32_t val, |
55 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
49617de1 | 56 | { |
54fd78ef | 57 | int ret = 0, lockret; |
49617de1 MD |
58 | |
59 | /* | |
60 | * Check if NULL. Don't let users expect that they are taken into | |
67ecffc0 | 61 | * account. |
49617de1 MD |
62 | */ |
63 | assert(!timeout); | |
64 | assert(!uaddr2); | |
65 | assert(!val3); | |
66 | ||
67 | /* | |
68 | * memory barriers to serialize with the previous uaddr modification. | |
69 | */ | |
5481ddb3 | 70 | cmm_smp_mb(); |
49617de1 | 71 | |
c8114d9b MD |
72 | lockret = pthread_mutex_lock(&__urcu_compat_futex_lock); |
73 | if (lockret) { | |
74 | errno = lockret; | |
b0a841b4 MD |
75 | ret = -1; |
76 | goto end; | |
77 | } | |
49617de1 MD |
78 | switch (op) { |
79 | case FUTEX_WAIT: | |
db21eff9 MD |
80 | /* |
81 | * Wait until *uaddr is changed to something else than "val". | |
82 | * Comparing *uaddr content against val figures out which | |
83 | * thread has been awakened. | |
84 | */ | |
f4fe9309 | 85 | while (CMM_LOAD_SHARED(*uaddr) == val) |
db21eff9 MD |
86 | pthread_cond_wait(&__urcu_compat_futex_cond, |
87 | &__urcu_compat_futex_lock); | |
49617de1 MD |
88 | break; |
89 | case FUTEX_WAKE: | |
db21eff9 MD |
90 | /* |
91 | * Each wake is sending a broadcast, thus attempting wakeup of | |
92 | * all awaiting threads, independently of their respective | |
93 | * uaddr. | |
94 | */ | |
5c02e37d | 95 | pthread_cond_broadcast(&__urcu_compat_futex_cond); |
49617de1 MD |
96 | break; |
97 | default: | |
b0a841b4 MD |
98 | errno = EINVAL; |
99 | ret = -1; | |
49617de1 | 100 | } |
c8114d9b MD |
101 | lockret = pthread_mutex_unlock(&__urcu_compat_futex_lock); |
102 | if (lockret) { | |
103 | errno = lockret; | |
b0a841b4 MD |
104 | ret = -1; |
105 | } | |
106 | end: | |
107 | return ret; | |
49617de1 MD |
108 | } |
109 | ||
110 | /* | |
111 | * _ASYNC SIGNAL-SAFE_. | |
112 | * For now, timeout, uaddr2 and val3 are unused. | |
113 | * Waiter will busy-loop trying to read the condition. | |
4f34ad79 MD |
114 | * It is OK to use compat_futex_async() on a futex address on which |
115 | * futex() WAKE operations are also performed. | |
49617de1 MD |
116 | */ |
117 | ||
6d841bc2 MD |
118 | int compat_futex_async(int32_t *uaddr, int op, int32_t val, |
119 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) | |
49617de1 | 120 | { |
b0a841b4 MD |
121 | int ret = 0; |
122 | ||
49617de1 MD |
123 | /* |
124 | * Check if NULL. Don't let users expect that they are taken into | |
67ecffc0 | 125 | * account. |
49617de1 MD |
126 | */ |
127 | assert(!timeout); | |
128 | assert(!uaddr2); | |
129 | assert(!val3); | |
130 | ||
131 | /* | |
132 | * Ensure previous memory operations on uaddr have completed. | |
133 | */ | |
5481ddb3 | 134 | cmm_smp_mb(); |
49617de1 MD |
135 | |
136 | switch (op) { | |
137 | case FUTEX_WAIT: | |
b0a841b4 MD |
138 | while (CMM_LOAD_SHARED(*uaddr) == val) { |
139 | if (poll(NULL, 0, 10) < 0) { | |
140 | ret = -1; | |
141 | /* Keep poll errno. Caller handles EINTR. */ | |
142 | goto end; | |
143 | } | |
144 | } | |
49617de1 MD |
145 | break; |
146 | case FUTEX_WAKE: | |
147 | break; | |
148 | default: | |
b0a841b4 MD |
149 | errno = EINVAL; |
150 | ret = -1; | |
49617de1 | 151 | } |
b0a841b4 MD |
152 | end: |
153 | return ret; | |
49617de1 | 154 | } |