1 /*
2  * Distributed under the Boost Software License, Version 1.0.
3  * (See accompanying file LICENSE_1_0.txt or copy at
4  * http://www.boost.org/LICENSE_1_0.txt)
5  *
6  * Copyright (c) 2020 Andrey Semashev
7  */
8 /*!
9  * \file   atomic/detail/wait_ops_dragonfly_umtx.hpp
10  *
11  * This header contains implementation of the waiting/notifying atomic operations based on DragonFly BSD umtx.
12  * https://man.dragonflybsd.org/?command=umtx&section=2
13  */
14 
15 #ifndef BOOST_ATOMIC_DETAIL_WAIT_OPS_DRAGONFLY_UMTX_HPP_INCLUDED_
16 #define BOOST_ATOMIC_DETAIL_WAIT_OPS_DRAGONFLY_UMTX_HPP_INCLUDED_
17 
18 #include <unistd.h>
19 #include <boost/memory_order.hpp>
20 #include <boost/atomic/detail/config.hpp>
21 #include <boost/atomic/detail/wait_operations_fwd.hpp>
22 #include <boost/atomic/detail/header.hpp>
23 
24 #ifdef BOOST_HAS_PRAGMA_ONCE
25 #pragma once
26 #endif
27 
28 namespace boost {
29 namespace atomics {
30 namespace detail {
31 
32 template< typename Base, bool Interprocess >
33 struct wait_operations< Base, sizeof(int), true, Interprocess > :
34     public Base
35 {
36     typedef Base base_type;
37     typedef typename base_type::storage_type storage_type;
38 
39     static BOOST_CONSTEXPR_OR_CONST bool always_has_native_wait_notify = true;
40 
has_native_wait_notifyboost::atomics::detail::wait_operations41     static BOOST_FORCEINLINE bool has_native_wait_notify(storage_type const volatile&) BOOST_NOEXCEPT
42     {
43         return true;
44     }
45 
waitboost::atomics::detail::wait_operations46     static BOOST_FORCEINLINE storage_type wait(storage_type const volatile& storage, storage_type old_val, memory_order order) BOOST_NOEXCEPT
47     {
48         storage_type new_val = base_type::load(storage, order);
49         while (new_val == old_val)
50         {
51             ::umtx_sleep(reinterpret_cast< int* >(const_cast< storage_type* >(&storage)), static_cast< int >(old_val), 0);
52             new_val = base_type::load(storage, order);
53         }
54 
55         return new_val;
56     }
57 
notify_oneboost::atomics::detail::wait_operations58     static BOOST_FORCEINLINE void notify_one(storage_type volatile& storage) BOOST_NOEXCEPT
59     {
60         ::umtx_wakeup(reinterpret_cast< int* >(const_cast< storage_type* >(&storage)), 1);
61     }
62 
notify_allboost::atomics::detail::wait_operations63     static BOOST_FORCEINLINE void notify_all(storage_type volatile& storage) BOOST_NOEXCEPT
64     {
65         ::umtx_wakeup(reinterpret_cast< int* >(const_cast< storage_type* >(&storage)), 0);
66     }
67 };
68 
69 } // namespace detail
70 } // namespace atomics
71 } // namespace boost
72 
73 #include <boost/atomic/detail/footer.hpp>
74 
75 #endif // BOOST_ATOMIC_DETAIL_WAIT_OPS_DRAGONFLY_UMTX_HPP_INCLUDED_
76