changeset 141:f2cb85c53ced

Fix Pause/Resume and rewrite Call Async in Rhope rather than C
author Mike Pavone <pavone@retrodev.com>
date Sat, 20 Nov 2010 17:48:22 -0500
parents c14698c512f1
children 7bbdc034e347
files cbackend_c.rhope kernel.rhope nworker_c.rhope runtime/builtin.c runtime/context.c runtime/context.h runtime/func.h testpauseresume.rhope
diffstat 8 files changed, 80 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/cbackend_c.rhope	Sat Nov 20 20:03:25 2010 +0000
+++ b/cbackend_c.rhope	Sat Nov 20 17:48:22 2010 -0500
@@ -1509,14 +1509,14 @@
 {
 	out <- [[[[["typedef enum {\n"
 		]Append[Fold[_Dispatch Enum[?], 
-			[Fold[_Dispatch Enum Methods[[program]Escape Pattern >>,?], "", all methods]]Append["\tFUNC_Build,\n\tFUNC_BlueprintSP_Of,\n\tFUNC_ID,\n\tFUNC_BlueprintSP_FromSP_ID,\n\tFUNC_Pause,\n\tRES_1_Pause,\n\tFUNC_Resume,\n\tFUNC_CallSP_Async,\n"], 
+			[Fold[_Dispatch Enum Methods[[program]Escape Pattern >>,?], "", all methods]]Append["\tFUNC_Build,\n\tFUNC_BlueprintSP_Of,\n\tFUNC_ID,\n\tFUNC_BlueprintSP_FromSP_ID,\n\tFUNC_Pause,\n\tRES_1_Pause,\n\tFUNC_Resume,\n"], 
 			[program]Functions >>]]
 		]Append["\tEND,\n\tEND_THREAD\n} funcids;\n\n"]
 		]Append["#define DispatchEntries \\\n"] 
 		]Append[Fold[_Dispatch Switch[?], 
-			[Fold[_Dispatch Switch Methods[[program]Escape Pattern >>,?], "", all methods]]Append["\tDispatchEntry(Build)\\\n\tDispatchEntry(BlueprintSP_Of)\\\n\tDispatchEntry(ID)\\\n\tDispatchEntry(BlueprintSP_FromSP_ID)\\\n\tDispatchEntry(Pause)\\\n\tResumeEntry(1,Pause)\\\n\tDispatchEntry(Resume)\\\n\tDispatchEntry(CallSP_Async)\\\n"], 
+			[Fold[_Dispatch Switch Methods[[program]Escape Pattern >>,?], "", all methods]]Append["\tDispatchEntry(Build)\\\n\tDispatchEntry(BlueprintSP_Of)\\\n\tDispatchEntry(ID)\\\n\tDispatchEntry(BlueprintSP_FromSP_ID)\\\n\tDispatchEntry(Pause)\\\n\tResumeEntry(1,Pause)\\\n\tDispatchEntry(Resume)\\\n"], 
 			[program]Functions >>]]
-		]Append["\tEndEntry\n\tEndThreadEntry\n\n"]
+		]Append["\tEndEntry\\\n\tEndThreadEntry\n\n"]
 }
 
 Not Native[func:out]
@@ -1600,6 +1600,7 @@
 	all methods <- Fold[Field to Types[?], Fold[Method to Types[?], Dictionary[], type defs], type defs]
 	headers <- "#include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include \"builtin.h\"
 #include \"object.h\"
 #include \"context.h\"
@@ -1640,10 +1641,7 @@
 	FuncDef(Build)
 	FuncDef(BlueprintSP_Of)
 	FuncDef(ID)
