00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 class COH_EXPORT SynchronizedBlock
00060 {
00061
00062
00063 public:
00064
00065
00066
00067
00068
00069 SynchronizedBlock(Object::View v)
00070 : m_cpObject(get_pointer(v)), m_vObject(v)
00071 {
00072 v->_enterMonitor();
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 SynchronizedBlock(const Object* p)
00086 : m_cpObject(p), m_vObject()
00087 {
00088 if (p == NULL)
00089 {
00090 coh_throw_npe(typeid(Object));
00091 }
00092 p->_enterMonitor();
00093 }
00094
00095
00096
00097
00098 SynchronizedBlock(const SynchronizedBlock& that)
00099 : m_cpObject(that.m_cpObject), m_vObject(that.m_vObject)
00100 {
00101 that.m_cpObject = NULL;
00102 that.m_vObject = NULL;
00103 }
00104
00105
00106
00107
00108
00109
00110 ~SynchronizedBlock()
00111 {
00112 const Object* cp = m_cpObject;
00113 if (NULL != cp)
00114 {
00115 m_cpObject = NULL;
00116 cp->_exitMonitor();
00117 m_vObject = NULL;
00118 }
00119 }
00120
00121
00122
00123
00124 public:
00125
00126
00127
00128
00129
00130 operator bool() const
00131 {
00132 return m_cpObject == NULL;
00133 }
00134
00135 private:
00136
00137
00138
00139 const SynchronizedBlock& operator=(const SynchronizedBlock&);
00140
00141
00142
00143
00144 static void* operator new(size_t);
00145
00146
00147
00148
00149 protected:
00150
00151
00152
00153 mutable const Object* m_cpObject;
00154
00155
00156
00157
00158 mutable Object::View m_vObject;
00159 };
00160
00161 COH_CLOSE_NAMESPACE2
00162
00163
00164
00165
00166
00167
00168
00169
00170 #define COH_SYNCHRONIZED(V) \
00171 if (coherence::lang::SynchronizedBlock COH_UNIQUE_IDENTIFIER(_coh_sync_) \
00172 = coherence::lang::SynchronizedBlock(V)) \
00173 { \
00174 COH_THROW(coherence::lang::IllegalStateException::create()); \
00175 } \
00176 else
00177
00178 #endif // COH_SYNCHRONIZED_BLOCK_HPP
Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.