diff --git a/resourceschedule/ffrt/BUILD.gn b/resourceschedule/ffrt/BUILD.gn index 9d6f81c76e9c21c93036b2366b72466035dfc9d5..8fb7e35b645fca609ace368aba41a469e2487ded 100644 --- a/resourceschedule/ffrt/BUILD.gn +++ b/resourceschedule/ffrt/BUILD.gn @@ -1,41 +1,45 @@ -# Copyright (c) 2023 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and # limitations under the License. - -import("//build/ohos.gni") -import("//build/ohos/ndk/ndk.gni") - -ohos_ndk_headers("ffrt_header") { - dest_dir = "$ndk_headers_out_dir/ffrt" - sources = [ - "c/condition_variable.h", - "c/mutex.h", - "c/queue.h", - "c/sleep.h", - "c/task.h", - "c/type_def.h", - ] -} - -ohos_ndk_library("libffrt_ndk") { - output_name = "ffrt" - ndk_description_file = "./ffrt.ndk.json" - system_capability = "SystemCapability.Resourceschedule.Ffrt.Core" - system_capability_headers = [ - "ffrt/condition_variable.h", - "ffrt/mutex.h", - "ffrt/queue.h", - "ffrt/sleep.h", - "ffrt/task.h", - "ffrt/type_def.h", - ] -} + +import("//build/ohos.gni") +import("//build/ohos/ndk/ndk.gni") + +ohos_ndk_headers("ffrt_header") { + dest_dir = "$ndk_headers_out_dir/ffrt" + sources = [ + "c/condition_variable.h", + "c/loop.h", + "c/mutex.h", + "c/queue.h", + "c/sleep.h", + "c/task.h", + "c/timer.h", + "c/type_def.h", + ] +} + +ohos_ndk_library("libffrt_ndk") { + output_name = "ffrt" + ndk_description_file = "./ffrt.ndk.json" + system_capability = "SystemCapability.Resourceschedule.Ffrt.Core" + system_capability_headers = [ + "ffrt/condition_variable.h", + "ffrt/loop.h", + "ffrt/mutex.h", + "ffrt/queue.h", + "ffrt/sleep.h", + "ffrt/task.h", + "ffrt/timer.h", + "ffrt/type_def.h", + ] +} diff --git a/resourceschedule/ffrt/c/loop.h b/resourceschedule/ffrt/c/loop.h new file mode 100644 index 0000000000000000000000000000000000000000..f6e9d4aa65294f8cb8a95bfa59039f1b26ef7bd6 --- /dev/null +++ b/resourceschedule/ffrt/c/loop.h @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FFRT_API_C_LOOP_H +#define FFRT_API_C_LOOP_H + +#include "queue.h" +#include "type_def.h" + +typedef void* ffrt_loop_t; + +/** + * @brief Creates a loop. + * + * @param queue Indicates a queue. + * @return Returns a non-null loop handle if the loop is created; + returns a null pointer otherwise. + * @since 12 + * @version 1.0 + */ +FFRT_C_API ffrt_loop_t ffrt_loop_create(ffrt_queue_t queue); + +/** + * @brief Destroys a loop. + * + * @param loop Indicates a loop handle. + * @return returns 0 if the loop is destroyed; + returns -1 otherwise. + * @since 12 + * @version 1.0 + */ +FFRT_C_API int ffrt_loop_destroy(ffrt_loop_t loop); + +/** + * @brief start loop run. + * + * @param loop Indicates a loop handle. + * @return returns -1 if loop run fail; + returns 0 otherwise. + * @since 12 + * @version 1.0 + */ +FFRT_C_API int ffrt_loop_run(ffrt_loop_t loop); + +/** + * @brief stop loop run. + * + * @param loop Indicates a loop handle. + * @since 12 + * @version 1.0 + */ +FFRT_C_API void ffrt_loop_stop(ffrt_loop_t loop); + +/** + * @brief control an epoll file descriptor on ffrt loop + * + * @param loop Indicates a loop handle. + * @param op Indicates operation on the target file descriptor. + * @param fd Indicates the target file descriptor on which to perform the operation. + * @param events Indicates the event type associated with the target file descriptor. + * @param data Indicates user data used in cb. + * @param cb Indicates user cb which will be executed when the target fd is polled. + * @return Returns 0 if success; + returns -1 otherwise. + * @since 12 + * @version 1.0 + */ +FFRT_C_API int ffrt_loop_epoll_ctl(ffrt_loop_t loop, int op, int fd, uint32_t events, void *data, ffrt_poller_cb cb); + +/** + * @brief Start a timer on ffrt loop + * + * @param loop Indicates a loop handle. + * @param timeout Indicates the number of milliseconds that specifies timeout. + * @param data Indicates user data used in cb. + * @param cb Indicates user cb which will be executed when timeout. + * @param repeat Indicates whether to repeat this timer. + * @return Returns a timer handle. + * @since 12 + * @version 1.0 + */ +FFRT_C_API ffrt_timer_t ffrt_loop_timer_start( + ffrt_loop_t loop, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat); + +/** + * @brief Stop a target timer on ffrt loop + * + * @param loop Indicates a loop handle. + * @param handle Indicates the target timer handle. + * @return Returns 0 if success; + returns -1 otherwise. + * @since 12 + * @version 1.0 + */ +FFRT_C_API int ffrt_loop_timer_stop(ffrt_loop_t loop, ffrt_timer_t handle); + +#endif \ No newline at end of file diff --git a/resourceschedule/ffrt/c/queue.h b/resourceschedule/ffrt/c/queue.h index 0ee3d375a99208edd67dfdc2540fc734b9dba3de..861868185f97a54772a865b78baeac2d81af15c2 100644 --- a/resourceschedule/ffrt/c/queue.h +++ b/resourceschedule/ffrt/c/queue.h @@ -39,7 +39,12 @@ #include "type_def.h" -typedef enum { ffrt_queue_serial, ffrt_queue_max } ffrt_queue_type_t; +typedef enum { + ffrt_queue_serial, + ffrt_queue_concurrent, + ffrt_queue_max +} ffrt_queue_type_t; + typedef void* ffrt_queue_t; /** @@ -122,6 +127,26 @@ FFRT_C_API void ffrt_queue_attr_set_callback(ffrt_queue_attr_t* attr, ffrt_funct */ FFRT_C_API ffrt_function_header_t* ffrt_queue_attr_get_callback(const ffrt_queue_attr_t* attr); +/** + * @brief Set the queue max concurrency. + * + * @param attr Queue Property Pointer. + * @param max_concurrency queue max_concurrency. + * @since 12 + * @version 1.0 + */ +FFRT_C_API void ffrt_queue_attr_set_max_concurrency(ffrt_queue_attr_t* attr, const int max_concurrency); + +/** + * @brief Get the queue max concurrency. + * + * @param attr Queue Property Pointer. + * @return Returns the queue max concurrency. + * @since 12 + * @version 1.0 + */ +FFRT_C_API int ffrt_queue_attr_get_max_concurrency(const ffrt_queue_attr_t* attr); + /** * @brief Creates a queue. * @@ -189,4 +214,22 @@ FFRT_C_API void ffrt_queue_wait(ffrt_task_handle_t handle); */ FFRT_C_API int ffrt_queue_cancel(ffrt_task_handle_t handle); +/** + * @brief Get application main thread queue. + * + * @return Returns application main thread queue. + * @since 12 + * @version 1.0 + */ +FFRT_C_API ffrt_queue_t ffrt_get_main_queue(); + +/** + * @brief Get application worker(ArkTs) thread queue. + * + * @return Returns application worker(ArkTs) thread queue. + * @since 12 + * @version 1.0 + */ +FFRT_C_API ffrt_queue_t ffrt_get_current_queue(); + #endif // FFRT_API_C_QUEUE_H \ No newline at end of file diff --git a/resourceschedule/ffrt/c/sleep.h b/resourceschedule/ffrt/c/sleep.h index 121ab7b732695cf0223f7e811c77ab1b4dac24e9..6cc9055200fbaf55c5859b64989a433cefce79ed 100644 --- a/resourceschedule/ffrt/c/sleep.h +++ b/resourceschedule/ffrt/c/sleep.h @@ -36,6 +36,7 @@ */ #ifndef FFRT_API_C_SLEEP_H #define FFRT_API_C_SLEEP_H +#include #include "type_def.h" /** diff --git a/resourceschedule/ffrt/c/task.h b/resourceschedule/ffrt/c/task.h index c6fd7f42c9abc982ed41e364bc03df1218ea1c9a..979fe43757ab09671e183457962b0e0ea361e4c1 100644 --- a/resourceschedule/ffrt/c/task.h +++ b/resourceschedule/ffrt/c/task.h @@ -36,6 +36,7 @@ */ #ifndef FFRT_API_C_TASK_H #define FFRT_API_C_TASK_H +#include #include "type_def.h" /** @@ -119,6 +120,26 @@ FFRT_C_API void ffrt_task_attr_set_delay(ffrt_task_attr_t* attr, uint64_t delay_ */ FFRT_C_API uint64_t ffrt_task_attr_get_delay(const ffrt_task_attr_t* attr); +/** + * @brief Sets the task priority. + * + * @param attr Indicates a pointer to the task attribute. + * @param priority Indicates the execute priority of concurrent queue task. + * @since 12 + * @version 1.0 + */ +FFRT_C_API void ffrt_task_attr_set_queue_priority(ffrt_task_attr_t* attr, ffrt_queue_priority_t priority); + +/** + * @brief Obtains the task priority. + * + * @param attr Indicates a pointer to the task attribute. + * @return Returns the priority of concurrent queue task. + * @since 12 + * @version 1.0 + */ +FFRT_C_API ffrt_queue_priority_t ffrt_task_attr_get_queue_priority(const ffrt_task_attr_t* attr); + /** * @brief Updates the QoS of this task. * @@ -130,6 +151,15 @@ FFRT_C_API uint64_t ffrt_task_attr_get_delay(const ffrt_task_attr_t* attr); */ FFRT_C_API int ffrt_this_task_update_qos(ffrt_qos_t qos); +/** + * @brief Obtains the qos of this task. + * + * @return Returns the task qos. + * @since 12 + * @version 1.0 + */ +FFRT_C_API ffrt_qos_t ffrt_this_task_get_qos(); + /** * @brief Obtains the ID of this task. * diff --git a/resourceschedule/ffrt/c/timer.h b/resourceschedule/ffrt/c/timer.h new file mode 100644 index 0000000000000000000000000000000000000000..515866695be3425888ccc13ecbbf5f3bc418c336 --- /dev/null +++ b/resourceschedule/ffrt/c/timer.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup Ffrt + * @{ + * + * @brief ffrt provides APIs. + * + * + * @syscap SystemCapability.Resourceschedule.Ffrt.Core + * + * @since 12 + */ + + /** + * @file timer.h + * + * @brief Declares the timer interfaces in C. + * + * @syscap SystemCapability.Resourceschedule.Ffrt.Core + * @since 12 + * @version 1.0 + */ +#ifndef FFRT_API_C_TIMER_H +#define FFRT_API_C_TIMER_H +#include + +#include "type_def.h" + +/** + * @brief Start a timer on ffrt worker + * + * @param qos Indicates qos of the worker that runs timer. + * @param timeout Indicates the number of milliseconds that specifies timeout. + * @param data Indicates user data used in cb. + * @param cb Indicates user cb which will be executed when timeout. + * @param repeat Indicates whether to repeat this timer. + * @return Returns a timer handle. + * @since 12 + * @version 1.0 + */ +FFRT_C_API ffrt_timer_t ffrt_timer_start(ffrt_qos_t qos, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat); + +/** + * @brief Stop a target timer on ffrt worker + * + * @param qos Indicates qos of the worker that runs timer. + * @param handle Indicates the target timer handle. + * @return Returns 0 if success; + returns -1 otherwise. + * @since 12 + * @version 1.0 + */ +FFRT_C_API int ffrt_timer_stop(ffrt_qos_t qos, ffrt_timer_t handle); +#endif \ No newline at end of file diff --git a/resourceschedule/ffrt/c/type_def.h b/resourceschedule/ffrt/c/type_def.h index 1cedf23b90c445f0aca98f937f39a9614ed3d66f..66506f85d6c95dbe7945ccee1c39e06cde14105f 100644 --- a/resourceschedule/ffrt/c/type_def.h +++ b/resourceschedule/ffrt/c/type_def.h @@ -45,6 +45,21 @@ #define FFRT_C_API #endif +/** + * @brief Enumerates the task priority types. + * + */ +typedef enum { + /** should be distributed at once if possible, handle time equals to send time, prior to high level */ + ffrt_queue_priority_immediate, + /** high priority, sorted by handle time, prior to low level. */ + ffrt_queue_priority_high, + /** low priority, sorted by handle time, prior to idle level. */ + ffrt_queue_priority_low, + /** lowest priority, sorted by handle time, only distribute when there is no other level inside queue. */ + ffrt_queue_priority_idle, +} ffrt_queue_priority_t; + /** * @brief Enumerates the task QoS types. * @@ -103,7 +118,7 @@ typedef enum { /** General task. */ ffrt_function_kind_general, /** Queue task. */ - ffrt_function_kind_queue + ffrt_function_kind_queue, } ffrt_function_kind_t; /** @@ -174,6 +189,13 @@ typedef struct { uint32_t storage[(ffrt_cond_storage_size + sizeof(uint32_t) - 1) / sizeof(uint32_t)]; } ffrt_cond_t; +typedef void (*ffrt_poller_cb)(void* data, uint32_t event); + +typedef void (*ffrt_timer_cb)(void* data); + +typedef int ffrt_timer_t; + + #ifdef __cplusplus namespace ffrt { enum qos_default { @@ -184,6 +206,7 @@ enum qos_default { qos_user_initiated = ffrt_qos_user_initiated, }; using qos = int; + } #endif #endif diff --git a/resourceschedule/ffrt/ffrt.h b/resourceschedule/ffrt/ffrt.h index de48d6f52afb583387c2076187349ecadc0f4fba..32a4ccf54238998d93b9a02de53230f29151a4a0 100644 --- a/resourceschedule/ffrt/ffrt.h +++ b/resourceschedule/ffrt/ffrt.h @@ -20,11 +20,13 @@ #include "cpp/condition_variable.h" #include "cpp/sleep.h" #include "cpp/queue.h" +#include "c/timer.h" #else #include "c/task.h" #include "c/mutex.h" #include "c/condition_variable.h" #include "c/sleep.h" #include "c/queue.h" +#include "c/timer.h" #endif #endif diff --git a/resourceschedule/ffrt/ffrt.ndk.json b/resourceschedule/ffrt/ffrt.ndk.json index 7623541874b87967d217b08af3783ebc0c46325d..ac87d49f85f54cb21d81a19bf277c0baf0600e3d 100644 --- a/resourceschedule/ffrt/ffrt.ndk.json +++ b/resourceschedule/ffrt/ffrt.ndk.json @@ -41,5 +41,21 @@ { "name": "ffrt_submit_h_base" }, { "name": "ffrt_task_handle_destroy" }, { "name": "ffrt_wait_deps" }, - { "name": "ffrt_wait" } + { "name": "ffrt_wait" }, + { "name": "ffrt_loop_create" }, + { "name": "ffrt_loop_destroy" }, + { "name": "ffrt_loop_run" }, + { "name": "ffrt_loop_stop" }, + { "name": "ffrt_loop_epoll_ctl" }, + { "name": "ffrt_loop_timer_start" }, + { "name": "ffrt_loop_timer_stop" }, + { "name": "ffrt_queue_attr_set_max_concurrency" }, + { "name": "ffrt_queue_atte_get_max_concurrency" }, + { "name": "ffrt_qet_main_queue" }, + { "name": "ffrt_get_current_queue" }, + { "name": "ffrt_task_attr_set_queue_priority" }, + { "name": "ffrt_task_attr_get_queue_priority" }, + { "name": "ffrt_this_task_get_qos" }, + { "name": "ffrt_timer_start" }, + { "name": "ffrt_timer_stop" } ] \ No newline at end of file