/*
 *  ZThreads, a platform-independant, multithreading and 
 *  synchroniation library
 *
 *  Copyright (C) 2000-2002, Eric Crahen, See LGPL.TXT for details
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 *  SUNY @ Buffalo, hereby disclaims all copyright interest in the
 *  ZThreads library written by Eric Crahen
 */

#ifndef __ZTABSTRACTEXECUTOR_H__
#define __ZTABSTRACTEXECUTOR_H__

#include "zthread/ZThread.h"
#include "zthread/Executor.h"
#include "zthread/MonitoredQueue.h"
#include "zthread/Mutex.h"

namespace ZThread {

/**
 * @class AbstractExecutor
 *
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:21:11-0500>
 * @version 1.3.0
 *
 * This template encapsulates the common behavior of threaded executors.
 * It provides a means to select a locking mechananism & task queue class
 * via template parameters.
 *
 * Any Executor can be extended and can override the createWorker() function to
 * alter how the Work threads behave.
 */
template <class L = Mutex, class Q = MonitoredQueue<Runnable*> >
class ZTHREAD_API AbstractExecutor : public Executor {
  private:
  
  // Restricted constructors & operators
  AbstractExecutor(AbstractExecutor*);
  AbstractExecutor(const AbstractExecutor&);
  AbstractExecutor& operator=(const AbstractExecutor&);
  
  protected:

  //! Typedef
  typedef Q QUEUE;
  //! Typedef
  typedef L LOCK;

  //! TaskQueue
  QUEUE _queue;
  //! Serialize access
  LOCK _lock;

  /**
   * Protected default constructor
   */
  AbstractExecutor() throw() {}

  /**
   * Create a new worker for the Executor, allows extedned classes 
   * to more easily special Worker objects w/o reimplementing the
   * entire executor.
   */
  virtual Thread* createWorker() = 0;

  public:

  /**
   * Destroy this Executor
   */
  virtual ~AbstractExecutor() throw() {}

  
  /**
   * Stop the execution of additional tasks by this executor.
   * After this method has be invoked then the Executor will
   * completed when the last task it was previously assigned is 
   * completed. 
   *
   * @exception InterruptedException 
   * 
   * @post the executor no longer accepts new tasks. At this point
   * the executor has a limited lifetime and is safe to join
   *
   * @see Executor::cancel()
   */
  inline virtual void cancel() throw(Synchronization_Exception) {
    _queue.cancel();
  }


  /**
   * Check the cancelation status of this Executor
   * 
   * @return bool - Executor canceled
   * @see Cancelable::isCanceled()
   */
  inline virtual bool isCanceled() throw(Synchronization_Exception) {
    return _queue.isCanceled();
  }

  /**
   * Block until this executor has become available to run new tasks.
   *
   * This does not guarantee that another thread won't have submitted a
   * task before some action is taken in response to this. However, it
   * is useful as a general heuristic
   *
   * @return boolean
   * @exception InterruptedException 
   */
  inline virtual bool available() throw(Synchronization_Exception) {
    return _queue.empty();
  }

};

    
};

#endif


