<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" 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/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>A Blog.</title>
	
	<link>http://w01fe.com</link>
	<description />
	<lastBuildDate>Thu, 03 Nov 2011 22:40:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/w01fe" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="w01fe" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Fixing paredit in the clojure REPL</title>
		<link>http://w01fe.com/blog/2011/11/fixing-paredit-in-the-clojure-repl/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://w01fe.com/blog/2011/11/fixing-paredit-in-the-clojure-repl/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 22:40:39 +0000</pubDate>
		<dc:creator>Jason Wolfe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://w01fe.com/?p=220</guid>
		<description><![CDATA[By default, paredit in the Clojure slime-repl doesn&#8217;t properly handle literal vectors and maps. This little snippet of elisp seems to fix it. (defun fix-paredit-repl () (interactive) (local-set-key &#34;{&#34; 'paredit-open-curly) (local-set-key &#34;}&#34; 'paredit-close-curly) (modify-syntax-entry ?\{ &#34;(}&#34;) (modify-syntax-entry ?\} &#34;){&#34;) (modify-syntax-entry ?\[ &#34;(]&#34;) (modify-syntax-entry ?\] &#34;)[&#34;))]]></description>
			<content:encoded><![CDATA[<p>By default, paredit in the Clojure slime-repl doesn&#8217;t properly handle literal vectors and maps.  This little snippet of elisp seems to fix it.</p>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">(defun fix-paredit-repl ()
  (interactive)
  (local-set-key &quot;{&quot; 'paredit-open-curly)
  (local-set-key &quot;}&quot; 'paredit-close-curly)
  (modify-syntax-entry ?\{ &quot;(}&quot;) 
  (modify-syntax-entry ?\} &quot;){&quot;)
  (modify-syntax-entry ?\[ &quot;(]&quot;)
  (modify-syntax-entry ?\] &quot;)[&quot;))</pre></div></div>

<img src="http://feeds.feedburner.com/~r/w01fe/~4/cKAb99ZNbYk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://w01fe.com/blog/2011/11/fixing-paredit-in-the-clojure-repl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Treat LaTeX command definitions as Textmate symbols</title>
		<link>http://w01fe.com/blog/2011/06/treat-latex-command-definitions-as-textmate-symbols/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://w01fe.com/blog/2011/06/treat-latex-command-definitions-as-textmate-symbols/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 20:58:19 +0000</pubDate>
		<dc:creator>Jason Wolfe</dc:creator>
				<category><![CDATA[LaTeX]]></category>

		<guid isPermaLink="false">http://w01fe.com/?p=214</guid>
		<description><![CDATA[I wanted LaTeX newcommand/renewcommand definitions to show up in the Textmate symbol list, so I could use the Cmd-Shift-T shortcut to jump to them. To accomplish this, I first went to the &#8220;LaTeX Language&#8221; entry in the bundle editor, and added this block to the &#8220;patterns&#8221; section: { name = 'latex.definition'; match = '(\\)(newcommand&#124;renewcommand)\{[^\}]+\}'; } [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted LaTeX newcommand/renewcommand definitions to show up in the Textmate symbol list, so I could use the Cmd-Shift-T shortcut to jump to them.  To accomplish this, I first went to the &#8220;LaTeX Language&#8221; entry in the bundle editor, and added this block to the &#8220;patterns&#8221; section:</p>
<pre>
{	name = 'latex.definition';
	match = '(\\)(newcommand|renewcommand)\{[^\}]+\}';
}
</pre>
<p>Then, I created a new LaTeX Preference with scope selector &#8220;latex.definition&#8221;, and contents:</p>
<pre>
{	showInSymbolList = 1;
	symbolTransformation = '
		s/^\\(newcommand|renewcommand)\{\\(.+)\}/$2/;';
}
</pre>
<img src="http://feeds.feedburner.com/~r/w01fe/~4/mLO7nxsUWdI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://w01fe.com/blog/2011/06/treat-latex-command-definitions-as-textmate-symbols/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Embedding fonts into an existing PDF file (via an ugly hack)</title>
		<link>http://w01fe.com/blog/2011/04/embedding-fonts-into-an-existing-pdf-file-via-an-ugly-hack/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://w01fe.com/blog/2011/04/embedding-fonts-into-an-existing-pdf-file-via-an-ugly-hack/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 23:00:18 +0000</pubDate>
		<dc:creator>Jason Wolfe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://w01fe.com/blog/2011/04/embedding-fonts-into-an-existing-pdf-file-via-an-ugly-hack/</guid>
		<description><![CDATA[Many academic conferences require that you submit PDF files with all fonts properly embedded. Making sure this happens for your LaTeX document using pdflatex is not difficult. However, some programs used to create figures don&#8217;t have an option to embed fonts, and when these figures are included in your LaTeX document, your final PDF ends [...]]]></description>
			<content:encoded><![CDATA[<p>Many academic conferences require that you submit PDF files with all fonts properly embedded.  Making sure this happens for your LaTeX document using pdflatex is not difficult. However, some programs used to create figures don&#8217;t have an option to embed fonts, and when these figures are included in your LaTeX document, your final PDF ends up short a few fonts. </p>
<p>I&#8217;m sure there&#8217;s a more elegant way to solve this, but when I last faced it a few years ago, I hacked together an ugly shell script that converts the PDF to PS to EPS back to PDF again.  I don&#8217;t recall why all these steps were necessary, but at the time this was the quickest thing I could find that worked:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GS_OPTIONS</span>=<span style="color: #ff0000;">'-dEmbedAllFonts=true -dPDFSETTINGS=/printer'</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #007800;">$1</span> <span style="color: #007800;">$1</span>.old
pdftops <span style="color: #007800;">$1</span> tmp.ps
ps2eps tmp.ps 
epstopdf tmp.eps 
<span style="color: #c20cb9; font-weight: bold;">mv</span> tmp.pdf <span style="color: #007800;">$1</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> tmp.ps tmp.eps</pre></div></div>

<p>As you can see here, it successfully embeds the fonts in a pdf file, without increasing the file size too much.  And each of the conversion steps should be lossless, so the final pdf looks exactly the same as the input:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">jawolfe<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>~<span style="color: #000000; font-weight: bold;">/</span>Projects<span style="color: #000000; font-weight: bold;">/</span>reports<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">11</span>-ijcai<span style="color: #000000; font-weight: bold;">/</span>graphs<span style="color: #7a0874; font-weight: bold;">&#93;</span>: pdffonts independent.pdf 
name                                 <span style="color: #7a0874; font-weight: bold;">type</span>              emb sub uni object ID
<span style="color: #660033;">------------------------------------</span> <span style="color: #660033;">-----------------</span> <span style="color: #660033;">---</span> <span style="color: #660033;">---</span> <span style="color: #660033;">---</span> <span style="color: #660033;">---------</span>
Helvetica                            Type <span style="color: #000000;">1</span>            no  no  no       <span style="color: #000000;">7</span>  <span style="color: #000000;">0</span>
Symbol                               Type <span style="color: #000000;">1</span>            no  no  no       <span style="color: #000000;">8</span>  <span style="color: #000000;">0</span>
jawolfe<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>~<span style="color: #000000; font-weight: bold;">/</span>Projects<span style="color: #000000; font-weight: bold;">/</span>reports<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">11</span>-ijcai<span style="color: #000000; font-weight: bold;">/</span>graphs<span style="color: #7a0874; font-weight: bold;">&#93;</span>: .<span style="color: #000000; font-weight: bold;">/</span>fixfonts independent.pdf 
... <span style="color: #7a0874; font-weight: bold;">&#40;</span>ignore the error messages<span style="color: #7a0874; font-weight: bold;">&#41;</span>
jawolfe<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>~<span style="color: #000000; font-weight: bold;">/</span>Projects<span style="color: #000000; font-weight: bold;">/</span>reports<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">11</span>-ijcai<span style="color: #000000; font-weight: bold;">/</span>graphs<span style="color: #7a0874; font-weight: bold;">&#93;</span>: <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span>
total <span style="color: #000000;">72</span>
-rwxr--r--<span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1</span> jawolfe  admin   <span style="color: #000000;">173</span> Apr <span style="color: #000000;">12</span> <span style="color: #000000;">15</span>:<span style="color: #000000;">45</span> fixfonts<span style="color: #000000; font-weight: bold;">*</span>
<span style="color: #660033;">-rw-r--r--</span>  <span style="color: #000000;">1</span> jawolfe  admin  <span style="color: #000000;">9642</span> Apr <span style="color: #000000;">12</span> <span style="color: #000000;">15</span>:<span style="color: #000000;">45</span> independent.pdf
<span style="color: #660033;">-rw-r--r--</span>  <span style="color: #000000;">1</span> jawolfe  admin  <span style="color: #000000;">4102</span> Apr <span style="color: #000000;">12</span> <span style="color: #000000;">15</span>:<span style="color: #000000;">45</span> independent.pdf.old
jawolfe<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>~<span style="color: #000000; font-weight: bold;">/</span>Projects<span style="color: #000000; font-weight: bold;">/</span>reports<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">11</span>-ijcai<span style="color: #000000; font-weight: bold;">/</span>graphs<span style="color: #7a0874; font-weight: bold;">&#93;</span>: pdffonts independent.pdf 
name                                 <span style="color: #7a0874; font-weight: bold;">type</span>              emb sub uni object ID
<span style="color: #660033;">------------------------------------</span> <span style="color: #660033;">-----------------</span> <span style="color: #660033;">---</span> <span style="color: #660033;">---</span> <span style="color: #660033;">---</span> <span style="color: #660033;">---------</span>
QHKFIS+Helvetica                     Type 1C           <span style="color: #c20cb9; font-weight: bold;">yes</span> <span style="color: #c20cb9; font-weight: bold;">yes</span> no       <span style="color: #000000;">8</span>  <span style="color: #000000;">0</span>
ONFWFL+Symbol                        Type 1C           <span style="color: #c20cb9; font-weight: bold;">yes</span> <span style="color: #c20cb9; font-weight: bold;">yes</span> no      <span style="color: #000000;">10</span>  <span style="color: #000000;">0</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/w01fe/~4/Z1AWQnt08Ps" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://w01fe.com/blog/2011/04/embedding-fonts-into-an-existing-pdf-file-via-an-ugly-hack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple multiplayer, HTML game timer</title>
		<link>http://w01fe.com/blog/2010/12/simple-multiplayer-html-game-timer/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://w01fe.com/blog/2010/12/simple-multiplayer-html-game-timer/#comments</comments>
		<pubDate>Sun, 05 Dec 2010 03:57:47 +0000</pubDate>
		<dc:creator>Jason Wolfe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://w01fe.com/blog/2010/12/simple-multiplayer-html-game-timer/</guid>
		<description><![CDATA[I made a simple game timer to learn jQuery, and keep our house&#8217;s Settlers of Catan games moving. It runs great in a mobile browser, allows arbitrary numbers of players, and has a couple configurable timing settings. Both the source and a demo are online.]]></description>
			<content:encoded><![CDATA[<p>I made a simple game timer to learn jQuery, and keep our house&#8217;s Settlers of Catan games moving.  It runs great in a mobile browser, allows arbitrary numbers of players, and has a couple configurable timing settings.  Both the <a href="https://github.com/jawolfe/GameTimer" onclick="pageTracker._trackPageview('/outgoing/github.com/jawolfe/GameTimer?referer=');">source</a> and a <a href="http://w01fe.com/gametimer/timer.html">demo</a> are online.</p>
<img src="http://feeds.feedburner.com/~r/w01fe/~4/Pl9mBfqAPa4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://w01fe.com/blog/2010/12/simple-multiplayer-html-game-timer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Arduino library for controlling HL1606-based RGB LED strips</title>
		<link>http://w01fe.com/blog/2010/12/new-arduino-library-for-controlling-hl1606-based-rgb-led-strips/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://w01fe.com/blog/2010/12/new-arduino-library-for-controlling-hl1606-based-rgb-led-strips/#comments</comments>
		<pubDate>Sun, 05 Dec 2010 03:39:04 +0000</pubDate>
		<dc:creator>Jason Wolfe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://w01fe.com/blog/2010/12/new-arduino-library-for-controlling-hl1606-based-rgb-led-strips/</guid>
		<description><![CDATA[I&#8217;ve posted a new library at Github for controlling HL1606-based RGB LED strips, along with some details on where they can be purchased and how they can be set up. I&#8217;ll post examples later, once I get some cool patterns up and running.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve posted a new library at <a href="https://github.com/jawolfe/HL1606" onclick="pageTracker._trackPageview('/outgoing/github.com/jawolfe/HL1606?referer=');">Github</a> for controlling HL1606-based RGB LED strips, along with some details on where they can be purchased and how they can be set up.  I&#8217;ll post examples later, once I get some cool patterns up and running.</p>
<img src="http://feeds.feedburner.com/~r/w01fe/~4/fro8I97YS_A" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://w01fe.com/blog/2010/12/new-arduino-library-for-controlling-hl1606-based-rgb-led-strips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reversing the order of Clojure’s exception chains in SLIME</title>
		<link>http://w01fe.com/blog/2009/10/reversing-the-order-of-clojures-exception-chains-in-slime/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://w01fe.com/blog/2009/10/reversing-the-order-of-clojures-exception-chains-in-slime/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 04:31:08 +0000</pubDate>
		<dc:creator>Jason Wolfe</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://w01fe.com/blog/2009/10/reversing-the-order-of-clojures-exception-chains-in-slime/</guid>
		<description><![CDATA[When debugging a complex Clojure project with SLIME, I would often find myself chasing chains of exceptions as much as 20 levels deep to get to the true cause of a problem. I finally tired of the &#8220;11111111111&#8243;-look-&#8221;0000000000&#8243; exercise, especially since in my experience the most useful stack traces are almost always the innermost ones. [...]]]></description>
			<content:encoded><![CDATA[<p>When debugging a complex Clojure project with SLIME, I would often find myself chasing chains of exceptions as much as 20 levels deep to get to the true cause of a problem.  I finally tired of the &#8220;11111111111&#8243;-look-&#8221;0000000000&#8243; exercise, especially since in my experience the most useful stack traces are almost always the innermost ones.</p>
<p>Thus, I decided to modify swank-clojure to present exceptions in the opposite order: rather than starting with the caught exception and providing the option to examine its &#8220;Cause&#8221;, start with the innermost cause and allow examining its &#8220;Source&#8221;.  In addition to providing the must useful stack traces up front, this also means that the first thing you see in the SLDB window is the real exception, not a mess of RuntimeExceptions.   </p>
<p>For the interested, I posted the change on <a href="http://github.com/jawolfe/swank-clojure/commit/a96f25148eded7a7749bd44d758598a31293477a" onclick="pageTracker._trackPageview('/outgoing/github.com/jawolfe/swank-clojure/commit/a96f25148eded7a7749bd44d758598a31293477a?referer=');">GitHub</a>; it&#8217;s my first real change to anything SLIME, so take care if you decide to use it.  </p>
<img src="http://feeds.feedburner.com/~r/w01fe/~4/1mj2OXO0OZ8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://w01fe.com/blog/2009/10/reversing-the-order-of-clojures-exception-chains-in-slime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic type hints in Clojure macros</title>
		<link>http://w01fe.com/blog/2009/07/dynamic-type-hints-in-clojure-macros/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://w01fe.com/blog/2009/07/dynamic-type-hints-in-clojure-macros/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 06:35:10 +0000</pubDate>
		<dc:creator>Jason Wolfe</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://w01fe.com/blog/2009/07/dynamic-type-hints-in-clojure-macros/</guid>
		<description><![CDATA[Sometimes, one wants to write a Clojure macro to assist in Java interoperability, where the object types are determined by the arguments to the macro. In this situation, the normal strategy of using a #^class type hint doesn&#8217;t work, since the class isn&#8217;t known at read time. Here&#8217;s a very contrived example, where the macro [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, one wants to write a Clojure macro to assist in Java interoperability, where the object types are determined by the arguments to the macro.  In this situation, the normal strategy of using a #^class type hint doesn&#8217;t work, since the class isn&#8217;t known at read time.  Here&#8217;s a very contrived example, where the macro declare-first-char defines a function first-char, which calls the (.charAt % 0) method on its argument.   The class for the type hint is passed as an argument to the macro.</p>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">user&gt; (set! *warn-on-reflection* true)
true
&nbsp;
user&gt; (defmacro declare-first-char [cls]
	(let [x (gensym)]
	  `(defn first-char [~(with-meta x {:tag cls})] 
             (.charAt ~x 0))))
#'user/declare-first-char
&nbsp;
user&gt; (declare-first-char String)
#'user/first-char ; No reflection warning!
&nbsp;
user&gt; (first-char &quot;test&quot;)
\t</pre></div></div>

<p>The key here is the [~(with-meta x {:tag cls})] fragment, which replaces the normal [#^String x#] version that could be used if the type was known in advance.</p>
<img src="http://feeds.feedburner.com/~r/w01fe/~4/V4fUZwpUN8g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://w01fe.com/blog/2009/07/dynamic-type-hints-in-clojure-macros/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C++ callbacks into Java via JNI made easy(ier)</title>
		<link>http://w01fe.com/blog/2009/05/c-callbacks-into-java-via-jni-made-easyier/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://w01fe.com/blog/2009/05/c-callbacks-into-java-via-jni-made-easyier/#comments</comments>
		<pubDate>Fri, 29 May 2009 07:06:35 +0000</pubDate>
		<dc:creator>Jason Wolfe</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[callbacks]]></category>
		<category><![CDATA[DetachCurrentThread]]></category>
		<category><![CDATA[JNI]]></category>

		<guid isPermaLink="false">http://w01fe.com/blog/2009/05/c-callbacks-into-java-via-jni-made-easyier/</guid>
		<description><![CDATA[I just finished writing a Java wrapper for a complex C++ library, and had a fair bit of trouble finding solutions to (or even basic documentation pertaining to) some of the problems that came up. Things seem pretty easy if you just need to call some C++ functions from Java: you use JNI and do [...]]]></description>
			<content:encoded><![CDATA[<p>I just finished writing a Java wrapper for a complex C++ library, and had a fair bit of trouble finding solutions to (or even basic documentation pertaining to) some of the problems that came up.  </p>
<p>Things seem pretty easy if you just need to call some C++ functions from Java: you use JNI and do a little gruntwork to translate data to and from Java objects, or let SWIG do the heavy lifting for you.  However, in my case things were harder, because the library is multithreaded (using boost threads) and makes heavy use of callbacks into the client (i.e., Java). </p>
<p>The main complication is that each native thread that calls a Java method must do so through its own JNIEnv pointer, obtained by calling AttachCurrentThread on a JavaVM instance.  Each such native thread (which may be created and managed by code you can&#8217;t change) must eventually be detached from the JVM by calling DetachCurrentThread within it, or it seems memory leaks and JVM shutdown issues can result.  There is no obvious way to detach the thread from the outside, so if you let the thread die or get away before it calls DetachCurrentThread you&#8217;re stuck.  </p>
<p>Since a C++ wrapper for the Java callbacks is needed anyway, one easy solution is to attach before each callback and detach afterwards.  However, this may be very expensive; I haven&#8217;t measured the cost, but one post I found quoted a factor of 6 slowdown.  My alternative solution uses boost::thread_specific_ptr to detach each native thread just before it dies:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;boost/thread/tss.hpp&gt;</span>
&nbsp;
<span style="color: #0000ff;">static</span> JavaVM <span style="color: #000040;">*</span>vm <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> ThreadJNIEnv <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">bool</span> _detach<span style="color: #008080;">;</span>
    JNIEnv <span style="color: #000040;">*</span>env<span style="color: #008080;">;</span>
    ThreadJNIEnv<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Attaching &quot;</span> <span style="color: #000080;">&lt;&lt;</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">this_thread</span><span style="color: #008080;">::</span><span style="color: #007788;">get_id</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        vm<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>AttachCurrentThread<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span> <span style="color: #000040;">**</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>env, <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        ASSERT<span style="color: #008000;">&#40;</span>env <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        _detach <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    ThreadJNIEnv<span style="color: #008000;">&#40;</span>JNIEnv <span style="color: #000040;">*</span>e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        env <span style="color: #000080;">=</span> e<span style="color: #008080;">;</span>
        _detach <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    ~ThreadJNIEnv<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>_detach<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Detaching &quot;</span> <span style="color: #000080;">&lt;&lt;</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">this_thread</span><span style="color: #008080;">::</span><span style="color: #007788;">get_id</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            vm<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>DetachCurrentThread<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">static</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">thread_specific_ptr</span><span style="color: #000080;">&lt;</span>ThreadJNIEnv<span style="color: #000080;">&gt;</span> envs<span style="color: #008080;">;</span>
&nbsp;
JNIEnv <span style="color: #000040;">*</span>getJNIEnv<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    ThreadJNIEnv <span style="color: #000040;">*</span>tenv <span style="color: #000080;">=</span> envs.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>tenv <span style="color: #000080;">==</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        tenv <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> ThreadJNIEnv<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        envs.<span style="color: #007788;">reset</span><span style="color: #008000;">&#40;</span>tenv<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> tenv<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>env<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> init<span style="color: #008000;">&#40;</span>JNIEnv <span style="color: #000040;">*</span>env<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>env<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetJavaVM<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>vm<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
   envs.<span style="color: #007788;">reset</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> ThreadJNIEnv<span style="color: #008000;">&#40;</span>env<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>If your main function is in Java, you initialize the library by calling init(env) with the current thread&#8217;s JNIEnv (with multiple Java threads, you may have to take care).    If it&#8217;s C++, you just set &#8220;vm&#8221; directly when you create the JVM instance.</p>
<p>Then, any function not in a direct JNI call just uses getJNIEnv() to get a JNIEnv pointer that&#8217;s valid for the current thread.  This will work correctly both for indirect calls from Java, and from native threads, which will be automatically attached on the first call to getJNIEnv and automatically detached before death.  </p>
<p>Seemed to work for me on Ubuntu; YMMV.</p>
<img src="http://feeds.feedburner.com/~r/w01fe/~4/Xz3oV2EKMLI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://w01fe.com/blog/2009/05/c-callbacks-into-java-via-jni-made-easyier/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Quick tip for debugging &amp; profiling Clojure code with multimethods</title>
		<link>http://w01fe.com/blog/2009/04/quick-tip-for-debugging-profiling-clojure-code-with-multimethods/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://w01fe.com/blog/2009/04/quick-tip-for-debugging-profiling-clojure-code-with-multimethods/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 23:47:40 +0000</pubDate>
		<dc:creator>Jason Wolfe</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://w01fe.com/blog/2009/04/quick-tip-for-debugging-profiling-clojure-code-with-multimethods/</guid>
		<description><![CDATA[Debugging and profiling Clojure code with multimethods can be a pain, since methods show up as anonymous functions in stack traces and profile results. When you&#8217;re debugging you at least get a line number, but if you&#8217;ve loaded your code in a nonstandard way (i.e., with SLIME) or you&#8217;re using a profiler like YourKit, you [...]]]></description>
			<content:encoded><![CDATA[<p>Debugging and profiling Clojure code with multimethods can be a pain, since methods show up as anonymous functions in stack traces and profile results.  When you&#8217;re debugging you at least get a line number, but if you&#8217;ve loaded your code in a nonstandard way (i.e., with SLIME) or you&#8217;re using a profiler like YourKit, you don&#8217;t get this information.  Then, the only clue you have to figure out what my.long.namespace$fn_12312 represents is context &#8212; who called it, and who it calls.</p>
<p>Similar issues exist for anonymous functions, which some time ago I realized you could give names by using (fn name [args] &#038; body).  What I didn&#8217;t realize is the same is true of multimethods.  By simply using (defmethod multifn dispatch-val *name* [args] &#038;body), you can give each instance of a multimethod a unique name, which will show up in stack traces and profile results.   </p>
<img src="http://feeds.feedburner.com/~r/w01fe/~4/qmd-o3-NKWQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://w01fe.com/blog/2009/04/quick-tip-for-debugging-profiling-clojure-code-with-multimethods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Time/Memory Limiting/Instrumenting Clojure code</title>
		<link>http://w01fe.com/blog/2009/02/timememory-limitinginstrumenting-clojure-code/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://w01fe.com/blog/2009/02/timememory-limitinginstrumenting-clojure-code/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 02:36:55 +0000</pubDate>
		<dc:creator>Jason Wolfe</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://w01fe.com/blog/2009/02/timememory-limitinginstrumenting-clojure-code/</guid>
		<description><![CDATA[I wrote some simple utilities for measuring time and memory usage of Clojure code, and running Clojure code within preset time and memory limits. The methods used for handling memory are a bit hacky, but they seem to work OK for my current purposes. Example snippets and code are posted over at the Google group [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote some simple utilities for measuring time and memory usage of Clojure code, and running Clojure code within preset time and memory limits.  The methods used for handling memory are a bit hacky, but they seem to work OK for my current purposes.  Example snippets and code are posted over at the <a href="http://groups.google.com/group/clojure/browse_thread/thread/231cc06b4b13744c?hl=en#" onclick="pageTracker._trackPageview('/outgoing/groups.google.com/group/clojure/browse_thread/thread/231cc06b4b13744c?hl=en&amp;referer=');">Google group</a> for now, and if there&#8217;s enough interest, maybe this will eventually find its way into clojure.contrib.</p>
<img src="http://feeds.feedburner.com/~r/w01fe/~4/ZM3HPJkUaoo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://w01fe.com/blog/2009/02/timememory-limitinginstrumenting-clojure-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

