diff --git a/en/application-dev/ffrt/ffrt-development-guideline.md b/en/application-dev/ffrt/ffrt-development-guideline.md index 44a2d093e0b84f01d4d1f647f239758db917fd91..b24061f81d3dff2516742c039ffcf89da02c1938 100644 --- a/en/application-dev/ffrt/ffrt-development-guideline.md +++ b/en/application-dev/ffrt/ffrt-development-guideline.md @@ -710,9 +710,10 @@ int main(int narg, char** argv) Expected output ``` -hello world, x = 2 +hello handle wait -x = 3 +x = 2 +world, x = 3 ``` @@ -744,7 +745,7 @@ ID of the task being executed. ##### Example ```{.c} -#include "timer.h" +#include "ffrt.h" int main(int narg, char** argv) { @@ -755,8 +756,9 @@ int main(int narg, char** argv) ffrt::submit([=]() { ffrt_qos_t taskQos = ffrt_this_task_get_qos(); + ffrt_timer_cb cb; ffrt_timer_start(taskQos, timeout1, data, cb, false); - usleep(200); + ffrt_usleep(200); }, {}, {}); ffrt::wait(); return 0; @@ -895,8 +897,6 @@ using namespace std; template struct Function { - template - Function(ffrt_function_header_t h, CT&& c) : header(h), closure(std::forward(c)) {} ffrt_function_header_t header; T closure; }; @@ -912,7 +912,7 @@ template void DestroyFunctionWrapper(void* t) { auto f = reinterpret_cast>*>(t); - f->closure = nullptr; + f = nullptr; } template @@ -921,8 +921,10 @@ static inline ffrt_function_header_t* create_function_wrapper(T&& func, { using function_type = Function>; auto p = ffrt_alloc_auto_managed_function_storage_base(kind); - auto f = - new (p)function_type({ ExecFunctionWrapper, DestroyFunctionWrapper, { 0 } }, std::forward(func)); + auto f = new (p)function_type; + f->header.exec = ExecFunctionWrapper; + f->header.destroy = DestroyFunctionWrapper; + f->closure = std::forward(func); return reinterpret_cast(f); } @@ -931,8 +933,9 @@ int main(int narg, char** argv) ffrt_queue_attr_t queue_attr; (void)ffrt_queue_attr_init(&queue_attr); ffrt_queue_t queue_handle = ffrt_queue_create(ffrt_queue_serial, "test_queue", &queue_attr); - - ffrt_queue_submit(queue_handle, create_function_wrapper([]() {printf("Task done.\n");}, ffrt_function_kind_queue), nullptr); + std::function&& queueFunc = [] () {printf("Task done.\n");}; + ffrt_function_header_t* queueFunc_t = create_function_wrapper((queueFunc), ffrt_function_kind_queue); + ffrt_queue_submit(queue_handle, queueFunc_t, nullptr); ffrt_queue_attr_destroy(&queue_attr); ffrt_queue_destroy(queue_handle); @@ -962,7 +965,15 @@ The main thread queue is obtained for the FFRT thread to communicate with the ma ##### Example ```{.c} -#include "queue.h" +/** +This use case needs to be tested in HarmonyOS. +*/ +#include "ffrt.h" + +inline void OnePlusForTest(void* data) +{ + *(int*)data += 1; +} int main(int narg, char** argv) { @@ -975,8 +986,7 @@ int main(int narg, char** argv) std::function&& basicFunc = [&result]() { OnePlusForTest(static_cast(&result)); OnePlusForTest(static_cast(&result)); - EXPECT_EQ(result, 2); - usleep(3000); + ffrt_usleep(3000); }; ffrt::task_handle handle = serialQueue->submit_h( @@ -992,12 +1002,6 @@ int main(int narg, char** argv) } ``` -Expected output - -``` -result=1 -``` - #### ffrt_get_current_queue Obtains the ArkTS Worker thread queue. @@ -1021,7 +1025,15 @@ The ArkTS Worker thread queue is obtained for the FFRT thread to communicate wit ##### Example ```{.c} -#include "queue.h" +/** +This use case needs to be tested in HarmonyOS. +*/ +#include "ffrt.h" + +inline void OnePlusForTest(void* data) +{ + *(int*)data += 1; +} int main(int narg, char** argv) { @@ -1034,8 +1046,7 @@ int main(int narg, char** argv) std::function&& basicFunc = [&result]() { OnePlusForTest(static_cast(&result)); OnePlusForTest(static_cast(&result)); - EXPECT_EQ(result, 3); - usleep(3000); + ffrt_usleep(3000); }; ffrt::task_handle handle = serialQueue->submit_h( @@ -1051,12 +1062,6 @@ int main(int narg, char** argv) } ``` -Expected output - -``` -result=1 -``` - ### Concurrent Queue @@ -1091,6 +1096,8 @@ If the concurrency is set to a large value, for example, 100, the actual concurr ##### Example ```{.c} +#include "ffrt.h" + int main(int narg, char** argv) { ffrt_queue_attr_t queue_attr; @@ -1099,6 +1106,7 @@ int main(int narg, char** argv) ffrt_queue_attr_set_max_concurrency(&queue_attr, concurrency); concurrency = ffrt_queue_attr_get_max_concurrency(&queue_attr); ffrt_queue_attr_destroy(&queue_attr); + printf("concurrency=%lu\n", concurrency); return 0; } ``` @@ -1133,6 +1141,8 @@ N/A ##### Example ```{.c} +#include "ffrt.h" + int main(int narg, char** argv) { ffrt_queue_attr_t queue_attr; @@ -1141,6 +1151,7 @@ int main(int narg, char** argv) ffrt_queue_attr_set_max_concurrency(&queue_attr, concurrency); concurrency = ffrt_queue_attr_get_max_concurrency(&queue_attr); ffrt_queue_attr_destroy(&queue_attr); + printf("concurrency=%lu\n", concurrency); return 0; } ``` @@ -1187,14 +1198,17 @@ N/A ##### Example ```{.c} +#include "ffrt.h" + int main(int narg, char** argv) { ffrt_task_attr_t task_attr; (void)ffrt_task_attr_init(&task_attr); - uint64_t priority = 3; + ffrt_queue_priority_t priority = ffrt_queue_priority_idle; ffrt_task_attr_set_queue_priority(&task_attr, priority); priority = ffrt_task_attr_get_queue_priority(&task_attr); ffrt_task_attr_destroy(&task_attr); + printf("priority=%d\n", priority); return 0; } ``` @@ -1229,14 +1243,17 @@ N/A ##### Example ```{.c} +#include "ffrt.h" + int main(int narg, char** argv) { ffrt_task_attr_t task_attr; (void)ffrt_task_attr_init(&task_attr); - uint64_t priority = 3; + ffrt_queue_priority_t priority = ffrt_queue_priority_idle; ffrt_task_attr_set_queue_priority(&task_attr, priority); priority = ffrt_task_attr_get_queue_priority(&task_attr); ffrt_task_attr_destroy(&task_attr); + printf("priority=%d\n", priority); return 0; } ``` @@ -1366,7 +1383,7 @@ static inline void ffrt_submit_c(ffrt_function_t func, const ffrt_function_t aft ffrt_submit_base(ffrt_create_function_wrapper(func, after_func, arg), in_deps, out_deps, attr); } -void ffrt_mutex_task() +void ffrt_mutex_task(void *) { int sum = 0; ffrt_mutex_t mtx; @@ -1380,7 +1397,7 @@ void ffrt_mutex_task() } ffrt_mutex_destroy(&mtx); ffrt_wait(); - printf("sum = %d", sum); + printf("sum = %d\n", sum); } int main(int narg, char** argv) @@ -1486,7 +1503,7 @@ void func1(void* arg) if (ret != ffrt_success) { printf("error\n"); } - printf("a = %d", *(t->a)); + printf("a = %d\n", *(t->a)); } void func2(void* arg) @@ -1551,7 +1568,7 @@ static inline void ffrt_submit_c(ffrt_function_t func, const ffrt_function_t aft ffrt_submit_base(ffrt_create_function_wrapper(func, after_func, arg), in_deps, out_deps, attr); } -void ffrt_cv_task() +void ffrt_cv_task(void *) { ffrt_cond_t cond; int ret = ffrt_cond_init(&cond, NULL); @@ -1621,9 +1638,11 @@ N/A void func(void* arg) { - printf("Time: %s", ctime(&(time_t){time(NULL)})); - ffrt_usleep(2000000); // Suspend for 2 seconds - printf("Time: %s", ctime(&(time_t){time(NULL)})); + time_t current_time = time(NULL); + printf("Time: %s", ctime(¤t_time)); + ffrt_usleep(2000000); + current_time = time(NULL); + printf("Time: %s", ctime(¤t_time)); } typedef struct { @@ -1678,6 +1697,13 @@ int main(int narg, char** argv) } ``` +An output case: + +``` +Time: Tue Aug 13 15:45:30 2024 +Time: Tue Aug 13 15:45:32 2024 +``` + #### ffrt_yield Passes control to other tasks so that they can be executed. If there is no other task that can be executed, this API is invalid. @@ -1751,6 +1777,9 @@ N/A ##### Example ```{.c} +#include +#include +#include "ffrt.h" static void testfun(void *data) { @@ -1768,6 +1797,7 @@ int main(int narg, char** argv) int handle = ffrt_timer_start(ffrt_qos_default, timeout, data, cb, false); usleep(300000); ffrt_timer_stop(ffrt_qos_default, handle); + printf("data: %d\n", x); return 0; } ``` @@ -1775,7 +1805,7 @@ int main(int narg, char** argv) Expected output ``` -data=1 in the callback function testfun +data: 1 ``` #### ffrt_timer_stop @@ -1806,6 +1836,9 @@ N/A ##### Example ```{.c} +#include +#include +#include "ffrt.h" static void testfun(void *data) { @@ -1823,6 +1856,7 @@ int main(int narg, char** argv) int handle = ffrt_timer_start(ffrt_qos_default, timeout, data, cb, false); usleep(300000); ffrt_timer_stop(ffrt_qos_default, handle); + printf("data: %d\n", x); return 0; } ``` @@ -1830,7 +1864,7 @@ int main(int narg, char** argv) Expected output ``` -data=1 in the callback function testfun +data: 1 ``` ### ffrt looper @@ -1863,6 +1897,11 @@ N/A ##### Example ```{.c} +#include +#include +#include +#include "c/loop.h" + int main(int narg, char** argv) { ffrt_queue_attr_t queue_attr; @@ -1871,6 +1910,10 @@ int main(int narg, char** argv) auto loop = ffrt_loop_create(queue_handle); + if (loop != NULL) { + printf("loop is not null.\n"); + } + int ret = ffrt_loop_destroy(loop); ffrt_queue_attr_destroy(&queue_attr); @@ -1882,7 +1925,7 @@ int main(int narg, char** argv) Expected output ``` -A non-null loop object +loop is not null. ``` #### ffrt_loop_destory @@ -1909,6 +1952,11 @@ N/A ##### Example ```{.c} +#include +#include +#include +#include "c/loop.h" + int main(int narg, char** argv) { ffrt_queue_attr_t queue_attr; @@ -1919,6 +1967,10 @@ int main(int narg, char** argv) int ret = ffrt_loop_destroy(loop); + if (ret == 0) { + printf("loop normal destruction."); + } + ffrt_queue_attr_destroy(&queue_attr); ffrt_queue_destroy(queue_handle); return 0; @@ -1928,7 +1980,7 @@ int main(int narg, char** argv) Expected output ``` -**0** if the loop object is destroyed. +loop normal destruction. ``` #### ffrt_loop_run @@ -1955,9 +2007,17 @@ N/A ##### Example ```{.c} +#include +#include +#include +#include "c/loop.h" + void* ThreadFunc(void* p) { int ret = ffrt_loop_run(p); + if (ret == 0) { + printf("loop normal operation."); + } return nullptr; } int main(int narg, char** argv) @@ -1982,7 +2042,7 @@ int main(int narg, char** argv) Expected output ``` -**0** if the loop object is started normally. +loop normal operation. ``` #### ffrt_loop_stop @@ -2009,6 +2069,11 @@ N/A ##### Example ```{.c} +#include +#include +#include +#include "c/loop.h" + void* ThreadFunc(void* p) { int ret = ffrt_loop_run(p); @@ -2083,6 +2148,15 @@ N/A ##### Example ```{.c} +#include +#include +#include +#include +#include +#include +#include "c/loop.h" +#include "ffrt.h" + void* ThreadFunc(void* p) { int ret = ffrt_loop_run(p); @@ -2096,6 +2170,13 @@ static void testfun(void* data) static void (*cb)(void*) = testfun; +void testCallBack(void *data, unsigned int events) {} + +struct TestData { + int fd; + uint64_t expected; +}; + int main(int narg, char** argv) { ffrt_queue_attr_t queue_attr; @@ -2104,8 +2185,8 @@ int main(int narg, char** argv) auto loop = ffrt_loop_create(queue_handle); int result1 = 0; - std::function &&basicFunc1 = [&result1]() {result += 10;}; - ffrt_task_handle_t task1 = ffrt_queue_submit_h(queue_handle, create_function_wrapper(basicFunc1, ffrt_function_kind_queue), nullptr); + std::function &&basicFunc1 = [&result1]() {result1 += 10;}; + ffrt_task_handle_t task1 = ffrt_queue_submit_h(queue_handle, ffrt::create_function_wrapper(basicFunc1, ffrt_function_kind_queue), nullptr); pthread_t thread; pthread_create(&thread, 0, ThreadFunc, loop); @@ -2117,11 +2198,14 @@ int main(int narg, char** argv) uint64_t timeout2 = 10; uint64_t expected = 0xabacadae; - int testFd = evetfd(0, EFD_NONBLOCK | EFD_CLOEXEC); + int testFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); struct TestData testData {.fd = testFd, .expected = expected}; ffrt_timer_t timeHandle = ffrt_loop_timer_start(loop, timeout1, data, cb, false); - ffrt_loop_epoll_ctl(loop, EPOLL_CTL_ADD, testFd, EPOLLIN, (void*)(&testData), testCallBack); + int ret = ffrt_loop_epoll_ctl(loop, EPOLL_CTL_ADD, testFd, EPOLLIN, (void*)(&testData), testCallBack); + if (ret == 0) { + printf("ffrt_loop_epoll_ctl if the operation is successful.\n"); + } ssize_t n = write(testFd, &expected, sizeof(uint64_t)); usleep(25000); ffrt_loop_epoll_ctl(loop, EPOLL_CTL_DEL, testFd, 0, nullptr, nullptr); @@ -2129,7 +2213,7 @@ int main(int narg, char** argv) ffrt_loop_stop(loop); pthread_join(thread, nullptr); ffrt_loop_timer_stop(loop, timeHandle); - int ret = ffrt_loop_destroy(loop); + ret = ffrt_loop_destroy(loop); ffrt_queue_attr_destroy(&queue_attr); ffrt_queue_destroy(queue_handle); @@ -2275,8 +2359,6 @@ libffrt.z.so // Method 1: Use the template. C++ is supported. template struct Function { - template - Function(ffrt_function_header_t h, CT&& c) : header(h), closure(std::forward(c)) {} ffrt_function_header_t header; T closure; }; @@ -2301,8 +2383,10 @@ libffrt.z.so { using function_type = Function>; auto p = ffrt_alloc_auto_managed_function_storage_base(kind); - auto f = - new (p)function_type({ ExecFunctionWrapper, DestroyFunctionWrapper, { 0 } }, std::forward(func)); + auto f = new (p)function_type; + f->header.exec = ExecFunctionWrapper; + f->header.destroy = DestroyFunctionWrapper; + f->closure = std::forward(func); return reinterpret_cast(f); } diff --git a/en/application-dev/napi/ffrt-guidelines.md b/en/application-dev/napi/ffrt-guidelines.md index eeff9fc303d67bc54de8b7380e10d28c99e21387..b165d7bd3d7738c2e34540a0c3bac2536e08dbf4 100644 --- a/en/application-dev/napi/ffrt-guidelines.md +++ b/en/application-dev/napi/ffrt-guidelines.md @@ -776,9 +776,10 @@ int main(int narg, char** argv) Expected output: ``` -hello world, x = 2 +hello handle wait -x = 3 +x = 2 +world, x = 3 ``` @@ -917,8 +918,6 @@ using namespace std; template struct Function { - template - Function(ffrt_function_header_t h, CT&& c) : header(h), closure(std::forward(c)) {} ffrt_function_header_t header; T closure; }; @@ -934,7 +933,7 @@ template void DestroyFunctionWrapper(void* t) { auto f = reinterpret_cast>*>(t); - f->closure = nullptr; + f = nullptr; } template @@ -943,8 +942,10 @@ static inline ffrt_function_header_t* create_function_wrapper(T&& func, { using function_type = Function>; auto p = ffrt_alloc_auto_managed_function_storage_base(kind); - auto f = - new (p)function_type({ ExecFunctionWrapper, DestroyFunctionWrapper, { 0 } }, std::forward(func)); + auto f = new (p)function_type; + f->header.exec = ExecFunctionWrapper; + f->header.destroy = DestroyFunctionWrapper; + f->closure = std::forward(func); return reinterpret_cast(f); } @@ -953,8 +954,9 @@ int main(int narg, char** argv) ffrt_queue_attr_t queue_attr; (void)ffrt_queue_attr_init(&queue_attr); ffrt_queue_t queue_handle = ffrt_queue_create(ffrt_queue_serial, "test_queue", &queue_attr); - - ffrt_queue_submit(queue_handle, create_function_wrapper([]() {printf("Task done.\n");}, ffrt_function_kind_queue), nullptr); + std::function&& queueFunc = [] () {printf("Task done.\n");}; + ffrt_function_header_t* queueFunc_t = create_function_wrapper((queueFunc), ffrt_function_kind_queue); + ffrt_queue_submit(queue_handle, queueFunc_t, nullptr); ffrt_queue_attr_destroy(&queue_attr); ffrt_queue_destroy(queue_handle); @@ -1079,7 +1081,7 @@ static inline void ffrt_submit_c(ffrt_function_t func, const ffrt_function_t aft ffrt_submit_base(ffrt_create_function_wrapper(func, after_func, arg), in_deps, out_deps, attr); } -void ffrt_mutex_task() +void ffrt_mutex_task(void *) { int sum = 0; ffrt_mutex_t mtx; @@ -1093,7 +1095,7 @@ void ffrt_mutex_task() } ffrt_mutex_destroy(&mtx); ffrt_wait(); - printf("sum = %d", sum); + printf("sum = %d\n", sum); } int main(int narg, char** argv) @@ -1199,7 +1201,7 @@ void func1(void* arg) if (ret != ffrt_success) { printf("error\n"); } - printf("a = %d", *(t->a)); + printf("a = %d\n", *(t->a)); } void func2(void* arg) @@ -1264,7 +1266,7 @@ static inline void ffrt_submit_c(ffrt_function_t func, const ffrt_function_t aft ffrt_submit_base(ffrt_create_function_wrapper(func, after_func, arg), in_deps, out_deps, attr); } -void ffrt_cv_task() +void ffrt_cv_task(void *) { ffrt_cond_t cond; int ret = ffrt_cond_init(&cond, NULL); @@ -1336,9 +1338,11 @@ N/A void func(void* arg) { - printf("Time: %s", ctime(&(time_t){time(NULL)})); - ffrt_usleep(2000000); // Suspend for 2 seconds - printf("Time: %s", ctime(&(time_t){time(NULL)})); + time_t current_time = time(NULL); + printf("Time: %s", ctime(¤t_time)); + ffrt_usleep(2000000); + current_time = time(NULL); + printf("Time: %s", ctime(¤t_time)); } typedef struct { @@ -1393,6 +1397,13 @@ int main(int narg, char** argv) } ``` +An output case: + +``` +Time: Tue Aug 13 15:45:30 2024 +Time: Tue Aug 13 15:45:32 2024 +``` + #### ffrt_yield Passes control to other tasks so that they can be executed. If there is no other task that can be executed, this API is invalid. @@ -1446,8 +1457,6 @@ libffrt.z.so // Method 1: Use the template. C++ is supported. template struct Function { - template - Function(ffrt_function_header_t h, CT&& c) : header(h), closure(std::forward(c)) {} ffrt_function_header_t header; T closure; }; @@ -1472,8 +1481,10 @@ libffrt.z.so { using function_type = Function>; auto p = ffrt_alloc_auto_managed_function_storage_base(kind); - auto f = - new (p)function_type({ ExecFunctionWrapper, DestroyFunctionWrapper, { 0 } }, std::forward(func)); + auto f = new (p)function_type; + f->header.exec = ExecFunctionWrapper; + f->header.destroy = DestroyFunctionWrapper; + f->closure = std::forward(func); return reinterpret_cast(f); } diff --git a/zh-cn/application-dev/ffrt/ffrt-development-guideline.md b/zh-cn/application-dev/ffrt/ffrt-development-guideline.md index 825df9e7e46c0381d3d0b6000486e37599170818..7aaaf49e6648ce51852541f16ed8b739176b7318 100644 --- a/zh-cn/application-dev/ffrt/ffrt-development-guideline.md +++ b/zh-cn/application-dev/ffrt/ffrt-development-guideline.md @@ -703,9 +703,10 @@ int main(int narg, char** argv) * 预期的输出为: ``` -hello world, x = 2 +hello handle wait -x = 3 +x = 2 +world, x = 3 ``` @@ -739,7 +740,7 @@ uint64_t ffrt_this_task_get_id(); ##### 样例 ```{.c} -#include "timer.h" +#include "ffrt.h" int main(int narg, char** argv) { @@ -750,8 +751,9 @@ int main(int narg, char** argv) ffrt::submit([=]() { ffrt_qos_t taskQos = ffrt_this_task_get_qos(); + ffrt_timer_cb cb; ffrt_timer_start(taskQos, timeout1, data, cb, false); - usleep(200); + ffrt_usleep(200); }, {}, {}); ffrt::wait(); return 0; @@ -889,8 +891,6 @@ using namespace std; template struct Function { - template - Function(ffrt_function_header_t h, CT&& c) : header(h), closure(std::forward(c)) {} ffrt_function_header_t header; T closure; }; @@ -906,7 +906,7 @@ template void DestroyFunctionWrapper(void* t) { auto f = reinterpret_cast>*>(t); - f->closure = nullptr; + f = nullptr; } template @@ -915,8 +915,10 @@ static inline ffrt_function_header_t* create_function_wrapper(T&& func, { using function_type = Function>; auto p = ffrt_alloc_auto_managed_function_storage_base(kind); - auto f = - new (p)function_type({ ExecFunctionWrapper, DestroyFunctionWrapper, { 0 } }, std::forward(func)); + auto f = new (p)function_type; + f->header.exec = ExecFunctionWrapper; + f->header.destroy = DestroyFunctionWrapper; + f->closure = std::forward(func); return reinterpret_cast(f); } @@ -925,8 +927,9 @@ int main(int narg, char** argv) ffrt_queue_attr_t queue_attr; (void)ffrt_queue_attr_init(&queue_attr); ffrt_queue_t queue_handle = ffrt_queue_create(ffrt_queue_serial, "test_queue", &queue_attr); - - ffrt_queue_submit(queue_handle, create_function_wrapper([]() {printf("Task done.\n");}, ffrt_function_kind_queue), nullptr); + std::function&& queueFunc = [] () {printf("Task done.\n");}; + ffrt_function_header_t* queueFunc_t = create_function_wrapper((queueFunc), ffrt_function_kind_queue); + ffrt_queue_submit(queue_handle, queueFunc_t, nullptr); ffrt_queue_attr_destroy(&queue_attr); ffrt_queue_destroy(queue_handle); @@ -955,7 +958,13 @@ NA ##### 样例 ```{.c} -#include "queue.h" +本用例需要在鸿蒙系统中执行 +#include "ffrt.h" + +inline void OnePlusForTest(void* data) +{ + *(int*)data += 1; +} int main(int narg, char** argv) { @@ -968,8 +977,7 @@ int main(int narg, char** argv) std::function&& basicFunc = [&result]() { OnePlusForTest(static_cast(&result)); OnePlusForTest(static_cast(&result)); - EXPECT_EQ(result, 2); - usleep(3000); + ffrt_usleep(3000); }; ffrt::task_handle handle = serialQueue->submit_h( @@ -985,11 +993,6 @@ int main(int narg, char** argv) } ``` -预期输出为: - -``` -result=1 -``` #### ffrt_get_current_queue
@@ -1013,7 +1016,13 @@ ArkTs Worker线程任务队列。 ##### 样例 ```{.c} -#include "queue.h" +//本用例需要在鸿蒙系统中执行 +#include "ffrt.h" + +inline void OnePlusForTest(void* data) +{ + *(int*)data += 1; +} int main(int narg, char** argv) { @@ -1026,8 +1035,7 @@ int main(int narg, char** argv) std::function&& basicFunc = [&result]() { OnePlusForTest(static_cast(&result)); OnePlusForTest(static_cast(&result)); - EXPECT_EQ(result, 3); - usleep(3000); + ffrt_usleep(3000); }; ffrt::task_handle handle = serialQueue->submit_h( @@ -1043,12 +1051,6 @@ int main(int narg, char** argv) } ``` -预期输出为: - -``` -result=1 -``` - ### 并行队列
@@ -1082,6 +1084,8 @@ FFRT并行队列设置最大并发度,需要注意的是,当设置很大的 ##### 样例 ```{.c} +#include "ffrt.h" + int main(int narg, char** argv) { ffrt_queue_attr_t queue_attr; @@ -1090,6 +1094,7 @@ int main(int narg, char** argv) ffrt_queue_attr_set_max_concurrency(&queue_attr, concurrency); concurrency = ffrt_queue_attr_get_max_concurrency(&queue_attr); ffrt_queue_attr_destroy(&queue_attr); + printf("concurrency=%lu\n", concurrency); return 0; } ``` @@ -1124,6 +1129,8 @@ int ffrt_queue_attr_get_max_concurrency(const ffrt_queue_attr_t* attr); ##### 样例 ```{.c} +#include "ffrt.h" + int main(int narg, char** argv) { ffrt_queue_attr_t queue_attr; @@ -1132,6 +1139,7 @@ int main(int narg, char** argv) ffrt_queue_attr_set_max_concurrency(&queue_attr, concurrency); concurrency = ffrt_queue_attr_get_max_concurrency(&queue_attr); ffrt_queue_attr_destroy(&queue_attr); + printf("concurrency=%lu\n", concurrency); return 0; } ``` @@ -1178,14 +1186,17 @@ NA ##### 样例 ```{.c} +#include "ffrt.h" + int main(int narg, char** argv) { ffrt_task_attr_t task_attr; (void)ffrt_task_attr_init(&task_attr); - uint64_t priority = 3; + ffrt_queue_priority_t priority = ffrt_queue_priority_idle; ffrt_task_attr_set_queue_priority(&task_attr, priority); priority = ffrt_task_attr_get_queue_priority(&task_attr); ffrt_task_attr_destroy(&task_attr); + printf("priority=%d\n", priority); return 0; } ``` @@ -1220,14 +1231,17 @@ ffrt_queue_priority_t ffrt_task_attr_get_queue_priority(const ffrt_task_attr_t* ##### 样例 ```{.c} +#include "ffrt.h" + int main(int narg, char** argv) { ffrt_task_attr_t task_attr; (void)ffrt_task_attr_init(&task_attr); - uint64_t priority = 3; + ffrt_queue_priority_t priority = ffrt_queue_priority_idle; ffrt_task_attr_set_queue_priority(&task_attr, priority); priority = ffrt_task_attr_get_queue_priority(&task_attr); ffrt_task_attr_destroy(&task_attr); + printf("priority=%d\n", priority); return 0; } ``` @@ -1357,7 +1371,7 @@ static inline void ffrt_submit_c(ffrt_function_t func, const ffrt_function_t aft ffrt_submit_base(ffrt_create_function_wrapper(func, after_func, arg), in_deps, out_deps, attr); } -void ffrt_mutex_task() +void ffrt_mutex_task(void *) { int sum = 0; ffrt_mutex_t mtx; @@ -1371,7 +1385,7 @@ void ffrt_mutex_task() } ffrt_mutex_destroy(&mtx); ffrt_wait(); - printf("sum = %d", sum); + printf("sum = %d\n", sum); } int main(int narg, char** argv) @@ -1478,7 +1492,7 @@ void func1(void* arg) if (ret != ffrt_success) { printf("error\n"); } - printf("a = %d", *(t->a)); + printf("a = %d\n", *(t->a)); } void func2(void* arg) @@ -1543,7 +1557,7 @@ static inline void ffrt_submit_c(ffrt_function_t func, const ffrt_function_t aft ffrt_submit_base(ffrt_create_function_wrapper(func, after_func, arg), in_deps, out_deps, attr); } -void ffrt_cv_task() +void ffrt_cv_task(void *) { ffrt_cond_t cond; int ret = ffrt_cond_init(&cond, NULL); @@ -1614,9 +1628,11 @@ int ffrt_usleep(uint64_t usec); void func(void* arg) { - printf("Time: %s", ctime(&(time_t){time(NULL)})); + time_t current_time = time(NULL); + printf("Time: %s", ctime(¤t_time)); ffrt_usleep(2000000); // 睡眠 2 秒 - printf("Time: %s", ctime(&(time_t){time(NULL)})); + current_time = time(NULL); + printf("Time: %s", ctime(¤t_time)); } typedef struct { @@ -1671,6 +1687,13 @@ int main(int narg, char** argv) } ``` +一种输出情况为: + +``` +Time: Tue Aug 13 15:45:30 2024 +Time: Tue Aug 13 15:45:32 2024 +``` + #### ffrt_yield
* 当前task 主动让出CPU 执行资源,让其他可以被执行的task 先执行,如果没有其他可被执行的task,yield 无效。 @@ -1744,6 +1767,9 @@ ffrt_timer_t ffrt_timer_start(ffrt_qos_t qos, uint64_t timeout, void* data, ffrt ##### 样例 ```{.c} +#include +#include +#include "ffrt.h" static void testfun(void *data) { @@ -1761,6 +1787,7 @@ int main(int narg, char** argv) int handle = ffrt_timer_start(ffrt_qos_default, timeout, data, cb, false); usleep(300000); ffrt_timer_stop(ffrt_qos_default, handle); + printf("data: %d\n", x); return 0; } ``` @@ -1768,7 +1795,7 @@ int main(int narg, char** argv) 预期输出为: ``` -回调函数testfun中的 data=1 +data: 1 ``` #### ffrt_timer_stop @@ -1799,6 +1826,9 @@ int ffrt_timer_stop(ffrt_qos_t qos, ffrt_timer_t handle); ##### 样例 ```{.c} +#include +#include +#include "ffrt.h" static void testfun(void *data) { @@ -1816,6 +1846,7 @@ int main(int narg, char** argv) int handle = ffrt_timer_start(ffrt_qos_default, timeout, data, cb, false); usleep(300000); ffrt_timer_stop(ffrt_qos_default, handle); + printf("data: %d\n", x); return 0; } ``` @@ -1823,7 +1854,7 @@ int main(int narg, char** argv) 预期输出为: ``` -回调函数testfun中的 data=1 +data: 1 ``` ### ffrt looper @@ -1856,6 +1887,11 @@ loop对象。 ##### 样例 ```{.c} +#include +#include +#include +#include "c/loop.h" + int main(int narg, char** argv) { ffrt_queue_attr_t queue_attr; @@ -1864,6 +1900,10 @@ int main(int narg, char** argv) auto loop = ffrt_loop_create(queue_handle); + if (loop != NULL) { + printf("loop is not null.\n"); + } + int ret = ffrt_loop_destroy(loop); ffrt_queue_attr_destroy(&queue_attr); @@ -1875,7 +1915,7 @@ int main(int narg, char** argv) 预期输出为: ``` -创建的loop对象不为空。 +loop is not null. ``` #### ffrt_loop_destory @@ -1902,6 +1942,11 @@ int ffrt_loop_destroy(ffrt_loop_t loop); ##### 样例 ```{.c} +#include +#include +#include +#include "c/loop.h" + int main(int narg, char** argv) { ffrt_queue_attr_t queue_attr; @@ -1912,6 +1957,10 @@ int main(int narg, char** argv) int ret = ffrt_loop_destroy(loop); + if (ret == 0) { + printf("loop normal destruction."); + } + ffrt_queue_attr_destroy(&queue_attr); ffrt_queue_destroy(queue_handle); return 0; @@ -1921,7 +1970,7 @@ int main(int narg, char** argv) 预期输出为: ``` -正常销毁loop对象,返回值是0。 +loop normal destruction. ``` #### ffrt_loop_run @@ -1948,9 +1997,17 @@ int ffrt_loop_run(ffrt_loop_t loop); ##### 样例 ```{.c} +#include +#include +#include +#include "c/loop.h" + void* ThreadFunc(void* p) { int ret = ffrt_loop_run(p); + if (ret == 0) { + printf("loop normal operation."); + } return nullptr; } int main(int narg, char** argv) @@ -1975,7 +2032,7 @@ int main(int narg, char** argv) 预期输出为: ``` -正常启动loop对象,返回值是0。 +loop normal operation. ``` #### ffrt_loop_stop @@ -2002,6 +2059,11 @@ NA。 ##### 样例 ```{.c} +#include +#include +#include +#include "c/loop.h" + void* ThreadFunc(void* p) { int ret = ffrt_loop_run(p); @@ -2076,6 +2138,15 @@ int ffrt_loop_epoll_ctl(ffrt_loop_t loop, int op, int fd, uint32_t events, void ##### 样例 ```{.c} +#include +#include +#include +#include +#include +#include +#include "c/loop.h" +#include "ffrt.h" + void* ThreadFunc(void* p) { int ret = ffrt_loop_run(p); @@ -2089,6 +2160,13 @@ static void testfun(void* data) static void (*cb)(void*) = testfun; +void testCallBack(void *data, unsigned int events) {} + +struct TestData { + int fd; + uint64_t expected; +}; + int main(int narg, char** argv) { ffrt_queue_attr_t queue_attr; @@ -2097,8 +2175,8 @@ int main(int narg, char** argv) auto loop = ffrt_loop_create(queue_handle); int result1 = 0; - std::function &&basicFunc1 = [&result1]() {result += 10;}; - ffrt_task_handle_t task1 = ffrt_queue_submit_h(queue_handle, create_function_wrapper(basicFunc1, ffrt_function_kind_queue), nullptr); + std::function &&basicFunc1 = [&result1]() {result1 += 10;}; + ffrt_task_handle_t task1 = ffrt_queue_submit_h(queue_handle, ffrt::create_function_wrapper(basicFunc1, ffrt_function_kind_queue), nullptr); pthread_t thread; pthread_create(&thread, 0, ThreadFunc, loop); @@ -2110,11 +2188,14 @@ int main(int narg, char** argv) uint64_t timeout2 = 10; uint64_t expected = 0xabacadae; - int testFd = evetfd(0, EFD_NONBLOCK | EFD_CLOEXEC); + int testFd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); struct TestData testData {.fd = testFd, .expected = expected}; ffrt_timer_t timeHandle = ffrt_loop_timer_start(loop, timeout1, data, cb, false); - ffrt_loop_epoll_ctl(loop, EPOLL_CTL_ADD, testFd, EPOLLIN, (void*)(&testData), testCallBack); + int ret = ffrt_loop_epoll_ctl(loop, EPOLL_CTL_ADD, testFd, EPOLLIN, (void*)(&testData), testCallBack); + if (ret == 0) { + printf("ffrt_loop_epoll_ctl执行成功。\n"); + } ssize_t n = write(testFd, &expected, sizeof(uint64_t)); usleep(25000); ffrt_loop_epoll_ctl(loop, EPOLL_CTL_DEL, testFd, 0, nullptr, nullptr); @@ -2122,7 +2203,7 @@ int main(int narg, char** argv) ffrt_loop_stop(loop); pthread_join(thread, nullptr); ffrt_loop_timer_stop(loop, timeHandle); - int ret = ffrt_loop_destroy(loop); + ret = ffrt_loop_destroy(loop); ffrt_queue_attr_destroy(&queue_attr); ffrt_queue_destroy(queue_handle); @@ -2268,8 +2349,6 @@ libffrt.z.so // 第一种使用模板,支持C++ template struct Function { - template - Function(ffrt_function_header_t h, CT&& c) : header(h), closure(std::forward(c)) {} ffrt_function_header_t header; T closure; }; @@ -2294,8 +2373,10 @@ libffrt.z.so { using function_type = Function>; auto p = ffrt_alloc_auto_managed_function_storage_base(kind); - auto f = - new (p)function_type({ ExecFunctionWrapper, DestroyFunctionWrapper, { 0 } }, std::forward(func)); + auto f = new (p)function_type; + f->header.exec = ExecFunctionWrapper; + f->header.destroy = DestroyFunctionWrapper; + f->closure = std::forward(func); return reinterpret_cast(f); } diff --git a/zh-cn/application-dev/napi/ffrt-guidelines.md b/zh-cn/application-dev/napi/ffrt-guidelines.md index cfdf0ac9593396705156b61a9e906718bcb83446..4e2f2e0bd911f6ae7ad36582d963b48eab3e98b6 100644 --- a/zh-cn/application-dev/napi/ffrt-guidelines.md +++ b/zh-cn/application-dev/napi/ffrt-guidelines.md @@ -770,9 +770,10 @@ int main(int narg, char** argv) * 预期的输出为: ``` -hello world, x = 2 +hello handle wait -x = 3 +x = 2 +world, x = 3 ``` @@ -911,8 +912,6 @@ using namespace std; template struct Function { - template - Function(ffrt_function_header_t h, CT&& c) : header(h), closure(std::forward(c)) {} ffrt_function_header_t header; T closure; }; @@ -928,7 +927,7 @@ template void DestroyFunctionWrapper(void* t) { auto f = reinterpret_cast>*>(t); - f->closure = nullptr; + f = nullptr; } template @@ -937,8 +936,10 @@ static inline ffrt_function_header_t* create_function_wrapper(T&& func, { using function_type = Function>; auto p = ffrt_alloc_auto_managed_function_storage_base(kind); - auto f = - new (p)function_type({ ExecFunctionWrapper, DestroyFunctionWrapper, { 0 } }, std::forward(func)); + auto f = new (p)function_type; + f->header.exec = ExecFunctionWrapper; + f->header.destroy = DestroyFunctionWrapper; + f->closure = std::forward(func); return reinterpret_cast(f); } @@ -947,8 +948,9 @@ int main(int narg, char** argv) ffrt_queue_attr_t queue_attr; (void)ffrt_queue_attr_init(&queue_attr); ffrt_queue_t queue_handle = ffrt_queue_create(ffrt_queue_serial, "test_queue", &queue_attr); - - ffrt_queue_submit(queue_handle, create_function_wrapper([]() {printf("Task done.\n");}, ffrt_function_kind_queue), nullptr); + std::function&& queueFunc = [] () {printf("Task done.\n");}; + ffrt_function_header_t* queueFunc_t = create_function_wrapper((queueFunc), ffrt_function_kind_queue); + ffrt_queue_submit(queue_handle, queueFunc_t, nullptr); ffrt_queue_attr_destroy(&queue_attr); ffrt_queue_destroy(queue_handle); @@ -1073,7 +1075,7 @@ static inline void ffrt_submit_c(ffrt_function_t func, const ffrt_function_t aft ffrt_submit_base(ffrt_create_function_wrapper(func, after_func, arg), in_deps, out_deps, attr); } -void ffrt_mutex_task() +void ffrt_mutex_task(void *) { int sum = 0; ffrt_mutex_t mtx; @@ -1087,7 +1089,7 @@ void ffrt_mutex_task() } ffrt_mutex_destroy(&mtx); ffrt_wait(); - printf("sum = %d", sum); + printf("sum = %d\n", sum); } int main(int narg, char** argv) @@ -1194,7 +1196,7 @@ void func1(void* arg) if (ret != ffrt_success) { printf("error\n"); } - printf("a = %d", *(t->a)); + printf("a = %d\n", *(t->a)); } void func2(void* arg) @@ -1259,7 +1261,7 @@ static inline void ffrt_submit_c(ffrt_function_t func, const ffrt_function_t aft ffrt_submit_base(ffrt_create_function_wrapper(func, after_func, arg), in_deps, out_deps, attr); } -void ffrt_cv_task() +void ffrt_cv_task(void *) { ffrt_cond_t cond; int ret = ffrt_cond_init(&cond, NULL); @@ -1332,9 +1334,11 @@ int ffrt_usleep(uint64_t usec); void func(void* arg) { - printf("Time: %s", ctime(&(time_t){time(NULL)})); + time_t current_time = time(NULL); + printf("Time: %s", ctime(¤t_time)); ffrt_usleep(2000000); // 睡眠 2 秒 - printf("Time: %s", ctime(&(time_t){time(NULL)})); + current_time = time(NULL); + printf("Time: %s", ctime(¤t_time)); } typedef struct { @@ -1389,6 +1393,13 @@ int main(int narg, char** argv) } ``` +一种输出情况为: + +``` +Time: Tue Aug 13 15:45:30 2024 +Time: Tue Aug 13 15:45:32 2024 +``` + #### ffrt_yield
* 当前task 主动让出CPU 执行资源,让其他可以被执行的task 先执行,如果没有其他可被执行的task,yield 无效。 @@ -1442,8 +1453,6 @@ libffrt.z.so // 第一种使用模板,支持C++ template struct Function { - template - Function(ffrt_function_header_t h, CT&& c) : header(h), closure(std::forward(c)) {} ffrt_function_header_t header; T closure; }; @@ -1468,8 +1477,10 @@ libffrt.z.so { using function_type = Function>; auto p = ffrt_alloc_auto_managed_function_storage_base(kind); - auto f = - new (p)function_type({ ExecFunctionWrapper, DestroyFunctionWrapper, { 0 } }, std::forward(func)); + auto f = new (p)function_type; + f->header.exec = ExecFunctionWrapper; + f->header.destroy = DestroyFunctionWrapper; + f->closure = std::forward(func); return reinterpret_cast(f); }