[Commit] nickle/examples coroutines.5c,NONE,1.1
Bart Massey
commit at keithp.com
Wed Feb 15 22:06:14 PST 2006
Committed by: bart
Update of /local/src/CVS/nickle/examples
In directory home.keithp.com:/tmp/cvs-serv6709/examples
Added Files:
coroutines.5c
Log Message:
2005-2-15 Bart Massey <bart at cs.pdx.edu>
* examples/coroutines.5c:
New example using continuations
to implement channels and coroutines.
--- NEW FILE: coroutines.5c ---
#!/usr/bin/env nickle
# Voluntary co-routines communicate on
# a channel using setjmp()/longjmp()
#
# Bart Massey 2006/02/15
#
# Uses some truly ugly magic;
namespace Channel {
typedef union {
void nothing;
poly just;
} maybe;
public typedef struct {
continuation cs, cr;
} channel;
public void send(&channel c, poly val) {
int res = setjmp(&c.cs, 0);
if (res == 0 && !is_uninit(&c.cr))
longjmp(c.cr, (maybe.just) val);
}
public poly recv(&channel c, void() gen) {
maybe res = setjmp(&c.cr, maybe.nothing);
union switch (res) {
case nothing:
if (is_uninit(&c.cs))
gen();
else
longjmp(c.cs, 1);
abort("internal error: recv from nowhere");
break;
case just v:
return v;
}
abort("internal error: union switch match");
}
}
import Channel;
{
channel c;
void generate() {
for (int i = 0; i < 10; i++) {
printf("sent %d\n", i);
send(&c, i);
}
}
void test() {
int i;
do {
i = recv(&c, generate);
printf("got %d\n", i);
} while (i != 7);
}
test();
}
More information about the Commit
mailing list