mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-08-06 07:00:05 +08:00
feat(pthread): Modify for ESP8266
This commit is contained in:
@ -1,48 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include "unity.h"
|
||||
|
||||
#if __GTHREADS && __GTHREADS_CXX0X
|
||||
|
||||
std::condition_variable cv;
|
||||
std::mutex cv_m;
|
||||
std::atomic<int> i{0};
|
||||
|
||||
static void waits(int idx, int timeout_ms)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(cv_m);
|
||||
auto now = std::chrono::system_clock::now();
|
||||
|
||||
if(cv.wait_until(lk, now + std::chrono::milliseconds(timeout_ms), [](){return i == 1;}))
|
||||
std::cout << "Thread " << idx << " finished waiting. i == " << i << '\n';
|
||||
else
|
||||
std::cout << "Thread " << idx << " timed out. i == " << i << '\n';
|
||||
}
|
||||
|
||||
static void signals(int signal_ms)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(signal_ms));
|
||||
std::cout << "Notifying...\n";
|
||||
cv.notify_all();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(signal_ms));
|
||||
i = 1;
|
||||
std::cout << "Notifying again...\n";
|
||||
cv.notify_all();
|
||||
}
|
||||
|
||||
TEST_CASE("C++ condition_variable", "[std::condition_variable]")
|
||||
{
|
||||
i = 0;
|
||||
std::thread t1(waits, 1, 100), t2(waits, 2, 800), t3(signals, 200);
|
||||
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
|
||||
std::cout << "All threads joined\n";
|
||||
}
|
||||
#endif
|
@ -1,31 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <future>
|
||||
#include <thread>
|
||||
#include "unity.h"
|
||||
|
||||
#if __GTHREADS && __GTHREADS_CXX0X
|
||||
TEST_CASE("C++ future", "[std::future]")
|
||||
{
|
||||
// future from a packaged_task
|
||||
std::packaged_task<int()> task([]{ return 7; }); // wrap the function
|
||||
std::future<int> f1 = task.get_future(); // get a future
|
||||
std::thread t(std::move(task)); // launch on a thread
|
||||
|
||||
// future from an async()
|
||||
std::future<int> f2 = std::async(std::launch::async, []{ return 8; });
|
||||
|
||||
// future from a promise
|
||||
std::promise<int> p;
|
||||
std::future<int> f3 = p.get_future();
|
||||
std::thread( [&p]{ p.set_value_at_thread_exit(9); }).detach();
|
||||
|
||||
std::cout << "Waiting..." << std::flush;
|
||||
f1.wait();
|
||||
f2.wait();
|
||||
f3.wait();
|
||||
std::cout << "Done!\nResults are: "
|
||||
<< f1.get() << ' ' << f2.get() << ' ' << f3.get() << '\n';
|
||||
t.join();
|
||||
}
|
||||
#endif
|
||||
|
@ -1,132 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "unity.h"
|
||||
|
||||
#if __GTHREADS && __GTHREADS_CXX0X
|
||||
|
||||
#define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL
|
||||
#include "esp_log.h"
|
||||
const static char *TAG = "pthread_test";
|
||||
|
||||
static std::shared_ptr<int> global_sp;
|
||||
static std::mutex mtx;
|
||||
static std::recursive_mutex recur_mtx;
|
||||
|
||||
static void thread_do_nothing() {}
|
||||
|
||||
static void thread_main()
|
||||
{
|
||||
int i = 0;
|
||||
std::cout << "thread_main CXX " << std::hex << std::this_thread::get_id() << std::endl;
|
||||
std::chrono::milliseconds dur = std::chrono::milliseconds(300);
|
||||
|
||||
while (i < 3) {
|
||||
int old_val, new_val;
|
||||
|
||||
// mux test
|
||||
mtx.lock();
|
||||
old_val = *global_sp;
|
||||
std::this_thread::yield();
|
||||
(*global_sp)++;
|
||||
std::this_thread::yield();
|
||||
new_val = *global_sp;
|
||||
mtx.unlock();
|
||||
std::cout << "thread " << std::hex << std::this_thread::get_id() << ": " << i++ << " val= " << *global_sp << std::endl;
|
||||
TEST_ASSERT_TRUE(new_val == old_val + 1);
|
||||
|
||||
// sleep_for test
|
||||
std::this_thread::sleep_for(dur);
|
||||
|
||||
// recursive mux test
|
||||
recur_mtx.lock();
|
||||
recur_mtx.lock();
|
||||
old_val = *global_sp;
|
||||
std::this_thread::yield();
|
||||
(*global_sp)++;
|
||||
std::this_thread::yield();
|
||||
new_val = *global_sp;
|
||||
recur_mtx.unlock();
|
||||
recur_mtx.unlock();
|
||||
std::cout << "thread " << std::hex << std::this_thread::get_id() << ": " << i++ << " val= " << *global_sp << std::endl;
|
||||
TEST_ASSERT_TRUE(new_val == old_val + 1);
|
||||
|
||||
// sleep_until test
|
||||
using std::chrono::system_clock;
|
||||
std::time_t tt = system_clock::to_time_t(system_clock::now());
|
||||
struct std::tm *ptm = std::localtime(&tt);
|
||||
ptm->tm_sec++;
|
||||
std::this_thread::sleep_until(system_clock::from_time_t(mktime(ptm)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("pthread C++", "[pthread]")
|
||||
{
|
||||
global_sp.reset(new int(1));
|
||||
|
||||
std::thread t1(thread_do_nothing);
|
||||
t1.join();
|
||||
|
||||
std::thread t2(thread_main);
|
||||
std::cout << "Detach thread " << std::hex << t2.get_id() << std::endl;
|
||||
t2.detach();
|
||||
TEST_ASSERT_FALSE(t2.joinable());
|
||||
|
||||
std::thread t3(thread_main);
|
||||
std::thread t4(thread_main);
|
||||
if (t3.joinable()) {
|
||||
std::cout << "Join thread " << std::hex << t3.get_id() << std::endl;
|
||||
t3.join();
|
||||
}
|
||||
if (t4.joinable()) {
|
||||
std::cout << "Join thread " << std::hex << t4.get_id() << std::endl;
|
||||
t4.join();
|
||||
}
|
||||
|
||||
global_sp.reset(); // avoid reported leak
|
||||
}
|
||||
|
||||
static void task_test_sandbox()
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
ESP_LOGI(TAG, "About to create a string stream");
|
||||
ESP_LOGI(TAG, "About to write to string stream");
|
||||
ss << "Hello World!";
|
||||
ESP_LOGI(TAG, "About to extract from stringstream");
|
||||
ESP_LOGI(TAG, "Text: %s", ss.str().c_str());
|
||||
}
|
||||
|
||||
static void task_test_sandbox_c(void *arg)
|
||||
{
|
||||
bool *running = (bool *)arg;
|
||||
|
||||
// wrap thread func to ensure that all C++ stack objects are cleaned up by their destructors
|
||||
task_test_sandbox();
|
||||
|
||||
ESP_LOGI(TAG, "Task stk_wm = %d", uxTaskGetStackHighWaterMark(NULL));
|
||||
if (running) {
|
||||
*running = false;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("pthread mix C/C++", "[pthread]")
|
||||
{
|
||||
bool c_running = true;
|
||||
|
||||
std::thread t1(task_test_sandbox);
|
||||
xTaskCreatePinnedToCore((TaskFunction_t)&task_test_sandbox_c, "task_test_sandbox", 3072, &c_running, 5, NULL, 0);
|
||||
while (c_running) {
|
||||
vTaskDelay(1);
|
||||
}
|
||||
if (t1.joinable()) {
|
||||
std::cout << "Join thread " << std::hex << t1.get_id() << std::endl;
|
||||
t1.join();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,5 +1,6 @@
|
||||
// Test pthread_create_key, pthread_delete_key, pthread_setspecific, pthread_getspecific
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include "unity.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
Reference in New Issue
Block a user