test_pthread_cxx.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <iostream>
  2. #include <sstream>
  3. #include <thread>
  4. #include <mutex>
  5. #include <memory>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "unity.h"
  9. #include "test_utils.h"
  10. #if __GTHREADS && __GTHREADS_CXX0X
  11. #include "esp_log.h"
  12. const static __attribute__((unused)) char *TAG = "pthread_test";
  13. static std::mutex mtx;
  14. static std::shared_ptr<int> global_sp_mtx; // protected by mux
  15. static std::recursive_mutex recur_mtx;
  16. static std::shared_ptr<int> global_sp_recur_mtx; // protected by recursive mux
  17. static void thread_do_nothing() {}
  18. static void thread_main()
  19. {
  20. std::cout << "thread_main CXX " << std::hex << std::this_thread::get_id() << std::endl;
  21. std::chrono::milliseconds dur = std::chrono::milliseconds(10);
  22. for (int i = 0; i < 10; i++) {
  23. for (int j = 0; j < 10; j++) {
  24. int old_val, new_val;
  25. // mux test
  26. mtx.lock();
  27. old_val = *global_sp_mtx;
  28. std::this_thread::yield();
  29. (*global_sp_mtx)++;
  30. std::this_thread::yield();
  31. new_val = *global_sp_mtx;
  32. mtx.unlock();
  33. std::cout << "thread " << std::hex << std::this_thread::get_id() << ": nrec " << i << " val= " << *global_sp_mtx << std::endl;
  34. TEST_ASSERT_EQUAL(old_val + 1, new_val);
  35. // sleep_for test
  36. std::this_thread::sleep_for(dur);
  37. // recursive mux test
  38. recur_mtx.lock();
  39. recur_mtx.lock();
  40. old_val = *global_sp_recur_mtx;
  41. std::this_thread::yield();
  42. (*global_sp_recur_mtx)++;
  43. std::this_thread::yield();
  44. new_val = *global_sp_recur_mtx;
  45. recur_mtx.unlock();
  46. recur_mtx.unlock();
  47. std::cout << "thread " << std::hex << std::this_thread::get_id() << ": rec " << i << " val= " << *global_sp_recur_mtx << std::endl;
  48. TEST_ASSERT_EQUAL(old_val + 1, new_val);
  49. }
  50. // sleep_until test
  51. using std::chrono::system_clock;
  52. std::time_t tt = system_clock::to_time_t(system_clock::now());
  53. struct std::tm *ptm = std::localtime(&tt);
  54. ptm->tm_sec++;
  55. std::this_thread::sleep_until(system_clock::from_time_t(mktime(ptm)));
  56. }
  57. }
  58. TEST_CASE("pthread C++", "[pthread]")
  59. {
  60. global_sp_mtx.reset(new int(1));
  61. global_sp_recur_mtx.reset(new int(-1000));
  62. std::thread t1(thread_do_nothing);
  63. t1.join();
  64. std::thread t2(thread_main);
  65. std::cout << "Detach thread " << std::hex << t2.get_id() << std::endl;
  66. t2.detach();
  67. TEST_ASSERT_FALSE(t2.joinable());
  68. std::thread t3(thread_main);
  69. std::thread t4(thread_main);
  70. TEST_ASSERT(t3.joinable());
  71. TEST_ASSERT(t4.joinable());
  72. std::cout << "Join thread " << std::hex << t3.get_id() << std::endl;
  73. t3.join();
  74. std::cout << "Join thread " << std::hex << t4.get_id() << std::endl;
  75. t4.join();
  76. // we don't know if/when t2 has finished, so delay another 2s before
  77. // deleting the common mutexes
  78. std::this_thread::sleep_for(std::chrono::seconds(2));
  79. global_sp_mtx.reset(); // avoid reported leak
  80. global_sp_recur_mtx.reset();
  81. }
  82. static void task_test_sandbox()
  83. {
  84. std::stringstream ss;
  85. ESP_LOGI(TAG, "About to create a string stream");
  86. ESP_LOGI(TAG, "About to write to string stream");
  87. ss << "Hello World!";
  88. ESP_LOGI(TAG, "About to extract from stringstream");
  89. ESP_LOGI(TAG, "Text: %s", ss.str().c_str());
  90. }
  91. static void task_test_sandbox_c(void *arg)
  92. {
  93. bool *running = (bool *)arg;
  94. // wrap thread func to ensure that all C++ stack objects are cleaned up by their destructors
  95. task_test_sandbox();
  96. ESP_LOGI(TAG, "Task stk_wm = %d", uxTaskGetStackHighWaterMark(NULL));
  97. if (running) {
  98. *running = false;
  99. vTaskDelete(NULL);
  100. }
  101. }
  102. TEST_CASE("pthread mix C/C++", "[pthread]")
  103. {
  104. bool c_running = true;
  105. std::thread t1(task_test_sandbox);
  106. xTaskCreatePinnedToCore((TaskFunction_t)&task_test_sandbox_c, "task_test_sandbox", 3072, &c_running, 5, NULL, 0);
  107. while (c_running) {
  108. vTaskDelay(1);
  109. }
  110. if (t1.joinable()) {
  111. std::cout << "Join thread " << std::hex << t1.get_id() << std::endl;
  112. t1.join();
  113. }
  114. }
  115. #endif