OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libstdc++-v3/] [include/] [std/] [future] - Blame information for rev 792

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 742 jeremybenn
//  -*- C++ -*-
2
 
3
// Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4
//
5
// This file is part of the GNU ISO C++ Library.  This library is free
6
// software; you can redistribute it and/or modify it under the
7
// terms of the GNU General Public License as published by the
8
// Free Software Foundation; either version 3, or (at your option)
9
// any later version.
10
 
11
// This library is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
// GNU General Public License for more details.
15
 
16
// Under Section 7 of GPL version 3, you are granted additional
17
// permissions described in the GCC Runtime Library Exception, version
18
// 3.1, as published by the Free Software Foundation.
19
 
20
// You should have received a copy of the GNU General Public License and
21
// a copy of the GCC Runtime Library Exception along with this program;
22
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23
// .
24
 
25
/** @file include/future
26
 *  This is a Standard C++ Library header.
27
 */
28
 
29
#ifndef _GLIBCXX_FUTURE
30
#define _GLIBCXX_FUTURE 1
31
 
32
#pragma GCC system_header
33
 
34
#ifndef __GXX_EXPERIMENTAL_CXX0X__
35
# include 
36
#else
37
 
38
#include 
39
#include 
40
#include 
41
#include 
42
#include 
43
#include 
44
#include 
45
#include 
46
#include 
47
 
