Robust Distributed System Nucleus (rDSN)  ver 1.0.0
Typedefs | Enumerations | Functions
Common Task Operations

Overview

rDSN adopts the event-driven programming model, where all computations (event handlers) are represented as individual tasks; each is the execution of a sequential piece of code in one thread. Specifically, rDSN categorizes the tasks into four types, as defined in dsn_task_type_t.

Unlike the traditional event-driven programming, rDSN enhances the model in the following ways, with which they control the application in many aspects in a declarative approach.

Typedefs

typedef enum dsn_task_type_t dsn_task_type_t
 
typedef void(* dsn_task_handler_t) (void *)
 
typedef void(* dsn_rpc_request_handler_t) (dsn_message_t, void *)
 
typedef void(* dsn_rpc_response_handler_t) (dsn_error_t, dsn_message_t, dsn_message_t, void *)
 
typedef void(* dsn_aio_handler_t) (dsn_error_t, size_t, void *)
 
typedef enum dsn_task_priority_t dsn_task_priority_t
 
typedef void(* dsn_task_cancelled_handler_t) (void *)
 

Enumerations

enum  dsn_task_type_t {
  TASK_TYPE_RPC_REQUEST, TASK_TYPE_RPC_RESPONSE, TASK_TYPE_COMPUTE, TASK_TYPE_AIO,
  TASK_TYPE_CONTINUATION, TASK_TYPE_COUNT, TASK_TYPE_INVALID
}
 
enum  dsn_task_priority_t {
  TASK_PRIORITY_LOW, TASK_PRIORITY_COMMON, TASK_PRIORITY_HIGH, TASK_PRIORITY_COUNT,
  TASK_PRIORITY_INVALID
}
 

Functions

DSN_API dsn_threadpool_code_t dsn_threadpool_code_register (const char *name)
 
DSN_API const char * dsn_threadpool_code_to_string (dsn_threadpool_code_t pool_code)
 
DSN_API dsn_threadpool_code_t dsn_threadpool_code_from_string (const char *s, dsn_threadpool_code_t default_code)
 
DSN_API int dsn_threadpool_code_max ()
 
DSN_API int dsn_threadpool_get_current_tid ()
 
DSN_API dsn_task_code_t dsn_task_code_register (const char *name, dsn_task_type_t type, dsn_task_priority_t, dsn_threadpool_code_t pool)
 
DSN_API void dsn_task_code_query (dsn_task_code_t code, dsn_task_type_t *ptype, dsn_task_priority_t *ppri, dsn_threadpool_code_t *ppool)
 
DSN_API void dsn_task_code_set_threadpool (dsn_task_code_t code, dsn_threadpool_code_t pool)
 
DSN_API void dsn_task_code_set_priority (dsn_task_code_t code, dsn_task_priority_t pri)
 
DSN_API const char * dsn_task_code_to_string (dsn_task_code_t code)
 
DSN_API dsn_task_code_t dsn_task_code_from_string (const char *s, dsn_task_code_t default_code)
 
DSN_API int dsn_task_code_max ()
 
DSN_API const char * dsn_task_type_to_string (dsn_task_type_t tt)
 
DSN_API const char * dsn_task_priority_to_string (dsn_task_priority_t tt)
 
DSN_API volatile int * dsn_task_queue_virtual_length_ptr (dsn_task_code_t code, int hash DEFAULT(0))
 
DSN_API void dsn_task_add_ref (dsn_task_t task)
 
DSN_API void dsn_task_release_ref (dsn_task_t task)
 
DSN_API int dsn_task_get_ref (dsn_task_t task)
 
DSN_API bool dsn_task_cancel (dsn_task_t task, bool wait_until_finished)
 
DSN_API void dsn_task_set_delay (dsn_task_t task, int delay_ms)
 
DSN_API bool dsn_task_cancel2 (dsn_task_t task, bool wait_until_finished, bool *finished)
 
DSN_API void dsn_task_cancel_current_timer ()
 
DSN_API void dsn_task_wait (dsn_task_t task)
 
DSN_API bool dsn_task_wait_timeout (dsn_task_t task, int timeout_milliseconds)
 
DSN_API dsn_error_t dsn_task_error (dsn_task_t task)
 
DSN_API bool dsn_task_is_running_inside (dsn_task_t t)
 
DSN_API dsn_task_tracker_t dsn_task_tracker_create (int task_bucket_count)
 
DSN_API void dsn_task_tracker_destroy (dsn_task_tracker_t tracker)
 
DSN_API void dsn_task_tracker_cancel_all (dsn_task_tracker_t tracker)
 
DSN_API void dsn_task_tracker_wait_all (dsn_task_tracker_t tracker)
 

Typedef Documentation

typedef void(* dsn_task_cancelled_handler_t) (void * )

callback prototype for task cancellation (called on task-being-cancelled)

