/*
 *  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 __ZTTIMERTHREAD_H__
#define __ZTTIMERTHREAD_H__

#include "zthread/ZThread.h"
#include "zthread/Timer.h"

namespace ZThread {

/**
 * @class TimerThread
 *
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:31:54-0500>
 * @version 1.5.0
 *
 * This is a Timer object that is based on a Thread. This template
 * can be used to manage the lifetime of the thread correctly. A 
 * static TimerThread should be labeled as a daemon thread using the
 * parameter template. This is very similar to how the Executor
 * templates work.
 *
 * Also, like Executors, static TimerThreads do no need to be joined 
 * while a non-static or local TimerThread should be joined. 
 */
template <bool DAEMON> 
class ZTHREAD_API TimerThread : public Timer, public Thread {
  
  bool _canceled;

 public:

  /**
   * Create a new TimerThread
   */
  TimerThread()
    throw(Synchronization_Exception) {

    if(DAEMON)
      setDaemon(true);

  }

  /**
   * Destroy this TimerThread
   */
  virtual ~TimerThread() throw() { }

  /**
   * Cancel the timer altogether. No further notifications are issued from the
   * timer thread.
   *
   * @exception Synchronization_Exception
   */
  virtual void cancel()
    throw(Synchronization_Exception) {

    _canceled = true;
    interrupt();

  }

  /**
   * Check if the Timer has been Canceled
   *
   * @return bool - cancelation status
   *
   * @exception Synchronization_Exception
   */
  inline virtual bool isCanceled()
    throw(Synchronization_Exception) {

    return _canceled;

  }

  /**
   * Delegate to timer 
   */
  inline virtual void run() throw() {
    Timer::run();
  }

};

}; 

#endif

