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

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

namespace ZThread {
  
class ConditionImpl;

/**
 * @class Condition
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:23:05-0500>
 * @version 2.0.0a
 *
 * Condition variables can be used to block threads until a condition has 
 * been signaled. This is very much like a POSIX condition variable.
 * Threads wait() ing on this object are blocked until they receive signal.
 *
 * A Condition variable must be used in conjunction with a Lockable object
 * The lockable object should be locked prior to calling one of the wait()
 * functions. That object will be released while the caller is blocked. When
 * the thread that has been blocked by the condition receives a signal that
 * lockable object will be locked once again before the wait() returns.
 *
 */
class ZTHREAD_API Condition {
  
  // Disable copy constructors
  Condition(const Condition&);
  Condition& operator=(const Condition&);
  
  ConditionImpl* _impl;
  
  public:
  
  /**
   * Create a new condition variable
   *
   * @exception Initialization_Exception thrown if resources to 
   * allocate the object could not be allocated
   */
  Condition() 
    throw(Synchronization_Exception);

  /**
   * Destroy Condition object
   */
  ~Condition()
    throw(); 
  
  /**
   * Wake ONE thread waiting on this condition variable.
   */
  void signal() 
    throw(Synchronization_Exception);

  /**
   * Wake ALL threads waiting on this condition variable.
   */
  void broadcast() 
    throw(Synchronization_Exception);


  /**
   * Wait for this Condition object to be signaled.
   *
   * @return bool indication the success of this operation
   *
   * @param Lockable& reference to a locked Lockable object
   *
   * @exception Interrupted_Exception thrown when the callers state is INTERRUPTED
   * @exception Unexpected_Exception thrown when the callers state is KILLED
   * @exception InvalidOp_Exception thrown when the callers state is not RUNNING
   * @exception Synchronization_Exception thrown when the predicate lock throws
   */
  void wait(Lockable&) 
    throw(Synchronization_Exception);   

  /**
   * Wait for this Condition object to be signaled for at most 
   * 'ms' milliseconds. 
   *
   * @return bool indicating the success of this operation
   *
   * @param Lockable& reference to a locked Lockable object
   * @param timeout maximum milliseconds to block.
   *
   * @exception Interrupted_Exception thrown when the callers state is INTERRUPTED
   * @exception Unexpected_Exception thrown when the callers state is KILLED
   * @exception InvalidOp_Exception thrown when the callers state is not RUNNING
   * @exception Synchronization_Exception thrown when the predicate lock throws
   * @param unsigned long milliseconds to wait on for a signal()
   */
  bool wait(Lockable&, unsigned long) 
    throw(Synchronization_Exception);
  
}; /* Condition */
  
};

#endif
