LWJGL
February 07, 2012, 23:54:07 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL 2.8.3 released!
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: Decision when to load 64bit?  (Read 785 times)
mstWeal
Newbie
*
Posts: 20


« on: September 12, 2010, 11:56:07 »

Hi there,

how does LWJGL determine when to load the 64bit DLLs and when to load the 32bit DLLs?

For I have a 64bit system. So far everything worked fine but I started to use PowerMock to write my unit tests. Curiously, if I start unit tests with the PowerMock test runner there is an unsatisfied exception, stating that 32bit DLLs cannot be loaded on AMD 64 bit processors. I removed the 32bit DLLs but then it complains that lwjgl.dll cannot be found ...
Logged
Matzon
Administrator
Demigod
*****
Posts: 2163



« Reply #1 on: September 12, 2010, 22:29:59 »

Sys.java:
Code:
    private static void loadLibrary(final String lib_name) {
        try {
            doLoadLibrary(lib_name);
        } catch (UnsatisfiedLinkError e) {
            if (implementation.has64Bit()) {
                try {
                    doLoadLibrary(lib_name + POSTFIX64BIT);
                    return;
                } catch (UnsatisfiedLinkError e2) {
                    LWJGLUtil.log("Failed to load 64 bit library: " + e2.getMessage());
                }
            }
            // Throw original error
            throw e;
        }
    }

one could argue that if 64 bit is available for the implementation it would try that first.

something similar to this:
Code:
    private static void loadLibrary(final String lib_name) {
        if (implementation.has64Bit()) {
            try {
                doLoadLibrary(lib_name + POSTFIX64BIT);
                return;
            } catch (UnsatisfiedLinkError e2) {
                LWJGLUtil.log("Failed to load 64 bit library: " + e2.getMessage());
            }
        }
        try {
            doLoadLibrary(lib_name);
        } catch (UnsatisfiedLinkError e) {
            // Throw original error
            throw e;
        }
    }
Logged

http://certusgames.com (Free Online Multiplayer Java Games)
http://lwjgl.org (OpenGL/OpenAL for Java)
mstWeal
Newbie
*
Posts: 20


« Reply #2 on: September 13, 2010, 00:59:02 »

Okay thank you. I've debugged into that code and the source of the exception says the following:

"Native Library E:\Dokumente\Workspace\lib\lwjgl\native\windows\lwjgl64.dll already loaded in another classloader"

Well that explains why the test works if I only run the PowerMock test alone but not when I run all my tests together. PowerMock uses it's own class loader and then this leads to this situation. Is it possible to "fix" this at LWJGL level?
Logged
Matzon
Administrator
Demigod
*****
Posts: 2163



« Reply #3 on: September 13, 2010, 01:12:56 »

I dont think we'd want to support this on a lwjgl level - not sure...

however the appletload has an 'unloadNatives' method that kappa added to fix this issue for applet. You may be able to use this.

Code:
    /**
     * Unload natives loaded by a different classloader.
     *
     * Due to limitations of the jvm, native files can only
     * be loaded once and only be used by the classloader
     * they were loaded from.
     *
     * Due to the way applets on plugin1 work, one jvm must
     * be used for all applets. We need to use multiple
     * classloaders in the same jvm due to LWJGL's static
     * nature. I order to solve this we simply remove the
     * natives from a previous classloader allowing a new
     * classloader to use those natives in the same jvm.
     *
     * This method will only attempt to unload natives from a
     * previous classloader if it detects that the natives have
     * been loaded in the same jvm.
     *
     * @param nativePath directory where natives are stored
     */
    private void unloadNatives(String nativePath) {
       
        // check whether natives have been loaded into this jvm
        if (!natives_loaded) {
            return;
        }
       
        try {
            Field field = ClassLoader.class.getDeclaredField("loadedLibraryNames");
            field.setAccessible(true);
            Vector libs = (Vector) field.get(getClass().getClassLoader());
           
            String path = new File(nativePath).getCanonicalPath();
           
            for (int i = 0; i < libs.size(); i++) {
                String s = (String) libs.get(i);
               
                // if a native from the nativePath directory is loaded, unload it
                if (s.startsWith(path)) {
                    libs.remove(i);
                    i--;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Logged

http://certusgames.com (Free Online Multiplayer Java Games)
http://lwjgl.org (OpenGL/OpenAL for Java)
mstWeal
Newbie
*
Posts: 20


« Reply #4 on: September 13, 2010, 04:23:53 »

Thank you, this should workaround the problem for me as I will be able to unload the libraries in the tearDown method of the test cases :-)
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!