async.pass.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. //
  10. // UNSUPPORTED: libcpp-has-no-threads
  11. // UNSUPPORTED: c++98, c++03
  12. // <future>
  13. // template <class F, class... Args>
  14. // future<typename result_of<F(Args...)>::type>
  15. // async(F&& f, Args&&... args);
  16. // template <class F, class... Args>
  17. // future<typename result_of<F(Args...)>::type>
  18. // async(launch policy, F&& f, Args&&... args);
  19. #include <future>
  20. #include <atomic>
  21. #include <memory>
  22. #include <cassert>
  23. #include "test_macros.h"
  24. typedef std::chrono::high_resolution_clock Clock;
  25. typedef std::chrono::milliseconds ms;
  26. std::atomic_bool invoked = ATOMIC_VAR_INIT(false);
  27. int f0()
  28. {
  29. invoked = true;
  30. std::this_thread::sleep_for(ms(200));
  31. return 3;
  32. }
  33. int i = 0;
  34. int& f1()
  35. {
  36. invoked = true;
  37. std::this_thread::sleep_for(ms(200));
  38. return i;
  39. }
  40. void f2()
  41. {
  42. invoked = true;
  43. std::this_thread::sleep_for(ms(200));
  44. }
  45. std::unique_ptr<int> f3(int j)
  46. {
  47. invoked = true;
  48. std::this_thread::sleep_for(ms(200));
  49. return std::unique_ptr<int>(new int(j));
  50. }
  51. std::unique_ptr<int> f4(std::unique_ptr<int>&& p)
  52. {
  53. invoked = true;
  54. std::this_thread::sleep_for(ms(200));
  55. return std::move(p);
  56. }
  57. void f5(int j)
  58. {
  59. std::this_thread::sleep_for(ms(200));
  60. TEST_THROW(j); ((void)j);
  61. }
  62. template <class Ret, class CheckLamdba, class ...Args>
  63. void test(CheckLamdba&& getAndCheckFn, bool IsDeferred, Args&&... args) {
  64. // Reset global state.
  65. invoked = false;
  66. // Create the future and wait
  67. std::future<Ret> f = std::async(std::forward<Args>(args)...);
  68. std::this_thread::sleep_for(ms(300));
  69. // Check that deferred async's have not invoked the function.
  70. assert(invoked == !IsDeferred);
  71. // Time the call to f.get() and check that the returned value matches
  72. // what is expected.
  73. Clock::time_point t0 = Clock::now();
  74. assert(getAndCheckFn(f));
  75. Clock::time_point t1 = Clock::now();
  76. // If the async is deferred it should take more than 100ms, otherwise
  77. // it should take less than 100ms.
  78. if (IsDeferred) {
  79. assert(t1-t0 > ms(100));
  80. } else {
  81. assert(t1-t0 < ms(100));
  82. }
  83. }
  84. int main()
  85. {
  86. // The default launch policy is implementation defined. libc++ defines
  87. // it to be std::launch::async.
  88. bool DefaultPolicyIsDeferred = false;
  89. bool DPID = DefaultPolicyIsDeferred;
  90. std::launch AnyPolicy = std::launch::async | std::launch::deferred;
  91. LIBCPP_ASSERT(AnyPolicy == std::launch::any);
  92. {
  93. auto checkInt = [](std::future<int>& f) { return f.get() == 3; };
  94. test<int>(checkInt, DPID, f0);
  95. test<int>(checkInt, false, std::launch::async, f0);
  96. test<int>(checkInt, true, std::launch::deferred, f0);
  97. test<int>(checkInt, DPID, AnyPolicy, f0);
  98. }
  99. {
  100. auto checkIntRef = [&](std::future<int&>& f) { return &f.get() == &i; };
  101. test<int&>(checkIntRef, DPID, f1);
  102. test<int&>(checkIntRef, false, std::launch::async, f1);
  103. test<int&>(checkIntRef, true, std::launch::deferred, f1);
  104. test<int&>(checkIntRef, DPID, AnyPolicy, f1);
  105. }
  106. {
  107. auto checkVoid = [](std::future<void>& f) { f.get(); return true; };
  108. test<void>(checkVoid, DPID, f2);
  109. test<void>(checkVoid, false, std::launch::async, f2);
  110. test<void>(checkVoid, true, std::launch::deferred, f2);
  111. test<void>(checkVoid, DPID, AnyPolicy, f2);
  112. }
  113. {
  114. using Ret = std::unique_ptr<int>;
  115. auto checkUPtr = [](std::future<Ret>& f) { return *f.get() == 3; };
  116. test<Ret>(checkUPtr, DPID, f3, 3);
  117. test<Ret>(checkUPtr, DPID, f4, std::unique_ptr<int>(new int(3)));
  118. }
  119. #ifndef TEST_HAS_NO_EXCEPTIONS
  120. {
  121. std::future<void> f = std::async(f5, 3);
  122. std::this_thread::sleep_for(ms(300));
  123. try { f.get(); assert (false); } catch ( int ) {}
  124. }
  125. {
  126. std::future<void> f = std::async(std::launch::deferred, f5, 3);
  127. std::this_thread::sleep_for(ms(300));
  128. try { f.get(); assert (false); } catch ( int ) {}
  129. }
  130. #endif
  131. }