4 * Userspace RCU library - sys_futex compatibility code
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
31 #include <urcu/arch.h>
32 #include <urcu/futex.h>
35 * Using attribute "weak" for __urcu_compat_futex_lock and
36 * __urcu_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).
43 pthread_mutex_t __urcu_compat_futex_lock
= PTHREAD_MUTEX_INITIALIZER
;
45 pthread_cond_t __urcu_compat_futex_cond
= PTHREAD_COND_INITIALIZER
;
48 * _NOT SIGNAL-SAFE_. pthread_cond is not signal-safe anyway. Though.
49 * For now, timeout, uaddr2 and val3 are unused.
50 * Waiter will relinquish the CPU until woken up.
53 int compat_futex_noasync(int32_t *uaddr
, int op
, int32_t val
,
54 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
59 * Check if NULL. Don't let users expect that they are taken into
67 * memory barriers to serialize with the previous uaddr modification.
71 ret
= pthread_mutex_lock(&__urcu_compat_futex_lock
);
76 * Wait until *uaddr is changed to something else than "val".
77 * Comparing *uaddr content against val figures out which
78 * thread has been awakened.
81 pthread_cond_wait(&__urcu_compat_futex_cond
,
82 &__urcu_compat_futex_lock
);
86 * Each wake is sending a broadcast, thus attempting wakeup of
87 * all awaiting threads, independently of their respective
90 pthread_cond_broadcast(&__urcu_compat_futex_cond
);
95 ret
= pthread_mutex_unlock(&__urcu_compat_futex_lock
);
101 * _ASYNC SIGNAL-SAFE_.
102 * For now, timeout, uaddr2 and val3 are unused.
103 * Waiter will busy-loop trying to read the condition.
106 int compat_futex_async(int32_t *uaddr
, int op
, int32_t val
,
107 const struct timespec
*timeout
, int32_t *uaddr2
, int32_t val3
)
110 * Check if NULL. Don't let users expect that they are taken into
118 * Ensure previous memory operations on uaddr have completed.
124 while (*uaddr
== val
)