<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: JNI wrapper compilation</title>
	<atom:link href="http://gbenson.net/?feed=rss2&#038;p=171" rel="self" type="application/rss+xml" />
	<link>http://gbenson.net/?p=171</link>
	<description></description>
	<lastBuildDate>Fri, 11 Jun 2010 13:17:58 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Jeffrey Yasskin</title>
		<link>http://gbenson.net/?p=171&#038;cpage=1#comment-79608</link>
		<dc:creator>Jeffrey Yasskin</dc:creator>
		<pubDate>Mon, 02 Nov 2009 16:49:55 +0000</pubDate>
		<guid isPermaLink="false">http://gbenson.net/?p=171#comment-79608</guid>
		<description>Oops, I failed at editing above. &quot;threads_wanting_to_JIT&quot; should be an &quot;atomic&lt;int&gt;&quot;; &quot;compiler_thread_running&quot; should be an &quot;atomic&lt;bool&gt;&quot;, and the second bullet should end at the first period.</description>
		<content:encoded><![CDATA[<p>Oops, I failed at editing above. &#8220;threads_wanting_to_JIT&#8221; should be an &#8220;atomic&lt;int&gt;&#8221;; &#8220;compiler_thread_running&#8221; should be an &#8220;atomic&lt;bool&gt;&#8221;, and the second bullet should end at the first period.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeffrey Yasskin</title>
		<link>http://gbenson.net/?p=171&#038;cpage=1#comment-79589</link>
		<dc:creator>Jeffrey Yasskin</dc:creator>
		<pubDate>Mon, 02 Nov 2009 06:11:03 +0000</pubDate>
		<guid isPermaLink="false">http://gbenson.net/?p=171#comment-79589</guid>
		<description>I don&#039;t know anything about HotSpot or Shark, so sorry if this is naive.

Can _thread_in_native threads call into Java code? If so, I think you can implement your own lock that&#039;ll do this safely:
&lt;ul&gt;
&lt;li&gt;Create three new variables. Using C++0x syntax:
  atomic threads_wanting_to_JIT(0);
  atomic compiler_thread_running(true);
  Object llvm_lock&lt;/li&gt;

&lt;li&gt;The JIT is locked whenever compiler_thread_running=true OR llvm_lock is held. , and by Create a global atomic variable, threads_wanting_to_JIT, initialized to 0. While this is 0, the standard compiler thread gets to run.&lt;/li&gt;

&lt;li&gt;When a thread discovers that it needs to JIT a JNI method, it runs:
&lt;pre&gt;  threads_wanting_to_JIT.fetch_add(1);
  synchronized(llvm_lock) {
    while (compiler_thread_running.load()) { llvm_lock.wait(); }
    // Now we know that the compiler thread isn&#039;t calling into the JIT (see below),
    // and at most one non-compiler thread can be here because of the llvm_lock.
    CompileStuff();
    threads_wanting_to_JIT.fetch_sub(1);
    llvm_lock.notifyAll();
  }&lt;/pre&gt;&lt;/list&gt;

&lt;li&gt;Between JIT operations, the compiler thread runs:
&lt;pre&gt;  if (threads_wanting_to_JIT.load() &gt; 0) {
    enter_java {
      synchronized(llvm_lock) {
        compiler_thread_running.store(false);
        try {
          llvm_lock.notifyAll();
          while (threads_wanting_to_JIT.load() &gt; 0) { llvm_lock.wait(); }
        } finally {
          compiler_thread_running.store(true);  
        }
      }
    }
  }&lt;/pre&gt;&lt;/li&gt;&lt;/ul&gt;

I&#039;m assuming all sequentially-consistent atomics, or Java volatiles or AtomicIntegers. You may be able to relax the memory ordering some, but it&#039;s probably not worth the risk. Of course, go over the above with a fine-toothed comb before actually using it; it&#039;s easy to make mistakes here.


Notwithstanding the above, using separate LLVMContexts will probably be more efficient overall, unless the JNI interfaces need access to anything from the main compilation Context. With separate LLVMContexts, you won&#039;t have to wait for the main compiler to finish before generating JNI interfaces.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t know anything about HotSpot or Shark, so sorry if this is naive.</p>
<p>Can _thread_in_native threads call into Java code? If so, I think you can implement your own lock that&#8217;ll do this safely:</p>
<ul>
<li>Create three new variables. Using C++0x syntax:<br />
  atomic threads_wanting_to_JIT(0);<br />
  atomic compiler_thread_running(true);<br />
  Object llvm_lock</li>
<li>The JIT is locked whenever compiler_thread_running=true OR llvm_lock is held. , and by Create a global atomic variable, threads_wanting_to_JIT, initialized to 0. While this is 0, the standard compiler thread gets to run.</li>
<li>When a thread discovers that it needs to JIT a JNI method, it runs:
<pre>  threads_wanting_to_JIT.fetch_add(1);
  synchronized(llvm_lock) {
    while (compiler_thread_running.load()) { llvm_lock.wait(); }
    // Now we know that the compiler thread isn't calling into the JIT (see below),
    // and at most one non-compiler thread can be here because of the llvm_lock.
    CompileStuff();
    threads_wanting_to_JIT.fetch_sub(1);
    llvm_lock.notifyAll();
  }</pre>
</li>
<li>Between JIT operations, the compiler thread runs:
<pre>  if (threads_wanting_to_JIT.load() &gt; 0) {
    enter_java {
      synchronized(llvm_lock) {
        compiler_thread_running.store(false);
        try {
          llvm_lock.notifyAll();
          while (threads_wanting_to_JIT.load() &gt; 0) { llvm_lock.wait(); }
        } finally {
          compiler_thread_running.store(true);
        }
      }
    }
  }</pre>
</li>
</ul>
<p>I&#8217;m assuming all sequentially-consistent atomics, or Java volatiles or AtomicIntegers. You may be able to relax the memory ordering some, but it&#8217;s probably not worth the risk. Of course, go over the above with a fine-toothed comb before actually using it; it&#8217;s easy to make mistakes here.</p>
<p>Notwithstanding the above, using separate LLVMContexts will probably be more efficient overall, unless the JNI interfaces need access to anything from the main compilation Context. With separate LLVMContexts, you won&#8217;t have to wait for the main compiler to finish before generating JNI interfaces.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gbenson</title>
		<link>http://gbenson.net/?p=171&#038;cpage=1#comment-79302</link>
		<dc:creator>gbenson</dc:creator>
		<pubDate>Wed, 28 Oct 2009 12:21:13 +0000</pubDate>
		<guid isPermaLink="false">http://gbenson.net/?p=171#comment-79302</guid>
		<description>I tried that already ;)
http://mail.openjdk.java.net/pipermail/zero-dev/2009-October/000237.html</description>
		<content:encoded><![CDATA[<p>I tried that already ;)<br />
<a href="http://mail.openjdk.java.net/pipermail/zero-dev/2009-October/000237.html" rel="nofollow">http://mail.openjdk.java.net/pipermail/zero-dev/2009-October/000237.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xerxes Rånby</title>
		<link>http://gbenson.net/?p=171&#038;cpage=1#comment-79301</link>
		<dc:creator>Xerxes Rånby</dc:creator>
		<pubDate>Wed, 28 Oct 2009 12:12:29 +0000</pubDate>
		<guid isPermaLink="false">http://gbenson.net/?p=171#comment-79301</guid>
		<description>Would it be possible to make the HotSpot thread that initialises the compile to put the native (JNI) method on the queue, like normal method compilation, instead of compiling it immediately; as a quick fix before we have full multi-thread compilation support in Shark using LLVM 2.6?</description>
		<content:encoded><![CDATA[<p>Would it be possible to make the HotSpot thread that initialises the compile to put the native (JNI) method on the queue, like normal method compilation, instead of compiling it immediately; as a quick fix before we have full multi-thread compilation support in Shark using LLVM 2.6?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
