-
Notifications
You must be signed in to change notification settings - Fork 19
Task natives
native wait_ticks(ticks);
Performs a non-blocking wait for the specified number of server ticks. If ticks
is zero, resumes the execution at the end of the supercontext. If ticks
is negative, waits forever.
native wait_ms(interval);
Performs a non-blocking wait for the specified number of milliseconds. If interval
is zero, resumes the execution at the end of the supercontext. If interval
is negative, waits forever.
native Task:task_new();
Creates a new non-finished task and returns its ID.
native bool:task_delete(Task:task);
Deletes a task. Tasks are automatically deleted when they are completed, unless task_keep
is used.
native bool:task_valid(Task:task);
Returns true if a given task is valid.
native Task:task_keep(Task:task, bool:keep=true);
If keep
is true, extends the lifetime of a task beyond its completion. Such a task must be deleted manually using task_delete
. Passing false restored the original behaviour.
native task_set_result(Task:task, AnyTag:result, TagTag:tag_id=tagof(result));
native task_set_result_arr(Task:task, const AnyTag:result[], size=sizeof(result), TagTag:tag_id=tagof(result));
native task_set_result_str(Task:task, const result[]);
native task_set_result_str_s(Task:task, ConstStringTag:result);
native task_set_result_var(Task:task, ConstVariantTag:result);
Sets the result of a task. This sequentially resumes all paused code that waits for the task. If task_keep
has not been use to extend the lifetime of the task, the task will be deleted at the end of the call, if it has not been reset (via task_reset
).
native task_get_result(Task:task, offset=0);
native task_get_result_arr(Task:task, AnyTag:result[], size=sizeof(result));
native task_get_result_str(Task:task, result[], size=sizeof(result)) = task_get_result_arr;
native String:task_get_result_str_s(Task:task);
native Variant:task_get_result_var(Task:task);
native bool:task_get_result_safe(Task:task, &AnyTag:result, offset=0, TagTag:tag_id=tagof(result));
native task_get_result_arr_safe(Task:task, AnyTag:result[], size=sizeof(result), TagTag:tag_id=tagof(result));
Returns the result of a task as an untagged value. If the task ended with an error, it is raised instead.
native task_set_result_ms(Task:task, AnyTag:result, interval, TagTag:tag_id=tagof(result));
native task_set_result_ms_arr(Task:task, const AnyTag:result[], interval, size=sizeof(result), TagTag:tag_id=tagof(result));
native task_set_result_ms_str(Task:task, const result[], interval);
native task_set_result_ms_str_s(Task:task, ConstStringTag:result, interval);
native task_set_result_ms_var(Task:task, interval);
Sets the result of a task after a specified number of milliseconds.
native task_set_result_ticks(Task:task, AnyTag:result, ticks, TagTag:tag_id=tagof(result));
native task_set_result_ticks_arr(Task:tasks, const AnyTag:result[], tick, size=sizeof(result), TagTag:tag_id=tagof(result));
native task_set_result_ticks_str(Task:task, const result[], ticks);
native task_set_result_ticks_str_s(Task:task, ConstStringTag:result, interval);
native task_set_result_ticks_var(Task:task, ConstVariantTag:result, ticks);
Sets the result of a task after a specified number of server ticks.
native task_set_error(Task:task, amx_err:error);
Sets the state of the task as faulted with the AMX error code. This resumes all code waiting for this task in a manner similar to task_set_result
.
native amx_err:task_get_error(Task:task);
If the task is in the faulted state, returns the error.
native task_set_error_ms(Task:task, amx_err:error, interval);
Sets the task as faulted after a specified number of milliseconds.
native task_set_error_ticks(Task:task, amx_err:error, ticks);
Sets the task as faulted after a specified number of server ticks.
native bool:task_completed(Task:task);
Returns true if the task has completed in a non-error state.
native bool:task_faulted(Task:task);
Returns true if the task has completed in an error state.
native task_state:task_state(Task:task);
Returns the state of completion of the task (task_state_default
, task_state_completed
, task_state_faulted
).
native task_reset(Task:task);
Restores a task to its initial state (i.e. not completed).
native Task:task_ticks(ticks);
Creates a new task that will be completed after the specified number of ticks passes, akin to wait_ticks
.
native Task:task_ms(interval);
Creates a new task that will be completed after the specified number of milliseconds passes, akin to wait_ms
.
native Task:task_any(Task:...);
Combines a number of other tasks into a single one which is completed when any of the tasks is completed. The first task that was completed is the result of the created task. Since its lifetime is extended, you must delete the resulting task manually.
native Task:task_all(Task:...);
Combines a number of other tasks into a single one which is completed when all of the tasks are completed. The last task that was completed is the result of the created task. Since its lifetime is extended, you must delete the resulting task manually.
native task_state:task_wait(Task:task);
Performs a non-blocking waiting for the completion of the specified task. When the task is completed (or faulted), the function returns the state and the execution of the code is resumed.
stock task_await(Task:task)
stock task_await_str(Task:task, result[], size=sizeof(result))
stock String:task_await_str_s(Task:task)
stock task_await_arr(Task:task, AnyTag:result[], size=sizeof(result))
stock Variant:task_await_var(Task:task)
Calls task_wait(task)
, and returns the result of the task after it is completed, or raises an AMX error if the task has faulted. Available as the await
/await_str(dest)
/await_str_s
/await_arr(dest)
/await_var
operator if PP_SYNTAX_AWAIT
is defined.
native task_yield(AnyTag:value);
Sets the result of the immediate containing public function after the execution is paused. Available as the yield
operator if PP_SYNTAX_YIELD
is efined.
If the current context is bound to a task (via task_bind
), the task is set to completed.
native task_set_yielded(AnyTag:value);
Like task_yield
, but doesn't touch the bound task (if one is present).
native task_get_yielded();
Returns the value set by task_yield
or task_set_yielded
.
native bool:task_bind(Task:task, const function[], const format[], AnyTag:...);
Calls the specified public function (with the passed arguments) and binds it to the provided task. When the public function logically ends, the task is set to completed (like when calling task_set_result
). If the function ends with an AMX error, the task faults. The function may call task_yield
to set the result of the bound task before its end.
Note that by default, tasks are collected right after they are set to completed (see task_set_result
). This means that if the inner function ends without pausing the execution (task_bind
returns after that in this case), the task will be immediately set to completed but likely without any continuations, so the result will be discarded when the task gets deleted.
The best way to get around this is to call task_keep
and task_delete
to manually control the lifetime of the task.
native Task:task_bound();
Returns the task bound to this context, or INVALID_TASK
if none exists.
native task_config(task_restore:heap=task_restore_full, task_restore:stack=task_restore_full);
Configures how the state of the AMX machine is stored when the code is paused to await a task. Possible values are task_restore_none
(do not store the memory section), task_restore_frame
(store only the memory associated with the stack frame), task_restore_context
(store only the memory in the current context), or task_restore_full
(store everything).
native task_continue_with(Task:task, const handler[], const additional_format[]="", AnyTag:...);
Registers a public function identified by handler
to be called when the task is completed.
native Task:task_continue_with_bound(Task:task, Task:bound, const handler[], const additional_format[]="", AnyTag:...);
Registers a public function identified by handler
to be called when the task is completed. The public function can be executed asynchronously, its completion being bound to bound
.
native task_detach();
"Detaches" the current stack frame from the caller frame, creating a new context that is immediately executed. Calling asynchronous functions after this function (in the same function) does not block the caller functions. If the current function exits normally, its return value is passed to the caller. If the current function exits asynchronously, the value provided to task_yield
is used.
native task_detach_bound(Task:task);
Like task_detach
, but makes the new context bound to task
, as if it had been created with task_bind
.