test_pthread_cxx.cpp 4.1 KB

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