FTN12: FutoIn Async API Version: 1.14 Date: 2023-03-29 Copyright: 2014-2023 FutoIn Project (http://futoin.org) Authors: Andrey Galkin
Mutex
and Throttle
Limiter
primitiveThis interface was born as a secondary option for executor concept. However, it quickly became clear that async/reactor/proactor/light threads/etc. should be the base of scalable high performance server implementations, even though it is more difficult for understanding and/or debugging. Traditional synchronous program flow becomes an addon on top of asynchronous base for legacy code and/or too complex logic. Academical and practical research in this direction was started in field of cooperative multitasking back in XX century.
Program flow is split into non-blocking execution steps, represented with execution callback function. Processing Unit (eg. CPU) halting/ spinning/switching-to-another-task is seen as a blocking action in program flow. Execution of such fragments is partially ordered.
Any step must not call any of blocking functions, except for synchronization with guaranteed minimal period of lock acquisition. Note: under minimal period, it is assumed that any acquired lock is immediately released after action with O(1) complexity and no delay caused by programmatic suspension/locking of executing task
Every step is executed sequentially. Success result of any step becomes input for the following step.
Each step can have own error handler. Error handler is called, if AsyncSteps.error() is called within step execution or any of its sub-steps. Typical behavior is to ignore error and continue or to make cleanup actions and complete job with error.
Each step can have own sequence of sub-steps. Sub-steps can be added only during that step execution. Sub-step sequence is executed after current step execution is finished.
If there are any sub-steps added then current step must not call AsyncSteps.success() or AsyncSteps.error(). Otherwise, InternalError is raised.
It is possible to create a special "parallel" sub-step and add independent sub-steps to it. Execution of each parallel sub-step is started all together. Parallel step completes with success when all sub-steps complete with success. If error is raised in any sub-step of parallel step then all other sub-steps are canceled.
Out-of-order cancel of execution can occur by timeout, execution control engine decision (e.g. Invoker disconnect) or failure of sibling parallel step. Each step can install custom on-cancel handler to free resources and/or cancel external jobs. After cancel, it must be safe to destroy AsyncSteps object.
AsyncSteps must be used in Executor request processing. The same [root] AsyncSteps object must be used for all asynchronous tasks within given request processing.
AsyncSteps may be used by Invoker implementation.
AsyncSteps must support derived classes in implementation-defined way. Typical use case: functionality extension (e.g. request processing API).
For performance reasons, it is not economical to initialize AsyncSteps with business logic every time. Every implementation must support platform-specific AsyncSteps cloning/duplicating.
When AsyncSteps (or derived) object is created all steps are added sequentially in Level 0 through add() and/or parallel(). Note: each parallel() is seen as a step.
After AsyncSteps execution is initiated, each step of Level 0 is executed. All sub-steps are added in Level n+1. Example:
add() -> Level 0 #1
add() -> Level 1 #1
add() -> Level 2 #1
parallel() -> Level 2 #2
add() -> Level 2 #3
parallel() -> Level 1 #2
add() -> Level 1 #3
parallel() -> Level 0 #2
add() -> Level 0 #3
Execution cannot continue to the next step of current Level until all steps of higher Level are executed.
The execution sequence would be:
Level 0 add #1
Level 1 add #1
Level 2 add #1
Level 2 parallel #2
Level 2 add #3
Level 1 parallel #2
Level 1 add #3
Level 0 parallel #2
Level 0 add #3
Due to non-linear programming, classic try/catch blocks are converted into execute/onerror. Each added step may have custom error handler. If error handler is not specified then control passed to lower Level error handler. If non is defined then execution is aborted.
Example:
add( -> Level 0
func( as ){
print( "Level 0 func" )
add( -> Level 1
func( as ){
print( "Level 1 func" )
as.error( "myerror" )
},
onerror( as, error ){
print( "Level 1 onerror: " + error )
as.error( "newerror" )
}
)
},
onerror( as, error ){
print( "Level 0 onerror: " + error )
as.success( "Prm" )
}
)
add( -> Level 0
func( as, param ){
print( "Level 0 func2: " + param )
as.success()
}
)
Output would be:
Level 0 func
Level 1 func
Level 1 onerror: myerror
Level 0 onerror: newerror
Level 0 func2: Prm
In synchronous way, it would look like:
variable = null
try
{
print( "Level 0 func" )
try
{
print( "Level 1 func" )
throw "myerror"
}
catch ( error )
{
print( "Level 1 onerror: " + error )
throw "newerror"
}
}
catch( error )
{
print( "Level 0 onerror: " + error )
variable = "Prm"
}
print( "Level 0 func2: " + variable )
Very often, error handler creates an alternative complex program path which
requires own async operation. Therefore, error handler must accept as.add()
as implicit as.success()
.
If steps are added inside error handler they must remain on the same async stack level while error handler itself gets removed.
Example:
add( -> Level 0
func( as ){
print( "Level 0 func" )
add( -> Level 1
func( as ){
print( "Level 1 func" )
as.error( "first" )
},
onerror( as, error ){
print( "Level 1 onerror: " + error )
as.add( -> Level 2
func() {
print( "Level 2 func" )
as.error( "second" );
},
onerror( as, error ) {
print( "Level 2 onerror: " + error )
}
)
}
)
},
onerror( as, error ){
print( "Level 0 onerror: " + error )
}
)
Output would be:
Level 0 func
Level 1 func
Level 1 onerror: first
Level 2 func
Level 2 onerror: second
Level 0 onerror: second
Note: "Level 1 onerror" is not executed second time!
Very often, execution of step cannot continue without waiting for external event like input from network or disk. It is forbidden to block execution in event waiting. As a solution, there are special setTimeout() and setCancel() methods.
Example:
add(
func( as ){
socket.read( function( data ){
as.success( data )
} )
as.setCancel( function(){
socket.cancel_read()
} )
as.setTimeout( 30_000 ) // 30 seconds
},
onerror( as, error ){
if ( error == timeout ) {
print( "Timeout" )
}
else
{
print( "Read Error" )
}
}
)
Definition of parallel steps makes no sense to continue execution if any of steps fails. To avoid excessive time and resources spent on other steps, there is a concept of canceling execution similar to timeout above.
Example:
as.parallel()
.add(
func( as ){
as.setCancel( function(){ ... } )
// do parallel job #1
as.state()->result1 = ...;
}
)
.add(
func( as ){
as.setCancel( function(){ ... } )
// do parallel job #1
as.state()->result2 = ...;
}
)
.add(
func( as ){
as.error( "Some Error" )
}
)
as.add(
func( as ){
print( as.state()->result1 + as.state->result2 )
as.success()
}
)
In long living applications the same business logic may be re-used multiple times during execution.
In a REST API server example, complex business logic can be defined only once and stored in a kind of AsyncSteps object repository. On each request, a reference object from the repository would be copied for actual processing with minimal overhead.
However, there would be no performance difference in sub-step definition unless its callback function is also created at initialization time, but not at parent step execution time (the default concept). So, it should be possible to predefine those as well and copy/inherit during step execution. Copying steps must also involve copying of state variables.
Example:
AsyncSteps req_repo_common;
req_repo_common.add(func( as ){
as.add( func( as ){ ... } );
as.copyFrom( as.state().business_logic );
as.add( func( as ){ ... } );
});
AsyncSteps req_repo_buslog1;
req_repo_buslog1
.add(func( as ){ ... })
.add(func( as ){ ... });
AsyncSteps actual_exec = copy req_repo_common;
actual_exec.state().business_logic = req_repo_buslog1;
actual_exec.execute();
However, this approach only makes sense for deep performance optimizations.
If there are no sub-steps added, no timeout set and no cancel handler set then
implicit as.success()
call is assumed to simplify code and increase efficiency.
as.add(func( as ){
doSomeStuff( as );
})
As in many cases it's required to wait for external event without any additional
conditions, the general approach used to be adding an empty cancel handler. To
avoid that, an explicit as.waitExternal()
API is available.
Pre-defined state variables:
Error code is not always descriptive enough, especially, if it can be generated in multiple ways.
As a convention, special error_info
state field should hold descriptive information of the last error.
Therefore, as.error()
is extended with optional parameter error_info
.
The last_exception
state variables may hold the last exception object caught, if feasible
to implement. It should be populated with FutoIn errors as well.
Almost always, async program flow is not linear. Sometimes, loops are required.
Basic principals of async loops:
as.loop( func( as ){
call_some_library( as );
as.add( func( as, result ){
if ( !result )
{
// exit loop
as.break();
}
} );
} )
Inner loops and identifiers:
// start loop
as.loop(
func( as ){
as.loop( func( as ){
call_some_library( as );
as.add( func( as, result ){
if ( !result )
{
// exit loop
as.continue( "OUTER" );
}
as.success( result );
} );
} );
as.add( func( as, result ){
// use it somehow
as.success();
} );
},
"OUTER"
)
Loop n times.
as.repeat( 3, func( as, i ){
print( 'Iteration: ' + i )
} )
Traverse through list or map:
as.forEach(
[ 'apple', 'banana' ],
func( as, k, v ){
print( k + " = " + v )
}
)
Normal loop termination is performed either by loop condition (e.g. as.forEach()
, as.repeat()
)
or by as.break()
call. Normal termination is seen as as.success() call.
Abnormal termination is possible through as.error()
, including timeout, or external as.cancel()
.
Abnormal termination is seen as as.error()
call.
It is possible to split a single step into several functional fragments. In this case, the
topmost function is assumed the one passed to as.add()
directly.
as.success()
should be called only in the topmost function of the step.as.setCancel()
and/or as.setTimeout()
must be called only in the topmost function
as repeated calls override those in the scope of the step.as.add()
internally for any
operation with side effects to ensure correct order of operations.If any of API identifiers clashes with one of reserved words or has illegal symbols then implementation-defined name mangling is allowed, but with the following guidelines in priority.
Pre-defined alternative method names, if the default matches language-specific reserved keywords:
As with any multi-threaded application, multi-step cases may also require synchronization to ensure not more than N steps enter the same critical section which spans over several fragments (steps) of the asynchronous flow.
Implemented as Mutex
class.
For general stability reasons and protection of self-DoS, it may be required to limit number of steps allowed to enter critical section within time period.
Implemented as Throttle
class.
A special as.sync(obj, step, err_handler)
API is available to synchronize against
any object supporting synchronization protocol as.sync(as, step, err_handler)
.
Synchronization object is allowed to add own steps and is responsible for adding request steps under protection of provided synchronization. Synchronization object must correctly handle canceled execution and possible errors.
Incoming success parameters must be passed to critical section step. Resulting success parameters must be forwarded to the following steps like there is no critical section logic.
All synchronization implementations must either allow multiple reentrance of the same AsyncSteps instance or properly detect and raise error on such event.
All implementations must correctly detect parallel flows in the scope of a single AsyncSteps instance and treat each as a separate one. None of paralleled steps should inherit the lock state of its parent step.
Deadlock detection is optional and is not mandatory required.
It may be required to limit maximum number of pending AsyncSteps flows. If overall queue limit is reached then new entries must get "DefenseRejected" error.
Request processing stability requires to limit both simultaneous connections and
request rate. Therefore a special synchronization primitive Limiter
wrapping
Mutex
and Throttle
is introduced to impose limits in scope.
Sometimes, it's required to return a value after inner steps are executed. It leads to code like:
value = 123;
as.add( subStep() );
as.add( (as) => as.success( value ) );
To optimize and make the code cleaner previously deprecated as.successStep()
is
returned. Example:
value = 123;
as.add( subStep() );
as.successStep( value );
As Promises and await
patterns become more and more popular in modern technologies,
AsyncSteps should support them through as.await(future_or_promise)
call.
Details of implementation is specific to particular technology. However, the following guidelines should be used:
future_or_promise
is cancellable then as.setCancel()
must be used.as.waitExternal()
to be used.as.error()
as.success()
For most GC-based technologies step closures can use objects allocated in outer steps without issues. However, object lifetime management is important for technologies like ISO C++.
A special Pointer stack(size)
execution API is provided. The raw version acts like
regular heap allocation, but allocated memory is automatically freed once step is destroyed.
If other lifetime is required then implementation-specific shared pointers should be used.
Technology-specific implementation should provide template or generic overload to better integrate with specific type system and other features. Example:
// Prototype
template<typename T, typename... Args>
T& stack(Args&&... args);
// to be used like
asi.stack<T>();
asi.stack<T>(SomeCtorParam);
void execute_callback( AsyncSteps as[, previous_success_args] )
:as.success()
or as.error()
,as.add()
and/or as.parallel()
,as.setTimeout()
and/or
set cancel handler through as.setCancel()
,as.error( InternalError )
;as.state()
for global current job state data.void error_callback( AsyncSteps as, error )
:as.error()
call;as.success()
- continue execution from the next step, after return,as.error()
- change error string,as.error( InternalError )
.as.state()
for global current job state data.void cancel_callback( AsyncSteps as )
:interface ISync
void sync( AsyncSteps, execute_callback[, error_callback] )
:It is assumed that all functions in this section are part of the single AsyncSteps interface. However, they are grouped by semantical scope of use.
AsyncSteps add( execute_callback func[, error_callback onerror] )
:AsyncSteps
object accessor for easy chaining.AsyncSteps parallel( [error_callback onerror] )
:add()
'ed sub-steps are executed in parallel (not strictly required),success()
does not allow any arguments - use state()
to pass results.Map state()
:null
to identify invalid state of AsyncSteps object.AsyncSteps copyFrom( AsyncSteps other )
:AsyncSteps sync(ISync obj, execute_callback func[, error_callback onerror] )
:obj
.AsyncSteps successStep( [result_arg, ...] )
:as.add( (as) => as.success( result_arg, ... ) )
.AsyncSteps await( future_or_promise[, error_callback onerror] )
:AsyncSteps newInstance()
:boolean cast()
:state()
notes.FutoInAsyncSteps cast()
:binary()
notes..FutoInAsyncSteps binary()
:AsyncSteps wrap(FutoInAsyncSteps)
:wrap()
is being called.Note: success()
and error()
can be used in error_callback as well
void success( [result_arg, ...] )
execute_callback
;AsyncSteps
stack during external event waiting.void error( name [, error_info] )
:FutoIn.Error
exception immediately;onerror( async_iface, name )
after returning to execution engine;error_info
- assigned to error_info
state field.void setTimeout( timeout_ms )
:Timeout
error is raised.call operator overloading
:as.success()
.void setCancel( cancel_callback oncancel )
:void waitExternal()
:as.success()
behavior of the current step.Pointer stack(size[, destroy_cb])
:void execute()
- must be called only once after root object steps are configured.void cancel()
- may be called on root object to asynchronously cancel execution.AsyncSteps
execution.Promise promise()
- must be called only once after the root object steps are configured.execute()
into a native Promise object.execute_callback
void loop( func, [, label] )
:as.break()
is called;func( as )
- the loop body;label
- am optional label to use for as.break()
and as.continue()
in inner loops.void forEach( map|list, func [, label] )
:map
or list
element, call func( as, key, value )
;func( as, key, value )
- the loop body;label
- an optional label to use for as.break()
and as.continue()
in inner loops.void repeat( count, func [, label] )
:func(as, i)
for count
times;count
- how many times to call the func
;func( as, i )
- the loop body, i - current iteration starting from 0
;label
- an optional label to use for as.break()
and as.continue()
in inner loops.void break( [label] )
:AsyncSteps
internally;label
- unwinds nested loops, until label
named loop is exited.void continue( [label] )
:AsyncSteps
internally;label
- break nested loops, until label
named loop is found.Mutex
classISync
interface.c-tor(unsigned integer max=1, unsigned integer max_queue=null)
:max_queue
- optionally, limit the queue length.Throttle
classISync
interface.c-tor(unsigned integer max, unsigned integer period_ms=1000, unsigned integer max_queue=null)
:period_ms
- the time period in milliseconds;max_queue
- optionally, limit the queue length.Limiter
classISync
interface.c-tor(options)
:options.concurrent=1
- the number maximum of concurrent flows;options.max_queue=0
- the number maximum of queued flows;options.rate=1
- the number maximum of the critical section entries in the given period;options.period_ms=1000
- the time period in milliseconds;options.burst=0
- the number maximum of queued flows for rate limiting.There is a strong assumption that AsyncSteps instances are executed in partial order by a common instance of event loop, with a historical name AsyncTool.
There is an assumption that AsyncTool will be extended with Input/Output event support to act as a true reactor, but it may not be always possible.
AsyncTool was not defined in previous versions of the specification while it was always existing because its interface is specific to technology. Below is only a general suggestion.
Handle immediate( func )
:func()
- general callback.Handle deferred( delay, func )
:delay
- typically time period in milliseconds;func()
- general callback.bool is_same_thread()
:void cancel( handle )
:Handle
object's interface.void is_valid( handle )
:Handle
object's interface.To achieve the initial goal of the FutoIn project - universal cross-technology interface, a certain minimal binary interface has to be defined to be passed as an ordinary memory pointer for the first parameter of callback functions, so any technology-specific solution could wrap that as necessary and allow mixing asynchronous step fragments written in different languages like C, C++, C#, ECMAScript, Java, Lua, Ruby, Rust and others in scope of a single asynchronous thread.
As the base idea, Java Native Interface approach is taken, where a pointer to an abstract plain structure is passed. The first field of such structure is a pointer to a table of plain C functions, each API functions also assumes to get the pointer to the structure as the first parameter. C++ virtual table is also working similar way.
Plain ISO C is supported one way or another in almost every technology to create bindings and other type of glue functionality. Therefore, it is used to describe the binary interface with assumption of only standard platform-defined paddings and pointer sizes while all API callbacks use the standard platform-defined calling convention.
There are certain limitations as it is problematic to guarantee type safety without significant overhead,
so binary interface user must be more aware of what is being done. State access is split into two API
functions which operate over abstract void
pointers.
Binary data interface is used to pass execute_callback
arguments between technologies. Directly
are supported:
For efficiency reasons, complex types like vectors may be stored both in agnostic C format and as a technology-specific object instance. Therefore, binary value holding object supports cleanup callbacks to properly destroy such objects even from C or Assembly code.
typedef struct FutoInBinaryValue_ FutoInBinaryValue;
typedef struct FutoInType_ FutoInType;
typedef uint8_t FutoInTypeFlags;
enum
{
FTN_TYPE_CUSTOM_OBJECT = 0x01,
FTN_TYPE_STRING = 0x02,
FTN_TYPE_STRING16 = 0x03,
FTN_TYPE_STRING32 = 0x04,
FTN_TYPE_BOOL = 0x05,
FTN_TYPE_INT8 = 0x06,
FTN_TYPE_INT16 = 0x07,
FTN_TYPE_INT32 = 0x08,
FTN_TYPE_INT64 = 0x09,
FTN_TYPE_UINT8 = 0x0A,
FTN_TYPE_UINT16 = 0x0B,
FTN_TYPE_UINT32 = 0x0C,
FTN_TYPE_UINT64 = 0x0D,
FTN_TYPE_FLOAT = 0x0E,
FTN_TYPE_DOUBLE = 0x0F,
FTN_BASE_TYPE_MASK = 0x0F,
// --
FTN_TYPE_ARRAY = 0x10,
FTN_COMPLEX_TYPE_MASK = 0xF0,
};
struct FutoInType_
{
const FutoInTypeFlags flags;
void (*const cleanup)(FutoInBinaryValue* v);
// NOTE: extendable by implementation
};
struct FutoInBinaryValue_
{
const FutoInType* type;
union
{
const void* p;
const char* cstr;
const char16_t* cstr16;
const char32_t* cstr32;
bool b;
int8_t i8;
int16_t i16;
int32_t i32;
int64_t i64;
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
float f;
double d;
};
void* custom_data;
uint32_t length;
};
static inline void futoin_reset_binval(FutoInBinaryValue* v)
{
auto tp = v->type;
if (tp) {
auto* f = tp->cleanup;
if (f) {
f(v);
}
}
v->type = 0;
v->u64 = 0;
v->custom_data = 0;
v->length = 0;
}
Binary interface has a maximum limit of 4 custom arguments according to the industry best practices. Thefore, argument object is a collection of 4 binary value holders.
Binary interface is inspired by a typical C++ vtable and Java Native Interface specification.
It is assumed that a pointer to an agnostic FutoInAsyncSteps
structure is passed instead
of technology-specific interface object. Such structure has the first field of a pointer to a function
table. Each function receives the same pointer to the structure as the first argument. There may
be additional implementation-defined fields. Therefore, business logic code must not assume that
it knows actual size of such structure.
Unlike most of traditional cases, ISO C11 does not support exceptions and that imposes some restrictions and duties for business logic. For example, raising errors requires returning from the handler function manually.
The meaning of functions is the same, except additional data
and similar arguments may be added to
bind dynamic data to callbacks user-defined way.
The function table is also extended with AsyncTool interface for convenience.
typedef struct FutoInAsyncStepsAPI_ FutoInAsyncStepsAPI;
typedef struct FutoInAsyncSteps_ FutoInAsyncSteps;
typedef struct FutoInSyncAPI_ FutoInSyncAPI;
typedef struct FutoInSync_ FutoInSync;
typedef struct FutoInArgs_ FutoInArgs;
typedef struct FutoInHandle_ FutoInHandle;
struct FutoInArgs_
{
union
{
struct
{
FutoInBinaryValue arg0;
FutoInBinaryValue arg1;
FutoInBinaryValue arg2;
FutoInBinaryValue arg3;
};
FutoInBinaryValue args[4];
};
};
struct FutoInHandle_
{
void* data1;
void* data2;
ptrdiff_t data3;
};
typedef void (*FutoInAsyncSteps_execute_callback)(
FutoInAsyncSteps* bsi, void* data, const FutoInArgs* args);
typedef void (*FutoInAsyncSteps_error_callback)(
FutoInAsyncSteps* bsi, void* data, const char* code);
typedef void (*FutoInAsyncSteps_cancel_callback)(
FutoInAsyncSteps* bsi, void* data);
struct FutoInAsyncStepsAPI_
{
union
{
struct
{
// Index 0
void (*add)(
FutoInAsyncSteps* bsi,
void* data,
FutoInAsyncSteps_execute_callback f,
FutoInAsyncSteps_error_callback eh);
// Index 1
FutoInAsyncSteps* (*parallel)(
FutoInAsyncSteps* bsi,
void* data,
FutoInAsyncSteps_error_callback eh);
// Index 2
void* (*stateVariable)(
FutoInAsyncSteps* bsi,
void* data,
const char* name,
void* (*allocate)(void* data),
void (*cleanup)(void* data, void* value));
// Index 3
void* (*stack)(
FutoInAsyncSteps* bsi,
size_t data_size,
void (*cleanup)(void* value));
// Index 4
void (*success)(FutoInAsyncSteps* bsi, FutoInArgs* args);
// Index 5
void (*handle_error)(
FutoInAsyncSteps* bsi, const char* code, const char* info);
// Index 6
void (*setTimeout)(FutoInAsyncSteps* bsi, uint32_t timeout_ms);
// Index 7
void (*setCancel)(
FutoInAsyncSteps* bsi,
void* data,
FutoInAsyncSteps_cancel_callback ch);
// Index 8
void (*waitExternal)(FutoInAsyncSteps* bsi);
// Index 9
void (*loop)(
FutoInAsyncSteps* bsi,
void* data,
void (*f)(FutoInAsyncSteps* bsi, void* data),
const char* label);
// Index 10
void (*repeat)(
FutoInAsyncSteps* bsi,
void* data,
size_t count,
void (*f)(FutoInAsyncSteps* bsi, void* data, size_t i),
const char* label);
// Index 11
void (*breakLoop)(FutoInAsyncSteps* bsi, const char* label);
// Index 12
void (*continueLoop)(FutoInAsyncSteps* bsi, const char* label);
// Index 13
void (*execute)(
FutoInAsyncSteps* bsi,
void* data,
FutoInAsyncSteps_error_callback unhandled_error);
// Index 14
void (*cancel)(FutoInAsyncSteps* bsi);
// Index 15
void (*addSync)(
FutoInAsyncSteps* bsi,
FutoInSync* sync,
void* data,
FutoInAsyncSteps_execute_callback f,
FutoInAsyncSteps_error_callback eh);
// Index 16
ptrdiff_t (*rootId)(FutoInAsyncSteps* bsi);
// Index 17
int (*isValid)(FutoInAsyncSteps* bsi);
// Index 18
FutoInAsyncSteps* (*newInstance)(FutoInAsyncSteps* bsi);
// Index 19
void (*free)(FutoInAsyncSteps* bsi);
// Index 20
FutoInHandle (*sched_immediate)(
FutoInAsyncSteps* bsi, void* data, void (*cb)(void* data));
// Index 21
FutoInHandle (*sched_deferred)(
FutoInAsyncSteps* bsi,
uint32_t delay_ms,
void* data,
void (*cb)(void* data));
// Index 22
void (*sched_cancel)(FutoInAsyncSteps* bsi, FutoInHandle* handle);
// Index 23
int (*sched_is_valid)(FutoInAsyncSteps* bsi, FutoInHandle* handle);
// Index 24
int (*is_same_thread)(FutoInAsyncSteps* bsi);
};
void* funcs[25];
};
// NOTE: extendable by implementation
};
struct FutoInAsyncSteps_
{
#ifdef __cplusplus
FutoInAsyncSteps_(const FutoInAsyncStepsAPI* api) noexcept : api(api) {}
#endif
const FutoInAsyncStepsAPI* const api;
// NOTE: extendable by implementation
};
Synchronization object interface is defined separately from the AsyncSteps one as it is quite possible that AsyncSteps may be implemented in one technology while the synchronization object is implemented in an absolutely different one.
struct FutoInSyncAPI_
{
union
{
struct
{
// Index 0
void (*lock)(FutoInAsyncSteps* bsi, FutoInSync* sync);
// Index 1
void (*unlock)(FutoInAsyncSteps* bsi, FutoInSync* sync);
};
void* funcs[2];
};
// NOTE: extendable by implementation
};
struct FutoInSync_
{
#ifdef __cplusplus
FutoInSync_() noexcept : api(nullptr) {}
#endif
const FutoInSyncAPI* const api;
// NOTE: extendable by implementation
};
In pseudo-code.
AsyncStepsImpl as;
as.add(
function( inner_as ){
if ( something )
inner_as.success( 1, 2 )
else
inner_as.error( NotImplemented )
},
function( inner_as, error ){
externalError( error );
}
).add(
function( inner_as, res1, res2 ){
externalSuccess( res1, res2 );
},
)
AsyncStepsImpl as;
as.add(
function( inner_as ){
inner_as.add(
function( inner2_as ){
if ( something )
inner2_as.success( 1 )
else
inner2_as.error( NotImplemented )
},
function( inner2_as, error )
{
log( "Spotted error " + error )
// continue with higher level error handlers
}
)
inner_as.add(
function( inner2_as, res1 ){
inner2_as.success( res1, 2 )
}
)
},
function( inner_as, error ){
externalError( error );
}
).add(
function( inner_as, res1, res2 ){
externalSuccess( res1, res2 );
},
)
AsyncStepsImpl as;
as.add(
function( inner_as ){
inner_as.parallel().add(
function( inner2_as ){
inner2_as.state().parallel_1 = 1;
},
function( inner2_as, error )
{
log( "Spotted error " + error )
// continue with higher level error handlers
}
).add(
function( inner2_as ){
inner2_as.state().parallel_2 = 2;
},
function( inner2_as, error )
{
inner2_as.state().parallel_2 = 0;
// ignore error
}
)
},
function( inner_as, error ){
externalError( error );
}
).add(
function( inner_as, res1, res2 ){
externalSuccess(
as.state().parallel_1,
as.state().parallel_2
);
},
)
AsyncStepsImpl as;
as.add(
function( as ){
as.repeat( 3, function( as, i ) {
print i;
} );
as.forEach( [ 1, 3, 3 ], function( as, k, v ) {
print k "=" v;
} );
as.forEach( as.state(), function( as, k, v ) {
print k "=" v;
} );
},
)
AsyncStepsImpl as;
as.add(
function( as ){
as.waitExternal();
callSomeExternal( function(err) {
if (err)
{
try {
as.error(err);
} catch {
// ignore
}
}
else
{
as.success();
}
} );
},
)
AsyncStepsImpl as;
MutexImpl mutex(10);
as.sync(
mutex,
function( as ){
// critical section with regular AsyncSteps
},
)
=END OF SPEC=