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