1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 use std::ffi::{CStr, CString}; 15 16 use libc::{c_char, c_void}; 17 18 use super::*; 19 20 type FfrtHook = extern "C" fn(*mut c_void); 21 22 type FfrtExecHook = extern "C" fn(*mut c_void) -> FfrtRet; 23 24 type RawTaskCtx = *mut c_void; 25 26 #[repr(C)] 27 pub enum FfrtRet { 28 FfrtCoroutinePending, 29 FfrtCoroutineReady, 30 } 31 32 impl Default for FfrtTaskAttr { default() -> Self33 fn default() -> Self { 34 Self::new() 35 } 36 } 37 38 impl FfrtTaskAttr { 39 /// Creates a default task attribute. new() -> Self40 pub fn new() -> Self { 41 Self { storage: [0; 128] } 42 } 43 44 /// Initializes the task attribute init(&mut self)45 pub fn init(&mut self) { 46 let attr = self as *mut FfrtTaskAttr; 47 unsafe { 48 ffrt_task_attr_init(attr); 49 } 50 } 51 52 /// Sets the name for the task attribute. set_name(&mut self, name: &str) -> &mut Self53 pub fn set_name(&mut self, name: &str) -> &mut Self { 54 let attr_ptr = self as *mut FfrtTaskAttr; 55 let c_name = CString::new(name).expect("FfrtTaskAttr::set_name failed"); 56 unsafe { 57 ffrt_task_attr_set_name(attr_ptr, c_name.as_ptr()); 58 } 59 self 60 } 61 62 /// Gets the name from the task attribtue. get_name(&self) -> String63 pub fn get_name(&self) -> String { 64 let attr_ptr = self as *const FfrtTaskAttr; 65 unsafe { 66 let c_name = ffrt_task_attr_get_name(attr_ptr); 67 CStr::from_ptr(c_name) 68 .to_str() 69 .expect("FfrtTaskAttr::get_name failed") 70 .to_string() 71 } 72 } 73 74 /// Sets qos level for the task attribute. set_qos(&mut self, qos: Qos) -> &mut Self75 pub fn set_qos(&mut self, qos: Qos) -> &mut Self { 76 unsafe { 77 let ptr = self as *mut FfrtTaskAttr; 78 ffrt_task_attr_set_qos(ptr, qos); 79 } 80 self 81 } 82 83 /// Gets the qos level from the task attribute. get_qos(&self) -> Qos84 pub fn get_qos(&self) -> Qos { 85 unsafe { ffrt_task_attr_get_qos(self as _) } 86 } 87 } 88 89 impl Drop for FfrtTaskAttr { drop(&mut self)90 fn drop(&mut self) { 91 unsafe { 92 ffrt_task_attr_destroy(self as _); 93 } 94 } 95 } 96 97 #[link(name = "ffrt")] 98 // task.h 99 extern "C" { 100 #![allow(unused)] 101 ffrt_task_attr_init(attr: *mut FfrtTaskAttr)102 fn ffrt_task_attr_init(attr: *mut FfrtTaskAttr); ffrt_task_attr_set_name(attr: *mut FfrtTaskAttr, name: *const c_char)103 fn ffrt_task_attr_set_name(attr: *mut FfrtTaskAttr, name: *const c_char); ffrt_task_attr_get_name(attr: *const FfrtTaskAttr) -> *const c_char104 fn ffrt_task_attr_get_name(attr: *const FfrtTaskAttr) -> *const c_char; ffrt_task_attr_destroy(attr: *mut FfrtTaskAttr)105 fn ffrt_task_attr_destroy(attr: *mut FfrtTaskAttr); ffrt_task_attr_set_qos(attr: *mut FfrtTaskAttr, qos: Qos)106 fn ffrt_task_attr_set_qos(attr: *mut FfrtTaskAttr, qos: Qos); ffrt_task_attr_get_qos(attr: *const FfrtTaskAttr) -> Qos107 fn ffrt_task_attr_get_qos(attr: *const FfrtTaskAttr) -> Qos; 108 109 // submit ffrt_alloc_auto_free_function_storage_base() -> *const c_void110 fn ffrt_alloc_auto_free_function_storage_base() -> *const c_void; 111 112 /// Submits a task. ffrt_submit_coroutine( data: *mut c_void, fp: FfrtExecHook, destroy_fp: FfrtHook, in_deps: *const FfrtDeps, out_deps: *const FfrtDeps, attr: *const FfrtTaskAttr, )113 pub fn ffrt_submit_coroutine( 114 // void* callable 115 data: *mut c_void, 116 // ffrt_function_tdd exec 117 fp: FfrtExecHook, 118 // ffrt_function_t destroy 119 destroy_fp: FfrtHook, 120 // const ffrt_deps_t* out_deps, 121 in_deps: *const FfrtDeps, 122 // const ffrt_deps_t* out_deps, 123 out_deps: *const FfrtDeps, 124 // const ffrt_task_attr_t* att 125 attr: *const FfrtTaskAttr, 126 ); 127 128 /// Gets the current task context. ffrt_get_current_task() -> RawTaskCtx129 pub fn ffrt_get_current_task() -> RawTaskCtx; 130 131 /// Wakes the task ffrt_wake_coroutine(task: RawTaskCtx)132 pub fn ffrt_wake_coroutine(task: RawTaskCtx); 133 } 134