Simple preemptive scheduler to execute functions asynchronously (for x86_64 linux only). Extremely fragile and entirely useless, obviously not intended to be used anywhere. Works for CPU-bound tasks and some specific non blocking syscalls. Silently breaks if something is blocking the thread. Implementation details are here and here.
- STACK_SIZE is the size of the dedicated stack used by every async function, default size is 8KB. If segfaults or core dumps are occuring for no obvious reasons (the reason is my incompetency), try increasing the STACK_SIZE. Optional to pass, but if passed then make sure that it is a multiple of the page size on your OS, I don't really know the reason for that but it works.
- NUM_FUNC is the number of maximum async function that can be stored, by default is 10007. Optional to pass.
-fno-omit-frame-pointerflag needs to be passed, as maintaining frame pointer is essential.
gcc -fno-omit-frame-pointer -DSTACK_SIZE=<stack_size> -DNUM_FUNC=<number_of_functions> /path/to/afx.c yourfile.c -o yourfile
#include "/path/to/afx.h"
// declaration
async_dec(<return_type>, <function_name>(arg1, arg2, ...));
// definition
async(
<return_type>, <function_name>, (arg1, arg2, ...), {
//body
}
)
// call
afx(function_name(arg1, arg2, ...)); //async
function_name(arg1, arg2, ...); //normalasync_decmacro is used to declare an async function.asyncmacro converts a normal function into an async function.afxmacro runs an async function. An async function can be run like a normal function as well.afx_recv,afx_send,afx_acceptandafx_connectare async alternatives for recv, send, accept and connect functions provided by socket api. In an async function normal blocking syscalls does not work, so these alternatives can be used. They accept the same type of arguments. When using async_connect, you can also just make the socket non-blocking and use normal connect, which will be a little bit faster.async_sleepandasync_usleepare alternatives to sleep and usleep.afx_yield()is a function that will schedule the next async function immediately.- Try not to use any blocking syscalls in an async function, their behaviour is undefined and may cause errors. These syscalls are basically skipped or cancelled in-between, if the scheduler schedules another function.