1

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.

  • By that other question's own admission, you need an API external to javascript just to implement asynchronicity in Javascript. So your original premise is incorrect; Javascript does not have built-in support for asynchronous behavior. – Robert Harvey Apr 28 '15 at 22:51
  • Given that your source language is in the same boat as your target language (in the sense that neither language implements asynchronicity without external libraries), maybe it makes more sense to leave asynchronicity up to each language's respective libraries. – Robert Harvey Apr 28 '15 at 22:52
  • 1
    In addition, fork actually spawns a new operating system process; seldom are asynchronous operations that heavyweight. – Robert Harvey Apr 28 '15 at 22:56
  • I have edited my question to reflect your concerns/comments. I simply used fork as part of the proof of concept, I could have just as easily used pthreads, I just did not want the main thread to block for execution of setTimeout. – Francesco Gramano Apr 28 '15 at 23:02
  • Since you already know that, your question seems a bit "crystal-ballsy;" given the exotic nature of your translation application, it seems unlikely that we would be able to predict every unfortunate eventuality that might befall you. – Robert Harvey Apr 28 '15 at 23:03
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.