Library major version number (soname) increment to 3
[urcu.git] / compat_futex.c
CommitLineData
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>
49617de1 33
5c02e37d
MD
34/*
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).
40 */
41
42__attribute__((weak))
43pthread_mutex_t __urcu_compat_futex_lock = PTHREAD_MUTEX_INITIALIZER;
44__attribute__((weak))
45pthread_cond_t __urcu_compat_futex_cond = PTHREAD_COND_INITIALIZER;
49617de1
MD
46
47/*
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.
51 */
52
6d841bc2
MD
53int compat_futex_noasync(int32_t *uaddr, int op, int32_t val,
54 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
49617de1 55{
5b80127f 56 int ret, gret = 0;
49617de1
MD
57
58 /*
59 * Check if NULL. Don't let users expect that they are taken into
60 * account.
61 */
62 assert(!timeout);
63 assert(!uaddr2);
64 assert(!val3);
65
66 /*
67 * memory barriers to serialize with the previous uaddr modification.
68 */
5481ddb3 69 cmm_smp_mb();
49617de1 70
5c02e37d 71 ret = pthread_mutex_lock(&__urcu_compat_futex_lock);
49617de1
MD
72 assert(!ret);
73 switch (op) {
74 case FUTEX_WAIT:
75 if (*uaddr != val)
76 goto end;
5c02e37d 77 pthread_cond_wait(&__urcu_compat_futex_cond, &__urcu_compat_futex_lock);
49617de1
MD
78 break;
79 case FUTEX_WAKE:
5c02e37d 80 pthread_cond_broadcast(&__urcu_compat_futex_cond);
49617de1
MD
81 break;
82 default:
83 gret = -EINVAL;
84 }
85end:
5c02e37d 86 ret = pthread_mutex_unlock(&__urcu_compat_futex_lock);
49617de1
MD
87 assert(!ret);
88 return gret;
89}
90
91/*
92 * _ASYNC SIGNAL-SAFE_.
93 * For now, timeout, uaddr2 and val3 are unused.
94 * Waiter will busy-loop trying to read the condition.
95 */
96
6d841bc2
MD
97int compat_futex_async(int32_t *uaddr, int op, int32_t val,
98 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
49617de1 99{
49617de1
MD
100 /*
101 * Check if NULL. Don't let users expect that they are taken into
102 * account.
103 */
104 assert(!timeout);
105 assert(!uaddr2);
106 assert(!val3);
107
108 /*
109 * Ensure previous memory operations on uaddr have completed.
110 */
5481ddb3 111 cmm_smp_mb();
49617de1
MD
112
113 switch (op) {
114 case FUTEX_WAIT:
115 while (*uaddr == val)
116 poll(NULL, 0, 10);
117 break;
118 case FUTEX_WAKE:
119 break;
120 default:
121 return -EINVAL;
122 }
1494da88 123 return 0;
49617de1 124}
This page took 0.030359 seconds and 4 git commands to generate.