alloc_ctor_pass.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Copyright (C) 2011 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/future.hpp>
  14. // class packaged_task<R>
  15. // template <class F, class Allocator>
  16. // explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f);
  17. #define BOOST_THREAD_VERSION 4
  18. #if BOOST_THREAD_VERSION == 4
  19. #define BOOST_THREAD_DETAIL_SIGNATURE double()
  20. #else
  21. #define BOOST_THREAD_DETAIL_SIGNATURE double
  22. #endif
  23. #include <boost/thread/detail/config.hpp>
  24. #include <boost/thread/future.hpp>
  25. #include <boost/detail/lightweight_test.hpp>
  26. #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
  27. #include "../test_allocator.hpp"
  28. double fct()
  29. {
  30. return 5.0;
  31. }
  32. long lfct()
  33. {
  34. return 5;
  35. }
  36. class A
  37. {
  38. long data_;
  39. public:
  40. BOOST_THREAD_COPYABLE_AND_MOVABLE(A)
  41. static int n_moves;
  42. static int n_copies;
  43. static int n_instances;
  44. static int n_destroy;
  45. explicit A(long i) : data_(i)
  46. {
  47. ++n_instances;
  48. }
  49. A(BOOST_THREAD_RV_REF(A) a) : data_(BOOST_THREAD_RV(a).data_)
  50. {
  51. ++n_instances;
  52. ++n_moves; BOOST_THREAD_RV(a).data_ = -1;
  53. }
  54. A& operator=(BOOST_THREAD_RV_REF(A) a)
  55. {
  56. data_ = BOOST_THREAD_RV(a).data_;
  57. BOOST_THREAD_RV(a).data_ = -1;
  58. ++n_moves;
  59. return *this;
  60. }
  61. A(const A& a) : data_(a.data_)
  62. {
  63. ++n_instances;
  64. ++n_copies;
  65. }
  66. A& operator=(BOOST_THREAD_COPY_ASSIGN_REF(A) a)
  67. {
  68. data_ = a.data_;
  69. ++n_copies;
  70. return *this;
  71. }
  72. ~A()
  73. {
  74. --n_instances;
  75. ++n_destroy;
  76. }
  77. long operator()() const
  78. { return data_;}
  79. long operator()(long i, long j) const
  80. { return data_ + i + j;}
  81. };
  82. int A::n_moves = 0;
  83. int A::n_copies = 0;
  84. int A::n_instances = 0;
  85. int A::n_destroy = 0;
  86. int main()
  87. {
  88. {
  89. boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(boost::allocator_arg,
  90. test_allocator<A>(), BOOST_THREAD_MAKE_RV_REF(A(5)));
  91. BOOST_TEST(test_alloc_base::count > 0);
  92. BOOST_TEST(p.valid());
  93. boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
  94. //p(3, 'a');
  95. p();
  96. BOOST_TEST(f.get() == 5.0);
  97. }
  98. BOOST_TEST(A::n_copies == 0);
  99. BOOST_TEST(A::n_moves > 0);
  100. BOOST_TEST(A::n_instances == 0);
  101. BOOST_TEST(A::n_destroy > 0);
  102. BOOST_TEST(test_alloc_base::count == 0);
  103. A::n_copies = 0;
  104. A::n_moves = 0;
  105. {
  106. A a(5);
  107. boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(boost::allocator_arg,
  108. test_allocator<A>(), a);
  109. BOOST_TEST(test_alloc_base::count > 0);
  110. BOOST_TEST(p.valid());
  111. boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
  112. //p(3, 'a');
  113. p();
  114. BOOST_TEST(f.get() == 5.0);
  115. }
  116. //BOOST_TEST(A::n_copies > 0);
  117. //BOOST_TEST(A::n_moves > 0);
  118. BOOST_TEST(test_alloc_base::count == 0);
  119. A::n_copies = 0;
  120. A::n_moves = 0;
  121. {
  122. const A a(5);
  123. boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(boost::allocator_arg,
  124. test_allocator<A>(), a);
  125. BOOST_TEST(test_alloc_base::count > 0);
  126. BOOST_TEST(p.valid());
  127. boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
  128. //p(3, 'a');
  129. p();
  130. BOOST_TEST(f.get() == 5.0);
  131. }
  132. //BOOST_TEST(A::n_copies > 0);
  133. //BOOST_TEST(A::n_moves > 0);
  134. BOOST_TEST(test_alloc_base::count == 0);
  135. {
  136. boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(boost::allocator_arg,
  137. test_allocator<A>(), fct);
  138. BOOST_TEST(test_alloc_base::count > 0);
  139. BOOST_TEST(p.valid());
  140. boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
  141. //p(3, 'a');
  142. p();
  143. BOOST_TEST(f.get() == 5.0);
  144. }
  145. {
  146. boost::packaged_task<BOOST_THREAD_DETAIL_SIGNATURE> p(boost::allocator_arg,
  147. test_allocator<A>(), &lfct);
  148. BOOST_TEST(test_alloc_base::count > 0);
  149. BOOST_TEST(p.valid());
  150. boost::future<double> f = BOOST_THREAD_MAKE_RV_REF(p.get_future());
  151. //p(3, 'a');
  152. p();
  153. BOOST_TEST(f.get() == 5.0);
  154. }
  155. return boost::report_errors();
  156. }
  157. #else
  158. int main()
  159. {
  160. return boost::report_errors();
  161. }
  162. #endif