1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* (C) Copyright 2013 Tim Blechmann
* (C) Copyright 2013 Andrey Semashev
*/
/*!
* \file futex.hpp
*
* \brief This header is the Boost.Sync library implementation, see the library documentation
* at http://www.boost.org/doc/libs/release/libs/sync/doc/html/index.html.
*
* This header contains Linux futex API declarations.
*/
#ifndef BOOST_SYNC_DETAIL_FUTEX_HPP_INCLUDED_
#define BOOST_SYNC_DETAIL_FUTEX_HPP_INCLUDED_
#include <stddef.h>
#include <limits.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/syscall.h>
#include <linux/futex.h>
#include <boost/sync/detail/config.hpp>
#include <boost/sync/detail/header.hpp>
#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif
// Some Android NDKs (Google NDK and older Crystax.NET NDK versions) don't define SYS_futex
#if defined(SYS_futex)
#define BOOST_SYNC_DETAIL_SYS_FUTEX SYS_futex
#else
#define BOOST_SYNC_DETAIL_SYS_FUTEX __NR_futex
#endif
namespace boost {
namespace sync {
namespace detail {
namespace linux_ {
//! Invokes an operation on the futex
BOOST_FORCEINLINE int futex_invoke(int* addr1, int op, int val1, const struct ::timespec* timeout = NULL, int* addr2 = NULL, int val3 = 0) BOOST_NOEXCEPT
{
return ::syscall(BOOST_SYNC_DETAIL_SYS_FUTEX, addr1, op, val1, timeout, addr2, val3);
}
//! Checks that the value \c pval is \c expected and blocks
BOOST_FORCEINLINE int futex_wait(int* pval, int expected) BOOST_NOEXCEPT
{
return futex_invoke
(
pval,
#ifdef FUTEX_WAIT_PRIVATE
FUTEX_WAIT_PRIVATE,
#else
FUTEX_WAIT,
#endif
expected
);
}
//! Checks that the value \c pval is \c expected and blocks until \c timeout_nsec expires
BOOST_FORCEINLINE int futex_timedwait(int* pval, int expected, uint64_t timeout_nsec) BOOST_NOEXCEPT
{
struct ::timespec timeout;
timeout.tv_sec = timeout_nsec / 1000000000u;
timeout.tv_nsec = timeout_nsec % 1000000000u;
return futex_invoke
(
pval,
#ifdef FUTEX_WAIT_PRIVATE
FUTEX_WAIT_PRIVATE,
#else
FUTEX_WAIT,
#endif
expected,
&timeout
);
}
//! Wakes all threads waiting on the futex
BOOST_FORCEINLINE int futex_broadcast(int* pval) BOOST_NOEXCEPT
{
return futex_invoke
(
pval,
#ifdef FUTEX_WAKE_PRIVATE
FUTEX_WAKE_PRIVATE,
#else
FUTEX_WAKE,
#endif
INT_MAX
);
}
//! Wakes the specified number of threads waiting on the futex
BOOST_FORCEINLINE int futex_signal(int* pval, int count = 1) BOOST_NOEXCEPT
{
return futex_invoke
(
pval,
#ifdef FUTEX_WAKE_PRIVATE
FUTEX_WAKE_PRIVATE,
#else
FUTEX_WAKE,
#endif
count
);
}
} // namespace linux_
} // namespace detail
} // namespace sync
} // namespace boost
#include <boost/sync/detail/footer.hpp>
#endif // BOOST_SYNC_DETAIL_FUTEX_HPP_INCLUDED_
|