Windows.Objects.Mod 56 KB

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