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

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

namespace ZThread {
  
class SemaphoreImpl;
 
/**
 * @class Semaphore
 *
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:29:01-0500>
 * @version 2.0.0a
 *
 * A semaphore is an owner-less lock. It can be used to control a range
 * or access to a set of elements. You create a Semaphore with an initial
 * count and a maximum count. A semaphore can be acquired until its count
 * drops to 0 - at which point it will block further attempts to acquire 
 * it. A semaphore is incremented until it hits the maximum, releasing
 * a blocked thread each time its count is placed above 0.
 *
 * A semaphore with a maximum of one is a binary semaphore and basically 
 * acts as a mutex. A mutex will be a little more efficient than a binary
 * semaphore in most cases.
 */
class ZTHREAD_API Semaphore : public Lockable {
  
  SemaphoreImpl* _impl;
  
  // Disable copy constructors
  Semaphore(const Semaphore&);
  Semaphore& operator=(const Semaphore&);
  
  public:
  
  /**
   * Create a new Semaphore. 
   *
   * @param count - initial count
   * @param maxCount - upper bound
   *
   * @exception Initialization_Exception - resource could not be acquired
   */
  Semaphore(int count = 1, unsigned int maxCount = 1) 
    throw(Synchronization_Exception);

  /**
   * Destroy the Semaphore
   */
  virtual ~Semaphore()
    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);

  /**
   * @see release()  - provided to reflect the traditional Semaphore
   * semantics
   */
  void post() 
    throw(Synchronization_Exception);
  
  /**
   * Current value of the semaphore
   *
   * @return int - 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 maximum number of milliseconds to block.
   * @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
   * Decrements the semaphore. If the count is 0 - block until its raised above 0.
   */
  virtual void acquire() 
    throw(Synchronization_Exception);

  /**
   * Increment the semaphore
   *
   * @exception InvalidOp_Exception - thrown if this would increase the
   * count beyond the maximum
   */
  virtual void release() 
    throw(Synchronization_Exception);

  
}; /* Semaphore */


};

#endif
