00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- 00002 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: 00003 * 00004 * Copyright (C) 2010 Brian Aker 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 are met: 00009 * * Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * * Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * * Neither the name of the <organization> nor the 00015 * names of its contributors may be used to endorse or promote products 00016 * derived from this software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00019 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00020 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00021 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 00022 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00023 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00024 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00025 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00026 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00027 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00028 * 00029 */ 00030 00031 #include <boost/thread/mutex.hpp> 00032 #include <boost/thread/condition_variable.hpp> 00033 #include <boost/shared_ptr.hpp> 00034 00035 #pragma once 00036 00037 namespace user_locks { 00038 00039 class Observer { 00040 public: 00041 typedef boost::shared_ptr<Observer> shared_ptr; 00042 typedef std::list <shared_ptr> list; 00043 00044 Observer(int64_t wait_until_arg) : 00045 woken(false), 00046 waiting_for(wait_until_arg) 00047 { 00048 _mutex.lock(); 00049 } 00050 00051 void sleep() 00052 { 00053 while (not woken) 00054 { 00055 cond.wait(_mutex); 00056 } 00057 _mutex.unlock(); 00058 } 00059 00060 void wake() 00061 { 00062 { 00063 boost::mutex::scoped_lock mutex; 00064 woken= true; 00065 } 00066 cond.notify_all(); 00067 } 00068 00069 ~Observer() 00070 { 00071 } 00072 00073 int64_t getLimit() const 00074 { 00075 return waiting_for; 00076 } 00077 00078 private: 00079 bool woken; 00080 const int64_t waiting_for; 00081 boost::mutex _mutex; 00082 boost::condition_variable_any cond; 00083 }; 00084 00085 } // namespace user_locks 00086