Activities.Mod 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. (* Runtime support for activities *)
  2. (* Copyright (C) Florian Negele *)
  3. (** The module provides the runtime support for activities associated with active objects. *)
  4. (** It implements a basic task scheduler that distributes the work of concurrent activities to logical processors. *)
  5. (** In addition, it also provides a framework for implementing synchronisation primitives. *)
  6. MODULE Activities;
  7. IMPORT SYSTEM, BaseTypes, Counters, CPU, Processors, Queues, Timer;
  8. (** Represents one of four different priorities of an activity. *)
  9. TYPE Priority* = SIZE;
  10. CONST SafeStackSize = 512 * SIZE OF ADDRESS;
  11. CONST InitialStackSize = CPU.StackSize;
  12. CONST MaximumStackSize* = 1024 * InitialStackSize;
  13. CONST (** Indicates the lowest priority used for idle processors. *) IdlePriority* = 0;
  14. CONST (** Indicates the default priority of new activities. *) DefaultPriority* = 1;
  15. CONST (** Indicates a higher priority than the default. *) HighPriority* = 2;
  16. CONST (** Indicates the highest of all priorities. *) RealtimePriority* = 3;
  17. CONST LowestPriority = IdlePriority; HighestPriority = RealtimePriority;
  18. CONST Priorities = HighestPriority - LowestPriority + 1;
  19. (** Represents a procedure that is called after the execution of an activity has been suspended by the {{{[[Activities.SwitchTo]]}}} procedure. *)
  20. TYPE SwitchFinalizer* = PROCEDURE (previous {UNTRACED}: Activity; value: ADDRESS);
  21. (* Represents the stack of an activity. *)
  22. TYPE Stack = POINTER {DISPOSABLE} TO ARRAY OF CHAR;
  23. TYPE StackRecord = POINTER {UNSAFE} TO RECORD
  24. prev {UNTRACED}, next {UNTRACED}: Stack;
  25. END;
  26. (** Represents the handler identifying activities that are currently either running or suspended. *)
  27. TYPE Activity* = OBJECT {DISPOSABLE} (Queues.Item)
  28. VAR processor {UNTRACED}: POINTER {UNSAFE} TO Processor;
  29. VAR firstStack {UNTRACED}: Stack;
  30. VAR stackLimit: ADDRESS;
  31. VAR quantum := CPU.Quantum: LONGWORD;
  32. VAR priority: Priority;
  33. VAR finalizer := NIL: SwitchFinalizer;
  34. VAR previous: Activity; argument: ADDRESS;
  35. VAR framePointer {UNTRACED}: BaseTypes.StackFrame;
  36. VAR procedure: PROCEDURE;
  37. VAR object-: BaseTypes.Object;
  38. VAR bound := FALSE: BOOLEAN;
  39. VAR startTime-: Timer.Counter;
  40. VAR time- := 0: HUGEINT;
  41. VAR stack {UNTRACED}: Stack;
  42. PROCEDURE &InitializeActivity (procedure: PROCEDURE; priority: Priority);
  43. VAR stackRecord {UNTRACED}: StackRecord; StackFrameDescriptor {UNTRACED} EXTERN "BaseTypes.StackFrame": BaseTypes.Descriptor;
  44. BEGIN {UNCOOPERATIVE, UNCHECKED}
  45. ASSERT (priority < Priorities);
  46. ASSERT (InitialStackSize > SafeStackSize);
  47. NEW (stack, InitialStackSize);
  48. ASSERT (stack # NIL);
  49. firstStack := stack;
  50. stackRecord := ADDRESS OF stack[0];
  51. stackRecord.next := NIL;
  52. stackRecord.prev := NIL;
  53. stackLimit := ADDRESS OF stack[SafeStackSize+3* SIZE OF ADDRESS]; SELF.priority := priority;
  54. framePointer := ADDRESS OF stack[InitialStackSize - 4 * SIZE OF ADDRESS - CPU.StackDisplacement];
  55. framePointer.caller := Start;
  56. framePointer.previous := NIL;
  57. framePointer.descriptor := ADDRESS OF StackFrameDescriptor;
  58. SELF.procedure := procedure;
  59. END InitializeActivity;
  60. PROCEDURE ~Finalize;
  61. VAR stackFrame {UNTRACED}: BaseTypes.StackFrame; currentActivity {UNTRACED}: Activity; stack{UNTRACED}, next{UNTRACED}: Stack; stackRecord{UNTRACED}: StackRecord;
  62. BEGIN {UNCOOPERATIVE, UNCHECKED}
  63. stackFrame := framePointer;
  64. currentActivity := SYSTEM.GetActivity ()(Activity); SYSTEM.SetActivity (SELF);
  65. WHILE stackFrame # NIL DO stackFrame.Reset; stackFrame := stackFrame.previous END;
  66. SYSTEM.SetActivity (currentActivity);
  67. stack := firstStack;
  68. REPEAT
  69. stackRecord := ADDRESS OF stack[0];
  70. next := stackRecord.next;
  71. DISPOSE (stack);
  72. stack := next;
  73. UNTIL stack = NIL;
  74. Finalize^;
  75. END Finalize;
  76. END Activity;
  77. (* Represents a handler for an activity that is associated with an active object. *)
  78. TYPE Process = OBJECT {DISPOSABLE} (Activity)
  79. PROCEDURE &InitializeProcess (procedure: PROCEDURE; priority: Priority; object: BaseTypes.Object);
  80. BEGIN {UNCOOPERATIVE, UNCHECKED}
  81. InitializeActivity (procedure, priority);
  82. ASSERT (object # NIL);
  83. ASSERT (object.action # NIL);
  84. SELF.object := object;
  85. object.action.activity := SELF;
  86. END InitializeProcess;
  87. PROCEDURE Unlink;
  88. BEGIN {UNCOOPERATIVE, UNCHECKED} object := NIL;
  89. END Unlink;
  90. PROCEDURE ~Finalize;
  91. VAR item: Queues.Item;
  92. BEGIN {UNCOOPERATIVE, UNCHECKED}
  93. IF object # NIL THEN
  94. object.action.activity := NIL;
  95. IF Queues.Dequeue (item, object.action.waitingQueue) THEN Resume (item(Activity)) END;
  96. Unlink;
  97. END;
  98. Finalize^;
  99. END Finalize;
  100. END Process;
  101. (* Stores information per processor. *)
  102. TYPE Processor = RECORD {ALIGNED (CPU.CacheLineSize)}
  103. assigning := FALSE: BOOLEAN;
  104. originalFramePointer: ADDRESS;
  105. readyQueue: ARRAY Priorities OF Queues.AlignedQueue;
  106. runningActivity: Activity;
  107. index: SIZE;
  108. END;
  109. VAR processors: ARRAY Processors.Maximum OF Processor;
  110. VAR readyQueue: ARRAY Priorities OF Queues.AlignedQueue;
  111. VAR working, physicalProcessors, virtualProcessors: Counters.AlignedCounter;
  112. (** Stores an atomic counter indicating the number of activities that are awaiting interrupts to occur. *)
  113. (** The scheduler stops its execution if all processors are idle, unless there are activities waiting for interrupts. *)
  114. VAR awaiting*: Counters.AlignedCounter;
  115. PROCEDURE GetProcessTime-(): HUGEINT;
  116. VAR activity: Activity; diff: Timer.Counter;
  117. BEGIN{UNCOOPERATIVE, UNCHECKED}
  118. activity := SYSTEM.GetActivity ()(Activity);
  119. diff := Timer.GetCounter()-activity.startTime;
  120. RETURN activity.time + diff;
  121. END GetProcessTime;
  122. (** Returns the handler of the current activity executing this procedure call. *)
  123. PROCEDURE GetCurrentActivity- (): {UNTRACED} Activity;
  124. BEGIN {UNCOOPERATIVE, UNCHECKED} RETURN SYSTEM.GetActivity ()(Activity);
  125. END GetCurrentActivity;
  126. (** Returns the unique index of the processor executing this procedure call. *)
  127. PROCEDURE GetCurrentProcessorIndex- (): SIZE;
  128. BEGIN {UNCOOPERATIVE, UNCHECKED} IF SYSTEM.GetActivity () # NIL THEN RETURN SYSTEM.GetActivity ()(Activity).processor.index ELSE RETURN 0 END;
  129. END GetCurrentProcessorIndex;
  130. (** Sets the priority of the current activity calling this procedure and returns the previous value. *)
  131. PROCEDURE SetCurrentPriority- (priority: Priority): Priority;
  132. VAR currentActivity {UNTRACED}: Activity; previousPriority: Priority;
  133. BEGIN {UNCOOPERATIVE, UNCHECKED}
  134. ASSERT (priority < Priorities);
  135. currentActivity := SYSTEM.GetActivity ()(Activity);
  136. previousPriority := currentActivity.priority;
  137. currentActivity.priority := priority;
  138. RETURN previousPriority;
  139. END SetCurrentPriority;
  140. (** Binds the calling activity to the currently executing processor. *)
  141. PROCEDURE BindToCurrentProcessor-;
  142. BEGIN {UNCOOPERATIVE, UNCHECKED}
  143. SYSTEM.GetActivity ()(Activity).bound := TRUE;
  144. END BindToCurrentProcessor;
  145. (** Returns whether there is an activity that is ready to run and has at least the specified priority. *)
  146. PROCEDURE Select- (VAR activity: Activity; minimum: Priority): BOOLEAN;
  147. VAR processor {UNTRACED}: POINTER {UNSAFE} TO Processor;
  148. VAR priority := HighestPriority + 1: Priority; item: Queues.Item;
  149. BEGIN {UNCOOPERATIVE, UNCHECKED}
  150. processor := SYSTEM.GetActivity ()(Activity).processor;
  151. REPEAT
  152. DEC (priority);
  153. IF Queues.Dequeue (item, processor.readyQueue[priority]) THEN
  154. activity := item(Activity); RETURN TRUE;
  155. ELSIF Queues.Dequeue (item, readyQueue[priority]) THEN
  156. activity := item(Activity);
  157. IF activity.bound & (activity.processor # processor) THEN
  158. Enqueue (activity, ADDRESS OF activity.processor.readyQueue[activity.priority]);
  159. ELSE
  160. RETURN TRUE;
  161. END;
  162. END;
  163. UNTIL priority = minimum;
  164. RETURN FALSE;
  165. END Select;
  166. (** Performs a cooperative task switch by suspending the execution of the current activity and resuming the execution of any other activity that is ready to continue. *)
  167. (** This procedure is called by the compiler whenever it detects that the time quantum of the current activity has expired. *)
  168. PROCEDURE Switch-;
  169. VAR currentActivity {UNTRACED}, nextActivity: Activity;
  170. BEGIN {UNCOOPERATIVE, UNCHECKED}
  171. currentActivity := SYSTEM.GetActivity ()(Activity);
  172. IF Select (nextActivity, currentActivity.priority) THEN
  173. SwitchTo (nextActivity, Enqueue, ADDRESS OF readyQueue[currentActivity.priority]);
  174. FinalizeSwitch;
  175. ELSE
  176. currentActivity.quantum := CPU.Quantum;
  177. END;
  178. END Switch;
  179. (* Switch finalizer that enqueues the previous activity to the specified ready queue. *)
  180. PROCEDURE Enqueue (previous {UNTRACED}: Activity; queue {UNTRACED}: POINTER {UNSAFE} TO Queues.Queue);
  181. BEGIN {UNCOOPERATIVE, UNCHECKED}
  182. Queues.Enqueue (previous, queue^);
  183. IF ADDRESS OF queue^ = ADDRESS OF readyQueue[IdlePriority] THEN RETURN END;
  184. IF Counters.Read (working) < Processors.count THEN Processors.ResumeAllProcessors END;
  185. END Enqueue;
  186. (** Resumes the execution of an activity that was suspended by a call to the {{{[[Activities.SwitchTo]]}}} procedure beforehand. *)
  187. PROCEDURE Resume- (activity: Activity);
  188. BEGIN {UNCOOPERATIVE, UNCHECKED}
  189. ASSERT (activity # NIL);
  190. Enqueue (activity, ADDRESS OF readyQueue[activity.priority])
  191. END Resume;
  192. (** Performs a synchronous task switch. *)
  193. (** The resumed activity continues its execution by first calling the specified finalizer procedure with the given argument. *)
  194. (** Each invocation of this procedure must be directly followed by a call to the {{{[[Activities.FinalizeSwitch]]}}} procedure. *)
  195. PROCEDURE SwitchTo- (VAR activity: Activity; finalizer: SwitchFinalizer; argument: ADDRESS);
  196. VAR currentActivity {UNTRACED}, nextActivity {UNTRACED}: Activity; diff: Timer.Counter;
  197. BEGIN {UNCOOPERATIVE, UNCHECKED}
  198. currentActivity := SYSTEM.GetActivity ()(Activity);
  199. currentActivity.framePointer := SYSTEM.GetFramePointer ();
  200. currentActivity.quantum := CPU.Quantum;
  201. diff := Timer.GetCounter() - currentActivity.startTime;
  202. currentActivity.time := currentActivity.time + diff;
  203. nextActivity := activity;
  204. nextActivity.processor := currentActivity.processor;
  205. nextActivity.finalizer := finalizer;
  206. nextActivity.argument := argument;
  207. nextActivity.previous := currentActivity;
  208. nextActivity.processor.runningActivity := nextActivity;
  209. nextActivity.startTime := Timer.GetCounter();
  210. activity := NIL;
  211. SYSTEM.SetActivity (nextActivity);
  212. SYSTEM.SetFramePointer (nextActivity.framePointer);
  213. END SwitchTo;
  214. (** Finalizes a task switch performed by calling the switch finalizer of the previously suspended activity. *)
  215. (** This procedure must be called after each invocation of the {{{[[Activities.SwitchTo]]}}} procedure. *)
  216. PROCEDURE FinalizeSwitch-;
  217. VAR currentActivity {UNTRACED}: Activity;
  218. BEGIN {UNCOOPERATIVE, UNCHECKED}
  219. currentActivity := SYSTEM.GetActivity ()(Activity);
  220. IF currentActivity.finalizer # NIL THEN currentActivity.finalizer (currentActivity.previous, currentActivity.argument) END;
  221. currentActivity.finalizer := NIL; currentActivity.previous := NIL;
  222. END FinalizeSwitch;
  223. (* Entry point for new activities. *)
  224. PROCEDURE Start;
  225. VAR currentActivity {UNTRACED}: Activity;
  226. VAR procedure {UNTRACED}: POINTER {UNSAFE} TO RECORD body: PROCEDURE (object {UNTRACED}: OBJECT) END;
  227. BEGIN {UNCOOPERATIVE, UNCHECKED}
  228. FinalizeSwitch;
  229. currentActivity := SYSTEM.GetActivity ()(Activity);
  230. procedure := ADDRESS OF currentActivity.procedure;
  231. procedure.body (currentActivity.object);
  232. TerminateCurrentActivity;
  233. END Start;
  234. (** This procedure is called by the compiler for each {{{NEW}}} statement that creates an active object. *)
  235. (** It associates an active object with a new activity that begins its execution with the specified body procedure. *)
  236. PROCEDURE Create- (body: PROCEDURE; priority: Priority; object {UNTRACED}: BaseTypes.Object);
  237. VAR activity: Process;
  238. BEGIN {UNCOOPERATIVE, UNCHECKED}
  239. IF priority = IdlePriority THEN priority := SYSTEM.GetActivity ()(Activity).priority END;
  240. NEW (activity, body, priority, object);
  241. ASSERT (activity # NIL);
  242. Resume (activity);
  243. END Create;
  244. (** Creates an activity that pretends to be executed on a distinct processor. *)
  245. PROCEDURE CreateVirtualProcessor- (): {UNTRACED} Activity;
  246. VAR activity {UNTRACED}: Activity; index: SIZE;
  247. BEGIN {UNCOOPERATIVE, UNCHECKED}
  248. NEW (activity, NIL, DefaultPriority);
  249. ASSERT (activity # NIL);
  250. index := Counters.Increment (virtualProcessors, 1) + Processors.count;
  251. ASSERT (index < Processors.Maximum);
  252. activity.processor := ADDRESS OF processors[index];
  253. activity.processor.index := index;
  254. activity.bound := TRUE;
  255. RETURN activity;
  256. END CreateVirtualProcessor;
  257. (** Temporarily exchanges the currently running activity with a virtual processor in order to call the specified procedure in a different context. *)
  258. PROCEDURE CallVirtual- (procedure: PROCEDURE (value: ADDRESS); value: ADDRESS; processor {UNTRACED}: Activity);
  259. VAR currentActivity {UNTRACED}: Activity; stackPointer: ADDRESS;
  260. BEGIN {UNCOOPERATIVE, UNCHECKED}
  261. ASSERT (processor # NIL);
  262. currentActivity := SYSTEM.GetActivity ()(Activity); stackPointer := SYSTEM.GetStackPointer (); SYSTEM.SetActivity (processor);
  263. SYSTEM.SetStackPointer (ADDRESS OF processor.stack[LEN (processor.stack) - CPU.StackDisplacement]);
  264. procedure (value); SYSTEM.SetActivity (currentActivity); SYSTEM.SetStackPointer (stackPointer);
  265. END CallVirtual;
  266. (** Creates a new activity that calls the specified procedure. *)
  267. PROCEDURE Call- (procedure: PROCEDURE);
  268. VAR activity: Activity;
  269. BEGIN {UNCOOPERATIVE, UNCHECKED}
  270. NEW (activity, procedure, DefaultPriority);
  271. ASSERT (activity # NIL);
  272. Resume (activity);
  273. END Call;
  274. (** Starts the scheduler on the current processor by creating a new activity that calls the specified procedure. *)
  275. (** This procedure is called by the runtime system once during the initialization of each processor. *)
  276. PROCEDURE Execute- (procedure: PROCEDURE);
  277. VAR previousActivity {UNTRACED}: Activity;
  278. BEGIN {UNCOOPERATIVE, UNCHECKED}
  279. SYSTEM.SetActivity (NIL);
  280. BeginExecution (procedure);
  281. previousActivity := SYSTEM.GetActivity ()(Activity);
  282. previousActivity.processor.runningActivity := NIL;
  283. SYSTEM.SetActivity (NIL);
  284. Dispose (previousActivity, NIL);
  285. END Execute;
  286. (* Turns the calling procedure temporarily into an activity that begins its execution with the specified procedure. *)
  287. PROCEDURE BeginExecution (procedure: PROCEDURE);
  288. VAR activity {UNTRACED}: Activity; index: SIZE;
  289. BEGIN {UNCOOPERATIVE, UNCHECKED}
  290. NEW (activity, procedure, DefaultPriority);
  291. ASSERT (activity # NIL);
  292. index := Counters.Increment (physicalProcessors, 1);
  293. ASSERT (index < Processors.count);
  294. activity.processor := ADDRESS OF processors[index];
  295. activity.processor.originalFramePointer := SYSTEM.GetFramePointer ();
  296. activity.processor.runningActivity := activity; activity.processor.index := index;
  297. ASSERT (Counters.Increment (working, 1) < Processors.count);
  298. IF (index = 0) & (Processors.count > 1) THEN Processors.StartAll END;
  299. SYSTEM.SetActivity (activity); SYSTEM.SetFramePointer (activity.framePointer);
  300. END BeginExecution;
  301. (* Yields the execution of the current activity to any activity with the given minimal priority. *)
  302. PROCEDURE YieldExecution (minimum: Priority; finalizer: SwitchFinalizer; value: ADDRESS);
  303. VAR nextActivity: Activity;
  304. BEGIN {UNCOOPERATIVE, UNCHECKED}
  305. LOOP
  306. IF Select (nextActivity, minimum) THEN
  307. SwitchTo (nextActivity, finalizer, value);
  308. FinalizeSwitch;
  309. ELSE
  310. IF Counters.Decrement (working, 1) + Counters.Read (awaiting) > 1 THEN Processors.SuspendCurrentProcessor END;
  311. IF Counters.Increment (working, 1) + Counters.Read (awaiting) = 0 THEN EXIT END;
  312. END;
  313. END;
  314. END YieldExecution;
  315. (* This procedure returns to the procedure that called BeginExecution. *)
  316. PROCEDURE EndExecution;
  317. VAR currentActivity {UNTRACED}: Activity;
  318. BEGIN {UNCOOPERATIVE, UNCHECKED}
  319. currentActivity := SYSTEM.GetActivity ()(Activity);
  320. currentActivity.framePointer := SYSTEM.GetFramePointer ();
  321. IF Counters.Decrement (working, 1) < Processors.count THEN Processors.ResumeAllProcessors END;
  322. SYSTEM.SetFramePointer (currentActivity.processor.originalFramePointer);
  323. END EndExecution;
  324. (** This is the default procedure for initially idle processors starting the scheduler using the {{{[[Activities.Execute]]}}} procedure. *)
  325. PROCEDURE Idle-;
  326. BEGIN {UNCOOPERATIVE, UNCHECKED}
  327. ASSERT (SetCurrentPriority (IdlePriority) = DefaultPriority);
  328. YieldExecution (IdlePriority + 1, Enqueue, ADDRESS OF readyQueue[IdlePriority]); EndExecution;
  329. END Idle;
  330. (** Terminates the execution of the current activity calling this procedure. *)
  331. (** This procedure is also invoked at the end of the body of an active object. *)
  332. PROCEDURE {NORETURN} TerminateCurrentActivity-;
  333. BEGIN {UNCOOPERATIVE, UNCHECKED}
  334. YieldExecution (IdlePriority, Dispose, NIL);
  335. EndExecution; HALT (1234);
  336. END TerminateCurrentActivity;
  337. (* Switch finalizer that disposes the resources of the terminated activity. *)
  338. PROCEDURE Dispose (previous {UNTRACED}: Activity; value: ADDRESS);
  339. BEGIN {UNCOOPERATIVE, UNCHECKED}
  340. DISPOSE (previous);
  341. END Dispose;
  342. (** This procedure is called by the compiler while executing a {{{WAIT}}} statement. *)
  343. (** It awaits the termination of all activities associated with an active object. *)
  344. PROCEDURE Wait- (object {UNTRACED}: BaseTypes.Object);
  345. VAR nextActivity: Activity; item: Queues.Item;
  346. BEGIN {UNCOOPERATIVE, UNCHECKED}
  347. ASSERT (object # NIL);
  348. IF object.action = NIL THEN RETURN END;
  349. IF object.action.activity = NIL THEN RETURN END;
  350. REPEAT UNTIL Select (nextActivity, IdlePriority);
  351. SwitchTo (nextActivity, EnqueueWaiting, object.action); FinalizeSwitch;
  352. WHILE Queues.Dequeue (item, object.action.waitingQueue) DO Resume (item (Activity)) END;
  353. END Wait;
  354. (* Switch finalizer that enqueues acitivities waiting on an active object. *)
  355. PROCEDURE EnqueueWaiting (previous {UNTRACED}: Activity; action {UNTRACED}: POINTER {UNSAFE} TO BaseTypes.Action);
  356. VAR item: Queues.Item;
  357. BEGIN {UNCOOPERATIVE, UNCHECKED}
  358. Queues.Enqueue (previous, action.waitingQueue);
  359. IF action.activity # NIL THEN RETURN END;
  360. IF Queues.Dequeue (item, action.waitingQueue) THEN Resume (item (Activity)) END;
  361. END EnqueueWaiting;
  362. PROCEDURE ReturnToStackSegment*;
  363. VAR
  364. stackFrame {UNTRACED}: BaseTypes.StackFrame;
  365. currentActivity {UNTRACED}: Activity;
  366. newStack {UNTRACED}: Stack;
  367. stackRecord {UNTRACED}: StackRecord;
  368. BEGIN{UNCOOPERATIVE, UNCHECKED}
  369. (* old stack pointer and base pointer have been pushed again, we have to revert this *)
  370. stackFrame := SYSTEM.GetFramePointer();
  371. (*
  372. TRACE(stackFrame.caller);
  373. TRACE(stackFrame.previous);
  374. previousFrame := stackFrame.previous;
  375. TRACE(ADDRESS OF previousFrame.caller + SIZE OF ADDRESS);
  376. TRACE(previousFrame.caller);
  377. TRACE(previousFrame.previous);
  378. *)
  379. currentActivity := SYSTEM.GetActivity ()(Activity);
  380. stackRecord := ADDRESS OF currentActivity.stack[0];
  381. newStack := stackRecord.prev;
  382. currentActivity.stack := newStack;
  383. currentActivity.stackLimit := ADDRESS OF newStack[SafeStackSize + 3 * SIZE OF ADDRESS];
  384. END ReturnToStackSegment;
  385. PROCEDURE {NOPAF} ReturnToStackSegment0;
  386. BEGIN{UNCOOPERATIVE, UNCHECKED}
  387. CPU.SaveResult;
  388. ReturnToStackSegment;
  389. CPU.RestoreResultAndReturn;
  390. END ReturnToStackSegment0;
  391. (** Expands the stack memory of the current activity to include the specified stack address and returns the new stack pointer to be set after the call. *)
  392. PROCEDURE ExpandStack- (address: ADDRESS; parSize: SIZE): ADDRESS;
  393. VAR
  394. currentActivity {UNTRACED}: Activity;
  395. varSize, minSize, newSize: SIZE; sp: ADDRESS;
  396. newStack {UNTRACED}: POINTER {DISPOSABLE} TO ARRAY OF CHAR;
  397. stackFrame {UNTRACED}, previousFrame {UNTRACED}, newFrame {UNTRACED}: BaseTypes.StackFrame;
  398. stackRecord{UNTRACED}, newStackRecord{UNTRACED}: StackRecord;
  399. BEGIN {UNCOOPERATIVE, UNCHECKED}
  400. (* check for valid argument *)
  401. currentActivity := SYSTEM.GetActivity ()(Activity);
  402. stackFrame := SYSTEM.GetFramePointer ();
  403. previousFrame := stackFrame.previous;
  404. varSize := stackFrame.previous - address;
  405. (*
  406. TRACE(SYSTEM.GetFramePointer(), address, varSize, parSize, size, stackFrame.caller);
  407. *)
  408. ASSERT(varSize >= 0);
  409. ASSERT(parSize >= 0);
  410. newSize := LEN (currentActivity.stack); (* current stack size *)
  411. minSize := SafeStackSize + parSize + varSize + 3 * SIZEOF(ADDRESS) (* stack frame *) + 3 * SIZEOF(ADDRESS) (* prev, next *);
  412. REPEAT INC (newSize, newSize) UNTIL newSize >= minSize;
  413. ASSERT (newSize <= MaximumStackSize);
  414. stackRecord := ADDRESS OF currentActivity.stack[0];
  415. newStack := stackRecord.next;
  416. IF (newStack = NIL) OR (LEN(newStack) < newSize) THEN
  417. NEW (newStack, newSize);
  418. ASSERT (newStack # NIL);
  419. newStackRecord := ADDRESS OF newStack[0];
  420. newStackRecord.prev := currentActivity.stack;
  421. newStackRecord.next := NIL;
  422. stackRecord.next := newStack;
  423. ELSE
  424. newStackRecord := ADDRESS OF newStack[0];
  425. ASSERT(newStackRecord.prev = currentActivity.stack);
  426. ASSERT(stackRecord.next = newStack);
  427. END;
  428. newSize := LEN(newStack);
  429. newFrame := ADDRESS OF newStack[0] + newSize- parSize - 3*SIZE OF ADDRESS;
  430. newFrame.previous := stackFrame.previous;
  431. newFrame.descriptor := previousFrame.descriptor;
  432. newFrame.caller := ReturnToStackSegment0;
  433. previousFrame.descriptor := stackFrame.descriptor; (* trick to get a base stack frame descriptor *)
  434. stackFrame.previous := newFrame;
  435. SYSTEM.MOVE(ADDRESS OF previousFrame.caller + SIZE OF ADDRESS, ADDRESS OF newFrame.caller + SIZE OF ADDRESS, parSize); (* copy parameters *)
  436. sp := ADDRESSOF(newFrame.descriptor) - varSize;
  437. currentActivity.stack := newStack;
  438. currentActivity.stackLimit := ADDRESS OF newStack[SafeStackSize + 3 * SIZE OF ADDRESS];
  439. RETURN sp;
  440. END ExpandStack;
  441. (** Returns whether the specified address corresponds to a local variable that resides on the stack of the current activity calling this procedure. *)
  442. PROCEDURE IsLocalVariable- (address: ADDRESS): BOOLEAN;
  443. VAR currentActivity {UNTRACED}: Activity; begin, end: ADDRESS; stack {UNTRACED}: Stack; stackRecord {UNTRACED}: StackRecord;
  444. BEGIN {UNCOOPERATIVE, UNCHECKED}
  445. currentActivity := SYSTEM.GetActivity ()(Activity);
  446. IF currentActivity = NIL THEN RETURN FALSE END;
  447. stack := currentActivity.firstStack;
  448. REPEAT
  449. begin := ADDRESS OF stack[0];
  450. end := begin + LEN (stack);
  451. IF (address >= begin) & (address < end) THEN RETURN TRUE END;
  452. stackRecord := begin;
  453. stack := stackRecord.next;
  454. UNTIL stack = NIL;
  455. RETURN FALSE;
  456. END IsLocalVariable;
  457. (** Returns whether any activity is currently executing an assignment statement. *)
  458. PROCEDURE AssignmentsInProgress- (): BOOLEAN;
  459. VAR i: SIZE;
  460. BEGIN {UNCOOPERATIVE, UNCHECKED}
  461. FOR i := 0 TO Processors.Maximum - 1 DO IF processors[i].assigning THEN RETURN TRUE END END; RETURN FALSE;
  462. END AssignmentsInProgress;
  463. (** Terminates the module and disposes all of its resources. *)
  464. PROCEDURE Terminate-;
  465. VAR priority: Priority;
  466. BEGIN {UNCOOPERATIVE, UNCHECKED}
  467. FOR priority := LowestPriority TO HighestPriority DO
  468. Queues.Dispose (readyQueue[priority]);
  469. END;
  470. END Terminate;
  471. END Activities.