00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #ifndef CEYLAN_SYNCHRONIZED_H_ 00028 #define CEYLAN_SYNCHRONIZED_H_ 00029 00030 00031 #include "CeylanMutex.h" // for Mutex 00032 00033 00034 00035 00036 namespace Ceylan 00037 { 00038 00039 00040 00041 namespace System 00042 { 00043 00044 00045 00059 template <typename X> 00060 class /* CEYLAN_DLL */ Synchronized 00061 { 00062 00063 00064 public: 00065 00066 00074 Synchronized( X value ) : 00075 _value( value ) 00076 { 00077 00078 } 00079 00080 00081 00082 /* 00083 ~Synchronized() throw() 00084 { 00085 00086 } 00087 */ 00088 00089 00090 00092 Synchronized & setValue( const volatile X & value ) 00093 { 00094 00095 _mutex.lock() ; 00096 _value = value ; 00097 _mutex.unlock() ; 00098 00099 return *this ; 00100 00101 } 00102 00103 00104 00106 const volatile X & getValue() const volatile 00107 { 00108 00109 return _value ; 00110 00111 } 00112 00113 00114 00115 00116 // Operators. 00117 00118 00119 00121 Synchronized & operator = ( const X & value ) 00122 { 00123 00124 return setValue( value ) ; 00125 00126 } 00127 00128 00129 00131 operator X () const volatile 00132 { 00133 00134 return _value ; 00135 00136 } 00137 00138 00139 00141 X operator ++() 00142 { 00143 00144 _mutex.lock() ; 00145 ++_value ; 00146 _mutex.unlock() ; 00147 00148 return _value ; 00149 00150 } 00151 00152 00153 00155 X operator ++(int) 00156 { 00157 00158 return operator ++() ; 00159 00160 } 00161 00162 00163 00165 X operator --() 00166 { 00167 00168 _mutex.lock() ; 00169 --_value ; 00170 _mutex.unlock() ; 00171 return _value ; 00172 00173 } 00174 00175 00176 00178 X operator --(int) 00179 { 00180 return operator --() ; 00181 } 00182 00183 00184 00185 00186 private: 00187 00188 00189 00191 volatile X _value ; 00192 00193 00195 Mutex _mutex ; 00196 00197 00198 00204 Synchronized() ; 00205 00206 00217 Synchronized( const Synchronized & source ) ; 00218 00219 00220 00229 Synchronized & operator = ( const Synchronized & source ) ; 00230 00231 00232 } ; 00233 00234 00235 00236 00237 00248 template<> 00249 class /* CEYLAN_DLL */ Synchronized<bool> 00250 { 00251 00252 00253 public: 00254 00255 00263 Synchronized( bool value ) : 00264 _value( value ) 00265 { 00266 00267 } 00268 00269 00270 00272 Synchronized & setValue( const volatile bool & value ) 00273 { 00274 00275 _mutex.lock() ; 00276 _value = value ; 00277 _mutex.unlock() ; 00278 00279 return *this ; 00280 00281 } 00282 00283 00284 00286 const volatile bool & getValue() const volatile 00287 { 00288 00289 return _value ; 00290 00291 } 00292 00293 00294 00295 // Operators. 00296 00297 00299 Synchronized & operator = ( const bool & value ) 00300 { 00301 00302 return setValue( value ) ; 00303 00304 } 00305 00306 00307 00309 operator bool () const volatile 00310 { 00311 00312 return _value ; 00313 00314 } 00315 00316 00317 00324 bool operator ++() 00325 { 00326 00327 _mutex.lock() ; 00328 _value = ! _value ; 00329 _mutex.unlock() ; 00330 00331 return _value ; 00332 00333 } 00334 00335 00336 00338 bool operator ++(int) 00339 { 00340 00341 return operator ++() ; 00342 00343 } 00344 00345 00346 00353 bool operator --() 00354 { 00355 00356 _mutex.lock() ; 00357 _value = ! _value ; 00358 _mutex.unlock() ; 00359 return _value ; 00360 00361 } 00362 00363 00364 00366 bool operator --(int) 00367 { 00368 00369 return operator --() ; 00370 00371 } 00372 00373 00374 00375 00376 private: 00377 00378 00380 volatile bool _value ; 00381 00382 00384 Mutex _mutex ; 00385 00386 00387 00393 Synchronized() ; 00394 00395 00396 00407 Synchronized( const Synchronized & source ) ; 00408 00409 00410 00419 Synchronized & operator = ( const Synchronized & source ) ; 00420 00421 00422 } ; 00423 00424 } 00425 00426 } 00427 00428 00429 00430 #endif // CEYLAN_SYNCHRONIZED_H_ 00431