Activities.Mod 23 KB

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