$search
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Willow Garage nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 #ifndef NETFT_ETHERCAT_HARDWARE__THREAD_SAFE_DOUBLE_BUFFER 00036 #define NETFT_ETHERCAT_HARDWARE__THREAD_SAFE_DOUBLE_BUFFER 00037 00038 #include <boost/thread/mutex.hpp> 00039 00040 namespace netft_ethercat_hardware 00041 { 00042 00043 template<class T> 00044 class ThreadSafeDoubleBuffer 00045 { 00046 public: 00047 ThreadSafeDoubleBuffer(); 00048 ThreadSafeDoubleBuffer(const T &initial_value); 00049 00050 void put(const T &value); 00051 void get(T &value); 00052 00053 protected: 00054 T v1_, v2_; 00055 boost::mutex mutex1_; 00056 boost::mutex mutex2_; 00057 unsigned blocked_counter_; 00058 }; 00059 00060 00061 template<class T> 00062 ThreadSafeDoubleBuffer<T>::ThreadSafeDoubleBuffer() : blocked_counter_(0) 00063 { 00064 // empty 00065 } 00066 00067 template<class T> 00068 ThreadSafeDoubleBuffer<T>::ThreadSafeDoubleBuffer(const T &initial_value) : 00069 blocked_counter_(0), v1_(initial_value), v2_(initial_value) 00070 { 00071 // empty 00072 } 00073 00074 template<class T> 00075 void ThreadSafeDoubleBuffer<T>::put(const T &value) 00076 { 00077 // Copy of value into both buffers, only hold lock to one buffer at a time 00078 { boost::unique_lock<boost::mutex> lock(mutex1_); 00079 v1_ = value; 00080 } 00081 { boost::unique_lock<boost::mutex> lock(mutex2_); 00082 v2_ = value; 00083 } 00084 } 00085 00086 template<class T> 00087 void ThreadSafeDoubleBuffer<T>::get(T &value) 00088 { 00089 // Try getting lock to one of the buffer's 00090 if (mutex1_.try_lock()) 00091 { 00092 value = v1_; 00093 mutex1_.unlock(); 00094 return; 00095 } 00096 if (mutex2_.try_lock()) 00097 { 00098 value = v2_; 00099 mutex2_.unlock(); 00100 return; 00101 } 00102 // OK, block waiting for buffer to open-up 00103 { boost::unique_lock<boost::mutex> lock(mutex1_); 00104 v1_ = value; 00105 ++blocked_counter_; 00106 } 00107 } 00108 00109 } //end namespace netft_ethercat_hardware 00110 00111 00112 #endif // NETFT_ETHERCAT_HARDWARE__THREAD_SAFE_DOUBLE_BUFFER