Created
March 21, 2020 17:51
-
-
Save GwenNelson/ae315c9aaadf9d4e354b0222958aed84 to your computer and use it in GitHub Desktop.
libdill async_await
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libdill.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define lambda(lambda$_ret, lambda$_args, lambda$_body)\ | |
({\ | |
lambda$_ret lambda$__anon$ lambda$_args\ | |
lambda$_body\ | |
&lambda$__anon$;\ | |
}) | |
#define async_await(ret_type,expr) \ | |
({ \ | |
int __async_await_ch[2];\ | |
chmake(__async_await_ch);\ | |
int __async_await_rc = go(lambda(void,(int ch),{\ | |
ret_type retval = expr;\ | |
chsend(ch, &retval, sizeof(ret_type),-1);\ | |
})(__async_await_ch[1]));\ | |
ret_type __async_await_retval;\ | |
chrecv(__async_await_ch[0],&__async_await_retval,sizeof(ret_type),-1);\ | |
__async_await_retval;\ | |
}) | |
int test() { | |
msleep(now()+1000); | |
return 1337; | |
} | |
int main() { | |
int retval = async_await(int,test()); | |
printf("Return value: %d\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment