/*
 *  ZThreads, a platform-independant, multithreading and 
 *  synchroniation library
 *
 *  Copyright (C) 2001 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
 */

#include "zthread/Thread.h"
#include <assert.h>

#ifdef _WIN32
#include "Win32_ThreadImpl.h"
#elif _POSIX
#include "POSIX_ThreadImpl.h"
#endif

using namespace ZThread;

FastThreadLocal<void*> Thread::_interruptKey;

/**
 * Create a new Thread, retreve and install an implementation obtained
 * from the SynchFactory
 */
Thread::Thread() 
  throw(Synchronization_Exception) {

  _impl = new ThreadImpl();
  _impl->acquire();

}

Thread::Thread(ThreadImpl* impl) : _impl(impl) {

  assert(_impl != 0);

  _impl->acquire();

}

/**
 * Destroy the Thread, release the underlying implementation
 * if appropriate to do so
 */
Thread::~Thread() 
  throw() {

  _impl->release();

}


/**
 * Make the currently executing thread block until this thread exits
 * 
 * @exception NotJoinable_Exception thrown if this thread is not 
 * joinable
 * @exception Synchronization_Exception - thrown on a general system error
 */
bool Thread::join(unsigned long timeout) 
  throw(Synchronization_Exception) {

  return _impl->join(timeout);

}

/**
 * Execute a Runnable object within the context of the Thread 
 * represented by this object
 */
void Thread::run() throw() {

  throw InvalidTask_Exception();

}

/**
 * Execute a Runnable object within the context of the Thread 
 * represented by this object
 */
void Thread::start() 
  throw (Synchronization_Exception) {

  _impl->run(this);

}

/**
 * Execute a Runnable object within the context of the Thread 
 * represented by this object
 */
void Thread::run(Runnable *task) 
  throw (Synchronization_Exception) {

  if(task == 0) {
    ZTDEBUG("Invalid task!\n");
    throw InvalidOp_Exception("Invalid task");
  }

  _impl->run(task);

}


/**
 * Kill a thread - this should not be normally used. Perhaps the only 
 * valid use of this method is to stop a thread that is causing execeptions
 * on every operation due to an unexpected problem in the system.
 *
 * - This throws no execeptions, returns no values. Use with caution or
 *   better yet create a design that will not necesitate the use of this
 *   function.
 *
 * This function was added mainly for completeness
 */
void Thread::kill() 
  throw(Synchronization_Exception) {

  _impl->kill();

}

/**
 * Get a Thread object that represents the current thread, this should
 * not be used to execute an Runnable objects - but rather used to test the
 * interrupt conditon
 *
 * Thread object representing the current thread.
 */
Thread Thread::current() 
  throw(Synchronization_Exception) {

  return Thread( ThreadImpl::current() );

}



void Thread::setPriority(Thread::PRIORITY n)
  throw(Synchronization_Exception) {

  _impl->setPriority(n);

}


Thread::PRIORITY Thread::getPriority()
  throw(Synchronization_Exception) {

  return _impl->getPriority();

}

  
/**
 * Interupt any threads waiting at an interruptable waiting point
 */
void Thread::interrupt() 
  throw(Synchronization_Exception) {

  _impl->interrupt();

}
  
/**
 * Check to see if this thread was interrupted
 *
 * @return bool  - true/false flag for interrupted status
 */
bool Thread::isInterrupted()
  throw() {

  return _impl->isInterrupted();

}

/**
 */
bool Thread::isDaemon()
  throw() {

  return _impl->isDaemon();

}

/**
 */
bool Thread::isKilled()
  throw() {

  return _impl->isKilled();

}

void Thread::setDaemon(bool flag)
  throw(Synchronization_Exception) {

  _impl->setDaemon(flag);

}


void Thread::setNotify(bool flag)
  throw(Synchronization_Exception) {

  _impl->setNotify(flag);

}

bool Thread::wantNotify()
  throw() {

  return _impl->wantNotify();

}



void Thread::sleep(unsigned long ms) 
 throw(Synchronization_Exception) {

  ThreadImpl::sleep(ms);

}


void Thread::yield() 
  throw() {

  ThreadImpl::yield();

}
