基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gcc_bugs.cpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. g++ --version:
  3. g++ (Ubuntu 10 - 20200416 - 0ubuntu1~18.04) 10.0.1 20200416 (experimental)[master revision 3c3f12e2a76:dcee354ce56:44b326839d864fc10c459916abcc97f35a9ac3de]
  4. Copyright(C) 2020 Free Software Foundation, Inc.
  5. This is free software; see the source for copying conditions.There is NO
  6. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  7. */
  8. #include "librf.h"
  9. using namespace resumef;
  10. #define GCC_FIX_BUGS 0
  11. static future_t<> gcc_bugs_if_await(event_t e)
  12. {
  13. #if GCC_FIX_BUGS
  14. auto result = co_await e.wait();
  15. if (result == false)
  16. #else
  17. if (co_await e.wait() == false) //internal compiler error: in fold_convert_loc, at fold-const.c:2435
  18. #endif
  19. std::cout << "time out!" << std::endl;
  20. else
  21. std::cout << "event signal!" << std::endl;
  22. }
  23. static future_t<> gcc_bugs_while_await(mutex_t lock)
  24. {
  25. #if GCC_FIX_BUGS
  26. for (;;)
  27. {
  28. auto result = co_await lock.try_lock();
  29. if (result) break;
  30. }
  31. #else
  32. while (!co_await lock.try_lock()); //internal compiler error: in fold_convert_loc, at fold-const.c:2435
  33. #endif
  34. std::cout << "OK." << std::endl;
  35. }
  36. #if GCC_FIX_BUGS
  37. static future_t<> gcc_bugs_lambda_coroutines_fixed(std::thread& other, channel_t<bool> c_done)
  38. {
  39. co_await c_done;
  40. std::cout << "other thread = " << other.get_id();
  41. co_await c_done;
  42. }
  43. #endif
  44. static void gcc_bugs_lambda_coroutines()
  45. {
  46. channel_t<bool> c_done{ 1 };
  47. std::thread other;
  48. #if GCC_FIX_BUGS
  49. go gcc_bugs_lambda_coroutines_fixed(other, c_done);
  50. #else
  51. go[&other, c_done]()->future_t<>
  52. {
  53. co_await c_done;
  54. std::cout << "other thread = " << other.get_id();
  55. co_await c_done;
  56. }; //internal compiler error: in captures_temporary, at cp/coroutines.cc:2716
  57. #endif
  58. }
  59. #if GCC_FIX_BUGS
  60. static future_t<> gcc_bugs_lambda_coroutines2_fixed(channel_t<intptr_t> head, channel_t<intptr_t> tail)
  61. {
  62. for (int i = 0; i < 100; ++i)
  63. {
  64. co_await(head << 0);
  65. co_await tail;
  66. }
  67. }
  68. #endif
  69. static void gcc_bugs_lambda_coroutines2()
  70. {
  71. channel_t<intptr_t> head{ 1 };
  72. channel_t<intptr_t> tail{ 0 };
  73. #if GCC_FIX_BUGS
  74. go gcc_bugs_lambda_coroutines2_fixed(head, tail);
  75. #else
  76. GO
  77. {
  78. for (int i = 0; i < 100; ++i)
  79. {
  80. co_await(head << 0);
  81. intptr_t value = co_await tail;
  82. (void)value;
  83. }
  84. }; //internal compiler error: in captures_temporary, at cp/coroutines.cc:2716
  85. #endif
  86. }
  87. template<class... _Mtxs>
  88. static future_t<> gcc_bugs_nameless_args(adopt_manual_unlock_t
  89. #if GCC_FIX_BUGS
  90. nameless
  91. #endif
  92. , _Mtxs&... mtxs)
  93. {
  94. #if GCC_FIX_BUGS
  95. (void)nameless;
  96. #endif
  97. co_await mutex_t::lock(adopt_manual_unlock, mtxs...);
  98. } //internal compiler error: Segmentation fault
  99. void gcc_bugs()
  100. {
  101. event_t e;
  102. go gcc_bugs_if_await(e);
  103. mutex_t mtx;
  104. go gcc_bugs_while_await(mtx);
  105. mutex_t a, b, c;
  106. go gcc_bugs_nameless_args(adopt_manual_unlock, a, b, c);
  107. }