Переглянути джерело

Added context to processes for commands context that can be inherited (important for ActiveCells access)

git-svn-id: https://svn.inf.ethz.ch/svn/lecturers/a2/trunk@7485 8c9fc860-2736-0410-a75d-ab315db34111
felixf 7 роки тому
батько
коміт
c321c541df

+ 5 - 5
source/Commands.Mod

@@ -134,8 +134,9 @@ TYPE
 			AWAIT(state >= this);
 			AWAIT(state >= this);
 			res := SELF.res; COPY(SELF.msg, msg);
 			res := SELF.res; COPY(SELF.msg, msg);
 		END Join;
 		END Join;
-
+		
 	BEGIN {ACTIVE, SAFE}
 	BEGIN {ACTIVE, SAFE}
+		Objects.SetContext(context);
 		IF ~exception THEN
 		IF ~exception THEN
 			exception := TRUE; (* catch exceptions from now on *)
 			exception := TRUE; (* catch exceptions from now on *)
 			module := Modules.ThisModule(moduleName, res, msg);
 			module := Modules.ThisModule(moduleName, res, msg);
@@ -184,7 +185,6 @@ VAR
 	trace: BOOLEAN;
 	trace: BOOLEAN;
 	defaultContext: Context; (* Fallback. Note that this context would be shared by different users -- may be used for tracing though *)
 	defaultContext: Context; (* Fallback. Note that this context would be shared by different users -- may be used for tracing though *)
 
 
-
 (* Create a ready on a empty string *)
 (* Create a ready on a empty string *)
 
 
 PROCEDURE GetEmptyReader() : Streams.Reader;
 PROCEDURE GetEmptyReader() : Streams.Reader;
@@ -233,10 +233,10 @@ END Split;
 (**	Can be called by a command to retrieve the context associated with its active object. *)
 (**	Can be called by a command to retrieve the context associated with its active object. *)
 
 
 PROCEDURE GetContext*() : Context;
 PROCEDURE GetContext*() : Context;
-VAR object: ANY;
+VAR context: ANY;
 BEGIN
 BEGIN
