On Tue, 10 Mar 2009, Bill Janssen wrote:

Andi Vajda <va...@apache.org> wrote:

I'm not sure (not near my computer at the moment) but if you are
prepared to call a piece of code, why not call attachCurrentThread()
then ? it's supposed to be a nop if it was called before.

Andi..

Well, it's not supposed to happen, so it indicates a bug in the program
flow, and I'd just as soon raise an exception and catch that bug, not
swallow it.

Good point. getVMEnv() returns the global env object created when you called initVM(). Before that call, it returns None. Once you've called initVM(), it returns that env from any thread.

This makes sense since you need that env object to call attachCurrentThread(), a method on it.

To see if you've already attached this thread, I added a new method on the env object returned by initVM() or getVMEnv(), called isCurrentThreadAttached(). This method should be very fast, it just checks that JCCENv::get_vm_env() returns non NULL.

For example:

   >>> from lucene import *
   >>> getVMEnv()
   >>> initVM(CLASSPATH)
   <jcc.JCCEnv object at 0x293a0>
   >>> from threading import Thread
   >>> def foo(): print getVMEnv().isCurrentThreadAttached()
   ...
   >>> Thread(target=foo).start()
   False
   >>> def bar(): getVMEnv().attachCurrentThread(); print 
getVMEnv().isCurrentThreadAttached()
   ...
   >>> Thread(target=bar).start()
   True

This got checked into rev 752295.

Andi..

Reply via email to