-	FuncDef(BlueprintSP_FromSP_ID)
-	FuncDef(Pause)
-	FuncDef(Resume)
-	FuncDef(CallSP_Async)\n"]
+	FuncDef(BlueprintSP_FromSP_ID)\n"]
 		]Append[Fold[Local Pointers[?], "", [program]Functions >>]]
 		]Append["
 	ct = new_context();
@@ -1716,26 +1714,19 @@
 	ct = new_context();
 	VCPrepCall(my_cdata->params[0], 1)
 	VCSetParam(my_cdata->params[0], 0, make_Context(temp_ct))
-	ValCall(my_cdata->params[0], 1, 1, Pause)
+	ValCallNoLocals(my_cdata->params[0], 1, 1, Pause)
 	release_ref(my_cdata->params[0]);
 	DISPATCH
-	ValCallPostlude(1, Pause)
+	ValCallNoLocalsPostlude(1, Pause)
 	for(idx = 0; idx < cdata->num_params; ++idx)
-	{
-		release_ref(cdata->params[idx]);
-	}
+		if(cdata->params[idx])
+		{ release_ref(cdata->params[idx]); }
 	free_context(ct);
 	ct = get_cqueue();
 	if (!ct) goto NOTHING_TO_DO;
 	cdata = ct->resume_cdata;
-	ct->resume_data = NULL;	
-	if(ct->start_func >= 0)
-	{
-		func = ct->start_func;
-		ct->start_func = -1;
-		DISPATCH
-	}
-EndFuncNoLocals(Pause)
+	ct->resume_cdata = NULL;
+EndFuncNoLocals
 DISPATCH
 
 FuncNoLocals(Resume, NumParams 2)
@@ -1746,7 +1737,7 @@
 	if(temp_ct->resumeable && temp_ct->resume_cdata)
 	{
 		temp_ct->resumeable = 0;
-		temp_ct->resume_cdata->params[0] = add_ref(my_cdata-params[1]);
+		temp_ct->resume_cdata->params[0] = add_ref(my_cdata->params[1]);
 		my_cdata->params[0] = my_cdata->params[1];
 		my_cdata->params[1] = NULL;
 		if(!put_cqueue(temp_ct))
@@ -1760,34 +1751,7 @@
 	} else {
 		my_cdata->params[0] = NULL;
 	}
-EndFuncNoLocals(Resume)
-DISPATCH
-
-FuncNoLocals(CallSP_Async, NumParams 1)
-	Param(0, TYPE_WORKER)
-
-	temp_ct = new_context();
-	cdata = alloc_cdata(temp_ct, NULL, ((t_Worker *)my_cdata->params[0])->payload.Size);
-	cdata->func = END_THREAD;
-	for(idx = 0; idx < ((t_Worker *)my_cdata->params[0])->payload.Size; ++idx)
-	{
-		cdata->params[idx] = add_ref(((object **)(((t_Worker *)my_cdata->params[0])+1))[idx]);
-	}
-	temp_ct->start_func = ((t_Worker *)my_cdata->params[0])->payload.Index;
-	temp_ct->resume_cdata = cdata;
-	if(put_cqueue(temp_ct))
-	{
-		cdata = my_cdata;
-	} else {
-		ct->resume_cdata = my_cdata;
-		temp_ct->runafter = ct;
-		ct = temp_ct;
-		ct->resume_cdata = NULL;
-		func = ct->start_func;
-		ct->start_func = -1;
-		DISPATCH
-	}
-EndFuncNoLocals(CallSP_Async)
+EndFuncNoLocals
 DISPATCH\n"]
 		]Append[Fold[_Text C Program[?, ?, [program]Type Registry >>], "", Filter[[program]Functions >>, Native[?]]]]
 		]Append["
@@ -1813,10 +1777,7 @@
 	if(ct = get_cqueue())
 	{
 		cdata = ct->resume_cdata;
-		if(ct->start_func >= 0)
-			func = ct->start_func;
-		else
-			func = cdata->func;
+		func = cdata->func;
 		DISPATCH
 	}
 NOTHING_TO_DO:
--- a/kernel.rhope	Sat Nov 20 20:03:25 2010 +0000
+++ b/kernel.rhope	Sat Nov 20 17:48:22 2010 -0500
@@ -287,3 +287,18 @@
 	]|[Int32[Trunc UInt16[[data]Index[3]]]]
 }
 
