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

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

namespace ZThread {
  
class SemaphoreImpl;
  
/**
 * @class CountingSemaphore
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:23:26-0500>
 * @version 2.0.0a
 *
 * Very similar to the Semaphore object. The difference here is that
 * there is no upper bound on this Semaphore and it will not throw
 * exceptions because a range has been exceeded
 *
 * @see Semaphore
 */
class ZTHREAD_API CountingSemaphore : public Lockable {
  
  SemaphoreImpl* _impl;
  
  // Disable copy constructors
  CountingSemaphore(const CountingSemaphore&);
  CountingSemaphore& operator = (const CountingSemaphore&);
  
  public:

  /**
   * Create a new semaphore of a given size with a given count
   *
   * @param initialCount initial count to assign this semaphore
   */ 
  CountingSemaphore(int initialCount = 0) 
    throw(Synchronization_Exception);

  /**
   * Destroy this Semaphore
   */
  virtual ~CountingSemaphore() throw();

  /**
   * @see acquire() - provided to reflect the traditional Semaphore
   * semantics
   */ 
  void wait() 
    throw(Synchronization_Exception);

  /**
   * @see tryAcquire() - provided to reflect the traditional Semaphore
   * semantics
   */
  bool tryWait(unsigned long) 
    throw(Synchronization_Exception);


  /**
   * This method will increment the semaphore count by one, if any
   * threads were wait()ing on this semaphore - then one will be 
   * unblocked.
   *
   * An exception will not be thrown because a range has been exceeded
   * 
   */
  void post() 
    throw(Synchronization_Exception);
  
  /**
   * Current count of this Semaphore
   *
   * @return int - current count
   */
  virtual int count() 
    throw();    
  
  /**
   * Decrement the count, blocking when that count becomes 0 or less until it 
   * is raised about 0 again.
   *
   * @param timeout milliseconds
   * @return bool true if the acquire() suceeded before the timeout.
   *
   * @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
   */
  virtual bool tryAcquire(unsigned long) 
    throw(Synchronization_Exception);

  /**
   * Decrement the count, blocking when that count becomes 0 or less until it 
   * is raised about 0 again.
   *
   * @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
   */
  virtual void acquire() 
    throw(Synchronization_Exception);

  /**
   * Increment the semaphore. 
   */
  virtual void release() 
    throw(Synchronization_Exception);

  
}; /* CountingSemaphore */


};

#endif
