Equivalent translation of Asynchronous behaviour in C

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.

10

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật