Activities.Mod 23 KB

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