in rDSN, tasks can be cancelled. For languages such as C++, when there are explicit resource release operations (e.g., ::free, release_ref()) in the task handlers, cancellation will cause resource leak due to not-executed task handleers. in order to support such scenario, rDSN provides dsn_task_cancelled_handler_t which is executed when a task is cancelled. Note this callback does not have thread affinity similar to task handlers above (which are configured to be executed in certain thread pools or even a fixed thread). Therefore, it is developers' resposibility to ensure this cancallation callback only does thread-insensitive operations (e.g., release_ref()).

Enumeration Type Documentation

task/event type definition

Enumerator
TASK_TYPE_RPC_REQUEST 

task handling rpc request

TASK_TYPE_RPC_RESPONSE 

task handling rpc response or timeout

TASK_TYPE_COMPUTE 

async calls or timers

TASK_TYPE_AIO 

callback for file read and write

TASK_TYPE_CONTINUATION 

above tasks are seperated into several continuation tasks by thread-synchronization operations.

so that each "task" is non-blocking

Function Documentation

DSN_API void dsn_task_add_ref ( dsn_task_t  task)

Add reference count for a task created from dsn_task_create etc.

Parameters
taskthe task handle.

Memory usage of tasks are controlled using reference-count. All returned dsn_task_t are NOT add_ref by rDSN, so you DO NOT need to call task_release_ref to release the tasks. the decision is made for easier programming, and you may consider the later dsn_rpc_xxx calls do the resource gc work for you.

however, before you emit the tasks (e.g., via dsn_task_call, dsn_rpc_call), AND you want to hold the task handle further after the emit API, you need to call dsn_task_add_ref to ensure the handle is still valid, and also call dsn_task_release_ref later to release the handle.

DSN_API void dsn_task_release_ref ( dsn_task_t  task)

release reference for a given task handle

Parameters
taskthe task handle

See more details of the comment in dsn_task_add_ref

DSN_API int dsn_task_get_ref ( dsn_task_t  task)

get reference for a given task handle

Parameters
taskthe task handle

See more details of the comment in dsn_task_add_ref

DSN_API bool dsn_task_cancel ( dsn_task_t  task,
bool  wait_until_finished 
)

cancel a task

Parameters
taskthe task handle
wait_until_finishedtrue if wait until finished is needed
Returns
true if THIS cancellation succeeds, false if the task is already running (when wait_until_finished = false), or completed successfully, or already cancelled.
DSN_API void dsn_task_set_delay ( dsn_task_t  task,
int  delay_ms 
)

set delay for a task

Parameters
taskthe task handle
delay_msthe delay milliseconds for a task
DSN_API bool dsn_task_cancel2 ( dsn_task_t  task,
bool  wait_until_finished,
bool *  finished 
)

cancel a task

Parameters
taskthe task handle
wait_until_finishedtrue if wait until finished is needed
finishedafter the call, whether the task is finished (completed successfully, or cancelled)
Returns
true if THIS cancellation succeeds, false if the task is already running (when wait_until_finished = false), or completed successfully, or cancelled.
DSN_API void dsn_task_wait ( dsn_task_t  task)

wait until a task is completed

Parameters
taskthe task handle
Returns
true if it succeeds, false if it fails.
DSN_API bool dsn_task_wait_timeout ( dsn_task_t  task,
int  timeout_milliseconds 
)

wait until a task is completed

Parameters
taskthe task handle
timeout_millisecondsmaximum time to wait
Returns
true if it succeeds, false if it timeouts
DSN_API dsn_error_t dsn_task_error ( dsn_task_t  task)

get result error code of a task

Parameters
taskthe task handle.
Returns
the result error code of the task
DSN_API bool dsn_task_is_running_inside ( dsn_task_t  t)

check whether the task is currently running inside the given task

Parameters
tthe given task handle
Returns
true if it is.
DSN_API dsn_task_tracker_t dsn_task_tracker_create ( int  task_bucket_count)

task trackers are used to track task context

When a task executes, it usually accesses certain context. When the context is gone, all tasks accessing this context needs to be cancelled automatically to avoid invalid context access. To release this burden from developers, rDSN provides task tracker which can be embedded into a context, and destroyed when the context is gone.

Parameters
task_bucket_countnumber of task buckets to reduce thread conflicts
Returns
task tracker handle
DSN_API void dsn_task_tracker_destroy ( dsn_task_tracker_t  tracker)

destroy a task tracker, which cancels all pending tasks as well

Parameters
trackertask tracker handle
DSN_API void dsn_task_tracker_cancel_all ( dsn_task_tracker_t  tracker)

cancels all pending tasks bound to this tracker

Parameters
trackertask tracker handle
DSN_API void dsn_task_tracker_wait_all ( dsn_task_tracker_t  tracker)

wait all pending tasks to be completed bound to this tracker

Parameters
trackertask tracker handle