Oracle® Fusion Middleware C++ API Reference for Oracle Coherence
12c (12.2.1.3.0)

E80355-01

coherence/lang/SynchronizedBlock.hpp

00001 /*
00002 * SynchronizedBlock.hpp
00003 *
00004 * Copyright (c) 2000, 2017, 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_SYNCHRONIZED_BLOCK_HPP
00017 #define COH_SYNCHRONIZED_BLOCK_HPP
00018 
00019 #include "coherence/lang/compatibility.hpp"
00020 
00021 #include "coherence/lang/IllegalStateException.hpp"
00022 #include "coherence/lang/Object.hpp"
00023 
00024 #include <sstream>
00025 
00026 COH_OPEN_NAMESPACE2(coherence,lang)
00027 
00028 
00029 /**
00030 * The SynchronizedBlock class allows for easy creation of synchronized code
00031 * blocks based on an Object's monitor. The SynchronizedBlock object will
00032 * ensure that the monitor is entered and exited as part of starting and
00033 * ending the code block.
00034 *
00035 * Example usage:
00036 * @code
00037 * // outside of sync block
00038 *     {
00039 *     SynchronizedBlock sync(vObject);
00040 *     // monitor entered
00041 *     // critical section goes here
00042 *     // ...
00043 *     } // monitor exited
00044 * // outside of sync block
00045 * @endcode
00046 *
00047 * A more friendly form is to use the COH_SYNCHRONIZED macro. Example usage:
00048 *
00049 * @code
00050 * // outside of sync block
00051 * COH_SYNCHRONIZED(vLock) // monitor entered
00052 *   {
00053 *   // critical section goes here
00054 *   // ...
00055 *   // ...
00056 *   } // monitor exited
00057 * // outside of sync block
00058 * @endcode
00059 *
00060 * @author mf 2007.12.09
00061 */
00062 class COH_EXPORT SynchronizedBlock
00063     {
00064     // ----- constructors ---------------------------------------------------
00065 
00066     public:
00067         /**
00068         * Construct a SynchronizedBlock object.
00069         *
00070         * This will automatically enter the monitor.
00071         */
00072         SynchronizedBlock(Object::View v)
00073             : m_cpObject(get_pointer(v)), m_vObject(v)
00074             {
00075             v->_enterMonitor();
00076             }
00077 
00078         /**
00079         * Construct a SynchronizedBlock object from a raw object pointer.
00080         *
00081         * This will automatically enter the monitor.
00082         *
00083         * The caller must ensure that the referenced Object is kept alive for
00084         * the lifetime of the SynchronizedBlock.  This method is generally
00085         * used for synchronizing on "this", which implicitly ensures the
00086         * lifetime guarantee.
00087         */
00088         SynchronizedBlock(const Object* p)
00089             : m_cpObject(p), m_vObject()
00090             {
00091             if (p == NULL)
00092                 {
00093                 coh_throw_npe(typeid(Object));
00094                 }
00095             p->_enterMonitor();
00096             }
00097 
00098         /**
00099         * Copy constructor for COH_SYNCHRONIZED macro.
00100         */
00101         SynchronizedBlock(const SynchronizedBlock& that)
00102             : m_cpObject(that.m_cpObject), m_vObject(that.m_vObject)
00103             {
00104             that.m_cpObject = NULL;
00105             that.m_vObject  = NULL;
00106             }
00107 
00108         /**
00109         * Destroy a SynchronizedBlock object.
00110         *
00111         * This will automatically exit the monitor.
00112         */
00113         ~SynchronizedBlock()
00114             {
00115             const Object* cp = m_cpObject;
00116             if (NULL != cp)
00117                 {
00118                 m_cpObject = NULL;
00119                 cp->_exitMonitor();
00120                 m_vObject  = NULL;
00121                 }
00122             }
00123 
00124 
00125     // ----- operators ------------------------------------------------------
00126 
00127     public:
00128         /*
00129         * Boolean conversion for use in COH_SYNCHRONIZED macro.
00130         *
00131         * @return false if synchronization is held, true otherwise
00132         */
00133         operator bool() const
00134             {
00135             return m_cpObject == NULL;
00136             }
00137 
00138     private:
00139         /**
00140         * Blocked assignment operator.
00141         */
00142         const SynchronizedBlock& operator=(const SynchronizedBlock&);
00143 
00144         /**
00145         * Blocked dynamic allocation.
00146         */
00147         static void* operator new(size_t);
00148 
00149 
00150     // ----- data members ---------------------------------------------------
00151 
00152     protected:
00153         /**
00154         * Raw pointer to Object used to protect the synchronized block.
00155         */
00156         mutable const Object* m_cpObject;
00157 
00158         /**
00159         * Object used to protect the synchronized block.
00160         */
00161         mutable Object::View m_vObject; // on stack
00162     };
00163 
00164 COH_CLOSE_NAMESPACE2
00165 
00166 
00167 /**
00168 * Macro for making more readable synchronized code blocks See the
00169 * documentation of SynchronizedBlock for a usage example.
00170 *
00171 * @see coherence::lang::SynchronizedBlock
00172 */
00173 #define COH_SYNCHRONIZED(V) \
00174     if (coherence::lang::SynchronizedBlock COH_UNIQUE_IDENTIFIER(_coh_sync_) \
00175         = coherence::lang::SynchronizedBlock(V)) \
00176         { \
00177         COH_THROW(coherence::lang::IllegalStateException::create()); \
00178         } \
00179     else
00180 
00181 #endif // COH_SYNCHRONIZED_BLOCK_HPP
Copyright © 2000, 2017, Oracle and/or its affiliates. All rights reserved.