Drizzled Public API Documentation

cache.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 #include <drizzled/session/cache.h>
00022 
00023 #include <boost/foreach.hpp>
00024 #include <drizzled/current_session.h>
00025 #include <drizzled/plugin/authorization.h>
00026 #include <drizzled/session.h>
00027 #include <vector>
00028 
00029 namespace drizzled {
00030 namespace session {
00031 
00032 bool volatile Cache::_ready_to_exit= false;
00033 Cache::list Cache::cache;
00034 boost::mutex Cache::_mutex;
00035 boost::condition_variable Cache::_end;
00036 
00037 Cache::session_ptr Cache::find(const session_id_t &id)
00038 {
00039   boost::mutex::scoped_lock scopedLock(_mutex);
00040   BOOST_FOREACH(list::const_reference it, cache)
00041   {
00042     if (it->thread_id == id)
00043       return it;
00044   }
00045   return session_ptr();
00046 }
00047 
00048 void Cache::shutdownFirst()
00049 {
00050   boost::mutex::scoped_lock scopedLock(_mutex);
00051   _ready_to_exit= true;
00052 
00053   /* do the broadcast inside the lock to ensure that my_end() is not called */
00054   _end.notify_all();
00055 }
00056 
00057   /* Wait until cleanup is done */
00058 void Cache::shutdownSecond()
00059 {
00060   boost::mutex::scoped_lock scopedLock(_mutex);
00061 
00062   while (not _ready_to_exit)
00063   {
00064     _end.wait(scopedLock);
00065   }
00066 }
00067 
00068 size_t Cache::count()
00069 {
00070   boost::mutex::scoped_lock scopedLock(_mutex);
00071 
00072   return cache.size();
00073 }
00074 
00075 void Cache::insert(const session_ptr& arg)
00076 {
00077   boost::mutex::scoped_lock scopedLock(_mutex);
00078   cache.push_back(arg);
00079 }
00080 
00081 void Cache::erase(const session_ptr& arg)
00082 {
00083   list::iterator iter= std::find(cache.begin(), cache.end(), arg);
00084   assert(iter != cache.end());
00085   cache.erase(iter);
00086 }
00087 
00088 } /* namespace session */
00089 } /* namespace drizzled */