summaryrefslogtreecommitdiff
path: root/boost/process/detail/windows/wait_group.hpp
blob: 449985f2fc4a30831ada1a0a45a33df3ded76a02 (plain)
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
// Copyright (c) 2016 Klemens D. Morgenstern
//
// 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)

#ifndef BOOST_PROCESS_DETAIL_WINDOWS_WAIT_GROUP_HPP_
#define BOOST_PROCESS_DETAIL_WINDOWS_WAIT_GROUP_HPP_

#include <boost/process/detail/config.hpp>
#include <boost/detail/winapi/jobs.hpp>
#include <boost/detail/winapi/wait.hpp>


namespace boost { namespace process { namespace detail { namespace windows {

struct group_handle;

inline void wait(const group_handle &p)
{
    if (::boost::detail::winapi::WaitForSingleObject(p.handle(),
        ::boost::detail::winapi::infinite) == ::boost::detail::winapi::wait_failed)
            throw_last_error("WaitForSingleObject() failed");

}

inline void wait(const group_handle &p, std::error_code &ec)
{
    if (::boost::detail::winapi::WaitForSingleObject(p.handle(),
        ::boost::detail::winapi::infinite) == ::boost::detail::winapi::wait_failed)
            ec = get_last_error();
}


template< class Rep, class Period >
inline bool wait_for(
        const group_handle &p,
        const std::chrono::duration<Rep, Period>& rel_time)
{

    std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(rel_time);

    ::boost::detail::winapi::DWORD_ wait_code;
    wait_code = ::boost::detail::winapi::WaitForSingleObject(p.handle(), ms.count());
    if (wait_code == ::boost::detail::winapi::wait_failed)
        throw_last_error("WaitForSingleObject() failed");
    else if (wait_code == ::boost::detail::winapi::wait_timeout)
        return false; //

    return true;
}


template< class Rep, class Period >
inline bool wait_for(
        const group_handle &p,
        const std::chrono::duration<Rep, Period>& rel_time,
        std::error_code &ec)
{

    std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(rel_time);


    ::boost::detail::winapi::DWORD_ wait_code;
    wait_code = ::boost::detail::winapi::WaitForSingleObject(p.handle(), ms.count());
    if (wait_code == ::boost::detail::winapi::wait_failed)
        ec = get_last_error();

    else if (wait_code == ::boost::detail::winapi::wait_timeout)
        return false; //

    return true;
}

template< class Clock, class Duration >
inline bool wait_until(
        const group_handle &p,
        const std::chrono::time_point<Clock, Duration>& timeout_time)
{
    std::chrono::milliseconds ms =
            std::chrono::duration_cast<std::chrono::milliseconds>(
                    timeout_time - std::chrono::system_clock::now());

    ::boost::detail::winapi::DWORD_ wait_code;
    wait_code = ::boost::detail::winapi::WaitForSingleObject(p.handle(), ms.count());

    if (wait_code == ::boost::detail::winapi::wait_failed)
        throw_last_error("WaitForSingleObject() failed");

    else if (wait_code == ::boost::detail::winapi::wait_timeout)
        return false; //

    return true;
}


template< class Clock, class Duration >
inline bool wait_until(
        const group_handle &p,
        const std::chrono::time_point<Clock, Duration>& timeout_time,
        std::error_code &ec)
{
    std::chrono::milliseconds ms =
            std::chrono::duration_cast<std::chrono::milliseconds>(
                    timeout_time - std::chrono::system_clock::now());

    ::boost::detail::winapi::DWORD_ wait_code;
    wait_code = ::boost::detail::winapi::WaitForSingleObject(p.handle(), ms.count());

    if (wait_code == ::boost::detail::winapi::wait_failed)
        ec = get_last_error();

    else if (wait_code == ::boost::detail::winapi::wait_timeout)
        return false; //

    return true;
;
}

}}}}

#endif /* BOOST_PROCESS_DETAIL_WINDOWS_WAIT_GROUP_HPP_ */