<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9 - Discussions by Dexter</title>
	<atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Niners/Dexter/Discussions/RSS"></atom:link>
	<image>
		<url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
		<title>Channel 9 - Discussions by Dexter</title>
		<link>http://channel9.msdn.com/Niners/Dexter/Discussions</link>
	</image>
	<description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
	<link>http://channel9.msdn.com/Niners/Dexter/Discussions</link>
	<language>en</language>
	<pubDate>Sat, 25 May 2013 10:38:45 GMT</pubDate>
	<lastBuildDate>Sat, 25 May 2013 10:38:45 GMT</lastBuildDate>
	<generator>Rev9</generator>
	<c9:totalResults>0</c9:totalResults>
	<c9:pageCount>0</c9:pageCount>
	<c9:pageSize>0</c9:pageSize>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">the exception that gets thrown when running the C&#43;&#43; app outside of the debugger is 0x40010006, which is DBG_PRINTEXCEPTION_C</div></blockquote><p></p><p>Hmm, I have the feeling that Windows won't always like this stack switching business.&nbsp;NT's structured exception handling relies on the stack and if you switch the stack... I'm not quite sure what can happen.</p><p></p><blockquote><div class="quoteText">I'm wondering whether the thread switching function is really saving enough registers</div></blockquote><p></p><p>It should be enough unless you use SSE. A normal kernel the relise on interrupts to switch threads has no choice but to save all registers because it doesn't know which registers are used. But what you're doing here is more like cooperative multithreading and more importantly, it's all done in C. The C/C&#43;&#43; compiler expects that a function will preserve those 4 registers (ebp, ebx, edi, esi), all other general purpose registers can be modified by the function as needed. Also, the FPU stack is supposed to be empty when a function is called so it doesn't contain anything that needs saving.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/ac2a816c9db04a649c099f49009bc5ef#ac2a816c9db04a649c099f49009bc5ef</link>
		<pubDate>Wed, 24 Aug 2011 09:27:09 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/ac2a816c9db04a649c099f49009bc5ef#ac2a816c9db04a649c099f49009bc5ef</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p>OK, but did you try the fix for the start function? Does it work?</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/bf8c9d8dfd794cea9c549f4701085b70#bf8c9d8dfd794cea9c549f4701085b70</link>
		<pubDate>Mon, 22 Aug 2011 16:02:29 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/bf8c9d8dfd794cea9c549f4701085b70#bf8c9d8dfd794cea9c549f4701085b70</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">Note I need 4 versions, depending on whether it is a static or instance call, and whether it passes the extra data or not</div></blockquote><p></p><p>That sounds a bit excesive. Normally there is a single thread start function and this isn't the function you pass to the Thread constructor through a delegate. This start function is a special function that takes care of initializing the thread and then calls the delegate.</p><p>Also, the static/instance distinction is the job of the delegate, the thread start function should not be sensitive to this.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/04081772010e44eab4da9f470099eea4#04081772010e44eab4da9f470099eea4</link>
		<pubDate>Mon, 22 Aug 2011 09:20:26 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/04081772010e44eab4da9f470099eea4#04081772010e44eab4da9f470099eea4</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p>Stupid me, I'm talking about the way threads exit and I forget that the return address of the start function needs to be pushed onto the stack to. Even if the start function never returns because it does a SwitchContext you still need to push it because otherwise the start function can't find its arguments.</p><p>Between push eax and push esi you need to add another push for the return address. This can be 0 if you chose never to return from the start function or it can be the address of an &quot;exit function&quot; that calls SwitchContext to get away from the thread.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/972ea52911f44c67af6e9f4700950723#972ea52911f44c67af6e9f4700950723</link>
		<pubDate>Mon, 22 Aug 2011 09:02:35 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/972ea52911f44c67af6e9f4700950723#972ea52911f44c67af6e9f4700950723</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">but EBP is null and so the stack is also messed up</div></blockquote><p></p><p>Well, it's null because that's how InitializeContext initializes it. But why do you care about the contents of EBP? The start function doesn't expect a value in this register.</p><p></p><blockquote><div class="quoteText">Below is what I have so far</div></blockquote><p></p><p>I see you're pushing&nbsp;ESP, why? Again, this will be poped as EBP but you don't need EBP to have any particular value.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/dae460b487254c11809b9f4700739b6a#dae460b487254c11809b9f4700739b6a</link>
		<pubDate>Mon, 22 Aug 2011 07:00:54 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/dae460b487254c11809b9f4700739b6a#dae460b487254c11809b9f4700739b6a</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">So instead, I need to have an additional void* in each thread whose address will be passed in to the SwitchContext call, correct?</div></blockquote><p></p><p>Yes, this value contains the current stack pointer for the thread. When you switch away from a thread its current stack pointer gets saved and the stack pointer of the new thread is loaded.</p><p></p><blockquote><div class="quoteText">The question is, if this is the very first context switch, what should be stored at the pNewStack value</div></blockquote><p></p><p>That's the role of InitializeContext, it sets up the initial stack so it looks similar to the one expected by SwitchContext. That's why the start address gets pushed on the stack, when SwitchContext will<strong>ret</strong>urn to what's stored on the stack, your start address. This might sound strange, the start function is not<strong>call</strong>ed but <strong>ret</strong>urned too, but it works fine as long as the stack is setup correctly. That's also the reason for those 4 push eax, they compensate the fact that SwitchContext pops edi, esi, ebp, ebx.</p><p></p><blockquote><div class="quoteText">I assume this should be at some negative offset into that thread's NativeStack</div></blockquote><p></p><p>The InitializeContext already computes the correct value but it looks like it doesn't store it correctly. Let's fix things a bit:</p><p>void NativePlatform::InitializeThreadContext(RuntimeThread* pThread, VirtualObject** pData) {<br>&nbsp;&nbsp;&nbsp;&nbsp;void* stack = pThread-&gt;NativeStack;<br>&nbsp;&nbsp;&nbsp;&nbsp;void* startAddress = (void*)pThread-&gt;GetStartAddress();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;__asm {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Save current stack<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov ecx, esp<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Switch to pThread's stack<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov esp, stack<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Push arguments for the start function<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// It looks like your start function expects pThread and pData so:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov eax, pData<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov eax, pThread<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Push the start function address, SwitchContext will return to this address<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov eax, startAddress<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Push 4 values to compensate for pop edi, esi, ebx, ebp in SwitchContext<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xor eax, eax<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// At this point the stack is similar to what SwitchContext expects, save the stack pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov stack, esp<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Switch back to this thread's stack<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov esp, ecx<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp; // save the thread's current stack pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;pThread-&gt;CurrentStackPointer = stack;<br>}</p><p>I added some comments to make it more clear and also added arguments for your start function (my previous example didn't pass any arguments to the start function).</p><p>After InitializeContext runs you should be able to switch to the new thread by doing SwitchContext(&amp;currentThread-&gt;CurrentStackPointer, &amp;newThread-&gt;CurrentStackPointer).</p><p>Now there's another fun thing to solve. What happens when the thread exits? <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-2.gif?v=c9' alt='Big Smile' /></p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/636f1689d6b6408b85dc9f46016aefea#636f1689d6b6408b85dc9f46016aefea</link>
		<pubDate>Sun, 21 Aug 2011 22:01:24 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/636f1689d6b6408b85dc9f46016aefea#636f1689d6b6408b85dc9f46016aefea</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p>He he, I was wondering about&nbsp;the stack size but I didn't expect cout to need&nbsp;that much. I'm glad it's working now.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/e1b6cc6c1ae3466a87f29f46012a49ce#e1b6cc6c1ae3466a87f29f46012a49ce</link>
		<pubDate>Sun, 21 Aug 2011 18:06:02 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/e1b6cc6c1ae3466a87f29f46012a49ce#e1b6cc6c1ae3466a87f29f46012a49ce</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p>Hmm, I guess this will be &quot;fun&quot; to debug. Couple more questions:</p><ol><li>you mentioned that it happens after using cout, what happens if you don't use it? </li><li>How many threads are you starting and from where? All from main thread? Or each thread starts another thread? </li></ol>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/0fdbfed9ccae4e1c9aba9f4601268f0c#0fdbfed9ccae4e1c9aba9f4601268f0c</link>
		<pubDate>Sun, 21 Aug 2011 17:52:27 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/0fdbfed9ccae4e1c9aba9f4601268f0c#0fdbfed9ccae4e1c9aba9f4601268f0c</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p>&quot;You are thinking that maybe the calling convention is wrong?&quot;</p><p>Unlikely. It can be wrong only if you changed the default from cdecl to stdcall or fastcall.</p><p>Hmm, the code looks correct. The 4 pops after the calls aren't really needed but they don't hurt.</p><p>Heap corruption &#43; stack allocated using malloc (I assume)... are you sure the intial native stack pointer is correct?</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/e310a9348a66451a9f9a9f42014aef47#e310a9348a66451a9f9a9f42014aef47</link>
		<pubDate>Wed, 17 Aug 2011 20:04:53 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/e310a9348a66451a9f9a9f42014aef47#e310a9348a66451a9f9a9f42014aef47</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p>How is startAddress function declared?</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/95a1c8a43db24b9ba1fe9f42014045af#95a1c8a43db24b9ba1fe9f42014045af</link>
		<pubDate>Wed, 17 Aug 2011 19:26:04 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/95a1c8a43db24b9ba1fe9f42014045af#95a1c8a43db24b9ba1fe9f42014045af</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">Interestingly, the compiler gives me this warning:</div></blockquote><p></p><p>I'll answer this first because the rest can't be done properly with this in the way. The compiler can generate prolog/epilog code for a function:</p><p>SwitchContext: <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push ebp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; save the previous frame pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;ebp, esp&nbsp;&nbsp;&nbsp;&nbsp;; setup the frame pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub&nbsp;&nbsp;esp, 8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;; space for local variables<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;; function code<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;esp, ebp&nbsp;&nbsp;&nbsp;&nbsp;; restore the stack pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;ebp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; restore the frame pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret</p><p><br>Now it should be obvious what's the warning about. The compiler uses ebp for its own purposes and warns you if you change it in inline asm. This compiler generated code can be a problem when you want to implement context switching because it affects the stack layout. You can implement SwitchContext even if such code is present but creating the stack for a new thread will be a problem because you don't know what stack layout SwitchContext expects.</p><p>A possible solution is to use __declspec(naked) (which prevents such code from being generated) and __fastcall (which causes the first 2 arguments of the function to be passed in registers ecx and edx).</p><p></p><blockquote><div class="quoteText">I'm a bit rusty on my x86 assembly, but what I don't understand is that the code is moving oldStack into eax, then a few lines later, esp is moved into eax. Is my translation of the original code even correct?</div></blockquote><p></p><p>Nope. In the original version there were some parathesis which you ignored. Something like mov %esp, (%eax) converts to mov [eax], esp.</p><p>Here's an example that uses __declspec(naked) and __fastcall):</p><p>__declspec(naked) void __fastcall SwitchContext(VirtualThread* pOld, VirtualThread* pNew)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;// N.B. the following code assumes that NativeStack is at offset 4 in VirtualThread. If that's not true<br>&nbsp;&nbsp;&nbsp;&nbsp;// then the appropiate offset needs to be used when storing/loading the stack.<br>&nbsp;&nbsp;&nbsp;&nbsp;__asm<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Save registers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push ebp<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push ebx<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push esi<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push edi<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Save old stack<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov [ecx&#43;4], esp&nbsp;&nbsp;&nbsp;&nbsp;// store to pOld-&gt;NativeStack<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Load new stack<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov esp, [edx&#43;4]&nbsp;&nbsp;&nbsp;&nbsp;// load from pNew-&gt;NativeStack<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Restore registers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop edi<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop esi<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop ebx<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pop ebp<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}</p><p></p><blockquote><div class="quoteText">So what I'm missing right now is a way to set up these stacks for the initial run</div></blockquote><p></p><p>To create a new thread you have to allocate memory for its native stack and setup the stack exactly the same as in SwitchContext does. Once the thread stack is properly initialized you can simply call SwitchContext to start the new thread.</p><p></p><blockquote><div class="quoteText">In addition, my assumption here is that my VirtualThread class will not only hold the &quot;virtual stack&quot;, but also this native stack that will be used to do context switching for each thread.</div></blockquote><p></p><p>Yes, if you create thread you'll need to also allocate the native stacks yourself.</p><p>void InitializeContext(VirtualThread* pNew, void *startFunction)<br>{&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;int stackSize = 4096;<br>&nbsp;&nbsp;&nbsp;&nbsp;char *stack = reinterpret_cast&lt;char *&gt;(malloc(stackSize)); // allocate some space, malloc is just an example<br>&nbsp;&nbsp;&nbsp;&nbsp;stack &#43;= stackSize; // go to the top of the stack because the stack grows down<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;__asm<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov ecx, esp // save current stack pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov esp, stack<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov eax, startFunction<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax&nbsp;&nbsp;&nbsp;&nbsp;// push the start function address as the return address (of SwitchContext)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Save registers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xor eax, eax // let's always start a thread with zeroed registers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push eax<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov stack, esp<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov esp, ecx // restore the stack pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;pNew-&gt;NativeStack = stack;<br>}</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/ecf9209938b142c4bc379f400091ef2d#ecf9209938b142c4bc379f400091ef2d</link>
		<pubDate>Mon, 15 Aug 2011 08:51:19 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/ecf9209938b142c4bc379f400091ef2d#ecf9209938b142c4bc379f400091ef2d</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">Closer inspection shows that as far as the test on line IL_0017 goes, it will always end up going to line IL_001d, hence it doesn't even bother.</div></blockquote><p></p><p>The test is not the problem, the problem is that the ldc 2 at IL_0019 is nowhere to be found in the generated C&#43;&#43; code. My guess is that the reason for this bug is the following:</p><p></p><blockquote><div class="quoteText">I'm using an evaluation stack based approach that accumulates expressions until an instruction comes along that does some real work, like storing</div></blockquote><p></p><p>The value (expression) that gets stored here was defined in two different places. Somehow you lost track of one of those definitions and ended up always writing 1. IL_001D can be reached with 2 different stack states but the generated code accounts for only for one.</p><p></p><blockquote><div class="quoteText">I'm just curious, what method is used to compile IL to native code?</div></blockquote><p></p><p>I don't know exactly what CLR does but what I'd do is convert the IL to some intermediate representation where there's no stack and where all instruction operands are explicit instead of implicit. Such explicit representation are far more easy to reason about.</p><p>You may say that you aren't writing a compiler but getting familiar with some compiler techniques would be a good idea. Reading the code generation chapter(s) from the &quot;Dragon Book&quot; (<a href="http://dragonbook.stanford.edu/">http://dragonbook.stanford.edu/)</a> is advisable.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/5b7f4620d4374d2f87b09f37008e4e5c#5b7f4620d4374d2f87b09f37008e4e5c</link>
		<pubDate>Sat, 06 Aug 2011 08:38:07 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/5b7f4620d4374d2f87b09f37008e4e5c#5b7f4620d4374d2f87b09f37008e4e5c</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - Environment.SetEnvironmentVariable native c++ ?</title>
		<description><![CDATA[<p>There must be one because otherwise Environement.SetEnvironmentVariable could not have been implemented <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-1.gif?v=c9' alt='Smiley' /></p><p><a href="http://msdn.microsoft.com/en-us/library/ms686206.aspx">http://msdn.microsoft.com/en-us/library/ms686206.aspx</a></p><p>&nbsp;</p><p>&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/EnvironmentSetEnvironmentVariable-native-c-/33a4a3eb118b43f4b4349f3501397e22#33a4a3eb118b43f4b4349f3501397e22</link>
		<pubDate>Thu, 04 Aug 2011 19:01:23 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/EnvironmentSetEnvironmentVariable-native-c-/33a4a3eb118b43f4b4349f3501397e22#33a4a3eb118b43f4b4349f3501397e22</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>3</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">If so, I need to get a new temp variable and assigne it the value of the variable, and also replace the previous variable with the temp variable everywhere it shows up on the stack.</div></blockquote><p></p><p>Mmm, seems like a cool solution to me.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/92604a85271e4fc899509f3500be7d52#92604a85271e4fc899509f3500be7d52</link>
		<pubDate>Thu, 04 Aug 2011 11:33:33 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/92604a85271e4fc899509f3500be7d52#92604a85271e4fc899509f3500be7d52</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">&nbsp;to step back through all of the previous instructions (up until the point it got pushed)</div></blockquote><p></p><p>Hmm, this is very easy to do when the push and pop are in the same basic block. If they're in different blocks then things can get complicate. One thing that you should note about these temporaries is that they have limited lifetime. Once a temporary is popped it can be reused to store another value.</p><p>IMO, you're trying to get things working and optimize the code at the same time. I think it's better to just get things working first.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/0569e7eaca344f1081949f320106eabe#0569e7eaca344f1081949f320106eabe</link>
		<pubDate>Mon, 01 Aug 2011 15:57:15 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/0569e7eaca344f1081949f320106eabe#0569e7eaca344f1081949f320106eabe</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p>I'm baffled. How did your compiler generate this line?</p><p><pre class="brush: cpp">IL_0041: // ldind.u2
temp42 = ((UInt16)*(UInt16*)pStr);</pre></p><p>Since we talk that the stack contains values and not variables, how did pStr got in this statement? It should be temp38 instead.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/ff2104391a03439a97a89f320081bf0a#ff2104391a03439a97a89f320081bf0a</link>
		<pubDate>Mon, 01 Aug 2011 07:52:23 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/ff2104391a03439a97a89f320081bf0a#ff2104391a03439a97a89f320081bf0a</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p>Hmm, you fixed the dup but the generated code is still not correct. In fact it behaves identically to the old code. It occurred to&nbsp;me now that the problem is more complicated than the buggy dup.</p><p>The IL execution stack contains values. When you do a ldarg (or any other ld) <strong>the value stored in the variable is loaded on the stack</strong>. After a ld you can modify the variable in any way you like, the stack will still contain the original value of the variable.</p><p>Your compiler treats the stack differently, it seems to store references to variables instead of values. This means that whenever you use a stack location that &quot;references&quot; a variable, you get the current value of the variable, not the original one that existed at the time ld was executed.</p><p>Let's see this in the code:</p><p><pre class="brush: cpp">dupValue0 = pStr;
pStr = (Char*)(((Int32)dupValue0 &#43; (Int32)2));
local0 = ((((UInt16)*(UInt16*)pStr) == 0) == 0);
</pre></p><p>Despite it being different, its behavior is exactly the same. The last statement still uses the modified value of pStr instead of using the original one. The correct code should look like this:</p><p><pre class="brush: cpp">oldpStr = pStr;
pStr = (Char*)(((Int32)pStr &#43; (Int32)2));
local0 = ((((UInt16)*(UInt16*)oldpStr) == 0) == 0);
</pre></p><p>PS: editor problems: don't paste from VS, it seems that it generates html that confuses the post editor. Paste the code to notepad and then copy paste from notepad. This ensures that you paste plain text and not some weird html.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/177ac8276bef429ca5d39f31015343dd#177ac8276bef429ca5d39f31015343dd</link>
		<pubDate>Sun, 31 Jul 2011 20:35:13 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/177ac8276bef429ca5d39f31015343dd#177ac8276bef429ca5d39f31015343dd</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p>Hmm, AFAIK &quot;reach the dup instructions with different types&quot; is not allowed. See 1.8.1.3 Merging Stack States in ECMA 335.</p><p>Even if this is allowed I think it's still possible to deal with. If a basic block containing dup is reached with 2 different stack states then this can be treated as if there actually were 2&nbsp;distinct basic blocks in the first place.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/1a17a12ff10b41328b029f3100898ad9#1a17a12ff10b41328b029f3100898ad9</link>
		<pubDate>Sun, 31 Jul 2011 08:20:46 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/1a17a12ff10b41328b029f3100898ad9#1a17a12ff10b41328b029f3100898ad9</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p>OK, let me detail this a bit:</p><p><pre class="brush: cpp">IL_003a: // ldarg.1 - load pStr
IL_003b: // dup - duplicate pStr, now the stack contains the value of pStr twice
...
IL_003e: // add - add one of the pStrs and 2
IL_003f: // starg.s: Char* pStr - store the value on top of the stack to pStr
// !!!the original value of pStr remains on top of the stack!!!
    pStr = (Char*)(((Int32)pStr &#43; (Int32)2));IL_0041: // ldind.u2 - load from the address on top of the stack
// that is, from the original pStr
...
    local0 = ((((UInt16)*(UInt16*)pStr) == 0) == 0); // load from pStr - not the original pStr but the incremented one</pre></p><p>The net result is that since you're using the incremented pStr you're one character ahead, this is where the -1 differences comes from.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/0cad1e07036d418084a29f300171c7ef#0cad1e07036d418084a29f300171c7ef</link>
		<pubDate>Sat, 30 Jul 2011 22:26:19 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/0cad1e07036d418084a29f300171c7ef#0cad1e07036d418084a29f300171c7ef</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">but it is going to bloat the code a lot (or at least the source files) since I'll need to create a unique structure for each call.</div></blockquote><p></p><p>Yes the sources will be a bit bigger but I'd say not by much. Note that you need one struct per method, not per call.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/79c7f095c01444c083819f300167e33d#79c7f095c01444c083819f300167e33d</link>
		<pubDate>Sat, 30 Jul 2011 21:50:18 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/79c7f095c01444c083819f300167e33d#79c7f095c01444c083819f300167e33d</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">I wonder if it has something to do the the &quot;dup&quot; instructions.</div></blockquote><p></p><p>Yep. If you look at the generated code for IL3A-3F you can easily note that dup has no effect. If you remove the dup then your compiler would generate identical code. That's a good indication that something isn't right.</p><p></p><blockquote><div class="quoteText">So when I perform the dup operation, the two top items on the stack now contain &quot;pStr&quot;.</div></blockquote><p></p><p>dup make a copy of the value, it seems to me that your implementation makes a copy of some sort of reference to the value.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/1435251b1e83470e95359f3001671831#1435251b1e83470e95359f3001671831</link>
		<pubDate>Sat, 30 Jul 2011 21:47:25 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/1435251b1e83470e95359f3001671831#1435251b1e83470e95359f3001671831</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">A better but more difficult solution: Any structs that have ref type fields also get the same hard-coded functions to manage their pointers just like classes get</div></blockquote><p></p><p>That sounds good. And I might have a slightly different idea, treat the stack frame as objects:</p><ul><li>for each method create a struct that contains all the method parameters and locals</li><li>when you call a method allocate such a struct on the stack (by using alloca or other means)</li><li>fill the parameters </li><li>call the method with a pointer to the allocated struct </li><li>you'll also need to store pointers to these stack frames on that virtual stack of yours or simply chain the stack frames into a linked list (pointed to by the thread)</li></ul><p>Pros:</p><ul><li>&quot;everything&quot; is an object, you don't need separate cases for &quot;real&quot; objects and stack frames</li><li>this works transparently with value types that contain references </li><li>this might avoid the register problem completly. if a method makes a &quot;alloc&quot; call the compiler won't be able to prove that the stack frame object hasn't been modified and it will have to reload any registers that have been previously read from stack frame</li></ul><p>Cons:</p><ul><li>This will very likely disable some compiler optimizations. Usually some of the parameters are passed through registers, this can't happen anymore since the only real parameter will be the pointer to stack frame. Similarly for local variables.</li></ul><p></p><blockquote><div class="quoteText">That is certainly not &quot;illegal&quot; code and the compiler can't guard against it. Now if the original ref to MyClass goes out of scope, then it is no longer reachable and it will be GC'd</div></blockquote><p></p><p>MyClass can't go out of scope, if one of its methods is running then there must be a stack frame for it and that stack frame must contain the &quot;this&quot; pointer for MyClass.</p><p></p><blockquote><div class="quoteText">On a microcontroller it might be more important for the GC to not lock everything up for long periods of times instead of absolute performance.</div></blockquote><p></p><p>Sounds like you want generational GC too. Even more complicated <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-2.gif?v=c9' alt='Big Smile' />.</p><p></p><blockquote><div class="quoteText">What I'm suggesting is improving on this process by stripping the fields out of types that were never referenced during all of the code paths.</div></blockquote><p></p><p>Sure, you could do that. Not sure if it helps with the List&lt;T&gt; case. I looked at the code and it's indeed possible that CultureInfo gets dragged in by an exception (see the Capacity prop). But it's unlikely that you can eliminate that by doing some static code analysis.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/5f140ca456de41f79d6b9f2c015db285#5f140ca456de41f79d6b9f2c015db285</link>
		<pubDate>Tue, 26 Jul 2011 21:13:12 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/5f140ca456de41f79d6b9f2c015db285#5f140ca456de41f79d6b9f2c015db285</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p>OK, I understand now. I don't know of a way to remove this overhead, std::function is horrible complex and the compiler has little chance to optimize it.</p><p>It seems to me that if you don't provide the function type in the template argument list (like in the original code or in your code) then the only alternative is to use std::function or something that does something similar: wraps a callable object. And this wrapping is complex because there are different types of callable objects: pointer to function, pointer to member, functors.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/481da36fd4f443e29dcf9f2c0130d26c#481da36fd4f443e29dcf9f2c0130d26c</link>
		<pubDate>Tue, 26 Jul 2011 18:29:49 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/481da36fd4f443e29dcf9f2c0130d26c#481da36fd4f443e29dcf9f2c0130d26c</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - IL to C++ compiler - Need advice on implementing context switching</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">and each of a function's ref type variables and parameters are really a pointer to a<em>location</em> on this stack like this:</div></blockquote><p></p><p>One thing&nbsp;I don't understand is what are you going to do about value types that contain pointers and are stored on the stack. Did I miss something?</p><p></p><blockquote><div class="quoteText">that might not work because classes that are not reachable will never be called. If those classes are not rooted but they are still performing some sort of operations (maybe in some long running loop or a timer callback), their class variables will not be updated.</div></blockquote><p></p><p>That should never happen, if an object is not reachable then it's garbage and it will be collected. If you pass a GC pointer to native code then you know what you need to do: use &quot;fixed&quot; or GCHandle to prevent the GC from&nbsp;moving/collecting the object.</p><p></p><blockquote><div class="quoteText">So probably I will need to call UpdatePointer on each class while walking the object heap.</div></blockquote><p></p><p>Watch out that moving objects and updating pointers can be extremly tricky. I'm not talking about finding the roots which can be itself a problem, I'm talking about how can you update pointers in an efficient way. One thing is clear: you can't move one object and then call UpdatePointers on all the other objects in the heap, that has quadratic complexity.</p><p>Since evildictait​or mentioned it: have you considered using Boehm GC instead?<a href="http://en.wikipedia.org/wiki/Boehm_garbage_collector">http://en.wikipedia.org/wiki/Boehm_garbage_collector</a></p><p></p><blockquote><div class="quoteText">And my C&#43;&#43; code suddenly got hugely bloated with all sorts of seemingly unrelated classes like the CultureInfo</div></blockquote><p></p><p>Hmm, not sure what CultureInfo has to do with List&lt;&gt;. Maybe the Sort method uses it somehow. Since you wrote that code you should be able to modify it to log some information about why a particular type has been included, right? <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-2.gif?v=c9' alt='Big Smile' /> If I understand correctly now you're including all the methods of an used type, you may consider excluding methods that are never called.</p><p></p><blockquote><div class="quoteText">so I think it should be possible to make a VS debugging plugin. Has anyone had experience with this, and how difficult would such a project be?</div></blockquote><p></p><p>Absolutely no idea but if you can make all the rest work (compiler, GC etc.) then you should be able to write such a plugin too <img src='http://ecn.channel9.msdn.com/o9/content/images/emoticons/emotion-1.gif?v=c9' alt='Smiley' /></p><p>&nbsp;</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/ad6475ae11294d19a2ea9f2c011532f2#ad6475ae11294d19a2ea9f2c011532f2</link>
		<pubDate>Tue, 26 Jul 2011 16:49:15 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/IL-to-C-compiler-Need-advice-on-implementing-a-GC/ad6475ae11294d19a2ea9f2c011532f2#ad6475ae11294d19a2ea9f2c011532f2</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>77</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - C++0x (vs2010) question, please tell me what i&#39;m doing wrong</title>
		<description><![CDATA[<p></p><blockquote><div class="quoteText">Best would be if you could make the optimizer inline , etc..</div></blockquote><p></p><p>I don't understand... that's exactly what the compiler did. The code in main is the code from normal_function, 3 calls to the stream insertion operator and nothing else.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/d701fa097b8c4289b5519f2c0059162a#d701fa097b8c4289b5519f2c0059162a</link>
		<pubDate>Tue, 26 Jul 2011 05:24:21 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/C0x-vs2010-question-please-tell-me-what-im-doing-wrong/d701fa097b8c4289b5519f2c0059162a#d701fa097b8c4289b5519f2c0059162a</guid>
		<dc:creator>Dexter</dc:creator>
		<slash:comments>34</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/Dexter/Discussions/RSS</wfw:commentRss>
	</item>
</channel>
</rss>