38 # include <dsn/service_api_c.h> 39 # include <dsn/cpp/auto_codes.h> 40 # include <dsn/cpp/utils.h> 41 # include <dsn/cpp/rpc_stream.h> 42 # include <dsn/cpp/serialization.h> 43 # include <dsn/cpp/zlocks.h> 44 # include <dsn/utility/autoref_ptr.h> 45 # include <dsn/utility/synchronize.h> 46 # include <dsn/utility/link.h> 47 # include <dsn/cpp/callocator.h> 51 # include <dsn/cpp/optional.h> 55 typedef std::function<void(error_code, size_t)> aio_handler;
56 class safe_task_handle;
57 typedef ::dsn::ref_ptr<safe_task_handle> task_ptr;
65 public ::dsn::ref_counter
71 _rpc_response =
nullptr;
78 if (_rpc_response !=
nullptr)
82 void set_task_info(dsn_task_t t)
88 dsn_task_t native_handle()
const 91 virtual bool cancel(
bool wait_until_finished,
bool* finished =
nullptr)
96 void set_delay(
int delay_ms)
106 bool wait(
int timeout_millieseconds)
const 116 size_t io_size()
const 121 void enqueue(std::chrono::milliseconds delay = std::chrono::milliseconds(0))
const 126 void enqueue_aio(
error_code err,
size_t size)
const 131 dsn_message_t response()
133 if (_rpc_response ==
nullptr)
135 return _rpc_response;
138 void enqueue_rpc_response(
error_code err, dsn_message_t resp)
const 145 dsn_message_t _rpc_response;
148 template<
typename THandler>
151 public transient_object
160 virtual bool cancel(
bool wait_until_finished,
bool* finished =
nullptr)
override 162 return safe_task_handle::cancel(wait_until_finished, finished);
165 static void on_cancel(
void*
task)
172 static void exec(
void* task)
175 dbg_dassert(t->_handler.is_some(),
"_handler is missing");
176 t->_handler.unwrap()();
181 static void exec_rpc_response(dsn_error_t err, dsn_message_t req, dsn_message_t resp,
void* task)
184 dbg_dassert(t->_handler.is_some(),
"_handler is missing");
185 t->_handler.unwrap()(err, req, resp);
190 static void exec_aio(dsn_error_t err,
size_t sz,
void* task)
193 dbg_dassert(t->_handler.is_some(),
"_handler is missing");
194 t->_handler.unwrap()(err, sz);
200 dsn::optional<THandler> _handler;
203 template<
typename THandler>
214 virtual bool cancel(
bool wait_until_finished,
bool* finished =
nullptr)
override 216 return safe_task_handle::cancel(wait_until_finished, finished);
219 static void on_cancel(
void*
task)
226 static void exec_timer(
void* task)
229 dbg_dassert(t->_handler.is_some(),
"_handler is missing");
230 t->_handler.unwrap()();
234 dsn::optional<THandler> _handler;
251 template<
typename THandler>
256 : _bound_handler(
nullptr), _handler(h)
260 operator task_ptr()
const 262 return task_ptr(
this);
265 virtual bool cancel(
bool wait_until_finished,
bool* finished =
nullptr)
override 267 bool r = safe_task_handle::cancel(wait_until_finished, finished);
270 _bound_handler =
nullptr;
275 void bind_and_enqueue(
276 std::function<std::function<
void()> (THandler&)> binder,
277 int delay_milliseconds = 0
280 _bound_handler = binder(_handler);
285 static void on_cancel(
void*
task)
291 static void exec(
void* task)
295 t->_bound_handler =
nullptr;
300 std::function<void()> _bound_handler;
Definition: task_helper.h:64
DSN_API bool dsn_task_cancel2(dsn_task_t task, bool wait_until_finished, bool *finished)
cancel a task
DSN_API void dsn_file_task_enqueue(dsn_task_t cb_task, dsn_error_t err, size_t size)
mimic io completion when no io operation is really issued
DSN_API void dsn_task_add_ref(dsn_task_t task)
Add reference count for a task created from dsn_task_create etc.
Definition: auto_codes.h:303
DSN_API void dsn_rpc_enqueue_response(dsn_task_t rpc_call, dsn_error_t err, dsn_message_t response)
this is to mimic a response is received when no real rpc is called
DSN_API void dsn_task_set_delay(dsn_task_t task, int delay_ms)
set delay for a task
Definition: task_helper.h:204
DSN_API void dsn_msg_release_ref(dsn_message_t msg)
release reference to the message, paired with /ref dsn_msg_add_ref
DSN_API void dsn_task_call(dsn_task_t task, int delay_milliseconds DEFAULT(0))
start the task
DSN_API dsn_error_t dsn_task_error(dsn_task_t task)
get result error code of a task
DSN_API void dsn_task_wait(dsn_task_t task)
wait until a task is completed
DSN_API bool dsn_task_wait_timeout(dsn_task_t task, int timeout_milliseconds)
wait until a task is completed
DSN_API dsn_message_t dsn_rpc_get_response(dsn_task_t rpc_call)
get response message from the response task, note returned msg must be explicitly released using dsn_...
Definition: task_helper.h:149
Definition: task_helper.h:252
DSN_API size_t dsn_file_get_io_size(dsn_task_t cb_task)
get read/written io size for the given aio task
DSN_API void dsn_task_release_ref(dsn_task_t task)
release reference for a given task handle