summaryrefslogtreecommitdiff
path: root/boost/thread/experimental/parallel/v2/task_region.hpp
blob: e4a5833115fd685cce61775e32cbd03acda9b257 (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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#ifndef BOOST_THREAD_EXPERIMENTAL_PARALLEL_V2_TASK_REGION_HPP
#define BOOST_THREAD_EXPERIMENTAL_PARALLEL_V2_TASK_REGION_HPP

//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Vicente J. Botet Escriba 2014-2015. 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://www.boost.org/libs/thread for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/thread/detail/config.hpp>

#include <boost/thread/future.hpp>
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
#include <boost/thread/executors/basic_thread_pool.hpp>
#endif
#include <boost/thread/experimental/exception_list.hpp>
#include <boost/thread/experimental/parallel/v2/inline_namespace.hpp>
#include <boost/thread/detail/move.hpp>

#include <boost/config/abi_prefix.hpp>

#define BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED

namespace boost
{
namespace experimental
{
namespace parallel
{
BOOST_THREAD_INLINE_NAMESPACE(v2)
{
  class BOOST_SYMBOL_VISIBLE task_canceled_exception: public std::exception
  {
  public:
    //task_canceled_exception() BOOST_NOEXCEPT {}
    //task_canceled_exception(const task_canceled_exception&) BOOST_NOEXCEPT {}
    //task_canceled_exception& operator=(const task_canceled_exception&) BOOST_NOEXCEPT {}
    virtual const char* what() const BOOST_NOEXCEPT_OR_NOTHROW
    { return "task_canceled_exception";}
  };

  template <class Executor>
  class task_region_handle_gen;

  namespace detail
  {
    void handle_task_region_exceptions(exception_list& errors)
    {
      try {
        throw;
      }
#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
      catch (task_canceled_exception&)
      {
      }
#endif
      catch (exception_list const& el)
      {
        for (exception_list::const_iterator it = el.begin(); it != el.end(); ++it)
        {
          boost::exception_ptr const& e = *it;
          try {
            rethrow_exception(e);
          }
          catch (...)
          {
            handle_task_region_exceptions(errors);
          }
        }
      }
      catch (...)
      {
        errors.add(boost::current_exception());
      }
    }

#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
    template <class TRH, class F>
    struct wrapped
    {
      TRH& tr;
      F f;
      wrapped(TRH& tr, BOOST_THREAD_RV_REF(F) f) : tr(tr), f(move(f))
      {}
      void operator()()
      {
        try
        {
          f();
        }
        catch (...)
        {
          lock_guard<mutex> lk(tr.mtx);
          tr.canceled = true;
          throw;
        }
      }
    };
#endif
  }

  template <class Executor>
  class task_region_handle_gen
  {
  private:
    // Private members and friends
#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
    template <class TRH, class F>
    friend struct detail::wrapped;
#endif
    template <typename F>
    friend void task_region(BOOST_THREAD_FWD_REF(F) f);
    template<typename F>
    friend void task_region_final(BOOST_THREAD_FWD_REF(F) f);
    template <class Ex, typename F>
    friend void task_region(Ex&, BOOST_THREAD_FWD_REF(F) f);
    template<class Ex, typename F>
    friend void task_region_final(Ex&, BOOST_THREAD_FWD_REF(F) f);

    void wait_all()
    {
      wait_for_all(group.begin(), group.end());

      for (group_type::iterator it = group.begin(); it != group.end(); ++it)
      {
        future<void>& f = *it;
        if (f.has_exception())
        {
          try
          {
            boost::rethrow_exception(f.get_exception_ptr());
          }
          catch (...)
          {
            detail::handle_task_region_exceptions(exs);
          }
        }
      }
      if (exs.size() != 0)
      {
        boost::throw_exception(exs);
      }
    }
protected:
#if ! defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && ! defined BOOST_THREAD_PROVIDES_EXECUTORS
    task_region_handle_gen()
    {}
#endif

#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && defined BOOST_THREAD_PROVIDES_EXECUTORS
    task_region_handle_gen()
    : canceled(false)
    , ex(0)
    {}
    task_region_handle_gen(Executor& ex)
    : canceled(false)
    , ex(&ex)
    {}

#endif

#if ! defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && defined BOOST_THREAD_PROVIDES_EXECUTORS
    task_region_handle_gen()
    : ex(0)
    {}
    task_region_handle_gen(Executor& ex)
    : ex(&ex)
    {}
#endif

#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && ! defined BOOST_THREAD_PROVIDES_EXECUTORS
    task_region_handle_gen()
    : canceled(false)
    {
    }
#endif

    ~task_region_handle_gen()
    {
      //wait_all();
    }

#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
    mutable mutex mtx;
    bool canceled;
#endif
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
    Executor* ex;
#endif
    exception_list exs;
    typedef csbl::vector<future<void> > group_type;
    group_type group;

  public:
    BOOST_DELETED_FUNCTION(task_region_handle_gen(const task_region_handle_gen&))
    BOOST_DELETED_FUNCTION(task_region_handle_gen& operator=(const task_region_handle_gen&))
    BOOST_DELETED_FUNCTION(task_region_handle_gen* operator&() const)

  public:
    template<typename F>
    void run(BOOST_THREAD_FWD_REF(F) f)
    {
#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
      {
        lock_guard<mutex> lk(mtx);
        if (canceled) {
          boost::throw_exception(task_canceled_exception());
        }
      }
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
      group.push_back(async(*ex, detail::wrapped<task_region_handle_gen<Executor>, F>(*this, forward<F>(f))));
#else
      group.push_back(async(detail::wrapped<task_region_handle_gen<Executor>, F>(*this, forward<F>(f))));
#endif
#else
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
      group.push_back(async(*ex, forward<F>(f)));
#else
      group.push_back(async(forward<F>(f)));
#endif
#endif
    }

    void wait()
    {
#if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
      {
        lock_guard<mutex> lk(mtx);
        if (canceled) {
          boost::throw_exception(task_canceled_exception());
        }
      }
#endif
      wait_all();
    }
  };
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
  typedef basic_thread_pool default_executor;
#else
  typedef int default_executor;
#endif
  class task_region_handle :
    public task_region_handle_gen<default_executor>
  {
    default_executor tp;
    template <typename F>
    friend void task_region(BOOST_THREAD_FWD_REF(F) f);
    template<typename F>
    friend void task_region_final(BOOST_THREAD_FWD_REF(F) f);

  protected:
    task_region_handle() : task_region_handle_gen<default_executor>()
    {
#if defined BOOST_THREAD_PROVIDES_EXECUTORS
      ex = &tp;
#endif
    }
    BOOST_DELETED_FUNCTION(task_region_handle(const task_region_handle&))
    BOOST_DELETED_FUNCTION(task_region_handle& operator=(const task_region_handle&))
    BOOST_DELETED_FUNCTION(task_region_handle* operator&() const)

  };

  template <typename Executor, typename F>
  void task_region_final(Executor& ex, BOOST_THREAD_FWD_REF(F) f)
  {
    task_region_handle_gen<Executor> tr(ex);
    try
    {
      f(tr);
    }
    catch (...)
    {
      detail::handle_task_region_exceptions(tr.exs);
    }
    tr.wait_all();
  }

  template <typename Executor, typename F>
  void task_region(Executor& ex, BOOST_THREAD_FWD_REF(F) f)
  {
    task_region_final(ex, forward<F>(f));
  }

  template <typename F>
  void task_region_final(BOOST_THREAD_FWD_REF(F) f)
  {
    task_region_handle tr;
    try
    {
      f(tr);
    }
    catch (...)
    {
      detail::handle_task_region_exceptions(tr.exs);
    }
    tr.wait_all();
  }

  template <typename F>
  void task_region(BOOST_THREAD_FWD_REF(F) f)
  {
    task_region_final(forward<F>(f));
  }

} // v2
} // parallel
} // experimental
} // boost

#include <boost/config/abi_suffix.hpp>

#endif // header