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

#include "zthread/Thread.h"
#include "zthread/SynchronizationExceptions.h"
#include "zthread/FastThreadLocal.h"

#include "WIN32_Monitor.h"
#include "State.h"
#include "StdAfx.h"
#include <vector>

namespace ZThread {

class AbstractThreadLocal;
class ValueHolder;

/**
 * @class ThreadImpl
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-03-10T10:49:54-0500>
 * @version 2.0.5
 *
 * This class is the abstraction used to represent and manage an actual thread
 * in the current process. 
 */
class ThreadImpl {

  typedef std::vector<ValueHolder*> KeyList;

  typedef struct params_t {

    ThreadImpl* parent;
    ThreadImpl* child;
    Runnable* task;

    params_t(ThreadImpl* p, ThreadImpl* c, Runnable* t) :
      parent(p), child(c), task(t) { }

    private: 

    // disable copy operators
    params_t(const params_t&);
    params_t operator=(const params_t&);

  } Params;

  public:

  typedef Monitor<State> MONITOR;
  
  ThreadImpl() 
    throw(Synchronization_Exception);
  
  ~ThreadImpl() throw();  

  void acquire() throw();

  bool release();
  
  /**
   * Get a reference to the state event used to control
   * this thread.
   *
   * @return MONITOR&
   */
  inline MONITOR& getMonitor() {
    return _monitor;
  }

  void kill()
    throw(Synchronization_Exception);
  
  bool join(unsigned long) 
    throw(Synchronization_Exception);
  
  void run(Runnable *target) 
    throw(Synchronization_Exception);
  
  void interrupt() 
    throw(Synchronization_Exception);
  
  void setPriority(Thread::PRIORITY)
    throw(Synchronization_Exception);

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

  bool isDaemon() throw();

  bool isInterrupted() throw();

  bool isKilled() throw();

  bool wantNotify() throw();

  void setDaemon(bool) 
    throw(Synchronization_Exception);

  void setNotify(bool) 
    throw(Synchronization_Exception);

  static void sleep(unsigned long) 
    throw(Synchronization_Exception);

  static void yield() throw();
  
  static ThreadImpl* current() throw(Synchronization_Exception);


  public:

  /**
   * Obtain the thread handle for a valid thread. If the thread 
   * represented by this object has been joined, or doesn't exist 
   * then this value will be invalid.
   *
   * @return HANDLE
   */
  inline operator HANDLE() {
    return _hThread;
  }


  ValueHolder* 
    createValueHolder(const AbstractThreadLocal* tls, void* value, bool inheritable=false);

  private:

  //! Create a Monitor
  MONITOR _monitor;

  //! Current joiner, if any.
  ThreadImpl* _joiner;

  //! TID while the thread is executing.
  DWORD _tid;
  
  //! HANDLE for the thread
  HANDLE _hThread;

  //! Reference count
  int _count;

  //! Lists of ValueHolders for ThreadLocals used by this thread. 
  KeyList _keys;
  KeyList _inheritableKeys;

  //! Dispatch for each new thread
  static unsigned int __stdcall _dispatch(void *arg);

  //! ThreadLocal to store implementation to current thread mapping.
  static FastThreadLocal<ThreadImpl*> _threadMap;

}; /* ThreadImpl */

struct InvalidTask_Exception {};

};

#endif