-	object := Objects.ActiveObject();
-	IF (object # NIL) & (object IS Runner) & (object(Runner).state = Loaded) THEN RETURN object(Runner).context;
+	context := Objects.CurrentContext();
+	IF (context # NIL) & (context IS Context) THEN RETURN context(Context)
 	ELSE RETURN defaultContext
 	ELSE RETURN defaultContext
 	END;
 	END;
 END GetContext;
 END GetContext;

+ 11 - 1
source/Coop.Objects.Mod

@@ -90,7 +90,6 @@ TYPE
 		restartPC-: LONGINT;   (** entry point of body, for SAFE exception recovery *)
 		restartPC-: LONGINT;   (** entry point of body, for SAFE exception recovery *)
 		restartSP-: LONGINT;   (** stack level at start of body, for SAFE exception recovery *)
 		restartSP-: LONGINT;   (** stack level at start of body, for SAFE exception recovery *)
 		cpuCycles, lastCpuCycles : CpuCyclesArray;
 		cpuCycles, lastCpuCycles : CpuCyclesArray;
-
 	END Process;
 	END Process;
 
 
 	FinalizedCollection* = OBJECT
 	FinalizedCollection* = OBJECT
@@ -218,6 +217,16 @@ BEGIN
 	RETURN NIL;
 	RETURN NIL;
 END CurrentProcess;
 END CurrentProcess;
 
 
+PROCEDURE CurrentContext*(): ANY;
+BEGIN
+	RETURN NIL; (* stub *)
+END CurrentContext;
+
+PROCEDURE SetContext*(context: ANY);
+BEGIN
+	(* stub *)
+END SetContext;
+
 (* Return stack bottom of process. For compatibility WinAos/UnixAos/NativeAos  *)
 (* Return stack bottom of process. For compatibility WinAos/UnixAos/NativeAos  *)
 PROCEDURE GetStackBottom*(p: Process): ADDRESS;
 PROCEDURE GetStackBottom*(p: Process): ADDRESS;
 BEGIN
 BEGIN
@@ -232,6 +241,7 @@ BEGIN {UNCOOPERATIVE, UNCHECKED}
 	IF activity.object # NIL THEN RETURN activity.object ELSE RETURN activity END;
 	IF activity.object # NIL THEN RETURN activity.object ELSE RETURN activity END;
 END ActiveObject;
 END ActiveObject;
 
 
+
 (** Return the ID of the active currently executing process. *)
 (** Return the ID of the active currently executing process. *)
 PROCEDURE GetProcessID* (): LONGINT;
 PROCEDURE GetProcessID* (): LONGINT;
 BEGIN
 BEGIN

+ 20 - 3
source/Generic.Unix.Objects.Mod

@@ -192,9 +192,9 @@ TYPE
 		procID-		: LONGINT;				(*! processor ID where running, not used in UnixAos *)
 		procID-		: LONGINT;				(*! processor ID where running, not used in UnixAos *)
 		state-			: Machine.State;	
 		state-			: Machine.State;	
 		context-		: Unix.McontextDesc;
 		context-		: Unix.McontextDesc;
-		state0	: ARRAY 2048 OF CHAR;		(* thread state at body start, used for restart after trap *)
-					
-				
+		state0	: ARRAY 2048 OF CHAR;		(* thread state at body start, used for restart after trap *)					
+		context: ANY; (* commands context *)
+
 		PROCEDURE FindRoots*;
 		PROCEDURE FindRoots*;
 		VAR sp, bp, n, a0, a1, adr: ADDRESS; desc: Modules.ProcedureDescPointer; i: LONGINT; p {UNTRACED}: ANY;
 		VAR sp, bp, n, a0, a1, adr: ADDRESS; desc: Modules.ProcedureDescPointer; i: LONGINT; p {UNTRACED}: ANY;
 		BEGIN
 		BEGIN
@@ -282,6 +282,7 @@ TYPE
 			flags := fl;
 			flags := fl;
 			priority := prio;
 			priority := prio;
 			nextProcess := NIL;
 			nextProcess := NIL;
+			context := CurrentContext();
 			IF root # NIL THEN
 			IF root # NIL THEN
 				newProcess := SELF;
 				newProcess := SELF;
 				ASSERT( bodyProc # NIL );
 				ASSERT( bodyProc # NIL );
@@ -530,6 +531,22 @@ TYPE
 		RETURN p
 		RETURN p
 	END CurrentProcess;
 	END CurrentProcess;
 	
 	
+	PROCEDURE CurrentContext*(): ANY;
+	VAR p: Process;
+	BEGIN
+		p := CurrentProcess();
+		IF p # NIL THEN RETURN p.context
+		ELSE RETURN NIL
+		END;
+	END CurrentContext;
+
+	PROCEDURE SetContext*(context: ANY);
+	VAR p: Process;
+	BEGIN
+		p := CurrentProcess();
+		IF p # NIL THEN p.context := context END;
+	END SetContext;
+	
 	PROCEDURE CurrentProcess0( ): Process;	
 	PROCEDURE CurrentProcess0( ): Process;	
 	VAR me: Unix.Thread_t;  p: Process;
 	VAR me: Unix.Thread_t;  p: Process;
 	BEGIN
 	BEGIN

+ 18 - 0
source/Objects.Mod

@@ -91,6 +91,7 @@ TYPE
 		oldReturnPC: ADDRESS;
 		oldReturnPC: ADDRESS;
 		cpuCycles, lastCpuCycles : CpuCyclesArray;
 		cpuCycles, lastCpuCycles : CpuCyclesArray;
 		prioRequests : ARRAY NumPriorities OF LONGINT; (* priorities of processes that wait for resources locked by this process, only the highest priority per resource is stored *)
 		prioRequests : ARRAY NumPriorities OF LONGINT; (* priorities of processes that wait for resources locked by this process, only the highest priority per resource is stored *)
+		context: ANY;
 
 
 		(* set priority of process: Machine.Objects lock is taken *)
 		(* set priority of process: Machine.Objects lock is taken *)
 		PROCEDURE SetPriority(p : LONGINT);
 		PROCEDURE SetPriority(p : LONGINT);
@@ -698,6 +699,22 @@ PROCEDURE CurrentProcess*( ): Process;
 BEGIN
 BEGIN
 	RETURN SYSTEM.VAL(Process, Machine.GetProcessPtr());
 	RETURN SYSTEM.VAL(Process, Machine.GetProcessPtr());
 END CurrentProcess;
 END CurrentProcess;
+	
+PROCEDURE CurrentContext*(): ANY;
+VAR p: Process;
+BEGIN
+	p := CurrentProcess();
+	IF p # NIL THEN RETURN p.context
+	ELSE RETURN NIL
+	END;
+END CurrentContext;
+
+PROCEDURE SetContext*(context: ANY);
+VAR p: Process;
+BEGIN
+	p := CurrentProcess();
+	IF p # NIL THEN p.context := context END;
+END SetContext;
 
 
 (* Return stack bottom of process. For compatibility WinAos/UnixAos/NativeAos  *)
 (* Return stack bottom of process. For compatibility WinAos/UnixAos/NativeAos  *)
 PROCEDURE GetStackBottom*(p: Process): ADDRESS;
 PROCEDURE GetStackBottom*(p: Process): ADDRESS;
@@ -1218,6 +1235,7 @@ BEGIN
 	NewProcess(body, flags, obj, t);
 	NewProcess(body, flags, obj, t);
 	Machine.Acquire(Machine.Objects);
 	Machine.Acquire(Machine.Objects);
 	t.id := nextProcessID; INC(nextProcessID);
 	t.id := nextProcessID; INC(nextProcessID);
+	t.context := CurrentContext();
 	IF priority = 0 THEN	(* no priority specified *)
 	IF priority = 0 THEN	(* no priority specified *)
 		t.priority := running[Machine.ID ()].priority (* inherit priority of creator *)
 		t.priority := running[Machine.ID ()].priority (* inherit priority of creator *)
 	ELSIF priority > 0 THEN (* positive priority specified *)
 	ELSIF priority > 0 THEN (* positive priority specified *)

+ 18 - 2
source/Unix.Objects.Mod

@@ -209,7 +209,7 @@ TYPE
 		procID-		: LONGINT;				(* processor ID where running, not used in UnixAos *)
 		procID-		: LONGINT;				(* processor ID where running, not used in UnixAos *)
 		state-			: Machine.State;		(*! not used in UnixAos! *)
 		state-			: Machine.State;		(*! not used in UnixAos! *)
 		state0	: ARRAY 2048 OF CHAR;		(* thread state at body start, used for restart after trap *)
 		state0	: ARRAY 2048 OF CHAR;		(* thread state at body start, used for restart after trap *)
-					
+		context: ANY; (* commands cotext *)
 				
 				
 		PROCEDURE FindRoots*;
 		PROCEDURE FindRoots*;
 		VAR sp, ptr: ADDRESS;
 		VAR sp, ptr: ADDRESS;
@@ -265,6 +265,7 @@ TYPE
 			flags := fl;
 			flags := fl;
 			priority := prio;
 			priority := prio;
 			nextProcess := NIL;
 			nextProcess := NIL;
+			context := CurrentContext();
 			IF root # NIL THEN
 			IF root # NIL THEN
 				newProcess := SELF;
 				newProcess := SELF;
 				ASSERT( bodyProc # NIL );
 				ASSERT( bodyProc # NIL );
@@ -463,7 +464,22 @@ TYPE
 		RETURN p
 		RETURN p
 	END CurrentProcess;
 	END CurrentProcess;
 
 
-	
+	PROCEDURE CurrentContext*(): ANY;
+	VAR p: Process;
+	BEGIN
+		p := CurrentProcess();
+		IF p # NIL THEN RETURN p.context
+		ELSE RETURN NIL
+		END;
+	END CurrentContext;
+
+	PROCEDURE SetContext*(context: ANY);
+	VAR p: Process;
+	BEGIN
+		p := CurrentProcess();
+		IF p # NIL THEN p.context := context END;
+	END SetContext;
+
 	(* Return the active object currently executing. *)
 	(* Return the active object currently executing. *)
 	PROCEDURE ActiveObject*( ): ANY;		
 	PROCEDURE ActiveObject*( ): ANY;		
 	VAR p: Process;
 	VAR p: Process;

+ 20 - 1
source/Win32.Objects.Mod

@@ -160,6 +160,7 @@ TYPE
 		*)
 		*)
 		lastThreadTimes: HUGEINT;   (*ALEX 2005.12.12*)
 		lastThreadTimes: HUGEINT;   (*ALEX 2005.12.12*)
 		gcContext: GCContext;
 		gcContext: GCContext;
+		context: ANY; (* commands contect *)
 
 
 		PROCEDURE FindRoots;   (* override, called while GC, replaces Threads.CheckStacks *)
 		PROCEDURE FindRoots;   (* override, called while GC, replaces Threads.CheckStacks *)
 		VAR sp: ADDRESS; res: Kernel32.BOOL; pc, bp: ADDRESS;
 		VAR sp: ADDRESS; res: Kernel32.BOOL; pc, bp: ADDRESS;
@@ -239,7 +240,7 @@ TYPE
 								FOR i := 0 TO LEN(desc.offsets)-1 DO
 								FOR i := 0 TO LEN(desc.offsets)-1 DO
 									adr := bp + desc.offsets[i]; (* pointer at offset *)
 									adr := bp + desc.offsets[i]; (* pointer at offset *)
 									SYSTEM.GET(adr, p); (* load pointer *)
 									SYSTEM.GET(adr, p); (* load pointer *)
-									IF p # NIL THEN 
+									IF p # NIL THEN
 										Heaps.Mark(p);
 										Heaps.Mark(p);
 									END;
 									END;
 								END;
 								END;
@@ -442,6 +443,22 @@ BEGIN{UNCHECKED} (* makes sure that Enter and Leave are not emitted *)
 	RETURN SYSTEM.VAL(Process, Kernel32.TlsGetValue(tlsIndex));
 	RETURN SYSTEM.VAL(Process, Kernel32.TlsGetValue(tlsIndex));
 END CurrentProcess;
 END CurrentProcess;
 
 
+PROCEDURE CurrentContext*(): ANY;
+VAR p: Process;
+BEGIN
+	p := CurrentProcess();
+	IF p # NIL THEN RETURN p.context
+	ELSE RETURN NIL
+	END;
+END CurrentContext;
+
+PROCEDURE SetContext*(context: ANY);
+VAR p: Process;
+BEGIN
+	p := CurrentProcess();
+	IF p # NIL THEN p.context := context END;
+END SetContext;
+
 (* Return stack bottom of process. For compatibility WinAos/UnixAos/NativeAos  *)
 (* Return stack bottom of process. For compatibility WinAos/UnixAos/NativeAos  *)
 PROCEDURE GetStackBottom*(p: Process): ADDRESS;
 PROCEDURE GetStackBottom*(p: Process): ADDRESS;
 BEGIN
 BEGIN
@@ -813,11 +830,13 @@ BEGIN
 	Kernel32.Sleep(999999);   (* wait until dependent threads terminated *)
 	Kernel32.Sleep(999999);   (* wait until dependent threads terminated *)
 END TerminateProc;
 END TerminateProc;
 
 
+
 (* Allocate a new process associated with "obj". Must be outside lock region, because of potential GC. *)
 (* Allocate a new process associated with "obj". Must be outside lock region, because of potential GC. *)
 PROCEDURE NewProcess(body: Body; priority: LONGINT;  flags: SET; obj: ProtectedObject; VAR new: Process);
 PROCEDURE NewProcess(body: Body; priority: LONGINT;  flags: SET; obj: ProtectedObject; VAR new: Process);
 VAR t,r: Process;  fn: Heaps.FinalizerNode;
 VAR t,r: Process;  fn: Heaps.FinalizerNode;
 BEGIN
 BEGIN
 	NEW(t);
 	NEW(t);
+	t.context := CurrentContext(); (* inherit contet from parent process *)
 	t.handle := 0;
 	t.handle := 0;
 	IF priority = 0 THEN  (* no priority specified *)
 	IF priority = 0 THEN  (* no priority specified *)
 		r := CurrentProcess();
 		r := CurrentProcess();