diff options
Diffstat (limited to 'test/cothreads/looper.c')
-rw-r--r-- | test/cothreads/looper.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/test/cothreads/looper.c b/test/cothreads/looper.c new file mode 100644 index 000000000..5b67a019f --- /dev/null +++ b/test/cothreads/looper.c @@ -0,0 +1,47 @@ +#include <stdio.h> +#include "looper.h" + +void looper_loopfunc(object *obj); + +void looper_init(looper *l,int source) { + l->source = source; + object_setloopfunc(OBJECT(l),looper_loopfunc); +} + +looper *looper_create(char *name,int source,cothread_context *ctx) { + looper *l = malloc(sizeof(looper)); + + if (l == NULL) { + fprintf(stderr,"sorry, couldn't allocate memory for looper\n"); + exit(2); + } + object_init(OBJECT(l),name,ctx); + looper_init(l,source); + + return l; +} + + +void looper_loopfunc(object *obj) { + looper *l = LOOPER(obj); + + if (l->source) { + while (1) { + char *buf = malloc(11); + sprintf(buf,"Hello World!"); + fprintf(stderr,"\npushing buffer %p with '%s'\n",buf,buf); + object_push(OBJECT(l)->peer,buf); // this should switch + } + } else { + while (1) { + char *buf; + fprintf(stderr,"\npulling buffer\n"); + buf = object_pull(OBJECT(l)); + printf("got %p: '%s' from peer\n",buf,buf); + free(buf); + // return to the main process now + cothread_switch(cothread_main(OBJECT(l)->threadstate->ctx)); + sleep(1000); + } + } +} |