48
namespace std _GLIBCXX_VISIBILITY(default)
49
{
50
_GLIBCXX_BEGIN_NAMESPACE_VERSION
51
 
52
  /**
53
   * @defgroup futures Futures
54
   * @ingroup concurrency
55
   *
56
   * Classes for futures support.
57
   * @{
58
   */
59
 
60
  /// Error code for futures
61
  enum class future_errc
62
  {
63
    future_already_retrieved = 1,
64
    promise_already_satisfied,
65
    no_state,
66
    broken_promise
67
  };
68
 
69
  /// Specialization.
70
  template<>
71
    struct is_error_code_enum : public true_type { };
72
 
73
  /// Points to a statically-allocated object derived from error_category.
74
  const error_category&
75
  future_category() noexcept;
76
 
77
  /// Overload for make_error_code.
78
  inline error_code
79
  make_error_code(future_errc __errc) noexcept
80
  { return error_code(static_cast(__errc), future_category()); }
81
 
82
  /// Overload for make_error_condition.
83
  inline error_condition
84
  make_error_condition(future_errc __errc) noexcept
85
  { return error_condition(static_cast(__errc), future_category()); }
86
 
87
  /**
88
   *  @brief Exception type thrown by futures.
89
   *  @ingroup exceptions
90
   */
91
  class future_error : public logic_error
92
  {
93
    error_code                  _M_code;
94
 
95
  public:
96
    explicit future_error(error_code __ec)
97
    : logic_error("std::future_error"), _M_code(__ec)
98
    { }
99
 
100
    virtual ~future_error() noexcept;
101
 
102
    virtual const char*
103
    what() const noexcept;
104
 
105
    const error_code&
106
    code() const noexcept { return _M_code; }
107
  };
108
 
109
  // Forward declarations.
110
  template
111
    class future;
112
 
113
  template
114
    class shared_future;
115
 
116
  template
117
    class atomic_future;
118
 
119
  template
120
    class packaged_task;
121
 
122
  template
123
    class promise;
124
 
125
  /// Launch code for futures
126
  enum class launch
127
  {
128
    async = 1,
129
    deferred = 2
130
  };
131
 
132
  constexpr launch operator&(launch __x, launch __y)
133
  {
134
    return static_cast(
135
        static_cast(__x) & static_cast(__y));
136
  }
137
 
138
  constexpr launch operator|(launch __x, launch __y)
139
  {
140
    return static_cast(
141
        static_cast(__x) | static_cast(__y));
142
  }
143
 
144
  constexpr launch operator^(launch __x, launch __y)
145
  {
146
    return static_cast(
147
        static_cast(__x) ^ static_cast(__y));
148
  }
149
 
150
  constexpr launch operator~(launch __x)
151
  { return static_cast(~static_cast(__x)); }
152
 
153
  inline launch& operator&=(launch& __x, launch __y)
154
  { return __x = __x & __y; }
155
 
156
  inline launch& operator|=(launch& __x, launch __y)
157
  { return __x = __x | __y; }
158
 
159
  inline launch& operator^=(launch& __x, launch __y)
160
  { return __x = __x ^ __y; }
161
 
162
  /// Status code for futures
163
  enum class future_status
164
  {
165
    ready,
166
    timeout,
167
    deferred
168
  };
169
 
170
  template
171
    future::type>
172
    async(launch __policy, _Fn&& __fn, _Args&&... __args);
173
 
174
  template
175
    struct __async_sfinae_helper
176
    {
177
      typedef future::type> type;
178
    };
179
 
180
  template
181
    struct __async_sfinae_helper
182
    { };
183
 
184
  template
185
    typename
186
    __async_sfinae_helper::type, _Fn, _Args...>::type
187
    async(_Fn&& __fn, _Args&&... __args);
188
 
189
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
190
  && (ATOMIC_INT_LOCK_FREE > 1)
191
 
192
  /// Base class and enclosing scope.
193
  struct __future_base
194
  {
195
    /// Base class for results.
196
    struct _Result_base
197
    {
198
      exception_ptr             _M_error;
199
 
200
      _Result_base(const _Result_base&) = delete;
201
      _Result_base& operator=(const _Result_base&) = delete;
202
 
203
      // _M_destroy() allows derived classes to control deallocation
204
      virtual void _M_destroy() = 0;
205
 
206
      struct _Deleter
207
      {
208
        void operator()(_Result_base* __fr) const { __fr->_M_destroy(); }
209
      };
210
 
211
    protected:
212
      _Result_base();
213
      virtual ~_Result_base();
214
    };
215
 
216
    /// Result.
217
    template
218
      struct _Result : _Result_base
219
      {
220
      private:
221
        typedef alignment_of<_Res>                            __a_of;
222
        typedef aligned_storage __align_storage;
223
        typedef typename __align_storage::type                  __align_type;
224
 
225
        __align_type            _M_storage;
226
        bool                    _M_initialized;
227
 
228
      public:
229
        _Result() noexcept : _M_initialized() { }
230
 
231
        ~_Result()
232
        {
233
          if (_M_initialized)
234
            _M_value().~_Res();
235
        }
236
 
237
        // Return lvalue, future will add const or rvalue-reference
238
        _Res&
239
        _M_value() noexcept { return *static_cast<_Res*>(_M_addr()); }
240
 
241
        void
242
        _M_set(const _Res& __res)
243
        {
244
          ::new (_M_addr()) _Res(__res);
245
          _M_initialized = true;
246
        }
247
 
248
        void
249
        _M_set(_Res&& __res)
250
        {
251
          ::new (_M_addr()) _Res(std::move(__res));
252
          _M_initialized = true;
253
        }
254
 
255
      private:
256
        void _M_destroy() { delete this; }
257
 
258
        void* _M_addr() noexcept { return static_cast(&_M_storage); }
259
    };
260
 
261
    /// A unique_ptr based on the instantiating type.
262
    template
263
      using _Ptr = unique_ptr<_Res, _Result_base::_Deleter>;
264
 
265
    /// Result_alloc.
266
    template
267
      struct _Result_alloc final : _Result<_Res>, _Alloc
268
      {
269
        typedef typename allocator_traits<_Alloc>::template
270
          rebind_alloc<_Result_alloc> __allocator_type;
271
 
272
        explicit
273
        _Result_alloc(const _Alloc& __a) : _Result<_Res>(), _Alloc(__a)
274
        { }
275
 
276
      private:
277
        void _M_destroy()
278
        {
279
          typedef allocator_traits<__allocator_type> __traits;
280
          __allocator_type __a(*this);
281
          __traits::destroy(__a, this);
282
          __traits::deallocate(__a, this, 1);
283
        }
284
      };
285
 
286
    template
287
      static _Ptr<_Result_alloc<_Res, _Allocator>>
288
      _S_allocate_result(const _Allocator& __a)
289
      {
290
        typedef _Result_alloc<_Res, _Allocator>   __result_type;
291
        typedef allocator_traits
292
          __traits;
293
        typename __traits::allocator_type __a2(__a);
294
        __result_type* __p = __traits::allocate(__a2, 1);
295
        __try
296
        {
297
          __traits::construct(__a2, __p, __a);
298
        }
299
        __catch(...)
300
        {
301
          __traits::deallocate(__a2, __p, 1);
302
          __throw_exception_again;
303
        }
304
        return _Ptr<__result_type>(__p);
305
      }
306
 
307
 
308
    /// Base class for state between a promise and one or more
309
    /// associated futures.
310
    class _State_base
311
    {
312
      typedef _Ptr<_Result_base> _Ptr_type;
313
 
314
      _Ptr_type                 _M_result;
315
      mutex                     _M_mutex;
316
      condition_variable        _M_cond;
317
      atomic_flag               _M_retrieved;
318
      once_flag                 _M_once;
319
 
320
    public:
321
      _State_base() noexcept : _M_result(), _M_retrieved(ATOMIC_FLAG_INIT) { }
322
      _State_base(const _State_base&) = delete;
323
      _State_base& operator=(const _State_base&) = delete;
324
      virtual ~_State_base();
325
 
326
      _Result_base&
327
      wait()
328
      {
329
        _M_run_deferred();
330
        unique_lock __lock(_M_mutex);
331
        _M_cond.wait(__lock, [&] { return _M_ready(); });
332
        return *_M_result;
333
      }
334
 
335
      template
336
        future_status
337
        wait_for(const chrono::duration<_Rep, _Period>& __rel)
338
        {
339
          unique_lock __lock(_M_mutex);
340
          if (_M_cond.wait_for(__lock, __rel, [&] { return _M_ready(); }))
341
            return future_status::ready;
342
          return future_status::timeout;
343
        }
344
 
345
      template
346
        future_status
347
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs)
348
        {
349
          unique_lock __lock(_M_mutex);
350
          if (_M_cond.wait_until(__lock, __abs, [&] { return _M_ready(); }))
351
            return future_status::ready;
352
          return future_status::timeout;
353
        }
354
 
355
      void
356
      _M_set_result(function<_Ptr_type()> __res, bool __ignore_failure = false)
357
      {
358
        bool __set = __ignore_failure;
359
        // all calls to this function are serialized,
360
        // side-effects of invoking __res only happen once
361
        call_once(_M_once, &_State_base::_M_do_set, this, ref(__res),
362
            ref(__set));
363
        if (!__set)
364
          __throw_future_error(int(future_errc::promise_already_satisfied));
365
      }
366
 
367
      void
368
      _M_break_promise(_Ptr_type __res)
369
      {
370
        if (static_cast(__res))
371
          {
372
            error_code __ec(make_error_code(future_errc::broken_promise));
373
            __res->_M_error = copy_exception(future_error(__ec));
374
            {
375
              lock_guard __lock(_M_mutex);
376
              _M_result.swap(__res);
377
            }
378
            _M_cond.notify_all();
379
          }
380
      }
381
 
382
      // Called when this object is passed to a future.
383
      void
384
      _M_set_retrieved_flag()
385
      {
386
        if (_M_retrieved.test_and_set())
387
          __throw_future_error(int(future_errc::future_already_retrieved));
388
      }
389
 
390
      template
391
        struct _Setter;
392
 
393
      // set lvalues
394
      template
395
        struct _Setter<_Res, _Arg&>
396
        {
397
          // check this is only used by promise::set_value(const R&)
398
          // or promise::set_value(R&)
399
          static_assert(is_same<_Res, _Arg&>::value  // promise
400
              || is_same::value,  // promise
401
              "Invalid specialisation");
402
 
403
          typename promise<_Res>::_Ptr_type operator()()
404
          {
405
            _State_base::_S_check(_M_promise->_M_future);
406
            _M_promise->_M_storage->_M_set(_M_arg);
407
            return std::move(_M_promise->_M_storage);
408
          }
409
          promise<_Res>*    _M_promise;
410
          _Arg&             _M_arg;
411
        };
412
 
413
      // set rvalues
414
      template
415
        struct _Setter<_Res, _Res&&>
416
        {
417
          typename promise<_Res>::_Ptr_type operator()()
418
          {
419
            _State_base::_S_check(_M_promise->_M_future);
420
            _M_promise->_M_storage->_M_set(std::move(_M_arg));
421
            return std::move(_M_promise->_M_storage);
422
          }
423
          promise<_Res>*    _M_promise;
424
          _Res&             _M_arg;
425
        };
426
 
427
      struct __exception_ptr_tag { };
428
 
429
      // set exceptions
430
      template
431
        struct _Setter<_Res, __exception_ptr_tag>
432
        {
433
          typename promise<_Res>::_Ptr_type operator()()
434
          {
435
            _State_base::_S_check(_M_promise->_M_future);
436
            _M_promise->_M_storage->_M_error = _M_ex;
437
            return std::move(_M_promise->_M_storage);
438
          }
439
 
440
          promise<_Res>*   _M_promise;
441
          exception_ptr&    _M_ex;
442
        };
443
 
444
      template
445
        static _Setter<_Res, _Arg&&>
446
        __setter(promise<_Res>* __prom, _Arg&& __arg)
447
        {
448
          return _Setter<_Res, _Arg&&>{ __prom, __arg };
449
        }
450
 
451
      template
452
        static _Setter<_Res, __exception_ptr_tag>
453
        __setter(exception_ptr& __ex, promise<_Res>* __prom)
454
        {
455
          return _Setter<_Res, __exception_ptr_tag>{ __prom, __ex };
456
        }
457
 
458
      static _Setter
459
      __setter(promise* __prom);
460
 
461
      template
462
        static bool
463
        _S_check(const shared_ptr<_Tp>& __p)
464
        {
465
          if (!static_cast(__p))
466
            __throw_future_error((int)future_errc::no_state);
467
        }
468
 
469
    private:
470
      void
471
      _M_do_set(function<_Ptr_type()>& __f, bool& __set)
472
      {
473
        _Ptr_type __res = __f();
474
        {
475
          lock_guard __lock(_M_mutex);
476
          _M_result.swap(__res);
477
        }
478
        _M_cond.notify_all();
479
        __set = true;
480
      }
481
 
482
      bool _M_ready() const noexcept { return static_cast(_M_result); }
483
 
484
      // Misnamed: waits for completion of async function.
485
      virtual void _M_run_deferred() { }
486
    };
487
 
488
    template
489
      class _Deferred_state;
490
 
491
    class _Async_state_common;
492
 
493
    template
494
      class _Async_state_impl;
495
 
496
    template
497
      class _Task_state;
498
 
499
    template
500
      static std::shared_ptr<_State_base>
501
      _S_make_deferred_state(_BoundFn&& __fn);
502
 
503
    template
504
      static std::shared_ptr<_State_base>
505
      _S_make_async_state(_BoundFn&& __fn);
506
 
507
    template
508
      struct _Task_setter;
509
 
510
    template
511
      class _Task_setter_helper
512
      {
513
        typedef typename remove_reference<_BoundFn>::type::result_type __res;
514
      public:
515
        typedef _Task_setter<_Res_ptr, __res> __type;
516
      };
517
 
518
    template
519
      static typename _Task_setter_helper<_Res_ptr, _BoundFn>::__type
520
      _S_task_setter(_Res_ptr& __ptr, _BoundFn&& __call)
521
      {
522
        typedef _Task_setter_helper<_Res_ptr, _BoundFn> __helper_type;
523
        typedef typename __helper_type::__type _Setter;
524
        return _Setter{ __ptr, std::ref(__call) };
525
      }
526
  };
