Skip to content

Commit

Permalink
Merge pull request #70 from parapluu/bugfix/pony_arg_t-in-futures
Browse files Browse the repository at this point in the history
Futures now store pony_arg_t rather than void*
  • Loading branch information
Stephan Brandauer committed Feb 4, 2015
2 parents bf69ec8 + a15aa24 commit d13f203
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 48 deletions.
11 changes: 6 additions & 5 deletions src/back/CodeGen/ClassDecl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,17 @@ translateActiveClass cdecl@(A.Class{A.cname, A.fields, A.methods}) =
then [one_way_send_dispatch_clause m]
else []

mthd_dispatch_clause mdecl@(A.Method{A.mname, A.mparams}) =
mthd_dispatch_clause mdecl@(A.Method{A.mname, A.mparams, A.mtype}) =
(method_msg_name cname mname,
Seq [Assign (Decl (Ptr $ Typ "future_t", Var "fut"))
((ArrAcc 0 ((Var "argv"))) `Dot` (Nam "p")),
Statement $ Call (Nam "future_fulfil")
[AsExpr $ Var "fut",
Cast (Ptr void)
(Call (method_impl_name cname mname)
((AsExpr . Var $ "p") :
(paramdecls_to_argv 1 $ mparams)))]])
Cast (pony_arg_t)
(UnionInst (pony_arg_t_tag (translate mtype)) $
Call (method_impl_name cname mname)
((AsExpr . Var $ "p") :
(paramdecls_to_argv 1 $ mparams)))]])
mthd_dispatch_clause mdecl@(A.StreamMethod{A.mname, A.mparams}) =
(method_msg_name cname mname,
Seq [Assign (Decl (Ptr $ Typ "future_t", Var "fut"))
Expand Down
11 changes: 2 additions & 9 deletions src/back/CodeGen/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
| Ty.isFutureType $ A.getType val =
do (nval, tval) <- translate val
let result_type = translate (Ty.getResultType $ A.getType val)
the_get = Cast result_type $ Call (Nam "future_get_actor") [nval]
the_get = Cast result_type $ Call (Nam "future_get_actor") [nval] `Dot` pony_arg_t_tag result_type
tmp <- Ctx.gen_sym
return (Var tmp, Seq [tval, Assign (Decl (result_type, Var tmp)) the_get])
| Ty.isStreamType $ A.getType val =
Expand Down Expand Up @@ -474,11 +474,4 @@ instance Translatable A.Expr (State Ctx.Context (CCode Lval, CCode Stat)) where
| Ty.isRealType ty = Nam "d"
| otherwise = Nam "p"

translate other = error $ "Expr.hs: can't translate: '" ++ show other ++ "'"

pony_arg_t_tag :: CCode Ty -> CCode Name
pony_arg_t_tag (Ptr _) = Nam "p"
pony_arg_t_tag (Typ "int64_t") = Nam "i"
pony_arg_t_tag (Typ "double") = Nam "d"
pony_arg_t_tag other =
error $ "Expr.hs: no pony_arg_t_tag for " ++ show other
translate other = error $ "Expr.hs: can't translate: '" ++ show other ++ "'"
11 changes: 9 additions & 2 deletions src/back/CodeGen/Type.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances, FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances, FlexibleInstances, GADTs #-}

{-| Make Type (see "AST") an instance of @Translatable@ (see
"CodeGen.Typeclasses"). -}
Expand Down Expand Up @@ -35,4 +35,11 @@ instance Translatable Ty.Type (CCode Ty) where
| Ty.isTypeVar ty = Ptr void
| Ty.isFutureType ty = future
| Ty.isStreamType ty = stream
| otherwise = error $ "I don't know how to translate "++ show ty ++" to pony.c"
| otherwise = error $ "I don't know how to translate "++ show ty ++" to pony.c"

pony_arg_t_tag :: CCode Ty -> CCode Name
pony_arg_t_tag (Ptr _) = Nam "p"
pony_arg_t_tag (Typ "int64_t") = Nam "i"
pony_arg_t_tag (Typ "double") = Nam "d"
pony_arg_t_tag other =
error $ "Type.hs: no pony_arg_t_tag for " ++ show other
26 changes: 13 additions & 13 deletions src/runtime/future/future.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct actor_entry

struct future
{
void *value;
pony_arg_t value;
bool fulfilled;
// Stupid limitation for now
actor_entry_t responsibilities[16];
Expand All @@ -98,11 +98,11 @@ future_t *future_mk(void)
return fut;
}

void *run_closure(closure_t *c, void *value, future_t *fut)
pony_arg_t run_closure(closure_t *c, pony_arg_t value, future_t *fut)
{
value_t result = closure_call(c, (value_t[1]) { { .p = value } });
future_fulfil(fut, result.p);
return result.p;
value_t result = closure_call(c, (value_t[1]) { value });
future_fulfil(fut, result);
return result;
}

bool future_fulfilled(future_t *fut)
Expand All @@ -115,16 +115,16 @@ bool future_fulfilled(future_t *fut)
return r;
}

void *future_read_value(future_t *fut)
pony_arg_t future_read_value(future_t *fut)
{
perr("future_read_value");
BLOCK;
void *v = fut->value;
pony_arg_t v = fut->value;
UNBLOCK;
return v;
}

void future_fulfil(future_t *fut, void *value)
void future_fulfil(future_t *fut, pony_arg_t value)
{
perr("future_fulfil");

Expand All @@ -145,7 +145,7 @@ void future_fulfil(future_t *fut, void *value)
}
}
if (!blocked) {
pony_arg_t argv[3] = { { .p = current->closure }, { .p = value }, { .p = current->future } };
pony_arg_t argv[3] = { { .p = current->closure }, value, { .p = current->future } };
pony_sendv(current->actor, FUT_MSG_RUN_CLOSURE, 3, argv);
}
current = current->next;
Expand All @@ -167,15 +167,15 @@ void future_fulfil(future_t *fut, void *value)
// Intended design: see https://github.com/parapluu/mylittlepony/wiki/Futures
case ATTACHED_CLOSURE:
{
pony_arg_t argv[3] = { { .p = e.closure.closure }, { .p = value }, { .p = e.closure.future } };
pony_arg_t argv[3] = { { .p = e.closure.closure }, value, { .p = e.closure.future } };
pony_sendv(e.closure.actor, FUT_MSG_RUN_CLOSURE, 3, argv);
break;
}
// Design 1: current thread executes closures (racy)
case DETACHED_CLOSURE:
{
value_t result = closure_call(e.closure.closure, (value_t[1]) { { .p = value } });
future_fulfil(e.closure.future, result.p);
value_t result = closure_call(e.closure.closure, (value_t[1]) { value });
future_fulfil(e.closure.future, result);
break;
}
default:
Expand All @@ -190,7 +190,7 @@ void future_fulfil(future_t *fut, void *value)
// ===============================================================
// Means for actors to get, block and chain
// ===============================================================
void *future_get_actor(future_t *fut)
pony_arg_t future_get_actor(future_t *fut)
{
// early return for simple case
if (fut->parent == NULL) {
Expand Down
26 changes: 13 additions & 13 deletions src/runtime/future/future.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ typedef struct future future_t;
// ===============================================================
// Create, inspect and fulfil
// ===============================================================
future_t *future_mk(void);
bool future_fulfilled (future_t *fut);
void *future_read_value (future_t *fut);
void future_fulfil (future_t *fut, void *value);
future_t *future_mk(void);
bool future_fulfilled (future_t *fut);
pony_arg_t future_read_value (future_t *fut);
void future_fulfil (future_t *fut, pony_arg_t value);

// ===============================================================
// Means for actors to get, block and chain
// ===============================================================
void *future_get_actor(future_t *fut);
future_t *future_chain_actor(future_t *fut, future_t* r, closure_t *c);
void future_block_actor(future_t *fut); // TODO: does this belong in the public interface?
void future_unblock_actor(future_t *fut);
pony_arg_t future_get_actor(future_t *fut);
future_t *future_chain_actor(future_t *fut, future_t* r, closure_t *c);
void future_block_actor(future_t *fut); // TODO: does this belong in the public interface?
void future_unblock_actor(future_t *fut);

// ===============================================================
// Possibly these functions do not belong in the future library
// ===============================================================
void future_suspend(void);
void future_suspend_resume(void *);
void future_await(future_t *);
void future_await_resume(void *);
void *run_closure(closure_t *c, void *value, future_t *fut);
void future_suspend(void);
void future_suspend_resume(void *);
void future_await(future_t *);
void future_await_resume(void *);
pony_arg_t run_closure(closure_t *c, pony_arg_t value, future_t *fut);

#endif
10 changes: 5 additions & 5 deletions src/runtime/stream/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ stream_t *stream_put(stream_t *s, pony_arg_t value){
struct scons *scons = scons_mk();
scons->element = value;
scons->next = fut;
future_fulfil((future_t *)s, scons);
future_fulfil((future_t *)s, (pony_arg_t){ .p = scons });
return fut;
}

bool stream_eos(stream_t *s){
struct scons *scons = future_get_actor((future_t *)s);
struct scons *scons = future_get_actor((future_t *)s).p;
return scons->eos;
}

pony_arg_t stream_get(stream_t *s){
struct scons *scons = future_get_actor((future_t *)s);
struct scons *scons = future_get_actor((future_t *)s).p;
return scons->element;
}

stream_t *stream_get_next(stream_t *s){
struct scons *scons = future_get_actor((future_t *)s);
struct scons *scons = future_get_actor((future_t *)s).p;
return scons->next;
}

void stream_close(stream_t *s){
struct scons *scons = scons_mk();
scons->eos = true;
future_fulfil((future_t *)s, scons);
future_fulfil((future_t *)s, (pony_arg_t){ .p = scons });
}
2 changes: 1 addition & 1 deletion src/tests/encore/basic/realfutures.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1
2.100000

0 comments on commit d13f203

Please sign in to comment.