liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// 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
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//
 
#ifndef BOOST_COMPUTE_ASYNC_FUTURE_HPP
#define BOOST_COMPUTE_ASYNC_FUTURE_HPP
 
#include <boost/compute/event.hpp>
 
namespace boost {
namespace compute {
 
/// \class future
/// \brief Holds the result of an asynchronous computation.
///
/// \see event, wait_list
template<class T>
class future
{
public:
    future()
        : m_event(0)
    {
    }
 
    future(const T &result, const event &event)
        : m_result(result),
          m_event(event)
    {
    }
 
    future(const future<T> &other)
        : m_result(other.m_result),
          m_event(other.m_event)
    {
    }
 
    future& operator=(const future<T> &other)
    {
        if(this != &other){
            m_result = other.m_result;
            m_event = other.m_event;
        }
 
        return *this;
    }
 
    ~future()
    {
    }
 
    /// Returns the result of the computation. This will block until
    /// the result is ready.
    T get()
    {
        wait();
 
        return m_result;
    }
 
    /// Returns \c true if the future is valid.
    bool valid() const
    {
        return m_event != 0;
    }
 
    /// Blocks until the computation is complete.
    void wait() const
    {
        m_event.wait();
    }
 
    /// Returns the underlying event object.
    event get_event() const
    {
        return m_event;
    }
 
    #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
    /// Invokes a generic callback function once the future is ready.
    ///
    /// The function specified by callback must be invokable with zero arguments.
    ///
    /// \see_opencl_ref{clSetEventCallback}
    /// \opencl_version_warning{1,1}
    template<class Function>
    future& then(Function callback)
    {
        m_event.set_callback(callback);
        return *this;
    }
    #endif // BOOST_COMPUTE_CL_VERSION_1_1
 
private:
    T m_result;
    event m_event;
};
 
/// \internal_
template<>
class future<void>
{
public:
    future()
        : m_event(0)
    {
    }
 
    template<class T>
    future(const future<T> &other)
        : m_event(other.get_event())
    {
    }
 
    explicit future(const event &event)
        : m_event(event)
    {
    }
 
    template<class T>
    future<void> &operator=(const future<T> &other)
    {
        m_event = other.get_event();
 
        return *this;
    }
 
    future<void> &operator=(const future<void> &other)
    {
        if(this != &other){
            m_event = other.m_event;
        }
 
        return *this;
    }
 
    ~future()
    {
    }
 
    void get()
    {
        wait();
    }
 
    bool valid() const
    {
        return m_event != 0;
    }
 
    void wait() const
    {
        m_event.wait();
    }
 
    event get_event() const
    {
        return m_event;
    }
 
    #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
    /// Invokes a generic callback function once the future is ready.
    ///
    /// The function specified by callback must be invokable with zero arguments.
    ///
    /// \see_opencl_ref{clSetEventCallback}
    /// \opencl_version_warning{1,1}
    template<class Function>
    future<void> &then(Function callback)
    {
        m_event.set_callback(callback);
        return *this;
    }
    #endif // BOOST_COMPUTE_CL_VERSION_1_1
 
private:
    event m_event;
};
 
/// \internal_
template<class Result>
inline future<Result> make_future(const Result &result, const event &event)
{
    return future<Result>(result, event);
}
 
} // end compute namespace
} // end boost namespace
 
#endif // BOOST_COMPUTE_ASYNC_FUTURE_HPP