527
 
528
  /// Partial specialization for reference types.
529
  template
530
    struct __future_base::_Result<_Res&> : __future_base::_Result_base
531
    {
532
      _Result() noexcept : _M_value_ptr() { }
533
 
534
      void _M_set(_Res& __res) noexcept { _M_value_ptr = &__res; }
535
 
536
      _Res& _M_get() noexcept { return *_M_value_ptr; }
537
 
538
    private:
539
      _Res*                     _M_value_ptr;
540
 
541
      void _M_destroy() { delete this; }
542
    };
543
 
544
  /// Explicit specialization for void.
545
  template<>
546
    struct __future_base::_Result : __future_base::_Result_base
547
    {
548
    private:
549
      void _M_destroy() { delete this; }
550
    };
551
 
552
 
553
  /// Common implementation for future and shared_future.
554
  template
555
    class __basic_future : public __future_base
556
    {
557
    protected:
558
      typedef shared_ptr<_State_base>                __state_type;
559
      typedef __future_base::_Result<_Res>&	__result_type;
560
 
561
    private:
562
      __state_type              _M_state;
563
 
564
    public:
565
      // Disable copying.
566
      __basic_future(const __basic_future&) = delete;
567
      __basic_future& operator=(const __basic_future&) = delete;
568
 
569
      bool
570
      valid() const noexcept { return static_cast(_M_state); }
571
 
572
      void
573
      wait() const
574
      {
575
        _State_base::_S_check(_M_state);
576
        _M_state->wait();
577
      }
578
 
579
      template
580
        future_status
581
        wait_for(const chrono::duration<_Rep, _Period>& __rel) const
582
        {
583
          _State_base::_S_check(_M_state);
584
          return _M_state->wait_for(__rel);
585
        }
586
 
587
      template
588
        future_status
589
        wait_until(const chrono::time_point<_Clock, _Duration>& __abs) const
590
        {
591
          _State_base::_S_check(_M_state);
592
          return _M_state->wait_until(__abs);
593
        }
594
 
595
    protected:
596
      /// Wait for the state to be ready and rethrow any stored exception
597
      __result_type
598
      _M_get_result()
599
      {
600
        _State_base::_S_check(_M_state);
601
        _Result_base& __res = _M_state->wait();
602
        if (!(__res._M_error == 0))
603
          rethrow_exception(__res._M_error);
604
        return static_cast<__result_type>(__res);
605
      }
606
 
607
      void _M_swap(__basic_future& __that) noexcept
608
      {
609
        _M_state.swap(__that._M_state);
610
      }
611
 
612
      // Construction of a future by promise::get_future()
613
      explicit
614
      __basic_future(const __state_type& __state) : _M_state(__state)
615
      {
616
        _State_base::_S_check(_M_state);
617
        _M_state->_M_set_retrieved_flag();
618
      }
619
 
620
      // Copy construction from a shared_future
621
      explicit
622
      __basic_future(const shared_future<_Res>&) noexcept;
623
 
624
      // Move construction from a shared_future
625
      explicit
626
      __basic_future(shared_future<_Res>&&) noexcept;
627
 
628
      // Move construction from a future
629
      explicit
630
      __basic_future(future<_Res>&&) noexcept;
631
 
632
      constexpr __basic_future() noexcept : _M_state() { }
633
 
634
      struct _Reset
635
      {
636
        explicit _Reset(__basic_future& __fut) noexcept : _M_fut(__fut) { }
637
        ~_Reset() { _M_fut._M_state.reset(); }
638
        __basic_future& _M_fut;
639
      };
640
    };
641
 
642
 
643
  /// Primary template for future.
644
  template
645
    class future : public __basic_future<_Res>
646
    {
647
      friend class promise<_Res>;
648
      template friend class packaged_task;
649
      template
650
        friend future::type>
651
        async(launch, _Fn&&, _Args&&...);
652
 
653
      typedef __basic_future<_Res> _Base_type;
654
      typedef typename _Base_type::__state_type __state_type;
655
 
656
      explicit
657
      future(const __state_type& __state) : _Base_type(__state) { }
658
 
659
    public:
660
      constexpr future() noexcept : _Base_type() { }
661
 
662
      /// Move constructor
663
      future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
664
 
665
      // Disable copying
666
      future(const future&) = delete;
667
      future& operator=(const future&) = delete;
668
 
669
      future& operator=(future&& __fut) noexcept
670
      {
671
        future(std::move(__fut))._M_swap(*this);
672
        return *this;
673
      }
674
 
675
      /// Retrieving the value
676
      _Res
677
      get()
678
      {
679
        typename _Base_type::_Reset __reset(*this);
680
        return std::move(this->_M_get_result()._M_value());
681
      }
682
 
683
      shared_future<_Res> share();
684
    };
685
 
686
  /// Partial specialization for future
687
  template
688
    class future<_Res&> : public __basic_future<_Res&>
689
    {
690
      friend class promise<_Res&>;
691
      template friend class packaged_task;
692
      template
693
        friend future::type>
694
        async(launch, _Fn&&, _Args&&...);
695
 
696
      typedef __basic_future<_Res&> _Base_type;
697
      typedef typename _Base_type::__state_type __state_type;
698
 
699
      explicit
700
      future(const __state_type& __state) : _Base_type(__state) { }
701
 
702
    public:
703
      constexpr future() noexcept : _Base_type() { }
704
 
705
      /// Move constructor
706
      future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
707
 
708
      // Disable copying
709
      future(const future&) = delete;
710
      future& operator=(const future&) = delete;
711
 
712
      future& operator=(future&& __fut) noexcept
713
      {
714
        future(std::move(__fut))._M_swap(*this);
715
        return *this;
716
      }
717
 
718
      /// Retrieving the value
719
      _Res&
720
      get()
721
      {
722
        typename _Base_type::_Reset __reset(*this);
723
        return this->_M_get_result()._M_get();
724
      }
725
 
726
      shared_future<_Res&> share();
727
    };
728
 
729
  /// Explicit specialization for future
730
  template<>
731
    class future : public __basic_future
732
    {
733
      friend class promise;
734
      template friend class packaged_task;
735
      template
736
        friend future::type>
737
        async(launch, _Fn&&, _Args&&...);
738
 
739
      typedef __basic_future _Base_type;
740
      typedef typename _Base_type::__state_type __state_type;
741
 
742
      explicit
743
      future(const __state_type& __state) : _Base_type(__state) { }
744
 
745
    public:
746
      constexpr future() noexcept : _Base_type() { }
747
 
748
      /// Move constructor
749
      future(future&& __uf) noexcept : _Base_type(std::move(__uf)) { }
750
 
751
      // Disable copying
752
      future(const future&) = delete;
753
      future& operator=(const future&) = delete;
754
 
755
      future& operator=(future&& __fut) noexcept
756
      {
757
        future(std::move(__fut))._M_swap(*this);
758
        return *this;
759
      }
760
 
761
      /// Retrieving the value
762
      void
763
      get()
764
      {
765
        typename _Base_type::_Reset __reset(*this);
766
        this->_M_get_result();
767
      }
768
 
769
      shared_future share();
770
    };
771
 
772
 
773
  /// Primary template for shared_future.
774
  template
775
    class shared_future : public __basic_future<_Res>
776
    {
777
      typedef __basic_future<_Res> _Base_type;
778
 
779
    public:
780
      constexpr shared_future() noexcept : _Base_type() { }
781
 
782
      /// Copy constructor
783
      shared_future(const shared_future& __sf) : _Base_type(__sf) { }
784
 
785
      /// Construct from a future rvalue
786
      shared_future(future<_Res>&& __uf) noexcept
787
      : _Base_type(std::move(__uf))
788
      { }
789
 
790
      /// Construct from a shared_future rvalue
791
      shared_future(shared_future&& __sf) noexcept
792
      : _Base_type(std::move(__sf))
793
      { }
794
 
795
      shared_future& operator=(const shared_future& __sf)
796
      {
797
        shared_future(__sf)._M_swap(*this);
798
        return *this;
799
      }
800
 
801
      shared_future& operator=(shared_future&& __sf) noexcept
802
      {
803
        shared_future(std::move(__sf))._M_swap(*this);
804
        return *this;
805
      }
806
 
807
      /// Retrieving the value
808
      const _Res&
809
      get()
810
      {
811
        typename _Base_type::__result_type __r = this->_M_get_result();
812
        _Res& __rs(__r._M_value());
813
        return __rs;
814
      }
815
    };
816
 
817
  /// Partial specialization for shared_future
818
  template
819
    class shared_future<_Res&> : public __basic_future<_Res&>
820
    {
821
      typedef __basic_future<_Res&>           _Base_type;
822
 
823
    public:
824
      constexpr shared_future() noexcept : _Base_type() { }
825
 
826
      /// Copy constructor
827
      shared_future(const shared_future& __sf) : _Base_type(__sf) { }
828
 
829
      /// Construct from a future rvalue
830
      shared_future(future<_Res&>&& __uf) noexcept
831
      : _Base_type(std::move(__uf))
832
      { }
833
 
834
      /// Construct from a shared_future rvalue
835
      shared_future(shared_future&& __sf) noexcept
836
      : _Base_type(std::move(__sf))
837
      { }
838
 
839
      shared_future& operator=(const shared_future& __sf)
840
      {
841
        shared_future(__sf)._M_swap(*this);
842
        return *this;
843
      }
844
 
845
      shared_future& operator=(shared_future&& __sf) noexcept
846
      {
847
        shared_future(std::move(__sf))._M_swap(*this);
848
        return *this;
849
      }
850
 
851
      /// Retrieving the value
852
      _Res&
853
      get() { return this->_M_get_result()._M_get(); }
854
    };
855
 
856
  /// Explicit specialization for shared_future
857
  template<>
858
    class shared_future : public __basic_future
859
    {
860
      typedef __basic_future _Base_type;
861
 
862
    public:
863
      constexpr shared_future() noexcept : _Base_type() { }
864
 
865
      /// Copy constructor
866
      shared_future(const shared_future& __sf) : _Base_type(__sf) { }
867
 
868
      /// Construct from a future rvalue
869
      shared_future(future&& __uf) noexcept
870
      : _Base_type(std::move(__uf))
871
      { }
872
 
873
      /// Construct from a shared_future rvalue
874
      shared_future(shared_future&& __sf) noexcept
875
      : _Base_type(std::move(__sf))
876
      { }
877
 
878
      shared_future& operator=(const shared_future& __sf)
879
      {
880
        shared_future(__sf)._M_swap(*this);
881
        return *this;
882
      }
883
 
884
      shared_future& operator=(shared_future&& __sf) noexcept
885
      {
886
        shared_future(std::move(__sf))._M_swap(*this);
887
        return *this;
888
      }
889
 
890
      // Retrieving the value
891
      void
892
      get() { this->_M_get_result(); }
893
    };
894
 
895
  // Now we can define the protected __basic_future constructors.
896
  template
897
    inline __basic_future<_Res>::
898
    __basic_future(const shared_future<_Res>& __sf) noexcept
899
    : _M_state(__sf._M_state)
900
    { }
901
 
902
  template
903
    inline __basic_future<_Res>::
904
    __basic_future(shared_future<_Res>&& __sf) noexcept
905
    : _M_state(std::move(__sf._M_state))
906
    { }
907
 
908
  template
909
    inline __basic_future<_Res>::
910
    __basic_future(future<_Res>&& __uf) noexcept
911
    : _M_state(std::move(__uf._M_state))
912
    { }
913
 
914
  template
915
    inline shared_future<_Res>
916
    future<_Res>::share()
917
    { return shared_future<_Res>(std::move(*this)); }
918
 
919
  template
920
    inline shared_future<_Res&>
921
    future<_Res&>::share()
922
    { return shared_future<_Res&>(std::move(*this)); }
923
 
924
  inline shared_future
925
  future::share()
926
  { return shared_future(std::move(*this)); }
927
 
928
  /// Primary template for promise
929
  template
930
    class promise
931
    {
932
      typedef __future_base::_State_base        _State;
933
      typedef __future_base::_Result<_Res>    _Res_type;
934
      typedef __future_base::_Ptr<_Res_type>       _Ptr_type;
935
      template friend class _State::_Setter;
936
 
937
      shared_ptr<_State>                        _M_future;
938
      _Ptr_type                                 _M_storage;
939
 
940
    public:
941
      promise()
942
      : _M_future(std::make_shared<_State>()),
943
        _M_storage(new _Res_type())
944
      { }
945
 
946
      promise(promise&& __rhs) noexcept
947
      : _M_future(std::move(__rhs._M_future)),
948
        _M_storage(std::move(__rhs._M_storage))
949
      { }
950
 
951
      template
952
        promise(allocator_arg_t, const _Allocator& __a)
953
        : _M_future(std::allocate_shared<_State>(__a)),
954
          _M_storage(__future_base::_S_allocate_result<_Res>(__a))
955
        { }
956
 
957
      template
958
        promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
959
        : _M_future(std::move(__rhs._M_future)),
960
          _M_storage(std::move(__rhs._M_storage))
961
        { }
962
 
963
      promise(const promise&) = delete;
964
 
965
      ~promise()
966
      {
967
        if (static_cast(_M_future) && !_M_future.unique())
968
          _M_future->_M_break_promise(std::move(_M_storage));
969
      }
970
 
971
      // Assignment
972
      promise&
973
      operator=(promise&& __rhs) noexcept
974
      {
975
        promise(std::move(__rhs)).swap(*this);
976
        return *this;
977
      }
978
 
979
      promise& operator=(const promise&) = delete;
980
 
981
      void
982
      swap(promise& __rhs) noexcept
983
      {
984
        _M_future.swap(__rhs._M_future);
985
        _M_storage.swap(__rhs._M_storage);
986
      }
987
 
988
      // Retrieving the result
989
      future<_Res>
990
      get_future()
991
      { return future<_Res>(_M_future); }
992
 
993
      // Setting the result
994
      void
995
      set_value(const _Res& __r)
996
      {
997
        auto __setter = _State::__setter(this, __r);
998
        _M_future->_M_set_result(std::move(__setter));
999
      }
1000
 
1001
      void
1002
      set_value(_Res&& __r)
1003
      {
1004
        auto __setter = _State::__setter(this, std::move(__r));
1005
        _M_future->_M_set_result(std::move(__setter));
1006
      }
1007
 
1008
      void
1009
      set_exception(exception_ptr __p)
1010
      {
1011
        auto __setter = _State::__setter(__p, this);
1012
        _M_future->_M_set_result(std::move(__setter));
1013
      }
1014
    };
