Skip to content

Commit

Permalink
Working examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
steinwaywhw committed Aug 27, 2018
1 parent fcb7ed8 commit e457467
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 64 deletions.
9 changes: 5 additions & 4 deletions board.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct board_t* board_make(const char* id) {
strcpy(b->id, id);
b->refcount = 1;

log_info("Board %s @ %p allocated.", b->id, b);
// Debug
g_boards[++g_bindex] = b;
return b;
Expand Down Expand Up @@ -104,7 +105,7 @@ PRIVATE void search(int i, struct msg_t* cur , struct search_env_t* env) {
SETENV(i, cur);

#ifndef NDEBUG
char buffer[50];
char buffer[100];
int j = sprintf(buffer, "Searching for %s ", msg_show_label(env->pattern));
j += msg_show_senders(env->pattern, buffer+j);
j += sprintf(buffer+j, " => ");
Expand All @@ -114,7 +115,7 @@ PRIVATE void search(int i, struct msg_t* cur , struct search_env_t* env) {
}

#ifndef NDEBUG
char buffer[50];
char buffer[100];
int j = sprintf(buffer, "Searching for %s ", msg_show_label(env->pattern));
j += msg_show_senders(env->pattern, buffer+j);
j += sprintf(buffer+j, " => ");
Expand Down Expand Up @@ -234,7 +235,7 @@ struct board_t* board_read(struct board_t* b, struct msg_t* pattern, struct msg_
pthread_cond_timedwait(&b->cond, &b->mutex, &timeout);

#ifndef NDEBUG
char buffer[50];
char buffer[100];
int i = 0;
i = sprintf(buffer, "Searching for %s ", msg_show_label(pattern));
i += msg_show_senders(pattern, buffer+i);
Expand Down Expand Up @@ -356,7 +357,7 @@ struct board_t* board_read(struct board_t* b, struct msg_t* pattern, struct msg_
}

PRIVATE void board_dbgfn(int i, struct msg_t* m, void* env) {
char prefix[10];
char prefix[30];
sprintf(prefix, " %s => ", env);
msg_show_prefix(m, prefix);
}
Expand Down
1 change: 1 addition & 0 deletions endpoint.sats
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ absvtype endpoint = ptr

fun ep_make {full,self:set|full>self} (set full, set self, !board): endpoint = "mac#"
fun ep_free (endpoint): void = "mac#"
fun ep_split {s:set} (!endpoint, set s): endpoint = "mac#"

fun ep_get_self (!endpoint): [s:set] set s = "mac#"
fun ep_get_full (!endpoint): [s:set] set s = "mac#"
Expand Down
12 changes: 10 additions & 2 deletions ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ void ep_free(struct ep_t* ep) {
return;
}

struct ep_t* ep_split(struct ep_t* ep, int32_t split) {
assert(MSG_SET_SUP(ep->self, split));

ep->self = MSG_SET_MINUS(ep->self, split);
struct ep_t* ret = ep_make(ep->full, split, board_ref(ep->board));
return ret;
}

void ep_send(struct ep_t* ep, int label, int32_t from, int32_t to, void* payload) {
struct msg_t* m = msg_make(label, from, to, payload);
struct board_t* child = board_write(ep->board, m);
Expand Down Expand Up @@ -71,7 +79,7 @@ void* ep_recv(struct ep_t* ep, int label, int32_t from, int32_t to) {
void ep_sync(struct ep_t* ep, int label, int syncer) {
int32_t senders = MSG_SET_MINUS(ep->full, ep->self);

char buffer[20];
char buffer[100];
int i = 0;
while (senders > 0) {
for(unsigned int mask = 0x80; mask; mask >>= 1) {
Expand Down Expand Up @@ -136,7 +144,7 @@ struct ep_t* ep_link(struct ep_t* ep1, struct ep_t* ep2) {
}

void ep_show(struct ep_t* ep) {
char buffer[64];
char buffer[100];
int i = sprintf(buffer, "Endpoint [");
for(unsigned int mask = 0x80; mask; mask >>= 1) {
i += sprintf(buffer+i, "%d", !!(mask & ep->full));
Expand Down
170 changes: 170 additions & 0 deletions examples/ex-cloud.dats
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@

//staload "set.sats"
//staload "libsession.sats"
//staload "blackboard.sats"
staload UN = "prelude/SATS/unsafe.sats"


#define C 0 // Cloud
#define S 1 // Services Provider
#define U 2 // Service User

stadef full = emp+0+1+2
stadef SC = emp+S+C
stadef UC = emp+U+C

vtypedef chanS (p:stype) = chan(full, emp+S, p)
vtypedef chanC (p:stype) = chan(full, emp+C, p)
vtypedef chanU (p:stype) = chan(full, emp+U, p)

stadef ints = pfix(lam p => pbranch(S,pmsg(C,S,int)::p, pend(S)))
stadef primes = pfix(lam p => pbranch(U,pmsg(C,U,int)::p, pend(U)))

vtypedef cloudfn (p:stype) = (chan(SC,emp+S,ints), chan(full,emp+C,p)) -<lincloptr1> void
stadef cloud = pquan2(S, lam p => pmsg(S,C,cloudfn(p)) :: p)


extern fun cloud (chan(full,emp+C,cloud)): void
extern fun primes (chan(SC,emp+S,ints), chan(full,emp+C,primes)): void
extern fun service {p:stype} (chan(full,emp+S+U,cloud), cloudfn p, chan(full,emp+S,p)->void): chan(full,emp+U,p)
extern fun primeuser (chan(full,emp+U,primes)): void

implement cloud (chC) = let
val [p:stype] chC = session_exify2 chC
val f = session_recv<cloudfn(p)> (chC, S, C)

fun from (n: int): chan(SC,emp+S,ints) = let
fun server (ch:chan(SC,emp+C,ints), n:int): void = let
prval _ = session_unroll ch
val choice = session_offer (ch, S)
in
case+ choice of
| ~Left() => (session_send (ch, C, S, n); server (ch, n+1))
| ~Right() => session_wait (ch, S)
end
in
session_fork (emp()+C, emp()+S, llam ch => server (ch, n))
end

val _ = f (from 2, chC)
val _ = $UN.castvwtp0{void} f
in
end


implement primes (chS, chC) = let
fun filter (inp: chan(SC,emp+S,ints), p: int -<cloref1> bool): chan(SC,emp+S,ints) = let
fun get (inp: !chan(SC,emp+S,ints)): int = let
prval _ = session_unroll inp
val _ = session_choose (inp, S, Left ())
val n = session_recv (inp, C, S)
in
if p n
then n
else get inp
end

fun server (out: chan(SC,emp+C,ints), inp: chan(SC,emp+S,ints)): void = let
prval _ = session_unroll out
val c = session_offer (out, S)
in
case+ c of
| ~Left() => (session_send (out, C, S, get inp); server (out, inp))
| ~Right() =>
let
val _ = session_wait (out, S)
prval _ = session_unroll inp
val _ = session_choose (inp, S, Right())
in
session_close (inp, S)
end
end
in
session_fork (emp()+C, emp()+S, llam out => server (out, inp))
end

fun sieve (out: chanC(primes), inp: chan(SC,emp+S,ints)): void = let
prval _ = session_unroll out
val c = session_offer (out, U)
in
case+ c of
| ~Right() =>
let
prval _ = session_unroll inp
val _ = session_choose (inp, S, Right())
val _ = session_close (inp, S)
in
session_wait (out, U)
end
| ~Left() =>
let
prval _ = session_unroll inp
val _ = session_choose (inp, S, Left())
val n = session_recv (inp, C, S)
val _ = session_send (out, C, U, n)
in
sieve (out, filter (inp, lam p => p mod n > 0))
end
end

in
sieve (chC, chS)
end


implement service {p} (chSU, f, dummy) = let
val chSU = session_unify2 chSU
val _ = session_send (chSU, S, C, llam (chIn, chOut) => let val _ = f (chIn, chOut) in $UN.castvwtp0{void} f end)
val _ = session_split {full,emp+U,emp+S} {p} (emp()+U, emp()+S, chSU, llam chS => dummy chS)
in
chSU
end


implement primeuser (chU) = let
fun loop (ch: chanU(primes), n:int): void =
if n <= 0
then
let
prval _ = session_unroll ch
val _ = session_choose (ch, U, Right())
in
session_close (ch, U)
end
else
let
prval _ = session_unroll ch
val _ = session_choose (ch, U, Left())
val _ = println! (session_recv (ch, C, U))
in
loop (ch, n-1)
end
in
loop (chU, 5)
end


extern fun test (string): void
implement test (msg) = let
val _ = println! msg

fun dummyPrimes (chS: chanS(primes)): void = let
prval _ = session_unroll chS
val c = session_offer (chS, U)
in
case+ c of
| ~Left() => let prval _ = session_skip chS in dummyPrimes chS end
| ~Right() => session_wait (chS, U)
end

val board = board_make "A"
val chC = session_make<board> {pinit(U)::cloud} (emp()+S+C+U, emp()+C, board)
val chSU = session_make<board> {pinit(U)::cloud} (emp()+S+C+U, emp()+S+U, board)
val _ = board_free board

val _ = session_request (chC, U, llam chC => cloud chC)
val _ = session_accept (chSU, U)
val chU = service {primes} (chSU, llam (inp, out) => primes (inp, out), dummyPrimes)
in
primeuser chU
end
16 changes: 8 additions & 8 deletions examples/ex-queue.dats
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ implement {a} empty () = let
val c = session_offer (left, C)
in
case+ c of
| ~Right() => (session_choose (left, Right()); session_close left)
| ~Right() => (session_choose (left, S, Right()); session_close (left, S))
| ~Left() =>
let
val x = session_brecv<a> (left, C)
Expand All @@ -45,15 +45,15 @@ implement {a} elem (right, x) = let
let
val y = session_brecv<a> (left, C)
prval _ = session_unroll right
val _ = session_choose (right, Left())
val _ = session_bsend<a> (right, y)
val _ = session_choose (right, C, Left())
val _ = session_bsend<a> (right, C, y)
in
server (left, right)
end
| ~Right() =>
let
val _ = session_choose (left, Left())
val _ = session_bsend<a> (left, x)
val _ = session_choose (left, S, Left())
val _ = session_bsend<a> (left, S, x)
in
session_emp (session_link (left, right))
end
Expand All @@ -64,14 +64,14 @@ end

implement {a} enq (ch, x) = let
prval _ = session_unroll ch
val _ = session_choose (ch, Left())
val _ = session_bsend<a> (ch, x)
val _ = session_choose (ch, C, Left())
val _ = session_bsend<a> (ch, C, x)
in
end

implement free<int> (ch) = let
prval _ = session_unroll ch
val _ = session_choose (ch, Right())
val _ = session_choose (ch, C, Right())
val c = session_offer (ch, S)
in
case+ c of
Expand Down
Loading

0 comments on commit e457467

Please sign in to comment.