2
0

Win64.Objects.Mod 50 KB

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