Unix.Kernel.Mod 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. MODULE Kernel; (** AUTHOR "pjm, G.F."; PURPOSE "Implementation-independent kernel interface"; *)
  2. IMPORT Trace, Machine, Heaps, Objects;
  3. CONST
  4. TimerFree = 0; TimerSleeping = 1; TimerWoken = 2; TimerExpired = 3; (* Timer state *)
  5. Second* = Machine.Second;
  6. TYPE
  7. (** Finalizer for FinalizedCollection.Add. *)
  8. Finalizer* = Heaps.Finalizer; (** PROCEDURE (obj: ANY) *)
  9. (** Enumerator for FinalizedCollection.Enumerate. *)
  10. Enumerator* = PROCEDURE {DELEGATE} (obj: ANY; VAR cont: BOOLEAN);
  11. FinalizerNode = POINTER TO RECORD (Heaps.FinalizerNode)
  12. nextObj {UNTRACED}: FinalizerNode; (* in collection *)
  13. END;
  14. (** Polling timer. *)
  15. MilliTimer* = RECORD start, target: LONGINT END;
  16. TYPE
  17. (** Delay timer. *)
  18. Timer* = OBJECT
  19. VAR timer: Objects.Timer; state: SHORTINT;
  20. PROCEDURE HandleTimeout;
  21. BEGIN {EXCLUSIVE}
  22. IF state # TimerFree THEN state := TimerExpired END
  23. END HandleTimeout;
  24. (** Delay the calling process the specified number of milliseconds or until Wakeup is called. Only one process may sleep on a specific timer at a time. *)
  25. PROCEDURE Sleep*(ms: LONGINT);
  26. BEGIN {EXCLUSIVE}
  27. ASSERT(state = TimerFree); (* only one process may sleep on a timer *)
  28. state := TimerSleeping;
  29. Objects.SetTimeout(timer, HandleTimeout, ms);
  30. AWAIT(state # TimerSleeping);
  31. IF state # TimerExpired THEN Objects.CancelTimeout( timer ) END;
  32. state := TimerFree
  33. END Sleep;
  34. (** Wake up the process sleeping on the timer, if any. *)
  35. PROCEDURE Wakeup*;
  36. BEGIN {EXCLUSIVE}
  37. IF state = TimerSleeping THEN state := TimerWoken END
  38. END Wakeup;
  39. (** Initializer. *)
  40. PROCEDURE &Init*;
  41. BEGIN
  42. state := TimerFree; NEW( timer )
  43. END Init;
  44. END Timer;
  45. TYPE
  46. (** A collection of objects that are finalized automatically by the garbage collector. *)
  47. FinalizedCollection* = OBJECT(Heaps.FinalizedCollection)
  48. VAR root: FinalizerNode; (* weak list of contents linked by nextObj *)
  49. (** Add obj to collection. Parameter fin specifies finalizer, or NIL if not required. *) (* may be called multiple times *)
  50. PROCEDURE Add*(obj: ANY; fin: Finalizer);
  51. VAR n: FinalizerNode;
  52. BEGIN
  53. NEW(n); n.collection := SELF; n.finalizer := fin;
  54. Heaps.AddFinalizer(obj, n);
  55. BEGIN {EXCLUSIVE}
  56. n.nextObj := root.nextObj; root.nextObj := n (* add to collection *)
  57. END
  58. END Add;
  59. (** Remove one occurrence of obj from collection. *)
  60. PROCEDURE Remove*(obj: ANY);
  61. VAR p, n: FinalizerNode;
  62. BEGIN {EXCLUSIVE}
  63. p := root; n := p.nextObj;
  64. WHILE (n # NIL) & (n.objWeak # obj) DO
  65. p := n; n := n.nextObj
  66. END;
  67. IF n # NIL THEN p.nextObj := n.nextObj END;
  68. (* leave in global finalizer list *)
  69. END Remove;
  70. (** Remove all occurrences of obj from collection. *)
  71. PROCEDURE RemoveAll*(obj: ANY);
  72. VAR p, n: FinalizerNode;
  73. BEGIN {EXCLUSIVE}
  74. p := root; n := p.nextObj;
  75. WHILE n # NIL DO
  76. IF n.objWeak = obj THEN
  77. p.nextObj := n.nextObj
  78. ELSE
  79. p := n
  80. END;
  81. n := n.nextObj
  82. END
  83. END RemoveAll;
  84. (** Enumerate all objects in the collection (Enumerator may not call Remove, Add, Enumerate or Clear). *)
  85. PROCEDURE Enumerate*(enum: Enumerator);
  86. VAR fn, next: FinalizerNode; cont: BOOLEAN;
  87. BEGIN {EXCLUSIVE}
  88. fn := root.nextObj; cont := TRUE;
  89. WHILE fn # NIL DO
  90. next := fn.nextObj; (* current (or other) object may be removed by enum call *)
  91. enum(fn.objWeak, cont);
  92. IF cont THEN fn := next ELSE fn := NIL END
  93. END
  94. END Enumerate;
  95. (** Enumerate all objects in the collection not being finalized (Enumerator may not call Remove, Add, Enumerate or Clear). *)
  96. PROCEDURE EnumerateN*( enum: Enumerator );
  97. VAR fn, next: FinalizerNode; cont: BOOLEAN; obj: ANY;
  98. BEGIN {EXCLUSIVE}
  99. fn := root.nextObj; cont := TRUE;
  100. WHILE fn # NIL DO
  101. next := fn.nextObj; (* current (or other) object may be removed by enum call *)
  102. obj := NIL;
  103. Machine.Acquire( Machine.Heaps ); (* prevent GC from running *)
  104. IF (fn.objWeak # NIL ) & (fn.objStrong = NIL ) THEN (* object is not yet on the finalizers list *)
  105. obj := fn.objWeak; (* now object is locally referenced, will therefore not be GCed *)
  106. END;
  107. Machine.Release( Machine.Heaps );
  108. IF obj # NIL THEN enum( obj, cont ); END;
  109. IF cont THEN fn := next ELSE fn := NIL END
  110. END
  111. END EnumerateN;
  112. (** Initialize new collection. May also be called to clear an existing collection. *)
  113. PROCEDURE &Clear*;
  114. BEGIN {EXCLUSIVE}
  115. NEW(root); root.nextObj := NIL (* head *)
  116. END Clear;
  117. END FinalizedCollection;
  118. VAR
  119. second*: LONGINT; (** number of timer counts per second (Hz) *)
  120. (** Activate the garbage collector immediately. *)
  121. PROCEDURE GC*;
  122. BEGIN
  123. Heaps.GC
  124. END GC;
  125. (** -- Timers -- *)
  126. (** Get the current timer count. Timer increment rate is stored in "second" variable in Hz. *)
  127. PROCEDURE GetTicks*(): LONGINT;
  128. BEGIN
  129. RETURN Machine.ticks
  130. END GetTicks;
  131. (** Set timer to expire in approximately "ms" milliseconds. *)
  132. PROCEDURE SetTimer*(VAR t: MilliTimer; ms: LONGINT);
  133. BEGIN
  134. IF Second # 1000 THEN (* convert to ticks *)
  135. ASSERT((ms >= 0) & (ms <= MAX(LONGINT) DIV Second));
  136. ms := ms * Second DIV 1000
  137. END;
  138. IF ms < 5 THEN INC(ms) END; (* Nyquist adjustment *)
  139. t.start := Machine.ticks;
  140. t.target := t.start + ms
  141. END SetTimer;
  142. (** Test whether a timer has expired. *)
  143. PROCEDURE Expired*(CONST t: MilliTimer): BOOLEAN;
  144. BEGIN
  145. RETURN Machine.ticks - t.target >= 0
  146. END Expired;
  147. (** Return elapsed time on a timer in milliseconds. *)
  148. PROCEDURE Elapsed*(CONST t: MilliTimer): LONGINT;
  149. BEGIN
  150. RETURN (Machine.ticks - t.start) * (1000 DIV Second)
  151. END Elapsed;
  152. (** Return time left on a timer in milliseconds. *)
  153. PROCEDURE Left*(CONST t: MilliTimer): LONGINT;
  154. BEGIN
  155. RETURN (t.target - Machine.ticks) * (1000 DIV Second)
  156. END Left;
  157. BEGIN
  158. ASSERT(1000 MOD Second = 0); (* for Elapsed *)
  159. second := Second;
  160. Heaps.GC := Heaps.InvokeGC;
  161. (* Machine.Acquire(Machine.TraceOutput);
  162. Trace.String( Machine.version ); Trace.String(": Kernel: Initialized and started."); Trace.Ln;
  163. Machine.Release(Machine.TraceOutput); *)
  164. END Kernel.
  165. (**
  166. Notes:
  167. o The FinalizedCollection object implements collections of finalized objects.
  168. o Objects added to a finalized collection (with Add) are removed automatically by the garbage collector when no references to them exist any more. They can also be removed explicitly with Remove.
  169. o All the objects currently in a collection can be enumerated by Enumerate, which takes an enumerator procedure as parameter. The enumerator can also be a method in an object, which is useful when state information is required during the enumeration. The enumerator may not call other methods of the same collection.
  170. o An object in a finalized collection can have an finalizer procedure associated with it, which gets called by a separate process when there are no references left to the object any more. A finalizer is usually used for some cleanup functions, e.g. releasing external resources. It is executed exactly once per object. During the next garbage collector cycle the object is finally removed.
  171. *)
  172. (*
  173. to do:
  174. o cancel finalizer when removing object
  175. o fix module free race: module containing finalizer is freed. although the finalizer list is cleared, the FinalizerCaller has already taken a reference to a finalizer, but hasn't called it yet.
  176. o consider: a module has a FinalizedCollection, without finalizers (NIL). when the module is freed, the objects are still in the finalization list, and will get finalized in the next garbage collection. The FinalizedCollection will survive the first collection, as the objects all have references to it through their c field. After all objects have been finalized, the FinalizedCollection itself is collected. No dangling pointers occur, except the untraced module field references from the type descriptors, which are only used for tracing purposes.
  177. o check cyclic dependencies between finalized objects.
  178. o GetTime(): LONGINT - return current time in ms
  179. o Delay(td: LONGINT) - wait td ms
  180. o AwaitTime(t: LONGINT) - wait at least until time t
  181. o Wakeup(obj: ANY) - wake up object that is waiting
  182. *)