Activities.Mod 23 KB

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