coherence/lang/System.hpp

00001 /*
00002 * System.hpp
00003 *
00004 * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_SYSTEM_HPP
00017 #define COH_SYSTEM_HPP
00018 
00019 #include "coherence/lang/compatibility.hpp"
00020 
00021 #include "coherence/lang/abstract_spec.hpp"
00022 #include "coherence/lang/HeapAnalyzer.hpp"
00023 #include "coherence/lang/ObjectArray.hpp"
00024 #include "coherence/lang/String.hpp"
00025 
00026 COH_OPEN_NAMESPACE2(coherence,lang)
00027 
00028 
00029 /**
00030 * A collection of general purpose utility methods.
00031 *
00032 * @author mf 2007.12.19
00033 */
00034 class COH_EXPORT System
00035     : public abstract_spec<System>
00036     {
00037     // ----- constructors ---------------------------------------------------
00038 
00039     private:
00040         /**
00041         * Blocked constructor.
00042         */
00043         System();
00044 
00045 
00046     // ----- System interface -----------------------------------------------
00047 
00048     public:
00049         /**
00050         * Return the number of milliseconds which have elapsed since the
00051         * process was started.
00052         *
00053         * @return the number of milliseconds which have elapsed since the
00054         *         process was started
00055         */
00056         static int64_t upTimeMillis();
00057 
00058         /**
00059         * Return the elapsed milliseconds since midnight January 1, 1970 UTC.
00060         *
00061         * This function does not guard against the clock rolling back or
00062         * jumping forward.
00063         *
00064         * @return the elapsed milliseconds
00065         */
00066         static int64_t currentTimeMillis();
00067 
00068         /**
00069         * Returns a "safe" current time in milliseconds.
00070         *
00071         * Unlike the #currentTimeMillis() this method guarantees that the
00072         * time never "goes back".
00073         *
00074         * More specifically, when called twice on the same thread, the second
00075         * call will never return a value that is less then the value returned
00076         * by the first call. If a system time correction becomes necessary, an
00077         * attempt will be made to gradually compensate the returned value, so
00078         * in the long run the value returned by this method is the same as the
00079         * system time.
00080         *
00081         * Additionally, the following always holds true:
00082         * <pre>
00083         *   System::safeTimeMillis() >= System::currentTimeMillis();
00084         * </pre>
00085         *
00086         * @return the difference, measured in milliseconds, between
00087         *         the corrected current time and midnight, January 1, 1970 UTC.
00088         */
00089         static int64_t safeTimeMillis();
00090 
00091         /**
00092         * Return the value of a system property.
00093         *
00094         * The standard naming convention for system properties is '.'
00095         * Separated property names, such as "tangosol.coherence.setting".
00096         *
00097         * As of Coherence 3.4 the only source for system properties is
00098         * environment variables. As some environemts do not allow '.' in
00099         * variable names, a failed lookup will be automatically re-issued
00100         * using a camel-cased version of the supplied name, i.e. the property
00101         * name "tangosol.coherence.setting" would translated to
00102         * "TangosolCoherenceSetting".  From a code level perspective
00103         * camel-case does not need to be a consideration, it is only when
00104         * setting environment variables that it may be needed.
00105         *
00106         * @param vsName     the name of the property to return
00107         * @param vsDefault  the default value for the property
00108         *
00109         * @return the corresponding property value, or the supplied default if
00110         *         no value could be found
00111         */
00112         static String::View getProperty(String::View vsName,
00113                 String::View vsDefault = String::NULL_STRING);
00114 
00115         /**
00116         * Return the same hash code for the given object as would be returned
00117         * by the default implementation provided in Object. In the case of
00118         * NULL zero is returned.
00119         *
00120         * @param v  the object to hash
00121         *
00122         * @return the object's identity hash code.
00123         */
00124         static size32_t identityHashCode(Object::View v);
00125 
00126         /**
00127         * Specify the system interrupt resolution.
00128         *
00129         * The resolution at which a blocked thread will test if it has been
00130         * interrupted.  Not all blocking states are guaranteed to utilize this
00131         * feature, but at a minimum Object::wait(), and Coherence based network
00132         * communications do respect this setting.
00133         *
00134         * Once in a blocking state the blocked call may not see updates to this
00135         * setting. If this setting is to be updated it is recommended that it
00136         * only occur once at application startup.
00137         *
00138         * Recommended values are between 100 and 1000 milliseconds, default is
00139         * 250 milliseconds.
00140         *
00141         * @param cMillis  the new interrupt resolution
00142         *
00143         * @throws IllegalArgumentException if cMillis <= 0
00144         */
00145         static void setInterruptResolution(int64_t cMillis);
00146 
00147         /**
00148         * Return the interrupt resolution.
00149         *
00150         * @return the interrupt resolution.
00151         */
00152         static int64_t getInterruptResolution();
00153 
00154         /**
00155         * Load the specified library.
00156         */
00157         static void loadLibrary(String::View vsLibName);
00158 
00159     // ----- diagnostics methods --------------------------------------------
00160 
00161     public:
00162         /**
00163         * Test that the attach count on the Object is the expected value.
00164         *
00165         * This method is for testing purposes only, and will throw an
00166         * exception if the count is not the expected value.
00167         *
00168         * @param v        the Object to test
00169         * @param cHandle  the expected number of handles attached to this
00170         *                 Object
00171         * @param fEscaped the expected object escape state
00172         */
00173         static void assertAttachCount(Object::View v, uint32_t cHandle,
00174                 uint32_t cView);
00175 
00176         /**
00177         * Return a string representation of the Object's life-cycle state.
00178         *
00179         * This is intended for diagnostics purposes only.
00180         *
00181         * @param v  the Object to describe
00182         *
00183         * @return a human-readable description of the object's life-cycle
00184         *         state
00185         */
00186         static String::View getLifeCycleDescription(Object::View v);
00187 
00188         /**
00189         * Return the System HeapAnalyzer.
00190         *
00191         * There may be only one HeapAnalyzer for the life of the process, the
00192         * default being the ObjectCountHeapAnalyzer, which is very low cost.
00193         *
00194         * The HeapAnalyzer to use may be specified via the
00195         * "tangosol.coherence.heap.analyzer" system property, can can be set
00196         * to either a registered class name, or to "none".
00197         *
00198         * If using a heap-analyzer other then the default, some Objects
00199         * created during static initialization may not be registered.
00200         *
00201         * @return the System HeapAnalyzer.
00202         */
00203         static HeapAnalyzer::Handle getHeapAnalyzer();
00204 
00205         /**
00206         * Return the common monitor associated with the specified integer
00207         * value.
00208         *
00209         * Common monitors allow for a low-cost means to reduce contention by
00210         * spreading synchronization over a large number of monitors. An
00211         * example usage would be to produce an "atomic array". For instance
00212         * to atomically change an element within an array which is being
00213         * simultaneously updated by multiple threads:
00214         * <pre>
00215         * COH_SYNCHRONIZED (System::getCommonMonitor(
00216         *   System::identityHashCode(aoShared) + i))
00217         *     {
00218         *     oOld = haoShared[i];
00219         *     haoShared[i] = oNew;
00220         *     }
00221         * </pre>
00222         * With this approach many threads may concurrently access various
00223         * array elements without having to synchronize on the array itself,
00224         * and contend with each other. The use of common monitors also avoids
00225         * the overhead of allocating a unique monitor per index. This example
00226         * additionally makes use of the array's identity hash code to avoid
00227         * frequent collisions against other atomic arrays for the same indices.
00228         * <p/>
00229         * As they are shared, these monitors will apply to any number of
00230         * unrelated entities, and as such certain precautions must be employed
00231         * when using them.
00232         * <ul>
00233         * <li>The holder of a common monitor MUST not synchronize on any other
00234         *     common monitor. Failure to adhere to this precaution will result
00235         *     in a deadlock.
00236         * <li>Notifications on a common monitor MUST use notifyAll() rather
00237         *     then notify(), as there may be unrelated threads waiting for
00238         *     notification on the same monitor which could consume a single
00239         *     notification. Thus the only way to ensure that the desired
00240         *     thread does receive notification is to notify all threads
00241         *     waiting on the monitor.
00242         * <li>Threads waiting for a notification must protect themselves
00243         *     against spurious style wakeups. While this is a general, though
00244         *     often overlooked part of the normal use of notification, with
00245         *     common monitors it is far more likely that a thread will be
00246         *     notified due to an unrelated event.
00247         * <li>A thread which is holding synchronization on a common monitor
00248         *     should avoid blocking operations as this could block unrelated
00249         *     threads which happen to be utilizing the same common monitor.
00250         * </ul>
00251         * The ideal number of common monitors in a process is one per
00252         * concurrently executing thread. As this number is generally unknown
00253         * the default number of monitors set to a relatively high value. The
00254         * value may also be manually specified via the
00255         * <code>tangosol.coherence.commonmonitors</code> system property.
00256         *
00257         * @param i the common monitor identifier
00258         *
00259         * @return the associated monitor
00260         */
00261         static Object::Handle getCommonMonitor(size32_t i);
00262 
00263         /**
00264         * Return the common monitor associated with the specified long value.
00265         *
00266         * @param l the common monitor identifier
00267         *
00268         * @return the associated monitor
00269         *
00270         * @see #getCommonMonitor(size32_t)
00271         */
00272         static Object::Handle getCommonMonitor(size64_t l);
00273 
00274         /**
00275         * Return a random common monitor for use as the guardian of
00276         * thread-safe handles which are not a data member of a managed object.
00277         */
00278         static Object& common();
00279 
00280         /**
00281         * Executable entrypoint for the System class.
00282         *
00283         * Print the Coherence library version information.
00284         */
00285         static void main(ObjectArray::View);
00286 
00287 
00288     // ----- nested class: CommonMonitor ------------------------------------
00289 
00290     public:
00291         /**
00292         * A class which acts as CommonMonitor.
00293         *
00294         * The custom class is produced to aid in identification while profiling.
00295         */
00296         class COH_EXPORT CommonMonitor
00297             : public class_spec<CommonMonitor>
00298             {
00299             friend class factory<CommonMonitor>;
00300 
00301             protected:
00302                 CommonMonitor() {}
00303             };
00304     };
00305 
00306 COH_CLOSE_NAMESPACE2
00307 
00308 #endif // COH_SYSTEM_HPP
Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.