AsyncIO

Class implementing AsyncIO support.

Constructors

this
this(EpollSelectDispatcher epoll, int number_of_threads, AsyncIO.Context delegate() makeContext, long thread_stack_size)

Constructor.

Members

Classes

Context
class Context

Base class for the thread worker context

Nonblocking
class Nonblocking

Set of non-blocking methods. The user is responsible to suspend the fiber, and AsyncIO will not resume it. Instead, the callback will be called where the user can do whatever is required.

TaskBlocking
class TaskBlocking

Task wrapper

Functions

callDelegate
void callDelegate(void delegate(AsyncIO.Context) user_delegate, JobNotification notification)

Calls an user provided delegate. The delegate is called from within a separate thread and it should not do anything non-thread safe (for example, using GC must be avoided) from the runtime's perspective. Delegate receives a reference to the per-thread context which it can use.

close
void close(int fd, JobNotification suspended_job)

Issues a close request, blocking the fiber connected to the provided suspendable request handler until the request finishes.

destroy
void destroy()

Destroys entire AsyncIO object. It's unusable after this point.

fsync
void fsync(int fd, JobNotification suspended_job)

Issues a fsync request, blocking the fiber connected to the provided suspended_job until the request finishes.

pread
size_t pread(void[] buf, int fd, size_t offset, JobNotification suspended_job)

Issues a pread request, blocking the fiber connected to the provided suspended_job until the request finishes.

write
size_t write(void[] buf, int fd, JobNotification notification)

Appends a buffer to the file.

Structs

ThreadInitializationContext
struct ThreadInitializationContext

Struct providing the initialization data for the thread.

Variables

blocking
TaskBlocking blocking;

Task wrapper

nonblocking
Nonblocking nonblocking;

Set of non-blocking methods. The user is responsible to suspend the fiber, and AsyncIO will not resume it. Instead, the callback will be called where the user can do whatever is required.

Examples

Example showing the task-blocking API

/// Per-thread context. Can be anything, but it needs to inherit
// from AsyncIO.Context
class AioContext: AsyncIO.Context
{
    int i;
}

// Creates the per thread context. This is executed inside
// each worker thread. Useful to initialize C libraries (e.g.
// curl_easy_init)
// This method is synchronized, so everything here is thread safe
// One must only pay attention not to call methods that need
// thread-local state
AsyncIO.Context makeContext()
{
    auto ctx = new AioContext;

    // set some things
    ctx.i = 0;

    return ctx;
}
/// Callback called from another thread to set the counter
void setCounter (AsyncIO.Context ctx)
{
    // cast the per-thread context
    auto myctx = cast(AioContext)ctx;
    myctx.i++;
}

void example()
{
    auto async_io = new AsyncIO(theScheduler.epoll, 10, &makeContext);

    // open a new file
    auto f = new File("var/output.txt", File.ReadWriteAppending);

    // write a file in another thread
    char[] buf = "Hello darkness, my old friend.".dup;
    async_io.blocking.write(buf, f.fileHandle());

    // read the file from another thread
    buf[] = '\0';
    async_io.blocking.pread(buf, f.fileHandle(), 0);

    test!("==")(buf[], "Hello darkness, my old friend.");
    test!("==")(f.length, buf.length);

    // call the delegate from another thread
    async_io.blocking.callDelegate(&setCounter);
}

Meta