001/*
002 * $RCSfile: NativeServices.java,v $
003 * $Revision: 1.1 $
004 * $Date: 2005/02/11 05:02:26 $
005 * $State: Exp $
006 *
007 * Class:                   NativeServices
008 *
009 * Description:             Static methods allowing to access to some
010 *                          native services. It uses native methods.
011 *
012 *
013 *
014 * COPYRIGHT:
015 *
016 * This software module was originally developed by Raphaël Grosbois and
017 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
018 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
019 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
020 * Centre France S.A) in the course of development of the JPEG2000
021 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
022 * software module is an implementation of a part of the JPEG 2000
023 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
024 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
025 * Partners) agree not to assert against ISO/IEC and users of the JPEG
026 * 2000 Standard (Users) any of their rights under the copyright, not
027 * including other intellectual property rights, for this software module
028 * with respect to the usage by ISO/IEC and Users of this software module
029 * or modifications thereof for use in hardware or software products
030 * claiming conformance to the JPEG 2000 Standard. Those intending to use
031 * this software module in hardware or software products are advised that
032 * their use may infringe existing patents. The original developers of
033 * this software module, JJ2000 Partners and ISO/IEC assume no liability
034 * for use of this software module or modifications thereof. No license
035 * or right to this software module is granted for non JPEG 2000 Standard
036 * conforming products. JJ2000 Partners have full right to use this
037 * software module for his/her own purpose, assign or donate this
038 * software module to any third party and to inhibit third parties from
039 * using this software module for non JPEG 2000 Standard conforming
040 * products. This copyright notice must be included in all copies or
041 * derivative works of this software module.
042 *
043 * Copyright (c) 1999/2000 JJ2000 Partners.
044 *
045 *
046 *
047 */
048
049
050package jj2000.j2k.util;
051
052/**
053 * This class presents a collection of static methods that allow access to
054 * some native methods. It makes use of native methods to access those thread
055 * properties.
056 *
057 * <P>Since the methods in this class require the presence of a shared library
058 * with the name defined in SHLIB_NAME it is necessary to load it prior to
059 * making use of any such methods. All methods that require the shared library
060 * will automatically load the library if that has not been already done. The
061 * library might also be manually loaded with the 'loadLibrary()' method of
062 * this class.
063 *
064 * <P>This class provides only static methods. It should not be instantiated.
065 *
066 * <P>Currently the only native services available is settings relative to
067 * POSIX threads, which are not accessible from the Java API.
068 *
069 * <P>Currently the methods in this class make sense with POSIX threads only,
070 * since they access POSIX threads settings. POSIX threads are most used under
071 * UNIX and UNIX-like operating systems and are mostly referred to as "native"
072 * threads in Java Virtual Machine (JVM) implementations.
073 *
074 * <P>The shared library SHLIB_NAME uses functions of the POSIX thread library
075 * (i.e. 'pthread'). Calling the methods that use the 'pthread' library will
076 * most prbably cause the Java Virtual Machine (JVM) to crash if it is not
077 * using the POSIX threads, due to unsatisfied references. For instance, JVMs
078 * that use "green" threads will most certainly crash. POSIX threads are
079 * referred to as "native" threads in JVMs under UNIX operating systems.
080 *
081 * <P>On Operating Systems where POSIX threads are not available (typically
082 * Windows 95/98/NT/2000, MacIntosh, OS/2) there is no problem since the
083 * SHLIB_NAME, if available, will not make use of POSIX threads library
084 * functions, thus no problem should occur.
085 * */
086public final class NativeServices {
087
088    /** The name of the shared library containing the implementation of the
089     * native methods: 'jj2000'. The actual file name of the library is system
090     * dependent. Under UNIX it will be 'libjj2000.so', while under Windows it
091     * will be 'jj2000.dll'.
092     * */
093    public static final String SHLIB_NAME = "jj2000";
094
095    /** The state of the library loading */
096    private static int libState;
097
098    /**
099     * Library load state ID indicating that no attept to load the library has
100     * been done yet.  */
101    private final static int LIB_STATE_NOT_LOADED = 0;
102
103    /**
104     * Library load state ID indicating that the library was successfully
105     * loaded. */
106    private final static int LIB_STATE_LOADED = 1;
107
108    /**
109     * Library load state ID indicating that an attempt to load the library
110     * was done and that it could not be found. */
111    private final static int LIB_STATE_NOT_FOUND = 2;
112
113    /**
114     * Private and only constructor, so that no class instance might be
115     * created. Since all methods are static creating a class instance is
116     * useless. If called it throws an 'IllegalArgumentException'.
117     * */
118    private NativeServices() {
119        throw new IllegalArgumentException("Class can not be instantiated");
120    }
121
122    /**
123     * Sets the concurrency level of the threading system of the Java Virtual
124     * Machine (JVM) to the specified level. The concurrency level specifies
125     * how many threads can run in parallel on different CPUs at any given
126     * time for JVM implementations that use POSIX threads with
127     * PTHREAD_SCOPE_PROCESS scheduling scope. A concurrency level of 0 means
128     * that the operating system will automatically adjust the concurrency
129     * level depending on the number of threads blocking on system calls, but
130     * this will probably not exploit more than one CPU in multiporocessor
131     * machines. If the concurrency level if set to more than the number of
132     * available processors in the machine the performance might degrade.
133     *
134     * <P>For JVM implementations that use POSIX threads with
135     * PTHREAD_SCOPE_SYSTEM scheduling scope or JVM implementations that use
136     * Windows(R) threads and maybe others, setting the concurrency level has
137     * no effect. In this cases the number of CPUs that can be exploited by
138     * the JVM is not limited in principle, all CPUs are available to the JVM.
139     *
140     * <P>For JVM implementations that use "green" threads setting the
141     * concurrency level, and thus calling this method, makes no sense, since
142     * "green" threads are all contained in one user process and can not use
143     * multiple CPUs. In fact calling this method can result in a JVM crash is
144     * the shared library SHLIB_NAME has been compiled to use POSIX threads.
145     *
146     * @param n The new concurrency level to set.
147     *
148     * @exception IllegalArgumentException Concurrency level is negative
149     *
150     * @exception UnsatisfiedLinkError If the shared native library
151     * implementing the functionality could not be loaded.
152     * */
153    public static void setThreadConcurrency(int n) {
154        // Check that the library is loaded
155        checkLibrary();
156        // Check argument
157        if (n < 0) throw new IllegalArgumentException();
158        // Set concurrency with native method
159        setThreadConcurrencyN(n);
160    }
161
162    /**
163     * Calls the POSIX threads 'pthread_setconcurrency', or equivalent,
164     * function with 'level' as the argument.
165     * */
166    private static native void setThreadConcurrencyN(int level);
167
168    /**
169     * Returns the current concurrency level. See 'setThreadConcurrency' for
170     * details on the meaning of concurrency
171     *
172     * @return The current concurrency level
173     *
174     * @see #setThreadConcurrency
175     * */
176    public static int getThreadConcurrency() {
177        // Check that the library is loaded
178        checkLibrary();
179        // Return concurrency from native method
180        return getThreadConcurrencyN();
181    }
182
183    /**
184     * Calls the POSIX threads 'pthread_getconcurrency', or equivalent,
185     * function and return the result.
186     *
187     * @return The current concurrency level.
188     * */
189    private static native int getThreadConcurrencyN();
190
191    /**
192     * Loads the shared library implementing the native methods of this
193     * class and returns true on success. Multiple calls to this method after
194     * a successful call have no effect and return true. Multiple calls to
195     * this method after unsuccesful calls will make new attempts to load the
196     * library.
197     *
198     * @return True if the libary could be loaded or is already loaded. False
199     * if the library can not be found and loaded.
200     * */
201    public static boolean loadLibrary() {
202        // If already loaded return true without doing anything
203        if (libState == LIB_STATE_LOADED) return true;
204        // Try to load the library
205        try {
206            System.loadLibrary(SHLIB_NAME);
207        } catch (UnsatisfiedLinkError e) {
208            // Library was not found
209            libState = LIB_STATE_NOT_FOUND;
210            return false;
211        }
212        // Library was found
213        libState = LIB_STATE_LOADED;
214        return true;
215    }
216
217    /**
218     * Checks if the library SHLIB_NAME is already loaded and attempts to load
219     * if not yet loaded. If the library can not be found (either in a
220     * previous attempt to load it or in an attempt in this method) an
221     * 'UnsatisfiedLinkError' exception is thrown.
222     *
223     * @exception UnsatisfiedLinkError If the library SHLIB_NAME can not be
224     * found.
225     * */
226    private static void checkLibrary() {
227        switch (libState) {
228        case LIB_STATE_LOADED: // Already loaded, nothing to do
229            return;
230        case LIB_STATE_NOT_LOADED: // Not yet loaded => load now
231            // If load successful break, otherwise continue to the
232            // LIB_STATE_NOT_LOADED state
233            if (loadLibrary()) break;
234        case LIB_STATE_NOT_FOUND: // Could not be found, throw exception
235            throw new UnsatisfiedLinkError("NativeServices: native shared "+
236                                           "library could not be loaded");
237        }
238    }
239
240}