00001 /* 00002 * System.hpp 00003 * 00004 * Copyright (c) 2000, 2020, 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 #include <locale> 00027 00028 COH_OPEN_NAMESPACE2(coherence,lang) 00029 00030 00031 /** 00032 * A collection of general purpose utility methods. 00033 * 00034 * @author mf 2007.12.19 00035 */ 00036 class COH_EXPORT System 00037 : public abstract_spec<System> 00038 { 00039 // ----- constructors --------------------------------------------------- 00040 00041 private: 00042 /** 00043 * Blocked constructor. 00044 */ 00045 System(); 00046 00047 00048 // ----- System interface ----------------------------------------------- 00049 00050 public: 00051 /** 00052 * Runs the garbage collector. 00053 * 00054 * While most managed objects are automatically reclaimed when their 00055 * reference count reaches zero, some special case objects utilize 00056 * deferred automatic cleanup. Calling this method will expedite this 00057 * deferred cleanup. 00058 * 00059 * @param fFull true iff an exhaustive collection is desired 00060 */ 00061 static void gc(bool fFull = false); 00062 00063 /** 00064 * Return the number of milliseconds which have elapsed since the 00065 * process was started. 00066 * 00067 * @return the number of milliseconds which have elapsed since the 00068 * process was started 00069 */ 00070 static int64_t upTimeMillis(); 00071 00072 /** 00073 * Return the elapsed milliseconds since midnight January 1, 1970 UTC. 00074 * 00075 * This function does not guard against the clock rolling back or 00076 * jumping forward. 00077 * 00078 * @return the elapsed milliseconds 00079 */ 00080 static int64_t currentTimeMillis(); 00081 00082 /** 00083 * Returns a "safe" current time in milliseconds. 00084 * 00085 * Unlike the #currentTimeMillis() this method guarantees that the 00086 * time never "goes back". 00087 * 00088 * More specifically, when called twice on the same thread, the second 00089 * call will never return a value that is less then the value returned 00090 * by the first call. If a system time correction becomes necessary, an 00091 * attempt will be made to gradually compensate the returned value, so 00092 * in the long run the value returned by this method is the same as the 00093 * system time. 00094 * 00095 * Additionally, the following always holds true: 00096 * <pre> 00097 * System::safeTimeMillis() >= System::currentTimeMillis(); 00098 * </pre> 00099 * 00100 * @return the difference, measured in milliseconds, between 00101 * the corrected current time and midnight, January 1, 1970 UTC. 00102 */ 00103 static int64_t safeTimeMillis(); 00104 00105 /** 00106 * Returns the last "safe" time as computed by a previous call to 00107 * the {@link #safeTimeMillis} method. 00108 * 00109 * Note: Since the underlying field is non-volatile, the returned value 00110 * is only guaranteed to be no less than the last value returned by 00111 * safeTimeMillis() call on the same thread. 00112 * 00113 * @return the last "safe" time in milliseconds 00114 */ 00115 static int64_t lastSafeTimeMillis(); 00116 00117 /** 00118 * Return the value of a system property. 00119 * 00120 * The standard naming convention for system properties is '.' 00121 * Separated property names, such as "coherence.setting". 00122 * 00123 * As of Coherence 3.4 the only source for system properties is 00124 * environment variables. As some environments do not allow '.' in 00125 * variable names, a failed lookup will be automatically re-issued 00126 * using a camel-cased version of the supplied name, i.e. the property 00127 * name "coherence.setting" would translate to 00128 * "CoherenceSetting". From a code level perspective 00129 * camel-case does not need to be a consideration, it is only when 00130 * setting environment variables that it may be needed. 00131 * 00132 * As of Coherence 12.2.1, the Coherence system property pattern 00133 * begins with "coherence.". This method looks up system property 00134 * by the provided vsName format. If vsName starts with "coherence." and 00135 * the system property lookup fails to find a value, this method retries 00136 * lookup with leading "tangosol." for backwards compatibility support. 00137 * 00138 * If vsName starts with "tangosol.", 00139 * it follows pre Coherence 12.2.1 Coherence system property pattern. 00140 * If lookup of vsName following this pattern occurs, then 00141 * a failed lookup is retried following Coherence 12.2.1 system 00142 * property pattern, replacing leading "tangosol." with 00143 * "coherence." 00144 * 00145 * @param vsName the name of the property to return 00146 * @param vsDefault the default value for the property 00147 * 00148 * @return the corresponding property value, or the supplied default if 00149 * no value could be found 00150 */ 00151 static String::View getProperty(String::View vsName, 00152 String::View vsDefault = String::null_string); 00153 00154 /** 00155 * Set the system property to a value. 00156 * 00157 * @param vsName the system property name 00158 * @param vsValue the value of the system property 00159 * 00160 * @return previous value of system property 00161 * 00162 * @since Coherence 12.2.1 00163 */ 00164 static String::View setProperty(String::View vsName, String::View vsValue); 00165 00166 /** 00167 * Clear system property's value. 00168 * 00169 * @param vsName the system property name to remove its value 00170 * 00171 * @return previous value of system property 00172 * 00173 * @since Coherence 12.2.1 00174 */ 00175 static String::View clearProperty(String::View vsName); 00176 00177 /** 00178 * Return the same hash code for the given object as would be returned 00179 * by the default implementation provided in Object. In the case of 00180 * NULL zero is returned. 00181 * 00182 * @param v the object to hash 00183 * 00184 * @return the object's identity hash code. 00185 */ 00186 static size32_t identityHashCode(Object::View v); 00187 00188 /** 00189 * Specify the system interrupt resolution. 00190 * 00191 * The resolution at which a blocked thread will test if it has been 00192 * interrupted. Not all blocking states are guaranteed to utilize this 00193 * feature, but at a minimum Object::wait(), and Coherence based network 00194 * communications do respect this setting. 00195 * 00196 * Once in a blocking state the blocked call may not see updates to this 00197 * setting. If this setting is to be updated it is recommended that it 00198 * only occur once at application startup. 00199 * 00200 * Recommended values are between 100 and 1000 milliseconds, default is 00201 * 250 milliseconds. 00202 * 00203 * @param cMillis the new interrupt resolution 00204 * 00205 * @throws IllegalArgumentException if cMillis <= 0 00206 */ 00207 static void setInterruptResolution(int64_t cMillis); 00208 00209 /** 00210 * Return the interrupt resolution. 00211 * 00212 * @return the interrupt resolution. 00213 */ 00214 static int64_t getInterruptResolution(); 00215 00216 /** 00217 * Load the specified library. 00218 */ 00219 static void loadLibrary(String::View vsLibName); 00220 00221 /** 00222 * Set the global locale. 00223 * 00224 * @param vsLocale the name of the locale to use, or NULL for the classic locale 00225 * 00226 * @return the name of the former locale 00227 */ 00228 COH_INLINE static String::View setLocale(String::View vsLocale) 00229 { 00230 if (vsLocale == NULL) 00231 { 00232 vsLocale = "C"; // classic locale 00233 } 00234 00235 // pass through to coh internals in case is using a different standard library. 00236 // Note we only do this by name as passing a std::local object across the lib 00237 // boundary may not be safe if different standard libraries are used 00238 String::View vsOld = System::setLocaleInternal(vsLocale); 00239 00240 // we also set it on the std version used by the caller, since this is inlined 00241 // it will pick up the callers library even if different from the one used internally 00242 std::locale loc(vsLocale->getCString()); 00243 std::cin.imbue(loc); 00244 std::cout.imbue(loc); 00245 std::cerr.imbue(loc); 00246 std::clog.imbue(loc); 00247 std::wcin.imbue(loc); 00248 std::wcout.imbue(loc); 00249 std::wcerr.imbue(loc); 00250 std::wclog.imbue(loc); 00251 std::locale::global(loc); 00252 00253 return vsOld; 00254 } 00255 00256 // ----- diagnostics methods -------------------------------------------- 00257 00258 public: 00259 /** 00260 * Test that the attach count on the Object is the expected value. 00261 * 00262 * This method is for testing purposes only, and will throw an 00263 * exception if the count is not the expected value. 00264 * 00265 * @param v the Object to test 00266 * @param cHandle the expected number of handles attached to this 00267 * Object 00268 * @param fEscaped the expected object escape state 00269 */ 00270 static void assertAttachCount(Object::View v, uint32_t cHandle, 00271 uint32_t cView); 00272 00273 /** 00274 * Return a string representation of the Object's life-cycle state. 00275 * 00276 * This is intended for diagnostics purposes only. 00277 * 00278 * @param v the Object to describe 00279 * 00280 * @return a human-readable description of the object's life-cycle 00281 * state 00282 */ 00283 static String::View getLifeCycleDescription(Object::View v); 00284 00285 /** 00286 * Return the System HeapAnalyzer. 00287 * 00288 * There may be only one HeapAnalyzer for the life of the process, the 00289 * default being the ObjectCountHeapAnalyzer, which is very low cost. 00290 * 00291 * The HeapAnalyzer to use may be specified via the 00292 * "coherence.heap.analyzer" system property, can can be set 00293 * to either a registered class name, or to "none". 00294 * 00295 * If using a heap-analyzer other then the default, some Objects 00296 * created during static initialization may not be registered. 00297 * 00298 * @return the System HeapAnalyzer. 00299 */ 00300 static HeapAnalyzer::Handle getHeapAnalyzer(); 00301 00302 /** 00303 * Return the common monitor associated with the specified integer 00304 * value. 00305 * 00306 * Common monitors allow for a low-cost means to reduce contention by 00307 * spreading synchronization over a large number of monitors. An 00308 * example usage would be to produce an "atomic array". For instance 00309 * to atomically change an element within an array which is being 00310 * simultaneously updated by multiple threads: 00311 * <pre> 00312 * COH_SYNCHRONIZED (System::getCommonMonitor( 00313 * System::identityHashCode(aoShared) + i)) 00314 * { 00315 * oOld = haoShared[i]; 00316 * haoShared[i] = oNew; 00317 * } 00318 * </pre> 00319 * With this approach many threads may concurrently access various 00320 * array elements without having to synchronize on the array itself, 00321 * and contend with each other. The use of common monitors also avoids 00322 * the overhead of allocating a unique monitor per index. This example 00323 * additionally makes use of the array's identity hash code to avoid 00324 * frequent collisions against other atomic arrays for the same indices. 00325 * 00326 * As they are shared, these monitors will apply to any number of 00327 * unrelated entities, and as such certain precautions must be employed 00328 * when using them. 00329 * <ul> 00330 * <li>The holder of a common monitor MUST not synchronize on any other 00331 * common monitor. Failure to adhere to this precaution will result 00332 * in a deadlock. 00333 * <li>Notifications on a common monitor MUST use notifyAll() rather 00334 * then notify(), as there may be unrelated threads waiting for 00335 * notification on the same monitor which could consume a single 00336 * notification. Thus the only way to ensure that the desired 00337 * thread does receive notification is to notify all threads 00338 * waiting on the monitor. 00339 * <li>Threads waiting for a notification must protect themselves 00340 * against spurious style wakeups. While this is a general, though 00341 * often overlooked part of the normal use of notification, with 00342 * common monitors it is far more likely that a thread will be 00343 * notified due to an unrelated event. 00344 * <li>A thread which is holding synchronization on a common monitor 00345 * should avoid blocking operations as this could block unrelated 00346 * threads which happen to be utilizing the same common monitor. 00347 * </ul> 00348 * The ideal number of common monitors in a process is one per 00349 * concurrently executing thread. As this number is generally unknown 00350 * the default number of monitors set to a relatively high value. The 00351 * value may also be manually specified via the 00352 * <code>tangosol.coherence.commonmonitors</code> system property. 00353 * 00354 * @param i the common monitor identifier 00355 * 00356 * @return the associated monitor 00357 */ 00358 static Object::Handle getCommonMonitor(size32_t i); 00359 00360 /** 00361 * Return the common monitor associated with the specified long value. 00362 * 00363 * @param l the common monitor identifier 00364 * 00365 * @return the associated monitor 00366 * 00367 * @see #getCommonMonitor(size32_t) 00368 */ 00369 static Object::Handle getCommonMonitor(size64_t l); 00370 00371 /** 00372 * Return a random common monitor for use as the guardian of 00373 * thread-safe handles which are not a data member of a managed object. 00374 */ 00375 static Object& common(); 00376 00377 /** 00378 * Return true if common monitors have been initialized. 00379 */ 00380 static bool isCommonMonitorInitialized(); 00381 00382 /** 00383 * Executable entrypoint for the System class. 00384 * 00385 * Print the Coherence library version information. 00386 */ 00387 static void main(ObjectArray::View); 00388 00389 00390 // ----- helper methods ------------------------------------------------- 00391 00392 protected: 00393 /** 00394 * Internal helper for specifying the global locale used within 00395 * the Coherence library. 00396 */ 00397 static String::View setLocaleInternal(String::View vsLocale); 00398 00399 00400 // ----- nested class: CommonMonitor ------------------------------------ 00401 00402 public: 00403 /** 00404 * A class which acts as CommonMonitor. 00405 * 00406 * The custom class is produced to aid in identification while profiling. 00407 */ 00408 class COH_EXPORT CommonMonitor 00409 : public class_spec<CommonMonitor> 00410 { 00411 friend class factory<CommonMonitor>; 00412 00413 protected: 00414 CommonMonitor() {} 00415 }; 00416 }; 00417 00418 COH_CLOSE_NAMESPACE2 00419 00420 #endif // COH_SYSTEM_HPP