Win64.Objects.Mod 50 KB

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