00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanLockable.h"
00028
00029 #include "CeylanUtils.h"
00030 #include "CeylanLogPlug.h"
00031
00032
00033
00034
00035 using std::string ;
00036
00037 using namespace Ceylan ;
00038 using namespace Ceylan::Log ;
00039
00040
00041
00042
00043 Lockable::LockException::LockException( const string & reason ) :
00044 Ceylan::Exception( reason )
00045 {
00046
00047 }
00048
00049
00050
00051 Lockable::LockException::~LockException() throw()
00052 {
00053
00054 }
00055
00056
00057
00058
00059
00060
00061
00062 Lockable::Lockable() :
00063 _locked( false )
00064 {
00065
00066 }
00067
00068
00069
00070 Lockable::~Lockable() throw()
00071 {
00072
00073 try
00074 {
00075
00076 if ( mustBeLocked() && _locked )
00077 {
00078
00079 LogPlug::warning( "Ceylan Lockable destructor: "
00080 "Lockable instance was still holding a lock at "
00081 "deallocation, this lock has been released.") ;
00082
00083 unlock() ;
00084 }
00085
00086 }
00087 catch( const Lockable::LockException & e )
00088 {
00089
00090 LogPlug::error( "Ceylan Lockable destructor: "
00091 "unable to unlock Lockable whereas "
00092 "it seemed to be locked: " + e.toString() ) ;
00093 }
00094
00095 }
00096
00097
00098
00099 bool Lockable::mustBeLocked() const
00100 {
00101
00102 return true ;
00103
00104 }
00105
00106
00107
00108 void Lockable::lock()
00109 {
00110
00111 if ( mustBeLocked() )
00112 {
00113
00114 if ( ! _locked )
00115 {
00116 _locked = true ;
00117 postLock() ;
00118 }
00119 else
00120 {
00121 throw LockException(
00122 "Attempt to lock an already locked Lockable." ) ;
00123 }
00124
00125 }
00126
00127
00128
00129 }
00130
00131
00132
00133 void Lockable::unlock()
00134 {
00135
00136 if ( mustBeLocked() )
00137 {
00138
00139 if ( _locked )
00140 {
00141 preUnlock() ;
00142 _locked = false ;
00143 }
00144 else
00145 {
00146 throw LockException( "Attempt to unlock a non locked Lockable." ) ;
00147 }
00148
00149 }
00150
00151
00152
00153 }
00154
00155
00156
00157 const string Lockable::toString( Ceylan::VerbosityLevels level ) const
00158 {
00159
00160 if ( _locked )
00161 return "Lockable currently locked" ;
00162 else
00163 return "Lockable currently not locked" ;
00164
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 void Lockable::postLock()
00174 {
00175
00176
00177
00178 }
00179
00180
00181
00182 void Lockable::preUnlock()
00183 {
00184
00185
00186
00187 }
00188
00189