+_Call Async[func,cont:out]
+{
+	Resume[cont,0]
+	{ out <- Call[func] }
+}
+Call Async[func:out]
+{
+	out <- Pause[_Call Async[func,?]]
+}
+
+Yield[:out]
+{
+	out <- Pause[Resume[?,Yes]]
+}
+
--- a/nworker_c.rhope	Sat Nov 20 20:03:25 2010 +0000
+++ b/nworker_c.rhope	Sat Nov 20 17:48:22 2010 -0500
@@ -469,6 +469,7 @@
 
 Infer Types@NWorker[worker,prog:out]
 {
+	Print[["Infer Types: "]Append[[worker]Name >>]]
 	out <- [worker]Nodes <<[Fold[Infer Types Node[?, ?, ?, prog, worker], [worker]Nodes >>, [worker]Nodes >>]]
 }
 
@@ -1601,7 +1602,7 @@
 
 Register Builtins@NProgram[prog:out]
 {
-	registered <- [[[[[[[[[[[prog]Register Worker["Print", "rhope", 1, 1]
+	registered <- [[[[[[[[[[prog]Register Worker["Print", "rhope", 1, 1]
 	]Register Worker["If@Boolean", "rhope", 1, 2]
 	]Register Worker["Build", "rhope", 1, 1]
 	]Register Worker["Blueprint Of", "rhope", 1, 1]
@@ -1610,10 +1611,9 @@
 	]Register Worker["Blueprint From ID", "rhope", 1, 2]
 	]Register Worker["Pause", "rhope", 1, 1]
 	]Register Worker["Resume", "rhope", 2, 2]
-	]Register Worker["Call Async", "rhope", 1, 1]
 	]Register Number Methods
 	
-	out <- [[[[[[[registered]Bind Worker["If@Boolean",
+	out <- [[[[[[[[[registered]Bind Worker["If@Boolean",
 		[[[[[NWorker["rhope"]
 		]Inputs <<[("condition")]
 		]Input Types <<[ [()]Append[Type Instance["Boolean"]] ]
@@ -1662,6 +1662,20 @@
 		]Outputs <<[("bp","none")]
 		]Output Types <<[ [[()]Append[Type Instance["Blueprint"]]]Append[Type Instance["Any Type"]]]
 		]Builtin? << [Yes]]
+	]Bind Worker["Pause",
+		[[[[[NWorker["rhope"]
+		]Inputs <<[("tocall")]
+		]Input Types <<[ [()]Append[Type Instance["Worker"]]]
+		]Outputs <<[("resumeval")]
+		]Output Types <<[ [()]Append[Type Instance["Any Type"]] ]
+		]Builtin? <<[Yes]]
+	]Bind Worker["Resume",
+		[[[[[NWorker["rhope"]
+		]Inputs <<[("toresume","resumeval")]
+		]Input Types <<[ [[()]Append[Type Instance["Contest"]]]Append[Type Instance["Any Type"]] ]
+		]Outputs <<[("success","cantresume")]
+		]Output Types <<[ [[()]Append[Type Instance["Any Type"]]]Append[Type Instance["Any Type"]] ]
+		]Builtin? <<[Yes]]
 }
 
 Find Worker@NProgram[prog, name:out,notfound]
--- a/runtime/builtin.c	Sat Nov 20 20:03:25 2010 +0000
+++ b/runtime/builtin.c	Sat Nov 20 17:48:22 2010 -0500
@@ -2,6 +2,7 @@
 #include "object.h"
 #include "integer.h"
 #include "bool.h"
+#include "context.h"
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -121,6 +122,13 @@
 	return (object*)b;
 }
 
+object * make_Context(context * ct)
+{
+	t_Context * c = (t_Context *)new_object(TYPE_CONTEXT);
+	c->ct = ct;
+	return (object *)c;
+}
+
 object * make_Worker(int32_t index, int16_t initialsize, int16_t initialcount)
 {
 	t_Worker * worker = (t_Worker *)_internal_worker_alloc(initialsize);
--- a/runtime/context.c	Sat Nov 20 20:03:25 2010 +0000
+++ b/runtime/context.c	Sat Nov 20 17:48:22 2010 -0500
@@ -60,7 +60,6 @@
 	c->current_stack = c->stack_begin;
 	c->resume_cdata = c->runafter = c->transaction = NULL;	
 	c->resumeable = 0;
-	c->start_func = -1;
 	return c;
 }
 
--- a/runtime/context.h	Sat Nov 20 20:03:25 2010 +0000
+++ b/runtime/context.h	Sat Nov 20 17:48:22 2010 -0500
@@ -22,7 +22,6 @@
 	struct context *runafter;
 	calldata       *resume_cdata;
 	int32_t        resumeable;
-	int32_t        start_func;
 } context;
 
 Box(context *,ct,Context);
@@ -35,4 +34,5 @@
 void cqueue_init();
 context * get_cqueue();
 int32_t put_cqueue(context * ct);
+object * make_Context(context * ct);
 #endif //_CONTEXT_H_
--- a/runtime/func.h	Sat Nov 20 20:03:25 2010 +0000
+++ b/runtime/func.h	Sat Nov 20 17:48:22 2010 -0500
@@ -28,8 +28,8 @@
 #define ResumeEntry(num,name) &&r##num##_##name,
 #define DispatchVar void * funcs[] = { DispatchEntries };
 #define DISPATCH goto *funcs[func];
-#define EndEntry &&DO_END
-#define EndThreadEntry &&DO_END_THREAD;
+#define EndEntry &&DO_END,
+#define EndThreadEntry &&DO_END_THREAD
 #endif
 
 
@@ -268,6 +268,24 @@
 	my_cdata = cdata->lastframe;\
 	RESUME_SELF\
 	lv_ ## myname = cdata->vars;
+	
+#define ValCallNoLocals(tocall,numparams,resumeto,myname)\
+	last_vcparam++;\
+	while(last_vcparam < ((t_Worker *)tocall)->payload.Size)\
+	{\
+		if (((object **)(((t_Worker *)tocall)+1))[last_vcparam]) \
+			cdata->params[last_vcparam] = add_ref(((object **)(((t_Worker *)tocall)+1))[last_vcparam]);\
+		++last_vcparam;\
+	}\
+	cdata->func = RES_  ## resumeto ## _ ## myname;\
+	cdata->num_params = numparams + ((t_Worker *)tocall)->payload.Count;\
+	func = ((t_Worker *)tocall)->payload.Index;\
+	PAUSE_SELF
+
+#define ValCallNoLocalsPostlude(resumeto,myname)\
+r ## resumeto ## _ ## myname:\
+	my_cdata = cdata->lastframe;\
+	RESUME_SELF
 
 
 #define Call(tocall, numparams, resumeto, myname)\
--- a/testpauseresume.rhope	Sat Nov 20 20:03:25 2010 +0000
+++ b/testpauseresume.rhope	Sat Nov 20 17:48:22 2010 -0500
@@ -1,20 +1,16 @@
-
-Yield[:out]
-{
-	out <- Pause[Resume[?,0]]
-}
 
 Foo[:out]
 {
-	Print["Foo!"]
+	a <- Yield[]
+	Val[a]
+	{ out <- Print["Foo!"] }
 }
 
 Main[:out]
 {
 	Call Async[Foo[?]]
-	{ Call Async[Foo[?]]
-	{ Call Async[Foo[?]]
-	{ Get Char[]
+	{ got char <- Get Char[] }
+	Val[got char]
 	{ out <- Yield[] }
 }