00001 // 00002 // AtomicCounter.cpp 00003 // 00004 // $Id: //poco/1.3/Foundation/src/AtomicCounter.cpp#3 $ 00005 // 00006 // Library: Foundation 00007 // Package: Core 00008 // Module: AtomicCounter 00009 // 00010 // Copyright (c) 2009, Applied Informatics Software Engineering GmbH. 00011 // and Contributors. 00012 // 00013 // Permission is hereby granted, free of charge, to any person or organization 00014 // obtaining a copy of the software and accompanying documentation covered by 00015 // this license (the "Software") to use, reproduce, display, distribute, 00016 // execute, and transmit the Software, and to prepare derivative works of the 00017 // Software, and to permit third-parties to whom the Software is furnished to 00018 // do so, all subject to the following: 00019 // 00020 // The copyright notices in the Software and this entire statement, including 00021 // the above license grant, this restriction and the following disclaimer, 00022 // must be included in all copies of the Software, in whole or in part, and 00023 // all derivative works of the Software, unless such copies or derivative 00024 // works are solely in the form of machine-executable object code generated by 00025 // a source language processor. 00026 // 00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00033 // DEALINGS IN THE SOFTWARE. 00034 // 00035 00036 00037 #include "Poco/AtomicCounter.h" 00038 00039 00040 namespace Poco { 00041 00042 00043 #if POCO_OS == POCO_OS_WINDOWS_NT 00044 // 00045 // Windows 00046 // 00047 AtomicCounter::AtomicCounter(): 00048 _counter(0) 00049 { 00050 } 00051 00052 00053 AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue): 00054 _counter(initialValue) 00055 { 00056 } 00057 00058 00059 AtomicCounter::AtomicCounter(const AtomicCounter& counter): 00060 _counter(counter.value()) 00061 { 00062 } 00063 00064 00065 AtomicCounter::~AtomicCounter() 00066 { 00067 } 00068 00069 00070 AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter) 00071 { 00072 InterlockedExchange(&_counter, counter.value()); 00073 return *this; 00074 } 00075 00076 00077 AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value) 00078 { 00079 InterlockedExchange(&_counter, value); 00080 return *this; 00081 } 00082 00083 00084 #elif POCO_OS == POCO_OS_MAC_OS_X 00085 // 00086 // Mac OS X 00087 // 00088 AtomicCounter::AtomicCounter(): 00089 _counter(0) 00090 { 00091 } 00092 00093 00094 AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue): 00095 _counter(initialValue) 00096 { 00097 } 00098 00099 00100 AtomicCounter::AtomicCounter(const AtomicCounter& counter): 00101 _counter(counter.value()) 00102 { 00103 } 00104 00105 00106 AtomicCounter::~AtomicCounter() 00107 { 00108 } 00109 00110 00111 AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter) 00112 { 00113 _counter = counter.value(); 00114 return *this; 00115 } 00116 00117 00118 AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value) 00119 { 00120 _counter = value; 00121 return *this; 00122 } 00123 00124 00125 #else 00126 // 00127 // Generic implementation based on FastMutex 00128 // 00129 AtomicCounter::AtomicCounter() 00130 { 00131 _counter.value = 0; 00132 } 00133 00134 00135 AtomicCounter::AtomicCounter(AtomicCounter::ValueType initialValue) 00136 { 00137 _counter.value = initialValue; 00138 } 00139 00140 00141 AtomicCounter::AtomicCounter(const AtomicCounter& counter) 00142 { 00143 _counter.value = counter.value(); 00144 } 00145 00146 00147 AtomicCounter::~AtomicCounter() 00148 { 00149 } 00150 00151 00152 AtomicCounter& AtomicCounter::operator = (const AtomicCounter& counter) 00153 { 00154 FastMutex::ScopedLock lock(_counter.mutex); 00155 _counter.value = counter.value(); 00156 return *this; 00157 } 00158 00159 00160 AtomicCounter& AtomicCounter::operator = (AtomicCounter::ValueType value) 00161 { 00162 FastMutex::ScopedLock lock(_counter.mutex); 00163 _counter.value = value; 00164 return *this; 00165 } 00166 00167 00168 #endif // POCO_OS 00169 00170 00171 } // namespace Poco