16304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner/*
26304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * Copyright (C) 2008 The Android Open Source Project
36304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * All rights reserved.
46304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner *
56304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * Redistribution and use in source and binary forms, with or without
66304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * modification, are permitted provided that the following conditions
76304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * are met:
86304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner *  * Redistributions of source code must retain the above copyright
96304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner *    notice, this list of conditions and the following disclaimer.
106304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner *  * Redistributions in binary form must reproduce the above copyright
116304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner *    notice, this list of conditions and the following disclaimer in
126304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner *    the documentation and/or other materials provided with the
136304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner *    distribution.
146304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner *
156304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
176304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
186304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
196304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
206304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
216304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
226304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
236304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
246304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
256304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner * SUCH DAMAGE.
276304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner */
286304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner#ifndef _BIONIC_FUTEX_H
296304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner#define _BIONIC_FUTEX_H
306304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner
31b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes#include <errno.h>
326304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner#include <linux/futex.h>
33d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughes#include <stdbool.h>
34d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughes#include <stddef.h>
35b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes#include <sys/cdefs.h>
36b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes#include <sys/syscall.h>
376304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner
383e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes__BEGIN_DECLS
393e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
40d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughesstruct timespec;
416304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner
42b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughesstatic inline __always_inline int __futex(volatile void* ftx, int op, int value, const struct timespec* timeout) {
43b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  // Our generated syscall assembler sets errno, but our callers (pthread functions) don't want to.
44b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  int saved_errno = errno;
451ee46520caa1a2a47c69d58f49f4042194ec0339Elliott Hughes  int result = syscall(__NR_futex, ftx, op, value, timeout);
461ee46520caa1a2a47c69d58f49f4042194ec0339Elliott Hughes  if (__predict_false(result == -1)) {
471ee46520caa1a2a47c69d58f49f4042194ec0339Elliott Hughes    result = -errno;
48b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes    errno = saved_errno;
49b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  }
501ee46520caa1a2a47c69d58f49f4042194ec0339Elliott Hughes  return result;
51b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes}
526304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner
53d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughesstatic inline int __futex_wake(volatile void* ftx, int count) {
54b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  return __futex(ftx, FUTEX_WAKE, count, NULL);
55d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughes}
56d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughes
57d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughesstatic inline int __futex_wake_ex(volatile void* ftx, bool shared, int count) {
58b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  return __futex(ftx, shared ? FUTEX_WAKE : FUTEX_WAKE_PRIVATE, count, NULL);
59d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughes}
60d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughes
61d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughesstatic inline int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
62b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  return __futex(ftx, FUTEX_WAIT, value, timeout);
63d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughes}
64d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughes
65d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughesstatic inline int __futex_wait_ex(volatile void* ftx, bool shared, int value, const struct timespec* timeout) {
66b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  return __futex(ftx, shared ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE, value, timeout);
67d5ed63a6a8290de88802172ce178656fbafe70c6Elliott Hughes}
686304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner
693e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes__END_DECLS
703e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes
716304d8b21891fd0cb7b5a4c25159a3d3b1709d62David 'Digit' Turner#endif /* _BIONIC_FUTEX_H */
72