001    // RealtimeSystem.java, created by wbeebee
002    // Copyright (C) 2001 Wes Beebee <wbeebee@mit.edu>
003    // Licensed under the terms of the GNU GPL; see COPYING for details.
004    package javax.realtime;
005    
006    /** <code>RealtimeSystem</code> provides a means for tuning the behavior
007     *  of the implementation by specifying parameters such as the maximum
008     *  number of locks that can be in use concurrently, and the monitor 
009     *  control policy.  In addition, <code>RealtimeSystem</code> provides
010     *  a mechanism for obtaining access to the security manager, garbage
011     *  collector and scheduler, to make queries from them or to set 
012     *  parameters.
013     */
014    public class RealtimeSystem {
015    
016        /** Value to set the byte ordering for the underlying hardware. */
017        public static final byte BIG_ENDIAN = 0;
018        /** Value to set the byte ordering for the underlying hardware. */
019        public static final byte LITTLE_ENDIAN = 1;    
020        /** The byte ordering of the underlying hardware. */    
021        public static final byte BYTE_ORDER = 2;
022    
023        private static GarbageCollector garbageCollector = null;
024        private static RealtimeSecurity securityManager = null;
025        private static int maxConcurrentLocks;
026        private static int concurrentLocksUsed;
027        private static boolean maxHard;
028    
029        public RealtimeSystem() {
030            maxConcurrentLocks = 0;
031            concurrentLocksUsed = 0;
032            maxHard = false;
033        }
034    
035        /** Native call to retrieve the current <code>GarbageCollector</code>. */
036        native static GarbageCollector getCurrentGC();
037    
038        /** Return a reference to the currently active garbage collector for the heap.
039         *
040         *  @return A <code>GarbageCollector</code> object which is the current
041         *          collector collecting objects on the traditional Java heap.
042         */
043        public static GarbageCollector currentGC() {
044            if (garbageCollector == null) {
045                garbageCollector = getCurrentGC();
046            }
047            return garbageCollector;
048        }
049    
050        /** Gets the maximum number of locks that have been used concurrently.
051         *  This value can be used for tuning the concurent locks parameter
052         *  which is used as a hint by systems that use a monitor cache.
053         *
054         *  @return An intenger whose value is the number of locks in use at
055         *          the time of the invocation of the method.
056         */
057        public static int getConcurrentLocksUsed() {
058            return concurrentLocksUsed;
059        }
060    
061        /** Gets the maximum number of locks that can be used concurrently
062         *  without incurring an execution time increase as set by the
063         *  <code>setMaximumConcurrentLocks()</code> methods.
064         *
065         *  @return An integer whose value is the maximum number of locks
066         *          that can be in simultaneous use.
067         */
068        public static int getMaximumConcurrentLocks() {
069            return maxConcurrentLocks;
070        }
071    
072        /** Gets a reference to the security manager used to control access
073         *  to real-time system features such as access to physical memory.
074         *
075         *  @return A <code>RealtimeSecurity</code> object representing the
076         *          default realtime security manager.
077         */
078        public static RealtimeSecurity getSecurityManager() {
079            return securityManager;
080        }
081    
082        /** Sets the anticipated maximum number of locks that may be held
083         *  or waited on concurrently.  Provide a hint to systems that use
084         *  a monitor cache as to how much space to dedicate to the cache.
085         *
086         *  @param number An integer whose value becomes the number of locks
087         *                that can be in simultaneous use without incurring
088         *                an execution time increase. If <code>number</code>
089         *                is less than or equal to zero nothing happens.
090         */
091        public static void setMaximumConcurrentLocks(int number) {
092            if (number > 0) {
093                maxConcurrentLocks = number;
094            }
095        }
096    
097        /** Sets the anticipated maximum number of locks that may be held or
098         *  waited on concurrently.  Provide a limit for the size of the 
099         *  monitor cache on systems that provide one if hard is true.
100         *
101         *  @param number The maximum number of locks that can be in simultaneous
102         *                use without incurring an execution time increase. If
103         *                <code>number</code> is less than or equal to zero
104         *                nothing happens.
105         *  @param hard If true, <code>number</code> sets the limit. If a lock is
106         *              attempted which would cause the number of locks to exceed
107         *              <code>number</code> then a <code>ResourceLimitError</code>
108         *              is thrown.
109         */
110        public static void setMaximumConcurrentLocks(int number, boolean hard) {
111            if (number > 0) {
112                maxConcurrentLocks = number;
113                maxHard = hard;
114            }
115        }
116    
117        /** Set a new real-time security manager.
118         *
119         *  @param manager A <code>RealtimeSecurity</code> object which will
120         *                 become the new security manager.
121         *  @throws java.lang.SecurityException Thrown if security manager
122         *                                      has already been set.
123         */
124        public static void setSecurityManager(RealtimeSecurity manager) 
125            throws SecurityException
126        {
127            if (securityManager == null) {
128                securityManager = manager;
129            } else {
130                throw new SecurityException("SecurityManager already set.");
131            }
132        }
133    }