1015
 
1016
  template
1017
    inline void
1018
    swap(promise<_Res>& __x, promise<_Res>& __y) noexcept
1019
    { __x.swap(__y); }
1020
 
1021
  template
1022
    struct uses_allocator, _Alloc>
1023
    : public true_type { };
1024
 
1025
 
1026
  /// Partial specialization for promise
1027
  template
1028
    class promise<_Res&>
1029
    {
1030
      typedef __future_base::_State_base        _State;
1031
      typedef __future_base::_Result<_Res&>    _Res_type;
1032
      typedef __future_base::_Ptr<_Res_type>       _Ptr_type;
1033
      template friend class _State::_Setter;
1034
 
1035
      shared_ptr<_State>                        _M_future;
1036
      _Ptr_type                                 _M_storage;
1037
 
1038
    public:
1039
      promise()
1040
      : _M_future(std::make_shared<_State>()),
1041
        _M_storage(new _Res_type())
1042
      { }
1043
 
1044
      promise(promise&& __rhs) noexcept
1045
      : _M_future(std::move(__rhs._M_future)),
1046
        _M_storage(std::move(__rhs._M_storage))
1047
      { }
1048
 
1049
      template
1050
        promise(allocator_arg_t, const _Allocator& __a)
1051
        : _M_future(std::allocate_shared<_State>(__a)),
1052
          _M_storage(__future_base::_S_allocate_result<_Res&>(__a))
1053
        { }
1054
 
1055
      template
1056
        promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1057
        : _M_future(std::move(__rhs._M_future)),
1058
          _M_storage(std::move(__rhs._M_storage))
1059
        { }
1060
 
1061
      promise(const promise&) = delete;
1062
 
1063
      ~promise()
1064
      {
1065
        if (static_cast(_M_future) && !_M_future.unique())
1066
          _M_future->_M_break_promise(std::move(_M_storage));
1067
      }
1068
 
1069
      // Assignment
1070
      promise&
1071
      operator=(promise&& __rhs) noexcept
1072
      {
1073
        promise(std::move(__rhs)).swap(*this);
1074
        return *this;
1075
      }
1076
 
1077
      promise& operator=(const promise&) = delete;
1078
 
1079
      void
1080
      swap(promise& __rhs) noexcept
1081
      {
1082
        _M_future.swap(__rhs._M_future);
1083
        _M_storage.swap(__rhs._M_storage);
1084
      }
1085
 
1086
      // Retrieving the result
1087
      future<_Res&>
1088
      get_future()
1089
      { return future<_Res&>(_M_future); }
1090
 
1091
      // Setting the result
1092
      void
1093
      set_value(_Res& __r)
1094
      {
1095
        auto __setter = _State::__setter(this, __r);
1096
        _M_future->_M_set_result(std::move(__setter));
1097
      }
1098
 
1099
      void
1100
      set_exception(exception_ptr __p)
1101
      {
1102
        auto __setter = _State::__setter(__p, this);
1103
        _M_future->_M_set_result(std::move(__setter));
1104
      }
1105
    };
1106
 
1107
  /// Explicit specialization for promise
1108
  template<>
1109
    class promise
1110
    {
1111
      typedef __future_base::_State_base        _State;
1112
      typedef __future_base::_Result    _Res_type;
1113
      typedef __future_base::_Ptr<_Res_type>       _Ptr_type;
1114
      template friend class _State::_Setter;
1115
 
1116
      shared_ptr<_State>                        _M_future;
1117
      _Ptr_type                                 _M_storage;
1118
 
1119
    public:
1120
      promise()
1121
      : _M_future(std::make_shared<_State>()),
1122
        _M_storage(new _Res_type())
1123
      { }
1124
 
1125
      promise(promise&& __rhs) noexcept
1126
      : _M_future(std::move(__rhs._M_future)),
1127
        _M_storage(std::move(__rhs._M_storage))
1128
      { }
1129
 
1130
      template
1131
        promise(allocator_arg_t, const _Allocator& __a)
1132
        : _M_future(std::allocate_shared<_State>(__a)),
1133
          _M_storage(__future_base::_S_allocate_result(__a))
1134
        { }
1135
 
1136
      template
1137
        promise(allocator_arg_t, const _Allocator&, promise&& __rhs)
1138
        : _M_future(std::move(__rhs._M_future)),
1139
          _M_storage(std::move(__rhs._M_storage))
1140
        { }
1141
 
1142
      promise(const promise&) = delete;
1143
 
1144
      ~promise()
1145
      {
1146
        if (static_cast(_M_future) && !_M_future.unique())
1147
          _M_future->_M_break_promise(std::move(_M_storage));
1148
      }
1149
 
1150
      // Assignment
1151
      promise&
1152
      operator=(promise&& __rhs) noexcept
1153
      {
1154
        promise(std::move(__rhs)).swap(*this);
1155
        return *this;
1156
      }
1157
 
1158
      promise& operator=(const promise&) = delete;
1159
 
1160
      void
1161
      swap(promise& __rhs) noexcept
1162
      {
1163
        _M_future.swap(__rhs._M_future);
1164
        _M_storage.swap(__rhs._M_storage);
1165
      }
1166
 
1167
      // Retrieving the result
1168
      future
1169
      get_future()
1170
      { return future(_M_future); }
1171
 
1172
      // Setting the result
1173
      void set_value();
1174
 
1175
      void
1176
      set_exception(exception_ptr __p)
1177
      {
1178
        auto __setter = _State::__setter(__p, this);
1179
        _M_future->_M_set_result(std::move(__setter));
1180
      }
1181
    };
