Win64.Objects.Mod 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. (* Aos, Copyright 2001, Pieter Muller, ETH Zurich; this module ported for the windows version, fof. *)
  2. MODULE Objects; (** AUTHOR "pjm, ejz, fof"; PURPOSE "Active object runtime support"; *)
  3. IMPORT SYSTEM, Trace, Kernel32, Machine, Modules, Heaps;
  4. CONST
  5. HandleExcp = TRUE; (* FALSE -> we asume that it is done correctly by Traps *)
  6. TraceVerbose = FALSE;
  7. StrongChecks = FALSE; defaultStackSize = 0;
  8. TraceOpenClose = FALSE;
  9. CONST
  10. (* Process flags *)
  11. Restart* = 0; (* Restart/Destroy process on exception *)
  12. PleaseHalt* = 10; (* Process requested to Halt itself soon *)
  13. Unbreakable* = 11;
  14. SelfTermination* = 12;
  15. Preempted* = 27; (* Has been preempted. *)
  16. Resistant* = 28; (* Can only be destroyed by itself *)
  17. PleaseStop* = 31; (* Process requested to Terminate or Halt itself soon *)
  18. InActive* = 26; (* needed to prevent processes to call finalizers while in await or lock or unlock, see Kernel.GC *)
  19. (** Process modes *)
  20. Unknown* = 0; Ready* = 1; (* for compatibility with native A2 *)
  21. Running* = 2; AwaitingLock* = 3; AwaitingCond* = 4; AwaitingEvent* = 5; Suspended* = 6; Terminated* = 7;
  22. (** Process priorities *)
  23. MinPriority = 0; (* only system idle processes run at this priority level *)
  24. Low* = 1; Normal* = 2; High* = 3; (* "user" priorities *)
  25. GCPriority* = 4; (* priority of garbage collector *)
  26. Realtime* = 5; (* reserved for interrupt handling and realtime apps, these processes are not allowed to allocate memory *)
  27. (* Process termination halt codes *)
  28. halt* = 2222;
  29. haltUnbreakable* = 2223;
  30. TYPE
  31. CpuCyclesArray* = ARRAY Machine.MaxCPU OF HUGEINT;
  32. ProtectedObject = POINTER TO RECORD END; (* protected object (10000) *)
  33. ProcessQueue = Heaps.ProcessQueue;
  34. Body = PROCEDURE (self: ProtectedObject);
  35. Condition = PROCEDURE (slink: ADDRESS): BOOLEAN;
  36. EventHandler* = PROCEDURE {DELEGATE};
  37. RealtimeEventHandler* = PROCEDURE {DELEGATE, REALTIME};
  38. Timer* = POINTER TO RECORD
  39. next, prev : Timer;
  40. trigger: LONGINT;
  41. handler: EventHandler
  42. END;
  43. RealtimeTimer* = POINTER TO RECORD
  44. next, prev: RealtimeTimer;
  45. trigger: LONGINT;
  46. handler: RealtimeEventHandler
  47. END;
  48. Clock = OBJECT
  49. VAR h: Timer;
  50. ticks: LONGINT;
  51. hevent: Kernel32.HANDLE;
  52. res: Kernel32.BOOL;
  53. mode: LONGINT;
  54. process: Process;
  55. PROCEDURE Wakeup;
  56. VAR res: Kernel32.BOOL;
  57. BEGIN {EXCLUSIVE}
  58. res := Kernel32.SetEvent(hevent)
  59. END Wakeup;
  60. PROCEDURE Finalize(ptr: ANY);
  61. VAR res: Kernel32.BOOL;
  62. BEGIN
  63. IF hevent # 0 THEN res := Kernel32.CloseHandle(hevent); hevent := 0 END
  64. END Finalize;
  65. PROCEDURE &Init*;
  66. VAR fn: Heaps.FinalizerNode;
  67. BEGIN
  68. hevent := Kernel32.CreateEvent(NIL, 0, 0, NIL);
  69. ASSERT(hevent # 0);
  70. NEW(fn); fn.finalizer := SELF.Finalize; Heaps.AddFinalizer(SELF, fn)
  71. END Init;
  72. BEGIN {ACTIVE, SAFE, PRIORITY(High)}
  73. process := CurrentProcess();
  74. mode := process.mode;
  75. LOOP
  76. Machine.Acquire(Machine.Objects);
  77. process.mode := mode;
  78. LOOP
  79. h := event.next; (* event: head of timer event queue *)
  80. ticks := Kernel32.GetTickCount();
  81. IF (h = event) OR (h.trigger - ticks > 0) THEN EXIT END;
  82. event.next := h.next; event.next.prev := event; (* unlink *)
  83. h.next := NIL; h.prev := NIL;
  84. Machine.Release(Machine.Objects);
  85. h.handler(); (* assume handler will return promptly *)
  86. Machine.Acquire(Machine.Objects)
  87. END;
  88. mode := process.mode;
  89. process.mode := AwaitingEvent;
  90. Machine.Release(Machine.Objects);
  91. IF h = event THEN (* sentinel head of timer event queue: wait forever until a new event has been entered in queue *)
  92. res := Kernel32.WaitForSingleObject(hevent, MAX(LONGINT));
  93. ELSE
  94. res := Kernel32.WaitForSingleObject(hevent, h.trigger - ticks);
  95. END;
  96. END
  97. END Clock;
  98. TYPE
  99. Win32Event = Kernel32.HANDLE;
  100. GCContext = RECORD
  101. nextPos: LONGINT; (* 0 to start with *)
  102. (*first,*) last: ARRAY 256 OF ADDRESS; (* first might be not required *)
  103. END;
  104. Process* = OBJECT(Heaps.ProcessLink)
  105. VAR
  106. rootedNext : Process; (* to prevent process to be GCed in WinAos *)
  107. obj-: ProtectedObject; (* associated active object *)
  108. state-: Kernel32.Context;
  109. (*
  110. sse: SSEState; (* fpu and sse state of preempted process (only valid if Preempted IN flag) *)
  111. sseAdr: LONGINT;
  112. *)
  113. condition-: Condition; (* awaited process' condition *)
  114. condFP-: ADDRESS; (* awaited process' condition's context *)
  115. mode-: LONGINT; (* process state *) (* only changed inside Objects lock ??? *)
  116. procID-: LONGINT; (* processor ID where running, exported for compatibilty , useless in WinAos *)
  117. waitingOn-: ProtectedObject; (* obj this process is waiting on (for lock or condition) *)
  118. id-: LONGINT; (* unique process ID for tracing *)
  119. flags*: SET; (* process flags *)
  120. priority-: LONGINT; (* process priority *)
  121. (*
  122. currPri: LONGINT;
  123. stack*: Machine.Stack; (** user-level stack of process *)
  124. *)
  125. stackBottom: ADDRESS;
  126. handle-: Kernel32.HANDLE; (* handle to corresponding Windows thread *)
  127. body: Body;
  128. event: Win32Event;
  129. restartPC-: ADDRESS; (** entry point of body, for SAFE exception recovery *)
  130. restartSP-: ADDRESS; (** stack level at start of body, for SAFE exception recovery *)
  131. (*
  132. perfCyc*: ARRAY Machine.MaxCPU OF HUGEINT;
  133. priInvCnt: LONGINT; (* counts the nummber of object locks hold that increased currPri of the process *)
  134. exp*: Machine.ExceptionState;
  135. oldReturnPC: LONGINT;
  136. *)
  137. lastThreadTimes: HUGEINT; (*ALEX 2005.12.12*)
  138. gcContext: GCContext;
  139. context: ANY; (* commands contect *)
  140. PROCEDURE FindRoots; (* override, called while GC, replaces Threads.CheckStacks *)
  141. VAR sp: ADDRESS; res: Kernel32.BOOL; pc, bp: ADDRESS;
  142. n,adr: ADDRESS; desc {UNTRACED}: Modules.ProcedureDescPointer; p {UNTRACED}: ANY; i: SIZE;
  143. a0,a1, obp, osb, osbp, opc, gbp: ADDRESS;
  144. O: ANY; ID: LONGINT;
  145. mod {UNTRACED}: Modules.Module;
  146. proc {UNTRACED}: Modules.ProcedureDescPointer;
  147. modName: ARRAY 128 OF CHAR;
  148. mode0,mode1: LONGINT;
  149. c: Kernel32.Context;
  150. contextPos: LONGINT;
  151. BEGIN
  152. O := obj; ID := id;
  153. IF (handle = 0) OR (mode = Terminated) OR (mode < Ready) (* procedure Wrapper not yet started *)
  154. OR (priority > High) (* stack of GC and realtime processes not traced *) THEN
  155. RETURN
  156. END;
  157. IF CurrentProcess() = SELF THEN
  158. sp := Machine.CurrentSP(); bp :=Machine.CurrentBP(); pc := Machine.CurrentPC();
  159. ELSE
  160. res := Kernel32.SuspendThread(handle); (* can suspend a suspended thread -- no problem at all *)
  161. c.ContextFlags := Kernel32.SetToDW(Kernel32.ContextControl + Kernel32.ContextInteger);
  162. res := Kernel32.GetThreadContext( handle, c );
  163. IF res = 0 THEN TRACE(Kernel32.GetLastError()) END;
  164. state := c;
  165. sp := state.SP; bp := state.BP; pc := state.PC;
  166. mod := Modules.ThisModuleByAdr0(pc);
  167. IF mod # NIL THEN
  168. COPY(mod.name, modName);
  169. proc := Modules.FindProc(pc,mod.procTable);
  170. END;
  171. obp := bp; osb := stackBottom; opc := pc;
  172. osbp := state.BP;
  173. END;
  174. IF TraceProcessHook # NIL THEN
  175. TraceProcessHook(SELF,pc,bp,sp,stackBottom);
  176. END;
  177. contextPos := gcContext.nextPos;
  178. (* stack garbage collection *)
  179. IF Heaps.GCType= Heaps.HeuristicStackInspectionGC THEN
  180. Heaps.Candidate( state.RDI ); Heaps.Candidate( state.RSI );
  181. Heaps.Candidate( state.RBX ); Heaps.Candidate( state.RDX );
  182. Heaps.Candidate( state.RCX ); Heaps.Candidate( state.RAX );
  183. IF (stackBottom # 0) & (sp # 0) THEN
  184. Heaps.RegisterCandidates( sp, stackBottom - sp );
  185. END;
  186. ELSIF Heaps.GCType = Heaps.MetaDataForStackGC THEN
  187. LOOP
  188. IF (bp = NIL) OR (bp >= stackBottom) THEN EXIT END;
  189. IF Machine.ValidHeapAddress(pc) THEN
  190. (* ok, valid stack frame from A2, we can trace this *)
  191. ELSE
  192. (* no, cannot trace this Windows stack frame, we have to check if we recorded when we exited A2 previously *)
  193. DEC(contextPos);
  194. IF (contextPos >= 0) THEN
  195. bp := gcContext.last[contextPos];
  196. ELSE
  197. EXIT;
  198. END;
  199. END;
  200. SYSTEM.GET(bp, n);
  201. IF ODD(n) THEN (* procedure descriptor at bp *)
  202. desc := SYSTEM.VAL(Modules.ProcedureDescPointer, n-1);
  203. IF desc # NIL THEN
  204. a0 := ADDRESSOF(desc.offsets);
  205. a1 := SYSTEM.VAL(ADDRESS, desc.offsets);
  206. ASSERT(a0+SIZEOF(ADDRESS)=a1,54321);
  207. FOR i := 0 TO LEN(desc.offsets)-1 DO
  208. adr := bp + desc.offsets[i]; (* pointer at offset *)
  209. SYSTEM.GET(adr, p); (* load pointer *)
  210. IF p # NIL THEN
  211. Heaps.Mark(p);
  212. END;
  213. END;
  214. END;
  215. SYSTEM.GET(bp + 2*SIZEOF(ADDRESS), pc);
  216. SYSTEM.GET(bp + SIZEOF(ADDRESS), bp);
  217. ELSE (* classical stack frame without procedure descriptor *)
  218. SYSTEM.GET(bp + SIZEOF(ADDRESS), pc);
  219. bp := n;
  220. END;
  221. END;
  222. (* ASSERT((bp = stackBottom) OR (bp=0) ,12345); can be violated when coming from windows *)
  223. END;
  224. IF (CurrentProcess() # SELF) (* & (mode # Suspended) *) THEN
  225. res := Kernel32.ResumeThread(handle);
  226. ASSERT(res # -1);
  227. END;
  228. END FindRoots;
  229. END Process;
  230. TYPE
  231. (* ExceptionHandler* = PROCEDURE( CONST exceptionPointers: Kernel32.ExceptionPointers): Kernel32.DWORD; *)
  232. ExceptionHandler* = PROCEDURE( VAR context: Kernel32.Context;
  233. VAR excpRec: Kernel32.ExceptionRecord;
  234. VAR handled: BOOLEAN);
  235. GCStatusExt = OBJECT(Heaps.GCStatus)
  236. (* called from Heaps.InvokeGC, i.e. this is a hidden upcall. However, it is necessary to take the Machine.Objects lock here since writing
  237. the set of variables here must not be interrupted, i.e. atomic writing of the set of variables is absolutely necessary. They system may hang
  238. if the lock is not taken. *)
  239. PROCEDURE SetgcOngoing(value: BOOLEAN);
  240. VAR p: Heaps.ProcessLink; cur, r: Process; res: Kernel32.BOOL; num: LONGINT; time: LONGINT;
  241. BEGIN (* serialize writers *)
  242. IF value THEN
  243. (* Low, Medium or High priority process calls this *)
  244. time := Kernel32.GetTickCount();
  245. Machine.Acquire(Machine.Objects);
  246. Machine.Acquire(Machine.Heaps); (* to protect agains concurrent LazySweep *)
  247. r := CurrentProcess();
  248. num := 0;
  249. p := ready.head;
  250. WHILE p # NIL DO
  251. cur := p(Process);
  252. IF ((cur.mode = Ready) OR (cur.mode = Running)) & (cur.priority <= High) & (cur # r) THEN
  253. res := Kernel32.SuspendThread(cur.handle);
  254. ASSERT(res >= 0);
  255. cur.mode := Suspended
  256. ELSE INC(num);
  257. END;
  258. p := p.next
  259. END;
  260. Heaps.CollectGarbage(Modules.root);
  261. p := ready.head;
  262. WHILE (p # NIL) DO
  263. cur := p(Process);
  264. (* only suspended and awaiting processes of ready queue are resumed *)
  265. IF cur.mode = Suspended THEN
  266. res := Kernel32.ResumeThread(cur.handle);
  267. ASSERT(res >= 0);
  268. cur.mode := Running
  269. END;
  270. p := p.next
  271. END;
  272. Machine.Release(Machine.Heaps);
  273. Machine.Release(Machine.Objects);
  274. time := Kernel32.GetTickCount()-time;
  275. IF Heaps.trace THEN Trace.String("GC Called -- duration "); Trace.Int(time,0); Trace.String(" ms."); Trace.Ln END;
  276. IF finalizerCaller # NIL THEN finalizerCaller.Activate() END;
  277. END;
  278. END SetgcOngoing;
  279. END GCStatusExt;
  280. FinalizedCollection* = OBJECT
  281. PROCEDURE RemoveAll*(obj: ANY); (** abstract *)
  282. BEGIN HALT(301) END RemoveAll;
  283. END FinalizedCollection;
  284. FinalizerNode* = POINTER TO RECORD (Heaps.FinalizerNode)
  285. c*: FinalizedCollection (* base type for collection containing object *)
  286. END;
  287. FinalizerCaller = OBJECT (* separate active object that calls finalizers *)
  288. VAR n: Heaps.FinalizerNode;
  289. event: Kernel32.HANDLE;
  290. process: Process;
  291. PROCEDURE &Init;
  292. BEGIN
  293. event := Kernel32.CreateEvent( NIL, Kernel32.False (* automatic *), Kernel32.False, NIL );
  294. ASSERT(event # 0);
  295. END Init;
  296. PROCEDURE Wait;
  297. VAR res: Kernel32.BOOL; mode: LONGINT;
  298. BEGIN
  299. mode := process.mode;
  300. process.mode := AwaitingEvent;
  301. res := Kernel32.WaitForSingleObject(event, Kernel32.Infinite);
  302. ASSERT(res = Kernel32.WaitObject0);
  303. process.mode := mode;
  304. END Wait;
  305. PROCEDURE Activate;
  306. VAR res: Kernel32.BOOL;
  307. BEGIN
  308. res := Kernel32.SetEvent(event);
  309. END Activate;
  310. BEGIN {ACTIVE, SAFE, PRIORITY(High)}
  311. process := CurrentProcess();
  312. LOOP
  313. Wait;
  314. LOOP
  315. n := Heaps.GetFinalizer();
  316. IF n = NIL THEN EXIT END;
  317. IF n IS FinalizerNode THEN
  318. n( FinalizerNode ).c.RemoveAll( n.objStrong ) (* remove it if it is not removed yet *)
  319. END;
  320. IF n.finalizer # NIL THEN
  321. n.finalizer( n.objStrong ) (* may acquire locks *)
  322. END
  323. END
  324. END;
  325. END FinalizerCaller;
  326. VAR
  327. awc-, awl-: LONGINT;
  328. oberonLoop*: ANY; (* Oberon Loop Process temporary workaround for Threads.oberonLoop *)
  329. break: ARRAY 16 OF CHAR;
  330. terminateProc: PROCEDURE;
  331. ready: ProcessQueue; (* contains running processes in this implementation *)
  332. numberOfProcessors: LONGINT; (* cached value of Machine.NumberOfProcessors() *)
  333. finalizerCaller: FinalizerCaller; (* active object for finalizer process, regarded as aprt of GC *)
  334. event: Timer; (* list of events *)
  335. clock: Clock;
  336. tlsIndex: LONGINT;
  337. nProcs: LONGINT;
  338. excplock: Kernel32.CriticalSection; exceptionhandler: ExceptionHandler;
  339. PROCEDURE TraceGCContext*;
  340. VAR p: Process; i: LONGINT; desc {UNTRACED}: Modules.ProcedureDescPointer;
  341. n: ADDRESS;
  342. BEGIN
  343. p := CurrentProcess();
  344. IF p # NIL THEN
  345. TRACE(p.gcContext.nextPos);
  346. FOR i := 0 TO p.gcContext.nextPos-1 DO
  347. TRACE(p.gcContext.last[i]);
  348. SYSTEM.GET(p.gcContext.last[i],n);
  349. IF ODD(n) THEN
  350. desc := SYSTEM.VAL(Modules.ProcedureDescPointer, n-1);
  351. IF desc # NIL THEN
  352. WriteType(desc);Trace.Ln;
  353. END;
  354. END;
  355. END;
  356. (*ASSERT(p.gcContext.nextPos <4);*)
  357. IF p.gcContext.nextPos >= 4 THEN Heaps.InvokeGC END;
  358. END;
  359. END TraceGCContext;
  360. (* Set the current process' priority. *)
  361. PROCEDURE SetPriority*( priority: LONGINT );
  362. VAR r: Process; prio: LONGINT; res: Kernel32.BOOL;
  363. BEGIN
  364. ASSERT((priority >= Low) & (priority <= Realtime)); (* priority in bounds *)
  365. r := CurrentProcess(); r.priority := priority;
  366. CASE priority OF
  367. MinPriority:
  368. prio := Kernel32.ThreadPriorityIdle
  369. | Low:
  370. prio := Kernel32.ThreadPriorityBelowNormal
  371. | High:
  372. prio := Kernel32.ThreadPriorityAboveNormal
  373. | GCPriority, Realtime:
  374. prio := Kernel32.ThreadPriorityTimeCritical
  375. ELSE (* Normal *)
  376. prio := Kernel32.ThreadPriorityNormal
  377. END;
  378. res := Kernel32.SetThreadPriority( r.handle, prio );
  379. ASSERT(r.handle # 0);
  380. ASSERT(res # 0)
  381. END SetPriority;
  382. (** Return TRUE iff the specified protected object is locked exclusive to the current process. *)
  383. PROCEDURE LockedByCurrent*( obj: ANY ): BOOLEAN;
  384. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; res: BOOLEAN;
  385. BEGIN
  386. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  387. ASSERT(hdr IS Heaps.ProtRecBlock);
  388. Machine.Acquire(Machine.Objects);
  389. res := (hdr.lockedBy = ActiveObject());
  390. Machine.Release(Machine.Objects);
  391. RETURN res
  392. END LockedByCurrent;
  393. PROCEDURE Yield*;
  394. BEGIN
  395. Kernel32.Sleep(0)
  396. END Yield;
  397. (** Return current process. (DEPRECATED, use ActiveObject) *)
  398. PROCEDURE CurrentProcess*( ): Process;
  399. BEGIN{UNCHECKED} (* makes sure that Enter and Leave are not emitted *)
  400. RETURN SYSTEM.VAL(Process, Kernel32.TlsGetValue(tlsIndex));
  401. END CurrentProcess;
  402. PROCEDURE CurrentContext*(): ANY;
  403. VAR p: Process;
  404. BEGIN
  405. p := CurrentProcess();
  406. IF p # NIL THEN RETURN p.context
  407. ELSE RETURN NIL
  408. END;
  409. END CurrentContext;
  410. PROCEDURE SetContext*(context: ANY);
  411. VAR p: Process;
  412. BEGIN
  413. p := CurrentProcess();
  414. IF p # NIL THEN p.context := context END;
  415. END SetContext;
  416. (* Return stack bottom of process. For compatibility WinAos/UnixAos/NativeAos *)
  417. PROCEDURE GetStackBottom*(p: Process): ADDRESS;
  418. BEGIN
  419. RETURN p.stackBottom
  420. END GetStackBottom;
  421. (** Return the active object currently executing. *)
  422. PROCEDURE ActiveObject* (): ANY;
  423. VAR r: Process;
  424. BEGIN
  425. r := SYSTEM.VAL(Process, Kernel32.TlsGetValue(tlsIndex));
  426. RETURN r.obj
  427. END ActiveObject;
  428. (** Return the ID of the active currently executing process. *)
  429. PROCEDURE GetProcessID* (): LONGINT;
  430. VAR r: Process;
  431. BEGIN
  432. r := SYSTEM.VAL (Process, Kernel32.TlsGetValue( tlsIndex ));
  433. RETURN r.id
  434. END GetProcessID;
  435. (* Get a process from a queue (NIL if none). Caller must hold lock for specific queue. *)
  436. PROCEDURE Get(VAR queue: ProcessQueue; VAR new: Process);
  437. VAR t: Heaps.ProcessLink;
  438. BEGIN
  439. t := queue.head;
  440. IF t = NIL THEN (* zero elements in queue *)
  441. (* skip *)
  442. ELSIF t = queue.tail THEN (* one element in queue *)
  443. queue.head := NIL; queue.tail := NIL (* {(t.next = NIL) & (t.prev = NIL)} *)
  444. ELSE (* more than one element in queue *)
  445. queue.head := t.next; t.next := NIL; queue.head.prev := NIL
  446. END;
  447. ASSERT((t = NIL) OR (t.next = NIL ) & (t.prev = NIL)); (* temp strong check *)
  448. IF t = NIL THEN
  449. new := NIL
  450. ELSE
  451. ASSERT(t IS Process);
  452. new := t(Process)
  453. END
  454. END Get;
  455. (* Put a process in a queue. Caller must hold lock for specific queue. *)
  456. (* If t was running, be careful to protect Put and the subsequent SwitchTo with the ready lock. *)
  457. PROCEDURE Put(VAR queue: ProcessQueue; t: Process);
  458. BEGIN (* {t # NIL & t.next = NIL & t.prev = NIL} *)
  459. IF StrongChecks THEN
  460. ASSERT((t.next = NIL) & (t.prev = NIL))
  461. END;
  462. t.next := NIL; t.prev := NIL; (* ug *)
  463. IF queue.head = NIL THEN (* queue empty *)
  464. queue.head := t
  465. ELSE (* queue not empty *)
  466. queue.tail.next := t; t.prev := queue.tail
  467. END;
  468. queue.tail := t
  469. END Put;
  470. (* starting address of user stack for current thread, called stack top in TIB.H *)
  471. PROCEDURE -StackBottom*( ): ADDRESS;
  472. CODE {SYSTEM.i386}
  473. DB 064H
  474. DB 08BH
  475. DB 005H
  476. DB 004H
  477. DB 000H
  478. DB 000H
  479. DB 000H
  480. END StackBottom;
  481. PROCEDURE {WINAPI} ExcpFrmHandler( CONST exceptionPointers: Kernel32.ExceptionPointers): Kernel32.DWORD ;
  482. VAR m: Modules.Module; eip, ebp, stack: ADDRESS; pc, handler, fp, sp: ADDRESS; handled: BOOLEAN; t: Process;
  483. BEGIN
  484. handled := FALSE;
  485. Kernel32.EnterCriticalSection( excplock );
  486. (*
  487. fof: commenting this resolved a problem with multiple traps that a are catched with FINALLY statements in Windows Vista
  488. in Windows XP not necessary if Kernel32.SetThreadContext is not used (better to return gracefully from this handler)
  489. SetCurrent(excpFrame);
  490. *)
  491. t := CurrentProcess();
  492. IF exceptionhandler = NIL THEN
  493. Trace.StringLn ( "Objects: No exception handler installed" );
  494. IF HandleExcp THEN
  495. Trace.String( "EXCEPTION " ); Trace.Address(exceptionPointers.exception.ExceptionCode);
  496. Trace.String( " at " ); Trace.Address(exceptionPointers.exception.ExceptionAddress);
  497. Trace.Ln(); Trace.String( "RAX " ); Trace.Address(exceptionPointers.context.RAX);
  498. Trace.String( " RBX " ); Trace.Address(exceptionPointers.context.RBX); Trace.Ln();
  499. Trace.String( "RCX " ); Trace.Address(exceptionPointers.context.RCX); Trace.String( " RDX " );
  500. Trace.Address(exceptionPointers.context.RDX); Trace.Ln(); Trace.String( "RDI " );
  501. Trace.Address(exceptionPointers.context.RDI); Trace.String( " RSI " );
  502. Trace.Address(exceptionPointers.context.RSI); Trace.Ln();
  503. Trace.String( "R8 " ); Trace.Address(exceptionPointers.context.R8);
  504. Trace.String( " R9 " ); Trace.Address(exceptionPointers.context.R9); Trace.Ln();
  505. Trace.String( "R10 " ); Trace.Address(exceptionPointers.context.R10);
  506. Trace.String( " R11 " ); Trace.Address(exceptionPointers.context.R11); Trace.Ln();
  507. Trace.String( "R12 " ); Trace.Address(exceptionPointers.context.R12);
  508. Trace.String( " R13 " ); Trace.Address(exceptionPointers.context.R13); Trace.Ln();
  509. Trace.String( "R14 " ); Trace.Address(exceptionPointers.context.R14);
  510. Trace.String( " R15 " ); Trace.Address(exceptionPointers.context.R15); Trace.Ln();
  511. Trace.Ln;
  512. Trace.String( "RBP " );
  513. Trace.Address(exceptionPointers.context.BP); Trace.String( " RSP " );
  514. Trace.Address(exceptionPointers.context.SP); Trace.Ln(); Trace.String( "PC " );
  515. Trace.Address(exceptionPointers.context.PC); Trace.Ln();
  516. Trace.Ln();
  517. eip := exceptionPointers.exception.ExceptionAddress; ebp := exceptionPointers.context.BP;
  518. IF eip = 0 THEN SYSTEM.GET( exceptionPointers.context.SP, eip ) END;
  519. stack := StackBottom();
  520. LOOP
  521. Trace.String( "at ebp= " ); Trace.Address(ebp); Trace.String( "H : " );
  522. m := Modules.ThisModuleByAdr( eip );
  523. IF m # NIL THEN
  524. Trace.String( m.name ); Trace.String( " " );
  525. Trace.Address(eip - SYSTEM.VAL( LONGINT, ADDRESSOF( m.code[0] ) ));
  526. ELSE Trace.String( "EIP " ); Trace.Address(eip)
  527. END;
  528. Trace.Ln();
  529. IF (ebp # 0) & (ebp < stack) THEN (* if ebp is 0 in first frame *)
  530. SYSTEM.GET( ebp + SIZEOF(ADDRESS), eip ); (* return addr from stack *)
  531. SYSTEM.GET( ebp, ebp ); (* follow dynamic link *)
  532. ELSE EXIT
  533. END
  534. END;
  535. Trace.Ln();
  536. (* finally and all that ...
  537. handled := FALSE; fp := exceptionPointers.context.BP; sp := exceptionPointers.context.SP;
  538. pc := exceptionPointers.context.PC; handler := Modules.GetExceptionHandler( pc );
  539. IF handler # -1 THEN (* Handler in the current PAF *)
  540. exceptionPointers.context.PC := handler; handled := TRUE;
  541. (*SetTrapVariable(pc, fp); SetLastExceptionState(exc)*)
  542. ELSE
  543. WHILE (fp # 0) & (handler = -1) DO
  544. SYSTEM.GET( fp + SIZEOF(ADDRESS), pc );
  545. pc := pc - 1; (* CALL instruction, machine dependant!!! *)
  546. handler := Modules.GetExceptionHandler( pc );
  547. sp := fp; (* Save the old framepointer into the stack pointer *)
  548. SYSTEM.GET( fp, fp ) (* Unwind PAF *)
  549. END;
  550. IF handler = -1 THEN handled := FALSE;
  551. ELSE
  552. TRACE("CHANGED HANDLER ADR ");
  553. exceptionPointers.context.PC := handler; exceptionPointers.context.BP := fp; exceptionPointers.context.SP := sp;
  554. (* SetTrapVariable(pc, fp); SetLastExceptionState(exc);*)
  555. handled := TRUE
  556. END
  557. END;
  558. *)
  559. ELSE Trace.StringLn ( "Warning: FINALLY statement cannot be treated !" );
  560. END
  561. ELSE exceptionhandler(exceptionPointers.context^, exceptionPointers.exception^,handled );
  562. END;
  563. IF ~handled THEN
  564. exceptionPointers.context.PC := (*TerminateProc*) t.restartPC ;
  565. exceptionPointers.context.SP := t.restartSP;
  566. exceptionPointers.context.BP := t.stackBottom;
  567. ELSIF TraceVerbose THEN Trace.StringLn ( "trying to jump to FINALLY pc..." );
  568. END;
  569. Kernel32.LeaveCriticalSection( excplock );
  570. IF TraceVerbose THEN
  571. Machine.Acquire (Machine.TraceOutput);
  572. Trace.String( "recover process; pc=" ); Trace.Address( exceptionPointers.context.PC );
  573. Trace.String( "; sp= " ); Trace.Address( exceptionPointers.context.SP); Trace.String( "; bp= " );
  574. Trace.Address( exceptionPointers.context.BP); Trace.Ln;
  575. Machine.Release (Machine.TraceOutput);
  576. END;
  577. RETURN Kernel32.ExceptionContinueExecution; (* sets thread context and continues where specified in context *)
  578. END ExcpFrmHandler;
  579. (*
  580. (* get the currently installed execption frame *)
  581. (* PROCEDURE -GetCur 64H, 8BH, 0DH, 0, 0, 0, 0; (* MOV ECX, FS:[0] *) *)
  582. (* Better *)
  583. PROCEDURE -GetCur;
  584. CODE {SYSTEM.AMD64}
  585. MOV RCX, [GS:0]
  586. ; DB 064H, 08BH, 00DH, 000H, 000H, 000H, 000H
  587. END GetCur;
  588. PROCEDURE GetCurrent( ): Kernel32.ExcpFrmPtr;
  589. VAR cur: Kernel32.ExcpFrmPtr;
  590. BEGIN
  591. GetCur;
  592. cur := SYSTEM.VAL(Kernel32.ExcpFrmPtr,Machine.GetRCX());
  593. TRACE(cur);
  594. (* RETURN ECX *)
  595. RETURN cur
  596. END GetCurrent;
  597. *)
  598. (*
  599. (* install a new exception frame *)
  600. (* PROCEDURE -SetCur 64H, 0A3H, 0, 0, 0, 0; (* MOV FS:[0], EAX *)*)
  601. (* Better *)
  602. PROCEDURE -SetCur;
  603. CODE {SYSTEM.AMD64}
  604. ; DB 064H, 0A3H, 000H, 000H, 000H, 000H
  605. MOV [GS:0], RAX
  606. END SetCur;
  607. PROCEDURE SetCurrent( cur: Kernel32.ExcpFrmPtr );
  608. BEGIN
  609. TRACE(cur);
  610. Machine.SetRAX(cur);
  611. (* EAX := cur *)
  612. CODE{SYSTEM.AMD64}
  613. MOV [GS:0], RAX
  614. END;
  615. TRACE(GetCurrent());
  616. (*SetCur*)
  617. END SetCurrent;
  618. *)
  619. PROCEDURE RemoveExcpFrm( VAR excpfrm: Kernel32.ExcpFrm );
  620. VAR this: Kernel32.ExcpFrmPtr;
  621. BEGIN
  622. (*
  623. this := GetCurrent();
  624. (* ASSERT ( this = ADDRESSOF( excpfrm ) ); *)
  625. IF this # ADDRESSOF( excpfrm ) THEN Trace.StringLn ( "RemoveExcpFrm: Problem with excpfrm pointer" );
  626. ELSE SetCurrent( excpfrm.link )
  627. END;
  628. *)
  629. (*
  630. Kernel32.RemoveVectoredExceptionHandler(ExcpFrmHandler);
  631. *)
  632. END RemoveExcpFrm;
  633. PROCEDURE InstallExcpFrm( VAR excpfrm: Kernel32.ExcpFrm );
  634. BEGIN
  635. Kernel32.AddVectoredContinueHandler(1, ExcpFrmHandler);
  636. (*
  637. excpfrm.link := GetCurrent(); excpfrm.handler := ExcpFrmHandler;
  638. SetCurrent( ADDRESSOF( excpfrm ) )
  639. *)
  640. END InstallExcpFrm;
  641. PROCEDURE InQueue( queue: ProcessQueue; t: Process ): BOOLEAN;
  642. VAR p: Heaps.ProcessLink;
  643. BEGIN
  644. p := queue.head;
  645. WHILE (p # NIL ) & (p # t) DO p := p.next; END;
  646. RETURN (p = t);
  647. END InQueue;
  648. (* Remove a process from a queue that contains it. Caller must hold lock for specific queue. *)
  649. (* Not intended for frequent use. *)
  650. (* does not check if queue contained t ! *)
  651. PROCEDURE Remove( VAR queue: ProcessQueue; t: Process );
  652. BEGIN
  653. IF StrongChecks THEN
  654. ASSERT(InQueue(queue, t));
  655. ASSERT(t # NIL);
  656. END;
  657. IF t.prev # NIL THEN t.prev.next := t.next END;
  658. IF t.next # NIL THEN t.next.prev := t.prev END;
  659. IF t = queue.head THEN queue.head := t.next END;
  660. IF t = queue.tail THEN queue.tail := t.prev END;
  661. ASSERT((queue.head = NIL) OR (queue.head.prev = NIL) & (queue.tail.next = NIL));
  662. t.prev := NIL; t.next := NIL
  663. END Remove;
  664. PROCEDURE WriteType(obj: ANY);
  665. VAR type: LONGINT;
  666. BEGIN
  667. IF obj = NIL THEN Trace.String(" > NIL");
  668. ELSE
  669. Trace.String(" > "); SYSTEM.GET(SYSTEM.VAL(LONGINT, obj) + Heaps.TypeDescOffset, type);
  670. Heaps.WriteType(type);
  671. END;
  672. END WriteType;
  673. PROCEDURE terminate( t: Process );
  674. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; res: Kernel32.BOOL; shutdown: BOOLEAN;
  675. BEGIN
  676. IF t = NIL THEN RETURN END;
  677. (* see Objects.TerminateThis *)
  678. Machine.Acquire( Machine.Objects );
  679. IF TraceVerbose OR TraceOpenClose THEN
  680. Machine.Acquire (Machine.TraceOutput);
  681. Trace.String( "Terminating process " ); Trace.Int( t.id, 1 ); WriteType( t.obj ); Trace.Ln;
  682. Machine.Release (Machine.TraceOutput);
  683. END;
  684. IF (t.mode = Ready) OR (t.mode = Running) THEN Remove( ready, t );
  685. ELSIF t.mode = AwaitingLock THEN
  686. SYSTEM.GET(SYSTEM.VAL(ADDRESS, t.waitingOn) + Heaps.HeapBlockOffset, hdr);
  687. ASSERT(hdr IS Heaps.ProtRecBlock);
  688. Remove( hdr.awaitingLock, t ); Machine.Release( Machine.Objects );
  689. HALT( 97 )
  690. ELSIF t.mode = AwaitingCond THEN
  691. SYSTEM.GET(SYSTEM.VAL(ADDRESS, t.waitingOn) + Heaps.HeapBlockOffset, hdr);
  692. ASSERT(hdr IS Heaps.ProtRecBlock);
  693. Remove( hdr.awaitingCond, t ); Machine.Release( Machine.Objects );
  694. HALT( 98 )
  695. ELSE Machine.Release( Machine.Objects );
  696. HALT( 99 )
  697. END;
  698. t.mode := Terminated; (* a process can also be "terminated" if the queue containing it is garbage collected *)
  699. t.stackBottom := 0; t.state.SP := 0;
  700. t.restartPC := 0;
  701. IF t.event # 0 THEN res := Kernel32.CloseHandle( t.event ); t.event := 0 END;
  702. DEC( nProcs ); shutdown := (nProcs = 0);
  703. Machine.Release( Machine.Objects );
  704. IF shutdown THEN
  705. Trace.StringLn ( " Objects: shutdown" ); Modules.Shutdown( -1 );
  706. Kernel32.ExitProcess( 0 )
  707. END
  708. END terminate;
  709. PROCEDURE {WINAPI} Wrapper( lpParameter: ANY ): LONGINT;
  710. VAR t: Process; obj: ProtectedObject; res: Kernel32.BOOL; bp,sp: ADDRESS;
  711. excpfrm: Kernel32.ExcpFrm;
  712. BEGIN
  713. (* it may happen that the garbage collector runs right here and ignores this procedure.
  714. This is not a problem since lpParameter (being a reference to a process) is protected by the process lists *)
  715. Machine.Acquire(Machine.Objects);
  716. res := Kernel32.TlsSetValue(tlsIndex, SYSTEM.VAL(LONGINT, lpParameter));
  717. t := lpParameter(Process); obj := t.obj;
  718. ASSERT(res # 0);
  719. SetPriority(t.priority);
  720. bp := Machine.CurrentBP();
  721. sp := Machine.CurrentSP();
  722. t.restartSP := sp;
  723. t.stackBottom := bp;
  724. IF t.restartPC = SYSTEM.VAL(ADDRESS, terminateProc) THEN DEC(t.restartSP, SIZEOF(ADDRESS))
  725. ELSE DEC(t.restartSP, 2*SIZEOF(ADDRESS))
  726. END;
  727. IF TraceVerbose THEN
  728. Machine.Acquire(Machine.TraceOutput);
  729. Trace.String("New process; restartPC= "); Trace.Address(t.restartPC);
  730. Trace.String("; stackBottom= ");
  731. Trace.Address(t.stackBottom); Trace.Ln;
  732. Machine.Release(Machine.TraceOutput);
  733. END;
  734. t.mode := Running;
  735. (* now gc is enabled for this process stack *)
  736. Machine.Release(Machine.Objects);
  737. (* loop all processes that the GC did not see during process suspending because they were in the very moment being generated (just before the locked section) *)
  738. (*! should not be necessary any more as GC runs immediately and without scheduling decisions
  739. WHILE (gcActivity # NIL) & (gcActivity.process # NIL) & (gcActivity.process.mode = Running) DO END;
  740. *)
  741. t.body(obj);
  742. terminate(t);
  743. RemoveExcpFrm(excpfrm);
  744. RETURN 0
  745. END Wrapper;
  746. PROCEDURE FinalizeProcess(t: ANY);
  747. VAR p: Process; res: Kernel32.BOOL;
  748. BEGIN
  749. p := t(Process);
  750. IF TraceVerbose THEN
  751. Machine.Acquire (Machine.TraceOutput);
  752. Trace.String("Finalizing Process"); Trace.Int(p.id, 1);
  753. WriteType(p.obj); Trace.Ln;
  754. Machine.Release (Machine.TraceOutput);
  755. END;
  756. IF p.mode # Terminated THEN
  757. IF p.mode = AwaitingLock THEN DEC(awl);
  758. ELSIF p.mode = AwaitingCond THEN DEC(awc);
  759. END;
  760. (* no reference to the object any more *)
  761. Trace.String ("Closing unreferenced process"); (*Trace.Int(p.mode,20); Trace.Int( p.id, 20 ); *) Trace.Ln; (* Trace.Ln *)
  762. (* this usually happens, when an objects process waits on its own objtec and no reference exists any more. Then the object is discarded and
  763. consequently the process is unreferenced (except in the object). This cannot happen when there are still other references on the object.
  764. example:
  765. TYPE
  766. Object= OBJECT VAR active: BOOLEAN; BEGIN{ACTIVE} active := FALSE; AWAIT(active) END Object;
  767. VAR o: Object;
  768. BEGIN NEW(o);
  769. END;
  770. *)
  771. END;
  772. p.mode := Terminated; (* fof for GC problem *)
  773. IF p.handle # 0 THEN
  774. res := Kernel32.CloseHandle(p.handle); p.handle := 0
  775. END
  776. END FinalizeProcess;
  777. PROCEDURE TerminateProc;
  778. BEGIN
  779. terminate(CurrentProcess());
  780. Kernel32.ExitThread(0);
  781. Kernel32.Sleep(999999); (* wait until dependent threads terminated *)
  782. END TerminateProc;
  783. (* Allocate a new process associated with "obj". Must be outside lock region, because of potential GC. *)
  784. PROCEDURE NewProcess(body: Body; priority: LONGINT; flags: SET; obj: ProtectedObject; VAR new: Process);
  785. VAR t,r: Process; fn: Heaps.FinalizerNode;
  786. BEGIN
  787. NEW(t);
  788. t.gcContext.nextPos := 0;
  789. t.context := CurrentContext(); (* inherit context from parent process *)
  790. t.handle := 0;
  791. IF priority = 0 THEN (* no priority specified *)
  792. r := CurrentProcess();
  793. t.priority := r.priority (* inherit priority of creator *)
  794. ELSIF priority > 0 THEN (* positive priority specified *)
  795. t.priority := priority
  796. ELSE (* negative priority specified (only for Idle process) *)
  797. t.priority := MinPriority
  798. END;
  799. NEW(fn); (* implicit call Heaps.NewRec -> might invoke GC *)
  800. Machine.Acquire(Machine.Objects);
  801. t.next := NIL; t.prev := NIL; t.rootedNext := NIL;
  802. t.waitingOn := NIL; t.flags := flags; t.obj := obj; t.mode := Unknown;
  803. t.body := body; t.event := 0; fn.finalizer := FinalizeProcess;
  804. Heaps.AddFinalizer(t, fn);
  805. IF Restart IN flags THEN (* restart object body *)
  806. t.restartPC := SYSTEM.VAL(ADDRESS, body);
  807. ELSE (* terminate process *)
  808. t.restartPC := SYSTEM.VAL(ADDRESS, terminateProc);
  809. END;
  810. (* t.restartPC := SYSTEM.VAL(ADDRESS, terminateProc);*)
  811. t.handle := Kernel32.CreateThread(0, defaultStackSize, Wrapper, t, Kernel32.SetToDW({}), t.id);
  812. IF TraceVerbose OR TraceOpenClose THEN
  813. Machine.Acquire(Machine.TraceOutput);
  814. Trace.String("NewProcess: " ); Trace.Int(t.id, 1); WriteType(obj); Trace.Ln;
  815. Machine.Release(Machine.TraceOutput);
  816. END;
  817. ASSERT(t.handle # 0);
  818. new := t;
  819. END NewProcess;
  820. (* Create the process associated with an active object (kernel call). *)
  821. PROCEDURE CreateProcess*(body: Body; priority: LONGINT; flags: SET; obj: ProtectedObject);
  822. VAR t : Process; heapBlock {UNTRACED}: Heaps.HeapBlock;
  823. BEGIN
  824. ASSERT(priority >= 0, 1000); ASSERT(priority <=Realtime, 1001);
  825. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, heapBlock);
  826. ASSERT(heapBlock IS Heaps.ProtRecBlock); (* protected object *)
  827. IF Restart IN flags THEN INCL(flags, Resistant) END; (* SAFE => Restart & Resistant *)
  828. NewProcess(body, priority, flags, obj, t); INC(nProcs); (* acquires Machine.Objects lock *)
  829. t.mode := Ready; Put(ready, t);
  830. Machine.Release(Machine.Objects);
  831. END CreateProcess;
  832. (* The procedure Lock, Unlock and Await do not use header locks since it turned out that the header locks sometimes were finalized
  833. too early. *)
  834. PROCEDURE Lock*(obj: ProtectedObject; exclusive: BOOLEAN );
  835. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; r: Process; res: LONGINT;
  836. BEGIN (* {called from user level} *)
  837. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  838. IF StrongChecks THEN
  839. ASSERT(hdr IS Heaps.ProtRecBlock); (* protected object *)
  840. ASSERT(exclusive) (* shared not implemented yet *)
  841. END;
  842. r := CurrentProcess();
  843. IF StrongChecks THEN
  844. ASSERT(hdr # NIL, 1001);
  845. ASSERT(r # NIL, 1002);
  846. END;
  847. Machine.Acquire(Machine.Objects);
  848. IF hdr.count = 0 THEN (* not locked *)
  849. hdr.count := -1; hdr.lockedBy := r;
  850. Machine.Release(Machine.Objects)
  851. ELSE (* already locked *)
  852. IF hdr.lockedBy = r THEN
  853. Machine.Release(Machine.Objects);
  854. HALT(2203) (* nested locks not allowed *)
  855. END;
  856. ASSERT(r.waitingOn = NIL); (* sanity check *)
  857. Remove(ready, r);
  858. IF r.event = 0 THEN
  859. r.event := Kernel32.CreateEvent( NIL, Kernel32.False (* auto *), Kernel32.False, NIL ); (* auto reset event with initial state = reset *)
  860. ASSERT ( r.event # 0, 1239 );
  861. END;
  862. r.waitingOn := obj; r.mode := AwaitingLock;
  863. Put(hdr.awaitingLock, r); INC(awl);
  864. Machine.Release(Machine.Objects);
  865. res := Kernel32.WaitForSingleObject(r.event, Kernel32.Infinite); (* block execution *)
  866. ASSERT(res = Kernel32.WaitObject0);
  867. IF StrongChecks THEN
  868. ASSERT(hdr.lockedBy = r); (* at this moment only this process can own the lock and only this process can release it*)
  869. END;
  870. END
  871. END Lock;
  872. (* Find the first true condition from the queue and remove it. Assume the object is currently locked. *)
  873. PROCEDURE FindCondition( VAR q: ProcessQueue ): Process;
  874. VAR first, cand: Process;
  875. BEGIN
  876. Get( q, first );
  877. IF first.condition( first.condFP ) THEN RETURN first END;
  878. Put( q, first );
  879. WHILE q.head # first DO
  880. Get( q, cand );
  881. IF cand.condition( cand.condFP ) THEN RETURN cand END;
  882. Put( q, cand )
  883. END;
  884. RETURN NIL
  885. END FindCondition;
  886. (* The procedure Lock, Unlock and Await do not use header locks since it turned out that the header locks sometimes were finalized
  887. too early. *)
  888. PROCEDURE Unlock*( obj: ProtectedObject; dummy: BOOLEAN );
  889. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; t, c: Process; res: LONGINT;
  890. BEGIN
  891. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  892. IF StrongChecks THEN
  893. ASSERT(hdr IS Heaps.ProtRecBlock) (* protected object *)
  894. END;
  895. ASSERT(hdr.count = -1); (* exclusive locked *)
  896. Machine.Acquire(Machine.Objects);
  897. IF hdr.awaitingCond.head # NIL THEN (* evaluate the waiting conditions *)
  898. (* we are holding the lock, so the queue can not change (to do: except in TerminateThis) *)
  899. c := FindCondition(hdr.awaitingCond); (* interrupts should be on during this call *)
  900. ELSE
  901. c := NIL
  902. END;
  903. IF c = NIL THEN (* no true condition found, check the lock queue *)
  904. Get(hdr.awaitingLock, t);
  905. IF t # NIL THEN
  906. hdr.lockedBy := t;
  907. t.waitingOn := NIL;
  908. ELSE
  909. hdr.lockedBy := NIL; hdr.count := 0
  910. END
  911. ELSE (* true condition found, transfer the lock *)
  912. c.waitingOn := NIL; hdr.lockedBy := c;
  913. t := NIL
  914. END;
  915. IF c # NIL THEN
  916. Put(ready, c); c.mode := Running; DEC(awc);
  917. res := Kernel32.SetEvent(c.event);
  918. ASSERT (res # 0, 1001);
  919. ELSIF t # NIL THEN
  920. Put(ready, t); t.mode := Running; DEC(awl);
  921. res := Kernel32.SetEvent(t.event);
  922. ASSERT (res # 0, 1002);
  923. END;
  924. Machine.Release( Machine.Objects )
  925. END Unlock;
  926. (* The procedure Lock, Unlock and Await do not use header locks since it turned out that the header locks sometimes were finalized
  927. too early. *)
  928. PROCEDURE Await*( cond: Condition; slink: LONGINT; obj: ProtectedObject; flags: SET );
  929. VAR hdr {UNTRACED}: Heaps.ProtRecBlock; r, c, t: Process; res: LONGINT;
  930. BEGIN
  931. IF 1 IN flags THEN (* compiler did not generate IF *)
  932. IF cond(slink) THEN
  933. RETURN (* condition already true *)
  934. END
  935. END;
  936. SYSTEM.GET(SYSTEM.VAL(ADDRESS, obj) + Heaps.HeapBlockOffset, hdr);
  937. IF StrongChecks THEN
  938. ASSERT(hdr IS Heaps.ProtRecBlock) (* protected object *)
  939. END;
  940. r := CurrentProcess();
  941. Machine.Acquire(Machine.Objects);
  942. IF hdr.lockedBy = r THEN (* current process holds exclusive lock *)
  943. IF StrongChecks THEN ASSERT(hdr.count = -1) END; (* exclusive locked *)
  944. IF hdr.awaitingCond.head # NIL THEN (* evaluate the waiting conditions *)
  945. (* we are holding the lock, so the queue can not change (to do: except in TerminateThis) *)
  946. c := FindCondition(hdr.awaitingCond) (* interrupts should be on during this call *)
  947. ELSE
  948. c := NIL
  949. END;
  950. IF c = NIL THEN
  951. Get(hdr.awaitingLock, t);
  952. IF t = NIL THEN (* none waiting - remove lock *)
  953. hdr.count := 0; hdr.lockedBy := NIL;
  954. ELSE (* transfer lock to first waiting process *)
  955. IF StrongChecks THEN ASSERT(t.mode = AwaitingLock) END;
  956. t.waitingOn := NIL;
  957. hdr.lockedBy := t;
  958. END;
  959. ELSE
  960. c.waitingOn := NIL; hdr.lockedBy := c;
  961. t := NIL;
  962. END;
  963. ELSE (* no lock, or some other process may hold the lock, but that's the user's indaba (may be monotonic condition) *)
  964. Machine.Release(Machine.Objects);
  965. HALT( 2204 ) (* await must be exclusive region *)
  966. END;
  967. r.condition := cond; r.condFP := slink;
  968. r.waitingOn := obj; r.mode := AwaitingCond;
  969. Remove(ready, r);
  970. IF r.event = 0 THEN
  971. r.event := Kernel32.CreateEvent( NIL, Kernel32.False (* auto *), Kernel32.False, NIL ); (* auto-reset event with initial state = reset *)
  972. ASSERT ( r.event # 0, 1239 );
  973. END;
  974. IF c # NIL THEN
  975. DEC(awc); Put(ready, c); c.mode := Running;
  976. res := Kernel32.SetEvent(c.event); (* restart execution *)
  977. ASSERT(res # 0, 1002);
  978. END;
  979. IF t # NIL THEN
  980. DEC(awl); Put(ready, t); t.mode := Running;
  981. res := Kernel32.SetEvent( t.event ); (* restart execution *)
  982. ASSERT(res # 0, 1003);
  983. END;
  984. Put(hdr.awaitingCond, r); INC(awc);
  985. Machine.Release(Machine.Objects);
  986. res := Kernel32.WaitForSingleObject(r.event, Kernel32.Infinite); (* block execution *)
  987. ASSERT(res = Kernel32.WaitObject0);
  988. IF StrongChecks THEN
  989. ASSERT(cond(slink));
  990. ASSERT(hdr.lockedBy = r) (* lock held again *)
  991. END
  992. END Await;
  993. PROCEDURE Break*( t: Process );
  994. CONST MaxTry = 50;
  995. VAR mod: Modules.Module; try: LONGINT; retBOOL: Kernel32.BOOL; (* Dan 09.11.05 *)
  996. PROCEDURE SafeForBreak( mod: Modules.Module ): BOOLEAN;
  997. BEGIN
  998. Trace.String( "Safe for break?: " );
  999. IF mod # NIL THEN
  1000. Trace.StringLn ( mod.name );
  1001. IF (mod.name = "Trace") OR (mod.name = "Machine") OR
  1002. (mod.name = "Heaps") OR (mod.name = "Modules") OR
  1003. (mod.name = "Objects") OR (mod.name = "Kernel") THEN
  1004. Trace.StringLn ( " - no" ); RETURN FALSE
  1005. ELSE Trace.StringLn ( " - yes" ); RETURN TRUE
  1006. END
  1007. ELSE Trace.StringLn ( "unknown module" ); RETURN FALSE
  1008. END
  1009. END SafeForBreak;
  1010. BEGIN
  1011. IF CurrentProcess() # t THEN
  1012. Machine.Acquire( Machine.Objects );
  1013. LOOP
  1014. retBOOL := Kernel32.SuspendThread( t.handle );
  1015. t.state.ContextFlags := SYSTEM.VAL(LONGINT, Kernel32.ContextControl);
  1016. retBOOL := Kernel32.GetThreadContext( t.handle, t.state );
  1017. mod := Modules.ThisModuleByAdr( t.state.PC ); Trace.String( "Objects Break at adr: " );
  1018. Trace.Int( t.state.PC, 5 ); Trace.Ln;
  1019. IF mod # NIL THEN
  1020. Trace.String( "In module: " ); Trace.StringLn ( mod.name );
  1021. END;
  1022. IF ~SafeForBreak( mod ) (* we do not break Kernel modules *) THEN
  1023. retBOOL := Kernel32.ResumeThread( t.handle ); INC( try );
  1024. IF try > MaxTry THEN
  1025. Trace.StringLn ( "Threads.Break: failed " );
  1026. Machine.Release( Machine.Objects );
  1027. RETURN
  1028. END
  1029. ELSE EXIT
  1030. END;
  1031. END;
  1032. (* push cont.Eip *) break[0] := 68X;
  1033. SYSTEM.MOVE( ADDRESSOF( t.state.PC ), ADDRESSOF( break[1] ), 4 );
  1034. (* push ebp *) break[5] := 055X;
  1035. (* mov ebp, esp *) break[6] := 08BX; break[7] := 0ECX;
  1036. (* push 13 *) break[8] := 06AX; break[9] := 0DX;
  1037. (* int 3 *) break[10] := 0CCX;
  1038. (* mov esp, ebp *) break[11] := 08BX; break[12] := 0E5X;
  1039. (* pop ebp *) break[13] := 05DX;
  1040. (* ret *) break[14] := 0C3X; t.state.PC := ADDRESSOF( break[0] );
  1041. retBOOL := Kernel32.SetThreadContext( t.handle, t.state );
  1042. retBOOL := Kernel32.ResumeThread( t.handle ); (* INC( Kernel.GClevel ); *)
  1043. Machine.Release( Machine.Objects );
  1044. ELSE HALT( 99 )
  1045. END;
  1046. END Break;
  1047. (* Attempt to terminate a specific process (mostly ignoring its locks). DEPRECATED *)
  1048. PROCEDURE TerminateThis*( t: Process; halt: BOOLEAN );
  1049. BEGIN
  1050. terminate(t);
  1051. END TerminateThis;
  1052. PROCEDURE Terminate*;
  1053. BEGIN
  1054. TerminateProc();
  1055. END Terminate;
  1056. TYPE KdHelp = RECORD
  1057. Thread: ADDRESS;
  1058. ThCallBackStack,ThCallBackStore,NextCallback,FramePointer: Kernel32.DWORD;
  1059. KiCallUserMode, KeUserCallbackDispatcher,SystemRangeStart,KiUserExceptionDispatcher,StackBase,StackLimit: ADDRESS;
  1060. Reserved: ARRAY 5 OF ADDRESS;
  1061. END;
  1062. (*
  1063. TYPE
  1064. ADDRESS64 = RECORD
  1065. offset: ADDRESS;
  1066. Segment: INTEGER;
  1067. AddressMode: INTEGER;
  1068. END;
  1069. TYPE StackFrame64 = RECORD
  1070. AddrPC, AddrReturn,AddrStack,AddrBSTore: ADDRESS64;
  1071. FuncTableEntry: ADDRESS;
  1072. Params: ARRAY 4 OF ADDRESS;
  1073. Far, Virtual: BOOLEAN;
  1074. Reserved: ARRAY 3 OF ADDRESS;
  1075. kdHelp: KdHelp;
  1076. END;
  1077. VAR StackWalk64: PROCEDURE {WINAPI} (MachineType: Kernel32.DWORD; hProcess: Kernel32.HANDLE;
  1078. hThread: Kernel32.HANDLE; VAR stackFrame: StackFrame64; VAR contextRecord: Kernel32.Context;
  1079. CONST ReadMemoryRoutine, FunctionTableAccessRoutine, GetModuleBaseRoutine,
  1080. TranslateAddress: ADDRESS): Kernel32.BOOL;
  1081. *)
  1082. PROCEDURE Init; (* can not use NEW *)
  1083. (*VAR lock: PROCEDURE(obj: ProtectedObject; exclusive: BOOLEAN);
  1084. unlock: PROCEDURE(obj: ProtectedObject; dummy: BOOLEAN);
  1085. await: PROCEDURE(cond: Condition; slink: LONGINT; obj: ProtectedObject; flags: SET);
  1086. create: PROCEDURE(body: Body; priority: LONGINT; flags: SET; obj: ProtectedObject);
  1087. *)
  1088. VAR t: Process; fn: Heaps.FinalizerNode; proc: Kernel32.HANDLE;
  1089. res: Kernel32.BOOL;
  1090. lib: Kernel32.HMODULE;
  1091. BEGIN
  1092. Kernel32.AddVectoredExceptionHandler(1, ExcpFrmHandler);
  1093. (* lib := Kernel32.LoadLibrary("DbgHelp.DLL");
  1094. Kernel32.GetProcAddress(lib, "StackWalk64",SYSTEM.VAL(ADDRESS,StackWalk64));
  1095. *)
  1096. Kernel32.InitializeCriticalSection(excplock);
  1097. numberOfProcessors := Machine.NumberOfProcessors();
  1098. (* lock := Lock; unlock := Unlock; await := Await; create := CreateProcess;*)
  1099. NEW(t); NEW(fn);
  1100. Machine.Acquire(Machine.Objects);
  1101. t.gcContext.nextPos := 0;
  1102. nProcs := 1;
  1103. t.next := NIL; t.prev := NIL;
  1104. t.waitingOn := NIL; t.flags := {}; t.obj := NIL;
  1105. t.mode := Unknown; t.body := NIL;
  1106. t.priority := Normal;
  1107. fn.finalizer := FinalizeProcess;
  1108. Heaps.AddFinalizer(t, fn);
  1109. t.handle := Kernel32.GetCurrentThread();
  1110. t.id := Kernel32.GetCurrentThreadId();
  1111. proc := Kernel32.GetCurrentProcess();
  1112. res := Kernel32.DuplicateHandle(proc, t.handle, proc, t.handle, Kernel32.SetToDW({}), 0, Kernel32.SetToDW({Kernel32.DuplicateSameAccess}));
  1113. ASSERT(res # 0);
  1114. res := Kernel32.TlsSetValue(tlsIndex, SYSTEM.VAL(LONGINT, t));
  1115. ASSERT(res # 0);
  1116. t.stackBottom := StackBottom(); t.mode := Running;
  1117. Put( ready, t );
  1118. ASSERT(t.handle # 0);
  1119. Machine.Release(Machine.Objects);
  1120. InitEventHandling; (* implicit call of NewProcess! *)
  1121. InitGCHandling; (* do. *)
  1122. Heaps.gcStatus := GCStatusFactory();
  1123. END Init;
  1124. (** Set (or reset) an event handler object's timeout value. *)
  1125. PROCEDURE SetTimeout*(t: Timer; h: EventHandler; ms: LONGINT );
  1126. VAR e: Timer; trigger: LONGINT;
  1127. BEGIN
  1128. ASSERT(Machine.Second= 1000); (* assume milliseconds for now *)
  1129. ASSERT((t # NIL) & (h # NIL));
  1130. ASSERT(ms >= 0);
  1131. Machine.Acquire(Machine.Objects);
  1132. trigger := Kernel32.GetTickCount() + ms; (* ignore overflow *)
  1133. IF t.next # NIL THEN (* cancel previous timeout *)
  1134. t.next.prev := t.prev; t.prev.next := t.next
  1135. END;
  1136. t.trigger := trigger; t.handler := h;
  1137. e := event.next; (* performance: linear search! *)
  1138. WHILE (e # event) & (e.trigger - trigger <= 0) DO e := e.next END;
  1139. t.prev := e.prev; e.prev := t; t.next := e; t.prev.next := t;
  1140. Machine.Release(Machine.Objects);
  1141. clock.Wakeup()
  1142. END SetTimeout;
  1143. (** Set (or reset) an event handler object's timeout value. Here ms is absolute *)
  1144. PROCEDURE SetTimeoutAt*(t: Timer; h: EventHandler; ms: LONGINT);
  1145. VAR e: Timer; trigger: LONGINT;
  1146. BEGIN
  1147. ASSERT(Machine.Second= 1000); (* assume milliseconds for now *)
  1148. ASSERT((t # NIL) & (h # NIL));
  1149. Machine.Acquire(Machine.Objects);
  1150. trigger := ms; (* ignore overflow *)
  1151. IF t.next # NIL THEN (* cancel previous timeout *)
  1152. t.next.prev := t.prev; t.prev.next := t.next
  1153. END;
  1154. t.trigger := trigger; t.handler := h;
  1155. e := event.next; (* performance: linear search! *)
  1156. WHILE (e # event) & (e.trigger - trigger <= 0) DO e := e.next END;
  1157. t.prev := e.prev; e.prev := t; t.next := e; t.prev.next := t;
  1158. Machine.Release(Machine.Objects);
  1159. clock.Wakeup()
  1160. END SetTimeoutAt;
  1161. (** Cancel an event handler object's timeout, if any. It is possible that the timer has expired, but not yet been scheduled to run. *)
  1162. PROCEDURE CancelTimeout*( t: Timer );
  1163. BEGIN
  1164. Machine.Acquire(Machine.Objects);
  1165. ASSERT (t # event );
  1166. IF t.next # NIL THEN
  1167. t.next.prev := t.prev;
  1168. IF t.prev#NIL THEN t.prev.next := t.next; END;
  1169. t.next := NIL;
  1170. t.prev := NIL
  1171. END;
  1172. Machine.Release(Machine.Objects);
  1173. END CancelTimeout;
  1174. PROCEDURE InitEventHandling;
  1175. BEGIN
  1176. NEW(event); event.next := event; event.prev := event; (* event: head of timer event queue, only a sentinel *)
  1177. NEW(clock);
  1178. END InitEventHandling;
  1179. PROCEDURE InitGCHandling;
  1180. BEGIN
  1181. NEW(finalizerCaller);
  1182. END InitGCHandling;
  1183. PROCEDURE GCStatusFactory(): Heaps.GCStatus;
  1184. VAR gcStatusExt : GCStatusExt;
  1185. BEGIN
  1186. ASSERT(Heaps.gcStatus = NIL);
  1187. NEW(gcStatusExt);
  1188. RETURN gcStatusExt
  1189. END GCStatusFactory;
  1190. PROCEDURE InstallExceptionHandler*( e: ExceptionHandler );
  1191. BEGIN
  1192. exceptionhandler := e;
  1193. END InstallExceptionHandler;
  1194. PROCEDURE UpdateProcessState*( p: Process );
  1195. VAR res: Kernel32.BOOL;
  1196. BEGIN
  1197. res := Kernel32.GetThreadContext( p.handle, p.state );
  1198. ASSERT (p.handle # 0);
  1199. END UpdateProcessState;
  1200. (*ALEX 2005.12.12 added for WMPerfMon needs*)
  1201. PROCEDURE NumReady*( ): LONGINT;
  1202. VAR n: LONGINT; p: Heaps.ProcessLink;
  1203. BEGIN
  1204. n := 0;
  1205. Machine.Acquire( Machine.Objects );
  1206. p := ready.head;
  1207. WHILE p # NIL DO INC( n ); p := p.next END;
  1208. Machine.Release( Machine.Objects );
  1209. RETURN n
  1210. END NumReady;
  1211. (** Return number of CPU cycles consumed by the specified process. If all is TRUE,
  1212. return the number of cycles since the process has been created. If FALSE, return the number of cycles
  1213. consumed since the last time asked. *)
  1214. PROCEDURE GetCpuCycles*(process : Process; VAR cpuCycles : CpuCyclesArray; all : BOOLEAN);
  1215. VAR res : Kernel32.BOOL; temp : HUGEINT;
  1216. BEGIN
  1217. ASSERT(process # NIL);
  1218. IF (Kernel32.QueryThreadCycleTime # NIL) THEN
  1219. res := Kernel32.QueryThreadCycleTime(process.handle, cpuCycles[0]);
  1220. ELSE
  1221. cpuCycles[0] := Machine.GetTimer(); res := Kernel32.True;
  1222. END;
  1223. IF ~all & (res = Kernel32.True) THEN
  1224. temp := process.lastThreadTimes;
  1225. process.lastThreadTimes := cpuCycles[0];
  1226. cpuCycles[0] := cpuCycles[0] - temp;
  1227. END;
  1228. END GetCpuCycles;
  1229. PROCEDURE CurrentProcessTime*(): HUGEINT;
  1230. VAR res: LONGINT; result: HUGEINT;
  1231. BEGIN
  1232. IF (Kernel32.QueryThreadCycleTime # NIL) THEN
  1233. res := Kernel32.QueryThreadCycleTime(CurrentProcess().handle, result);
  1234. ELSE (* fallback *)
  1235. result := Machine.GetTimer();
  1236. END;
  1237. RETURN result;
  1238. END CurrentProcessTime;
  1239. PROCEDURE TimerFrequency*(): HUGEINT;
  1240. BEGIN
  1241. RETURN 1000000000;
  1242. END TimerFrequency;
  1243. VAR GetProcedureName*: PROCEDURE (pc: ADDRESS; VAR n: ARRAY OF CHAR; VAR spc: ADDRESS);
  1244. PROCEDURE LeaveA2;
  1245. VAR cur: Process; ebp,n: ADDRESS;
  1246. BEGIN
  1247. CODE
  1248. PUSH RCX
  1249. PUSH RDX
  1250. PUSH R8
  1251. PUSH R9
  1252. END;
  1253. IF clock = NIL THEN
  1254. RETURN
  1255. END;
  1256. cur := CurrentProcess();
  1257. IF cur # NIL THEN
  1258. ebp := Machine.CurrentBP();
  1259. SYSTEM.GET(ebp, n);
  1260. IF ODD(n) THEN SYSTEM.GET(ebp + SIZEOF(ADDRESS), ebp) ELSE ebp := n END;
  1261. cur.gcContext.last[cur.gcContext.nextPos] := ebp;
  1262. INC(cur.gcContext.nextPos);
  1263. ASSERT(cur.gcContext.nextPos < 255);
  1264. IF cur.gcContext.nextPos > 255 THEN cur.gcContext.nextPos := 255 END;
  1265. (* IF (cur.gcContext.nextPos > 4) THEN cur.gcContext.nextPos := 2 END;*)
  1266. END;
  1267. CODE
  1268. POP R9
  1269. POP R8
  1270. POP RDX
  1271. POP RCX
  1272. END;
  1273. END LeaveA2;
  1274. PROCEDURE ReenterA2;
  1275. VAR cur: Process;
  1276. BEGIN
  1277. IF clock = NIL THEN RETURN END;
  1278. cur := CurrentProcess();
  1279. IF cur # NIL THEN
  1280. (* cur.gcContext.first[cur.gcContext.next] := NIL;*)
  1281. DEC(cur.gcContext.nextPos);
  1282. IF (cur.gcContext.nextPos <0 ) THEN cur.gcContext.nextPos := 0 END;
  1283. cur.gcContext.last[cur.gcContext.nextPos] := NIL; (* returned *)
  1284. END;
  1285. END ReenterA2;
  1286. VAR
  1287. TraceProcessHook*: PROCEDURE (prcoess: Process; pc, bp: ADDRESS; stacklow, stackhigh: ADDRESS);
  1288. BEGIN
  1289. TraceProcessHook := NIL;
  1290. exceptionhandler := NIL;
  1291. terminateProc := TerminateProc;
  1292. ready.head := NIL; ready.tail := NIL;
  1293. tlsIndex := Kernel32.TlsAlloc();
  1294. ASSERT ( tlsIndex # Kernel32.TLSOutOfIndexes );
  1295. Kernel32.SendToDebugger("Modules.root", ADDRESSOF(Modules.root));
  1296. Init;
  1297. END Objects.
  1298. (*
  1299. 24.03.1998 pjm Started
  1300. 06.05.1998 pjm CreateProcess init process, page fault handler
  1301. 06.08.1998 pjm Moved exception interrupt handling here for current process
  1302. 17.08.1998 pjm FindRoots method
  1303. 02.10.1998 pjm Idle process
  1304. 06.11.1998 pjm snapshot
  1305. 25.03.1999 pjm Scope removed
  1306. 28.05.1999 pjm EventHandler object
  1307. 01.06.1999 pjm Fixed InterruptProcess lock error
  1308. 16.06.1999 pjm Flat IRQ priority model to avoid GC deadlock
  1309. 23.06.1999 pjm Flat IRQ priority experiment failed, rather do STI in FieldIRQ to avoid GC deadlock
  1310. 29.06.1999 pjm Timeout in EventHandler object
  1311. 13.01.2000 pjm Overed (Interrupt Objects, Event Handlers, Process ID, Process state, Process mode, Process stack, Await)
  1312. 17.10.2000 pjm Priorities
  1313. 22.10.2003 mib SSE2 extension
  1314. 24.10.2003 phk Priority inversion / cycle counters
  1315. Stack invariant for GC:
  1316. o if process is running, the processor registers contain its state
  1317. o if process is not running, at least state.ESP is valid, and between stack.adr and stack.high (for GC)
  1318. o when releasing the Ready lock, make sure the process state is up to date
  1319. *)
  1320. SystemTools.ShowStacks ~
  1321. Heaps.SetMetaData
  1322. StaticLinker.Link --fileFormat=PE32 --fileName=A2GC.exe --extension=GofW --displacement=401000H Runtime Trace Kernel32 Machine Heaps Modules Objects Kernel KernelLog Streams Commands FIles WinFS Clock Dates Reals Strings Diagnostics BitSets StringPool ObjectFile GenericLinker Reflection GenericLoader BootConsole ~