<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx</link><description>Refresher Course Before you read this, you might want to take a quick refresher on my original posting on the script engine threading model. That was a somewhat simplified (!) description of the actual script engine contract. Let me just sum up: free</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#120831</link><pubDate>Tue, 27 Apr 2004 05:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:120831</guid><dc:creator>Nicholas Allen</dc:creator><description>I'm trying to plan out how you're going to support the thread model for InterruptScriptThread and Clone.  Let me know if you're looking at a different direction.&lt;br&gt;&lt;br&gt;The basic problem is that InterruptScriptThread and Clone can be called from any thread at any time on an initialized engine.  These unruly calls look like the hardest part of the threading model to provide.&lt;br&gt;&lt;br&gt;For InterruptScriptThread it doesn't seem that bad, maybe.  Calling InterruptScriptThread trips a latch and then returns successfully.  Scripts periodically pause and run a callback to the engine after chunks of work.  One of the things this callback does is check the latch, and if it was tripped the script is killed.  And, stopping a script (interrupt or normal termination) resets the latch.&lt;br&gt;&lt;br&gt;Does InterruptScriptThread guarantee that the script stops before the function returns or just at some hopefully near point in the future?  If it has to stop right away, I guess after tripping the latch, InterruptScriptThread could wait for a signal from the callback that the script has stopped before returning.&lt;br&gt;&lt;br&gt;Clone is a little harder because it has to have the return result right away.  I'm going to guess it uses the second approach where Clone sleeps until the next callback  and then copies the state.  This looks safe because from outside the engine, it's impossible to distinguish that delay from the script already being at the callback and Clone just taking a while.  The assumption here is that there's no practical way to Clone while a script is actually executing.  That is just madness, right, since the script could mutate some of the engine state while you're copying?</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#120841</link><pubDate>Tue, 27 Apr 2004 06:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:120841</guid><dc:creator>Nicholas Allen</dc:creator><description>&amp;gt; boy, do I ever not want to implement single-writer-multi-reader mutexes&lt;br&gt;&lt;br&gt;Is that really so bad?  A basic one is just a semaphore if we cheat and only allow a bounded number of simultaneous readers, right?  And a writer preference read-write lock is just a mutex+semaphore.  Or did you mean a robust, extremely scalable, high performance implementation in which case it probably would suck to do given the win32 sync primitives.&lt;br&gt;&lt;br&gt;I'd be surprised if they didn't turn out to be useful in at least a couple more places.</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#120852</link><pubDate>Tue, 27 Apr 2004 06:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:120852</guid><dc:creator>Eric Lippert</dc:creator><description>Re: InterruptScriptThread&lt;br&gt;&lt;br&gt;You're on the right track.  I'll cover all this stuff in a whole lot more detail when we get there, but basically ISC sets a threadsafe &amp;quot;halt soon&amp;quot; flag, VBScript and JScript check the flag at the beginning of every statement.</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#120855</link><pubDate>Tue, 27 Apr 2004 06:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:120855</guid><dc:creator>Eric Lippert</dc:creator><description>Re: Clone -- you need to be able to clone a running engine.  The trick there will be the fact that once the engine has compiled state, that state is read-only, and hence can be easily shared amongst engines.  I'll have to build a mutex into the code block list similar to the named item list.  </description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#120856</link><pubDate>Tue, 27 Apr 2004 06:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:120856</guid><dc:creator>Eric Lippert</dc:creator><description>Re: SWMR mutexes -- yeah, it's not that hard, but I'm doing this in my spare time, and the straightforward critsec code is easy.</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#120942</link><pubDate>Tue, 27 Apr 2004 10:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:120942</guid><dc:creator>Mike Dimmick</dc:creator><description>I thought that the 'Both' threading model meant that if created from a STA-model thread (I can't remember the correct terminology - it called CoInitialize, OleInitialize or CoInitializeEx with the COINIT_APARTMENTTHREADED flag) the object ends up in the STA, whereas if created from an MTA-model thread (called CoInitializeEx with COINIT_MULTITHREADED) it ends up in the MTA.</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#121073</link><pubDate>Tue, 27 Apr 2004 14:34:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:121073</guid><dc:creator>Eric Lippert</dc:creator><description>Correct -- like I said, I was simplifying my description of the model.  The important point is that both-threaded objects do not automatically aggregate the free threaded marshaler.</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#121192</link><pubDate>Tue, 27 Apr 2004 17:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:121192</guid><dc:creator>Nicholas Allen</dc:creator><description>Does &amp;quot;beginning of every statement&amp;quot; mean once per language statement?  So if I call in JScript:&lt;br&gt;&lt;br&gt;a.sort();&lt;br&gt;&lt;br&gt;and a happens to have 100M elements I have to end task the engine?  How about if I do:&lt;br&gt;&lt;br&gt;a.sort(function f(x,y){return x-y;});&lt;br&gt;&lt;br&gt;Does each invocation of f count as a statement so that interrupt works as expected?</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#121201</link><pubDate>Tue, 27 Apr 2004 17:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:121201</guid><dc:creator>Nicholas Allen</dc:creator><description>I think I may have the wrong impression of what Clone does in a running engine.  Does it:&lt;br&gt;&lt;br&gt;a) Copy the engine state such as code and items but not the executing scripts.  The new engine has no activity initially after Clone.&lt;br&gt;&lt;br&gt;b) Copy the engine state and the executing scripts.  The new engine is off and running at the exact same instruction point as before the Clone.&lt;br&gt;&lt;br&gt;I had in my mind that it was b) which doesn't seem to support the implementation you describe.  Specifically, if I'm doing an eval and want a b) type Clone I need to bring along the compiled eval block so the state isn't really read-only.  If I'm doing an eval and want a a) type Clone, well it's no problem since the eval code is an anonymous block which doesn't need to be copied.  Closures would probably have the same problem.&lt;br&gt;&lt;br&gt;Or is a b) type Clone really possible by being careful enough about when eval can happen?</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#121202</link><pubDate>Tue, 27 Apr 2004 17:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:121202</guid><dc:creator>Eric Lippert</dc:creator><description>By &amp;quot;beginning of every statement&amp;quot; I mean just that.  Before we start a statement, we check to see if the halt flag is set.  In your second example, since &amp;quot;return&amp;quot; is a statement, we'll check once every comparison.  &lt;br&gt;&lt;br&gt;In your first example, you're out of luck.  You call a method that takes half an hour to come back, you don't get to halt the script for half an hour.  &lt;br&gt;&lt;br&gt;Of course, an array would never have 100 million entries in JScript -- that would take up more memory than the entire process space, and forget sorting -- _garbage collection_ would take forever.  &lt;br&gt;&lt;br&gt;If you have a hundred million entries to sort, I recommend using a rather more buff tool, like SQL Server.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#121204</link><pubDate>Tue, 27 Apr 2004 17:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:121204</guid><dc:creator>Eric Lippert</dc:creator><description>Re: what does clone do:&lt;br&gt;&lt;br&gt;Clone does (a).  Clone is motivated by Active Server Pages.  You have an engine compiled and running which, when executed, serves up foo.asp.  A second request for foo.asp comes in on another thread while the first one is still running.  Without clone you have two choices:&lt;br&gt;&lt;br&gt;1) wait for the first one to finish, uninitialize the engine, reinitialize it on the new thread -- thereby serializing access to that page.  &lt;br&gt;&lt;br&gt;2) create a new engine on the new thread and recompile the state, which is potentially expensive.&lt;br&gt;&lt;br&gt;Clone mitigates both these problems.  A cloned engine doesn't have the cost of compiling the same program all over again and it can run at the same time as the clonee on another thread.&lt;br&gt;&lt;br&gt;Read &lt;a target="_new" href="http://blogs.msdn.com/ericlippert/archive/2003/09/18/53046.aspx"&gt;http://blogs.msdn.com/ericlippert/archive/2003/09/18/53046.aspx&lt;/a&gt;  for more details.&lt;br&gt;</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#121231</link><pubDate>Tue, 27 Apr 2004 18:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:121231</guid><dc:creator>Nicholas Allen</dc:creator><description>True, 100M is probably out of reach for 32 bit code.  But even 100K is going to take a very long time to complete.  If I'm browsing the web and some malicious page decides to chew my CPU, I don't get to choose the tool.&lt;br&gt;&lt;br&gt;As for GC taking forever, I'm guessing that's because you've got a stop the world, mark and sweep collector in mind.  Which is an adequate tool for the job but similarly not industrial strength.&lt;br&gt;&lt;br&gt;re: Clone.  Thanks, that removes a lot of the hairy problems I had dreamed up.</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#121236</link><pubDate>Tue, 27 Apr 2004 18:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:121236</guid><dc:creator>Eric Lippert</dc:creator><description>Re: DOS: Correct -- though we have some defences in place against simple denial-of-service attacks, it is possible for a hostile web page to chew up a whole lot of CPU and memory, and make IE look hung.  &lt;br&gt;&lt;br&gt;Re: GC: Correct, JScript uses a stop-the-world, mark-n-sweep collector and runs it every time 256 variants, 4096 array slots or 64KB of strings are allocated, so allocating large chunks of memory becomes an order n-squared operation as n becomes large.  JScript was designed for simple scripts on simple pages, not for implementing databases.&lt;br&gt;&lt;br&gt;See &lt;a target="_new" href="http://blogs.msdn.com/ericlippert/archive/2003/09/17/53038.aspx"&gt;http://blogs.msdn.com/ericlippert/archive/2003/09/17/53038.aspx&lt;/a&gt; for details.&lt;br&gt;&lt;br&gt;I haven't decided what kind of GC to put into SimpleScript yet.</description></item><item><title>re: SimpleScript Part Six: Threading Technicalities</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#121503</link><pubDate>Tue, 27 Apr 2004 22:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:121503</guid><dc:creator>Eric Lippert</dc:creator><description>&amp;gt; Does InterruptScriptThread guarantee that the script stops before the function returns or just at some hopefully near point in the future?&lt;br&gt;&lt;br&gt;InterruptScriptThread guarantees nothing as far as timing goes.  For example, what if IST is called while the engine is executing the LAST statement?  Then we never check the halt flag and never return SCRIPT_E_ABORTED back to the host.  We do however put the engine into &amp;quot;halted mode&amp;quot;.  &lt;br&gt;&lt;br&gt;We'll get into all this in way more detail later, I promise, so hold your horses for now.  There are all kinds of questions that we'll have to consider:  what happens when you attempt to halt an already halted engine? What if you attempt to call ParseScriptText on a halted engine?  How do you reset an engine out of halted mode?  Etc, etc, etc.  </description></item><item><title>Links about COM threading models</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#129762</link><pubDate>Tue, 11 May 2004 17:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129762</guid><dc:creator>The Old New Thing</dc:creator><description>Raymond makes other people discuss stuff so he doesn't have to.</description></item><item><title>Links about COM Interop and threading</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#129805</link><pubDate>Tue, 11 May 2004 18:14:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129805</guid><dc:creator>Hirlpoo</dc:creator><description /></item><item><title>Links about COM Interop and threading</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#129823</link><pubDate>Tue, 11 May 2004 18:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129823</guid><dc:creator>Hirlpoo</dc:creator><description /></item><item><title>New and Notable 55</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#130419</link><pubDate>Wed, 12 May 2004 15:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130419</guid><dc:creator>Sam Gentile's Blog</dc:creator><description /></item><item><title>New and Notable 55</title><link>http://blogs.msdn.com/ericlippert/archive/2004/04/26/120756.aspx#130534</link><pubDate>Wed, 12 May 2004 19:17:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130534</guid><dc:creator>Sam Gentile's Blog</dc:creator><description /></item></channel></rss>