1182
 
1183
  // set void
1184
  template<>
1185
    struct __future_base::_State_base::_Setter
1186
    {
1187
      promise::_Ptr_type operator()()
1188
      {
1189
        _State_base::_S_check(_M_promise->_M_future);
1190
        return std::move(_M_promise->_M_storage);
1191
      }
1192
 
1193
      promise*    _M_promise;
1194
    };
1195
 
1196
  inline __future_base::_State_base::_Setter
1197
  __future_base::_State_base::__setter(promise* __prom)
1198
  {
1199
    return _Setter{ __prom };
1200
  }
1201
 
1202
  inline void
1203
  promise::set_value()
1204
  {
1205
    auto __setter = _State::__setter(this);
1206
    _M_future->_M_set_result(std::move(__setter));
1207
  }
1208
 
1209
 
1210
  template
1211
    struct __future_base::_Task_setter
1212
    {
1213
      _Ptr_type operator()()
1214
      {
1215
        __try
1216
          {
1217
            _M_result->_M_set(_M_fn());
1218
          }
1219
        __catch(...)
1220
          {
1221
            _M_result->_M_error = current_exception();
1222
          }
1223
        return std::move(_M_result);
1224
      }
1225
      _Ptr_type&                _M_result;
1226
      std::function<_Res()>     _M_fn;
1227
    };
1228
 
1229
  template
1230
    struct __future_base::_Task_setter<_Ptr_type, void>
1231
    {
1232
      _Ptr_type operator()()
1233
      {
1234
        __try
1235
          {
1236
            _M_fn();
1237
          }
1238
        __catch(...)
1239
          {
1240
            _M_result->_M_error = current_exception();
1241
          }
1242
        return std::move(_M_result);
1243
      }
1244
      _Ptr_type&                _M_result;
1245
      std::function     _M_fn;
1246
    };
1247
 
1248
  template
1249
    struct __future_base::_Task_state<_Res(_Args...)> final
1250
    : __future_base::_State_base
1251
    {
1252
      typedef _Res _Res_type;
1253
 
1254
      _Task_state(std::function<_Res(_Args...)> __task)
1255
      : _M_result(new _Result<_Res>()), _M_task(std::move(__task))
1256
      { }
1257
 
1258
      template
1259
        _Task_state(_Func&& __task, const _Alloc& __a)
1260
        : _M_result(_S_allocate_result<_Res>(__a)),
1261
          _M_task(allocator_arg, __a, std::move(__task))
1262
        { }
1263
 
1264
      void
1265
      _M_run(_Args... __args)
1266
      {
1267
        // bound arguments decay so wrap lvalue references
1268
        auto __boundfn = std::__bind_simple(std::ref(_M_task),
1269
            _S_maybe_wrap_ref(std::forward<_Args>(__args))...);
1270
        auto __setter = _S_task_setter(_M_result, std::move(__boundfn));
1271
        _M_set_result(std::move(__setter));
1272
      }
1273
 
1274
      typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1275
      _Ptr_type _M_result;
1276
      std::function<_Res(_Args...)> _M_task;
1277
 
1278
      template
1279
        static reference_wrapper<_Tp>
1280
        _S_maybe_wrap_ref(_Tp& __t)
1281
        { return std::ref(__t); }
1282
 
1283
      template
1284
        static typename enable_if::value,
1285
                        _Tp>::type&&
1286
        _S_maybe_wrap_ref(_Tp&& __t)
1287
        { return std::forward<_Tp>(__t); }
1288
    };
1289
 
1290
  template
1291
           = is_same<_Task, typename decay<_Fn>::type>::value>
1292
    struct __constrain_pkgdtask
1293
    { typedef void __type; };
1294
 
1295
  template
1296
    struct __constrain_pkgdtask<_Task, _Fn, true>
1297
    { };
1298
 
1299
  /// packaged_task
1300
  template
1301
    class packaged_task<_Res(_ArgTypes...)>
1302
    {
1303
      typedef __future_base::_Task_state<_Res(_ArgTypes...)>  _State_type;
1304
      shared_ptr<_State_type>                   _M_state;
1305
 
1306
    public:
1307
      // Construction and destruction
1308
      packaged_task() noexcept { }
1309
 
1310
      template
1311
        explicit
1312
        packaged_task(allocator_arg_t, const _Allocator& __a) noexcept
1313
        { }
1314
 
1315
      template
1316
               __constrain_pkgdtask::__type>
1317
        explicit
1318
        packaged_task(_Fn&& __fn)
1319
        : _M_state(std::make_shared<_State_type>(std::forward<_Fn>(__fn)))
1320
        { }
1321
 
1322
      template
1323
               __constrain_pkgdtask::__type>
1324
        explicit
1325
        packaged_task(allocator_arg_t, const _Allocator& __a, _Fn&& __fn)
1326
        : _M_state(std::allocate_shared<_State_type>(__a,
1327
                                                     std::forward<_Fn>(__fn)))
1328
        { }
1329
 
1330
      ~packaged_task()
1331
      {
1332
        if (static_cast(_M_state) && !_M_state.unique())
1333
          _M_state->_M_break_promise(std::move(_M_state->_M_result));
1334
      }
1335
 
1336
      // No copy
1337
      packaged_task(const packaged_task&) = delete;
1338
      packaged_task& operator=(const packaged_task&) = delete;
1339
 
1340
      template
1341
        explicit
1342
        packaged_task(allocator_arg_t, const _Allocator&,
1343
                      const packaged_task&) = delete;
1344
 
1345
      // Move support
1346
      packaged_task(packaged_task&& __other) noexcept
1347
      { this->swap(__other); }
1348
 
1349
      template
1350
        explicit
1351
        packaged_task(allocator_arg_t, const _Allocator&,
1352
                      packaged_task&& __other) noexcept
1353
        { this->swap(__other); }
1354
 
1355
      packaged_task& operator=(packaged_task&& __other) noexcept
1356
      {
1357
        packaged_task(std::move(__other)).swap(*this);
1358
        return *this;
1359
      }
1360
 
1361
      void
1362
      swap(packaged_task& __other) noexcept
1363
      { _M_state.swap(__other._M_state); }
1364
 
1365
      bool
1366
      valid() const noexcept
1367
      { return static_cast(_M_state); }
1368
 
1369
      // Result retrieval
1370
      future<_Res>
1371
      get_future()
1372
      { return future<_Res>(_M_state); }
1373
 
1374
      // Execution
1375
      void
1376
      operator()(_ArgTypes... __args)
1377
      {
1378
        __future_base::_State_base::_S_check(_M_state);
1379
        _M_state->_M_run(std::forward<_ArgTypes>(__args)...);
1380
      }
1381
 
1382
      void
1383
      reset()
1384
      {
1385
        __future_base::_State_base::_S_check(_M_state);
1386
        packaged_task(std::move(_M_state->_M_task)).swap(*this);
1387
      }
1388
    };
