Drizzled Public API Documentation

scheduler.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 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 
00022 #include <algorithm>
00023 
00024 #include <drizzled/errmsg_print.h>
00025 #include <drizzled/gettext.h>
00026 #include <drizzled/plugin/scheduler.h>
00027 
00028 namespace drizzled
00029 {
00030 
00031 extern size_t my_thread_stack_size;
00032 
00033 std::vector<plugin::Scheduler *> all_schedulers;
00034 
00035 /* Globals (TBK) */
00036 static plugin::Scheduler *scheduler= NULL;
00037 
00038 
00039 class FindSchedulerByName : public std::unary_function<plugin::Scheduler *, bool>
00040 {
00041   const std::string *name;
00042 public:
00043   FindSchedulerByName(const std::string *name_arg)
00044     : name(name_arg) {}
00045   result_type operator() (argument_type sched)
00046   {
00047     return (bool)((name->compare(sched->getName()) == 0));
00048   }
00049 };
00050 
00051 
00052 bool plugin::Scheduler::addPlugin(plugin::Scheduler *sched)
00053 {
00054   std::vector<plugin::Scheduler *>::iterator iter=
00055     std::find_if(all_schedulers.begin(), all_schedulers.end(), 
00056             FindSchedulerByName(&sched->getName()));
00057 
00058   if (iter != all_schedulers.end())
00059   {
00060     errmsg_printf(error::ERROR,
00061                   _("Attempted to register a scheduler %s, but a scheduler "
00062                     "has already been registered with that name.\n"),
00063                     sched->getName().c_str());
00064     return true;
00065   }
00066 
00067   sched->deactivate();
00068   all_schedulers.push_back(sched);
00069 
00070   return false;
00071 }
00072 
00073 
00074 void plugin::Scheduler::removePlugin(plugin::Scheduler *sched)
00075 {
00076   all_schedulers.erase(std::find(all_schedulers.begin(),
00077                             all_schedulers.end(),
00078                             sched));
00079 }
00080 
00081 
00082 bool plugin::Scheduler::setPlugin(const std::string& name)
00083 {
00084   std::vector<plugin::Scheduler *>::iterator iter=
00085     std::find_if(all_schedulers.begin(), all_schedulers.end(), 
00086             FindSchedulerByName(&name));
00087 
00088   if (iter != all_schedulers.end())
00089   {
00090     if (scheduler != NULL)
00091       scheduler->deactivate();
00092     scheduler= *iter;
00093     scheduler->activate();
00094     return false;
00095   }
00096 
00097   errmsg_printf(error::WARN,
00098                 _("Attempted to configure %s as the scheduler, which did "
00099                   "not exist.\n"), name.c_str());
00100   return true;
00101 }
00102 
00103 
00104 plugin::Scheduler *plugin::Scheduler::getScheduler()
00105 {
00106   return scheduler;
00107 }
00108 
00109 } /* namespace drizzled */