1
2// Copyright Oliver Kowalke 2016.
3// Distributed under the Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#ifndef BOOST_FIBERS_DETAIL_FUTEX_H
8#define BOOST_FIBERS_DETAIL_FUTEX_H
9
10#include <boost/config.hpp>
11#include <boost/predef.h>
12
13#include <boost/fiber/detail/config.hpp>
14
15#ifndef SYS_futex
16#define SYS_futex SYS_futex_time64
17#endif
18
19#if BOOST_OS_LINUX
20extern "C" {
21#include <linux/futex.h>
22#include <sys/syscall.h>
23}
24#elif BOOST_OS_WINDOWS
25#include <windows.h>
26#endif
27
28namespace boost {
29namespace fibers {
30namespace detail {
31
32#if BOOST_OS_LINUX
33BOOST_FORCEINLINE
34int sys_futex( void * addr, std::int32_t op, std::int32_t x) {
35 return ::syscall( SYS_futex, addr, op, x, nullptr, nullptr, 0);
36}
37
38BOOST_FORCEINLINE
39int futex_wake( std::atomic< std::int32_t > * addr) {
40 return 0 <= sys_futex( addr: static_cast< void * >( addr), FUTEX_WAKE_PRIVATE, x: 1) ? 0 : -1;
41}
42
43BOOST_FORCEINLINE
44int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) {
45 return 0 <= sys_futex( addr: static_cast< void * >( addr), FUTEX_WAIT_PRIVATE, x) ? 0 : -1;
46}
47#elif BOOST_OS_WINDOWS
48BOOST_FORCEINLINE
49int futex_wake( std::atomic< std::int32_t > * addr) {
50 ::WakeByAddressSingle( static_cast< void * >( addr) );
51 return 0;
52}
53
54BOOST_FORCEINLINE
55int futex_wait( std::atomic< std::int32_t > * addr, std::int32_t x) {
56 ::WaitOnAddress( static_cast< volatile void * >( addr), & x, sizeof( x), INFINITE);
57 return 0;
58}
59#else
60# warn "no futex support on this platform"
61#endif
62
63}}}
64
65#endif // BOOST_FIBERS_DETAIL_FUTEX_H
66

source code of boost/libs/fiber/include/boost/fiber/detail/futex.hpp