Fix: i386 compat code duplicated mutex instances
[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
MD
33
34static pthread_mutex_t compat_futex_lock = PTHREAD_MUTEX_INITIALIZER;
35static pthread_cond_t compat_futex_cond = PTHREAD_COND_INITIALIZER;
36
37/*
38 * _NOT SIGNAL-SAFE_. pthread_cond is not signal-safe anyway. Though.
39 * For now, timeout, uaddr2 and val3 are unused.
40 * Waiter will relinquish the CPU until woken up.
41 */
42
6d841bc2
MD
43int compat_futex_noasync(int32_t *uaddr, int op, int32_t val,
44 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
49617de1 45{
5b80127f 46 int ret, gret = 0;
49617de1
MD
47
48 /*
49 * Check if NULL. Don't let users expect that they are taken into
50 * account.
51 */
52 assert(!timeout);
53 assert(!uaddr2);
54 assert(!val3);
55
56 /*
57 * memory barriers to serialize with the previous uaddr modification.
58 */
5481ddb3 59 cmm_smp_mb();
49617de1
MD
60
61 ret = pthread_mutex_lock(&compat_futex_lock);
62 assert(!ret);
63 switch (op) {
64 case FUTEX_WAIT:
65 if (*uaddr != val)
66 goto end;
67 pthread_cond_wait(&compat_futex_cond, &compat_futex_lock);
68 break;
69 case FUTEX_WAKE:
5b80127f 70 pthread_cond_broadcast(&compat_futex_cond);
49617de1
MD
71 break;
72 default:
73 gret = -EINVAL;
74 }
75end:
76 ret = pthread_mutex_unlock(&compat_futex_lock);
77 assert(!ret);
78 return gret;
79}
80
81/*
82 * _ASYNC SIGNAL-SAFE_.
83 * For now, timeout, uaddr2 and val3 are unused.
84 * Waiter will busy-loop trying to read the condition.
85 */
86
6d841bc2
MD
87int compat_futex_async(int32_t *uaddr, int op, int32_t val,
88 const struct timespec *timeout, int32_t *uaddr2, int32_t val3)
49617de1 89{
49617de1
MD
90 /*
91 * Check if NULL. Don't let users expect that they are taken into
92 * account.
93 */
94 assert(!timeout);
95 assert(!uaddr2);
96 assert(!val3);
97
98 /*
99 * Ensure previous memory operations on uaddr have completed.
100 */
5481ddb3 101 cmm_smp_mb();
49617de1
MD
102
103 switch (op) {
104 case FUTEX_WAIT:
105 while (*uaddr == val)
106 poll(NULL, 0, 10);
107 break;
108 case FUTEX_WAKE:
109 break;
110 default:
111 return -EINVAL;
112 }
1494da88 113 return 0;
49617de1 114}
This page took 0.041069 seconds and 4 git commands to generate.