/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
#ifndef NMM_WORKERTHREAD_HPP
#define NMM_WORKERTHREAD_HPP

#include "nmm/nmm_prefix.hpp"

#include "ThreadedObject.hpp"
#include "ThreadMutex.hpp"
#include "ThreadCondition.hpp"
#include "Task.hpp"

#include "visibility.hpp"

namespace NMM {
  /** Provides threads for ThreadPool. 
   *    
   */  
  class DLLEXPORT WorkerThread : public ThreadedObject
  {
  public:
   /** Constructor
    */  
    WorkerThread();

   /** Destructor 
    */  
    ~WorkerThread();

   /** Sets a Task that should run in its own thread
    *
    * @param task 
    */  
    void setTask(Task* task) { __current_task = task; }

   /** Iterates a task obbject. 
    *
    * @param count Specifies the number of iterations for the task that has been set by method setTask 
    */  
    void iterateTask(int count = 1);

   /** Returns the number of iterations that has to be done. 
    *
    * @returns number of iterations that has to be done. 
    */  
    int getIterationCount() const { return __iteration_count; }

   /** Returns the relase flag. 
    * Returns true if an instance of this class notifies ThreadPool if all iterations for a 
    * certain task has been executed and the instance can be reused by ThreadPool. Otherwise this method returns false.
    * 
    * @return true if an instance of this class notifies ThreadPool if all iterations for a 
    * certain task has been executed and the instance can be reused by ThreadPool. Otherwise this method returns false.
    */  
    bool getReleaseFlag()   const { return __release_flag; }

   /** Sets the release flag. 
    *
    * @param release_flag If true, an instance of this class notifies ThreadPool if all iterations for a 
    * certain task has been executed and the instance can be reused by ThreadPool. 
    */  
    void setReleaseFlag( bool release_flag ) { __release_flag = release_flag; }
  protected:

   /** Executes a task in its own thread
    */  
    void run();
  private:
    Task*             __current_task;
    ThreadMutex       __thread_mutex;
    ThreadCondition   __thread_condition;
    int               __iteration_count;
    bool              __is_running;
    bool              __release_flag;
  };

}


#include "nmm/nmm_suffix.hpp"

#endif // NMM_WORKERTHREAD_HPP
