solarpowerlog trunk
/home/tobi/workspace/solarpowerlog/src/interfaces/CWorkScheduler.cpp
Go to the documentation of this file.
00001 
00002 /* ----------------------------------------------------------------------------
00003  solarpowerlog
00004  Copyright (C) 2009  Tobias Frost
00005 
00006  This file is part of solarpowerlog.
00007 
00008  Solarpowerlog is free software; However, it is dual-licenced
00009  as described in the file "COPYING".
00010 
00011  For this file (CWorkScheduler.cpp), the license terms are:
00012 
00013  You can redistribute it and/or modify it under the terms of the GNU
00014  General Public License as published by the Free Software Foundation; either
00015  version 3 of the License, or (at your option) any later version.
00016 
00017  This program is distributed in the hope that it will be useful, but
00018  WITHOUT ANY WARRANTY; without even the implied warranty of
00019  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  Lesser General Public License for more details.
00021 
00022  You should have received a copy of the GNU Library General Public
00023  License along with this proramm; if not, see
00024  <http://www.gnu.org/licenses/>.
00025  ----------------------------------------------------------------------------
00026  */
00033 #ifdef HAVE_CONFIG_H
00034 #include "config.h"
00035 #endif
00036 
00037 #include <boost/version.hpp>
00038 // exception.hpp is deprecated since 1.40
00039 #if BOOST_VERSION / 100 % 1000 < 40
00040 #include <boost/exception.hpp>
00041 #else
00042 #include <boost/exception/all.hpp>
00043 #endif
00044 
00045 #include <boost/bind.hpp>
00046 #include <boost/thread.hpp>
00047 
00048 #include "CWorkScheduler.h"
00049 
00050 #include "patterns/ICommand.h"
00051 
00052 #include "CTimedWork.h"
00053 
00054 #include "semaphore.h"
00055 #include "interfaces/CMutexHelper.h"
00056 
00057 using namespace std;
00058 
00059 CWorkScheduler::CWorkScheduler()
00060 {
00061      sem_init(&semaphore, 0, 0);
00062 
00063      // generate thread for the timed work facility.
00064      timedwork = new CTimedWork(this);
00065      timedwork->run();
00066 }
00067 
00068 CWorkScheduler::~CWorkScheduler()
00069 {
00070      delete timedwork;
00071 }
00072 
00073 bool CWorkScheduler::DoWork( bool block )
00074 {
00075      if (!block) {
00076           CMutexAutoLock(&(this->mut));
00077           if (CommandsDue.empty()) {
00078                return false;
00079           }
00080      }
00081 
00082      sem_wait(&semaphore);
00083      ICommand *cmd = getnextcmd();
00084      cmd->execute();
00085      delete cmd;
00086      return true;
00087 }
00088 
00089 ICommand *CWorkScheduler::getnextcmd( void )
00090 {
00091      // Obtain Mutex to make sure...
00092      CMutexAutoLock CMutexHelper(&(this->mut));
00093      ICommand *cmd = CommandsDue.front();
00094      CommandsDue.pop_front();
00095      return cmd;
00096 }
00097 
00098 void CWorkScheduler::ScheduleWork( ICommand *Command )
00099 {
00100      CMutexAutoLock CMutexHelper(&(this->mut));
00101      CommandsDue.push_back(Command);
00102      sem_post(&semaphore);
00103 }
00104 
00105 void CWorkScheduler::ScheduleWork( ICommand *Command, struct timespec ts )
00106 {
00107      timedwork->ScheduleWork(Command, ts);
00108 }
00109