Drizzled Public API Documentation

barriers.cc
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  *
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; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <plugin/user_locks/module.h>
00023 
00024 #include <boost/thread/locks.hpp>
00025 
00026 #include <string>
00027 
00028 namespace user_locks {
00029 namespace barriers {
00030 
00031 bool Barriers::create(const user_locks::Key &arg, drizzled::session_id_t owner)
00032 {
00033   boost::unique_lock<boost::mutex> scope(mutex);
00034   return barrier_map.insert(std::make_pair(arg, new Barrier(owner))).second;
00035 }
00036 
00037 bool Barriers::create(const user_locks::Key &arg, drizzled::session_id_t owner, int64_t wait_count)
00038 {
00039   boost::unique_lock<boost::mutex> scope(mutex);
00040   return barrier_map.insert(std::make_pair(arg, new Barrier(owner, wait_count))).second;
00041 }
00042 
00043 /*
00044   @note return
00045 
00046   true -> release happened.
00047   false -> no release, we were not the owner
00048   indeterminate -> barrier was not found.
00049 
00050 */
00051 return_t Barriers::release(const user_locks::Key &arg, drizzled::session_id_t owner)
00052 {
00053   boost::unique_lock<boost::mutex> scope(mutex);
00054   Map::iterator iter= barrier_map.find(arg);
00055 
00056   // Nothing is found
00057   if ( iter == barrier_map.end())
00058     return NOT_FOUND;
00059 
00060   if (not iter->second->getOwner() == owner)
00061     return NOT_OWNED_BY;
00062 
00063   iter->second->signal(); // We tell anyone left to start running
00064   (void)barrier_map.erase(arg);
00065 
00066   return SUCCESS;
00067 }
00068 
00069 Barrier::shared_ptr Barriers::find(const user_locks::Key &arg)
00070 {
00071   boost::unique_lock<boost::mutex> scope(mutex);
00072   Map::iterator iter= barrier_map.find(arg);
00073 
00074   if (iter != barrier_map.end())
00075     return iter->second;
00076 
00077   return Barrier::shared_ptr();
00078 }
00079 
00080 void Barriers::Copy(Map &arg)
00081 {
00082   boost::unique_lock<boost::mutex> scope(mutex);
00083   arg= barrier_map;
00084 }
00085 
00086 } /* namespace barriers */
00087 } /* namespace user_locks */