1389
 
1390
  /// swap
1391
  template
1392
    inline void
1393
    swap(packaged_task<_Res(_ArgTypes...)>& __x,
1394
         packaged_task<_Res(_ArgTypes...)>& __y) noexcept
1395
    { __x.swap(__y); }
1396
 
1397
  template
1398
    struct uses_allocator, _Alloc>
1399
    : public true_type { };
1400
 
1401
 
1402
  template
1403
    class __future_base::_Deferred_state final
1404
    : public __future_base::_State_base
1405
    {
1406
    public:
1407
      explicit
1408
      _Deferred_state(_BoundFn&& __fn)
1409
      : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1410
      { }
1411
 
1412
    private:
1413
      typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1414
      _Ptr_type _M_result;
1415
      _BoundFn _M_fn;
1416
 
1417
      virtual void
1418
      _M_run_deferred()
1419
      {
1420
        // safe to call multiple times so ignore failure
1421
        _M_set_result(_S_task_setter(_M_result, _M_fn), true);
1422
      }
1423
    };
1424
 
1425
  class __future_base::_Async_state_common : public __future_base::_State_base
1426
  {
1427
  protected:
1428
#ifdef _GLIBCXX_HAVE_TLS
1429
    ~_Async_state_common();
1430
#else
1431
    ~_Async_state_common() { _M_join(); }
1432
#endif
1433
 
1434
    // Allow non-timed waiting functions to block until the thread completes,
1435
    // as if joined.
1436
    virtual void _M_run_deferred() { _M_join(); }
1437
 
1438
    void _M_join() { std::call_once(_M_once, &thread::join, ref(_M_thread)); }
1439
 
1440
    thread _M_thread;
1441
    once_flag _M_once;
1442
  };
1443
 
1444
  template
1445
    class __future_base::_Async_state_impl final
1446
    : public __future_base::_Async_state_common
1447
    {
1448
    public:
1449
      explicit
1450
      _Async_state_impl(_BoundFn&& __fn)
1451
      : _M_result(new _Result<_Res>()), _M_fn(std::move(__fn))
1452
      {
1453
        _M_thread = std::thread{ [this] {
1454
          _M_set_result(_S_task_setter(_M_result, _M_fn));
1455
        } };
1456
      }
1457
 
1458
    private:
1459
      typedef __future_base::_Ptr<_Result<_Res>> _Ptr_type;
1460
      _Ptr_type _M_result;
1461
      _BoundFn _M_fn;
1462
    };
1463
 
1464
  template
1465
    inline std::shared_ptr<__future_base::_State_base>
1466
    __future_base::_S_make_deferred_state(_BoundFn&& __fn)
1467
    {
1468
      typedef typename remove_reference<_BoundFn>::type __fn_type;
1469
      typedef _Deferred_state<__fn_type> __state_type;
1470
      return std::make_shared<__state_type>(std::move(__fn));
1471
    }
1472
 
1473
  template
1474
    inline std::shared_ptr<__future_base::_State_base>
1475
    __future_base::_S_make_async_state(_BoundFn&& __fn)
1476
    {
1477
      typedef typename remove_reference<_BoundFn>::type __fn_type;
1478
      typedef _Async_state_impl<__fn_type> __state_type;
1479
      return std::make_shared<__state_type>(std::move(__fn));
1480
    }
1481
 
1482
 
1483
  /// async
1484
  template
1485
    future::type>
1486
    async(launch __policy, _Fn&& __fn, _Args&&... __args)
1487
    {
1488
      typedef typename result_of<_Fn(_Args...)>::type result_type;
1489
      std::shared_ptr<__future_base::_State_base> __state;
1490
      if ((__policy & (launch::async|launch::deferred)) == launch::async)
1491
        {
1492
          __state = __future_base::_S_make_async_state(std::__bind_simple(
1493
              std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1494
        }
1495
      else
1496
        {
1497
          __state = __future_base::_S_make_deferred_state(std::__bind_simple(
1498
              std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
1499
        }
1500
      return future(__state);
1501
    }
1502
 
1503
  /// async, potential overload
1504
  template
1505
    inline typename
1506
    __async_sfinae_helper::type, _Fn, _Args...>::type
1507
    async(_Fn&& __fn, _Args&&... __args)
1508
    {
1509
      return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
1510
                   std::forward<_Args>(__args)...);
1511
    }
1512
 
1513
#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
1514
       // && ATOMIC_INT_LOCK_FREE
1515
 
1516
  // @} group futures
1517
_GLIBCXX_END_NAMESPACE_VERSION
1518
} // namespace
1519
 
1520
#endif // __GXX_EXPERIMENTAL_CXX0X__
1521
 
1522
#endif // _GLIBCXX_FUTURE

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.