Question

As a follow-up to my other question, if one were to build a general-purpose translator from a language that has support (be it with an external library or otherwise) for asynchronous behaviour to a language that may or may not natively support it (let's use C as an example), then is it a semantically equivalent translation of an asynchronous callback to simply invoke the callback function before returning/completing from the successor? Note that in the example to follow I refer to JavaScript as a language that has support for asynchronous behaviour although it does not natively support asynchronous behaviour but in practice is used frequently through some libraries.

For example, given the following JavaScript program (from this simple example of an asynchronous JavaScript function)

window.setTimeout(function() {
    console.log("World");
}, 1000);
console.log("Hello");

would it be a semantically equivalent translation of the above to use fork in the above such that the child process sleeps for 1000 milliseconds and then invokes prints "World" to stdout, and in the parent process continue with printing "Hello" to stdout? Are there any problems with this specific translation? Does this give rise to any problems with translation in the general case?

Note that my motivation is to find a way to implement asynchronous behaviour, not to outsource the implementation to the use of a library.

Était-ce utile?

La solution

Here's a program that implements a similar code in C. Note that I removed all error checking for brevity:

#include <sys/timerfd.h>
#include <stdlib.h>
#include <sys/epoll.h>

static int pending_jobs;
static int efd;
struct cb_data {
    int fd;
    void (*fn)(void);
};

static void set_timeout(void (*fn)(void), int secs)
{
    struct epoll_event ev;
    int fd;
    struct itimerspec sleep_time;
    struct timespec now;
    struct cb_data *d;

    /* Setup a timer */
    clock_gettime(CLOCK_REALTIME, &now);

    sleep_time.it_value.tv_sec = now.tv_sec + secs;
    sleep_time.it_value.tv_nsec = now.tv_nsec;
    sleep_time.it_interval.tv_sec = 0;
    sleep_time.it_interval.tv_nsec = 0;

    fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
    timerfd_settime(fd, TFD_TIMER_ABSTIME, &sleep_time, NULL);

    /* Add it to epoll */
    ev.events = EPOLLIN;
    d = malloc(sizeof(*d));
    d->fd = fd;
    d->fn = fn;
    ev.data.ptr = d;
    pending_jobs++;
    epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev);

}

static void main_program_loop(void)
{
    /* Loop around epoll wait until there are no more jobs */
    while (pending_jobs > 0) {
        struct epoll_event events[10];
        int n, nfds;
        nfds = epoll_wait(efd, events, 10, -1);
        for (n = 0; n < nfds; ++n) {
            struct cb_data *d = events[n].data.ptr;
            epoll_ctl(efd, EPOLL_CTL_DEL, d->fd, NULL);
            pending_jobs--;
            close(d->fd);
            d->fn();
            free(d);
        }

    }
    /* No more jobs pending, exit */
}

void print_world(void)
{
    puts("World");
}

int main(void)
{
    pending_jobs = 0;
    efd = epoll_create1(0);

    /* The JS equivalent */
    set_timeout(print_world, 1);
    puts("hello");

    /* loop until all pending events are done */
    main_program_loop();

    close(efd);
}

You can see that the C equivalent to the javascript snippet is:

void print_world(void)
{
    puts("World");
}
...
set_timeout(print_world, 1);
puts("hello");

So it's pretty close. All the rest is the instrumentation that's needed both in C and Javascript. Except in Javascript it's bundled in the engine. In C, you can roll your own (something similar to the program above), or use a library such a libev or libevent.

Licencié sous: CC-BY-SA avec attribution
scroll top