blob: ab48c69218fd178feb96657066808d7d290ce8ec [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <limits.h>
19
20#include <sys/time.h>
21#include <sched.h>
22
23#include <errno.h>
24
25#include <private/utils/futex_synchro.h>
26
27
28// This futex glue code is need on desktop linux, but is already part of bionic.
29#if !defined(HAVE_FUTEX_WRAPPERS)
30
Marco Nelissend95aabc2009-05-22 12:25:56 -070031#include <unistd.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include <sys/syscall.h>
33typedef unsigned int u32;
34#define asmlinkage
35#define __user
36#include <linux/futex.h>
37#include <utils/Atomic.h>
38
39
40int futex (int *uaddr, int op, int val, const struct timespec *timeout, int *uaddr2, int val3)
41{
42 int err = syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3);
43 return err == 0 ? 0 : -errno;
44}
45
46int __futex_wait(volatile void *ftx, int val, const struct timespec *timeout)
47{
48 return futex((int*)ftx, FUTEX_WAIT, val, timeout, NULL, 0);
49}
50
51int __futex_wake(volatile void *ftx, int count)
52{
53 return futex((int*)ftx, FUTEX_WAKE, count, NULL, NULL, 0);
54}
55
56int __atomic_cmpxchg(int old, int _new, volatile int *ptr)
57{
58 return android_atomic_cmpxchg(old, _new, ptr);
59}
60
61int __atomic_swap(int _new, volatile int *ptr)
62{
63 return android_atomic_swap(_new, ptr);
64}
65
66int __atomic_dec(volatile int *ptr)
67{
68 return android_atomic_dec(ptr);
69}
70
71#else // !defined(__arm__)
72
73int __futex_wait(volatile void *ftx, int val, const struct timespec *timeout);
74int __futex_wake(volatile void *ftx, int count);
75
76int __atomic_cmpxchg(int old, int _new, volatile int *ptr);
77int __atomic_swap(int _new, volatile int *ptr);
78int __atomic_dec(volatile int *ptr);
79
80#endif // !defined(HAVE_FUTEX_WRAPPERS)
81
82
83// lock states
84//
85// 0: unlocked
86// 1: locked, no waiters
87// 2: locked, maybe waiters
88
89void futex_mutex_init(futex_mutex_t *m)
90{
91 m->value = 0;
92}
93
94int futex_mutex_lock(futex_mutex_t *m, unsigned msec)
95{
96 if(__atomic_cmpxchg(0, 1, &m->value) == 0) {
97 return 0;
98 }
99 if(msec == FUTEX_WAIT_INFINITE) {
100 while(__atomic_swap(2, &m->value) != 0) {
101 __futex_wait(&m->value, 2, 0);
102 }
103 } else {
104 struct timespec ts;
105 ts.tv_sec = msec / 1000;
106 ts.tv_nsec = (msec % 1000) * 1000000;
107 while(__atomic_swap(2, &m->value) != 0) {
108 if(__futex_wait(&m->value, 2, &ts) == -ETIMEDOUT) {
109 return -1;
110 }
111 }
112 }
113 return 0;
114}
115
116int futex_mutex_trylock(futex_mutex_t *m)
117{
118 if(__atomic_cmpxchg(0, 1, &m->value) == 0) {
119 return 0;
120 }
121 return -1;
122}
123
124void futex_mutex_unlock(futex_mutex_t *m)
125{
126 if(__atomic_dec(&m->value) != 1) {
127 m->value = 0;
128 __futex_wake(&m->value, 1);
129 }
130}
131
132/* XXX *technically* there is a race condition that could allow
133 * XXX a signal to be missed. If thread A is preempted in _wait()
134 * XXX after unlocking the mutex and before waiting, and if other
135 * XXX threads call signal or broadcast UINT_MAX times (exactly),
136 * XXX before thread A is scheduled again and calls futex_wait(),
137 * XXX then the signal will be lost.
138 */
139
140void futex_cond_init(futex_cond_t *c)
141{
142 c->value = 0;
143}
144
145int futex_cond_wait(futex_cond_t *c, futex_mutex_t *m, unsigned msec)
146{
147 if(msec == FUTEX_WAIT_INFINITE){
148 int oldvalue = c->value;
149 futex_mutex_unlock(m);
150 __futex_wait(&c->value, oldvalue, 0);
151 futex_mutex_lock(m, FUTEX_WAIT_INFINITE);
152 return 0;
153 } else {
154 int oldvalue = c->value;
155 struct timespec ts;
156 ts.tv_sec = msec / 1000;
157 ts.tv_nsec = (msec % 1000) * 1000000;
158 futex_mutex_unlock(m);
159 const int err = __futex_wait(&c->value, oldvalue, &ts);
160 futex_mutex_lock(m, FUTEX_WAIT_INFINITE);
161 return err;
162 }
163}
164
165void futex_cond_signal(futex_cond_t *c)
166{
167 __atomic_dec(&c->value);
168 __futex_wake(&c->value, 1);
169}
170
171void futex_cond_broadcast(futex_cond_t *c)
172{
173 __atomic_dec(&c->value);
174 __futex_wake(&c->value, INT_MAX);
175}
176