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

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

namespace ZThread { 

/**
 * @class Lockable
 *
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:26:02-0500>
 * @version 1.2.1
 *
 * Abstract base class that defines a lockable interface. 
 * Concrete implementations of this class provide various 
 * synchronization strategies.
 *
 * Literature on this subject often refers to P() & V() functions;
 * these functions map to this class type as shown below.
 *
 * P() => acquire() : Acquire a lock
 * V() => release() : Release a lock
 *
 * The meaning of these phrases can vary from implementation to 
 * implementation. For instance, acquire a lock on a mutex means you
 * have exclusive control of that lock. Acquiring a lock on a semaphore
 * means you have acquired a permit and do not necessarily have exclusive
 * control of the object.
 *
 * All of these methods can possibly block the calling thread for some
 * period of time. This depends on the implementation, so you should 
 * assume that a call will block unless you know that the specific 
 * Lockable implementation won't block. 
 */
class Lockable {
  public:  

  /** 
   * V() - Release
   *
   * Release a lock. If an exception is thrown 
   * the state of this object is not altered. 
   *
   * @exception Synchronization_Exception - thrown if some error
   * occurs, see the concrete implementation documents for an accurate
   * description of the error
   *
   * @post - If no exception occurs, a lock is 'released ' by the 
   * calling thread.
   */    
  virtual void release() 
    throw(Synchronization_Exception) = 0;
  
  /** 
   * P() - Acquire
   *
   * Acquire a lock.
   *
   * @exception Synchronization_Exception - thrown if some error
   * occurs, see the concrete implementation documents for an accurate
   * description of the error
   *
   * @post - If no exception occurs, a lock is 'acquired' by the 
   * calling thread.
   */
  virtual void acquire() 
    throw(Synchronization_Exception) = 0;


  /** 
   * P() - Acquire
   *
   * Acquire a lock.
   *
   * @param unsigned long - amount of time (milliseconds) to spend in an 
   * attempt to acquire a lock.
   *
   * @return bool - success of operation, true if a lock was
   * acquired, false if it was not
   *
   * @exception Synchronization_Exception - thrown if some error
   * occurs, see the concrete implementation documents for an accurate
   * description of the error
   *
   * @post - If no exception occurs, a lock is 'acquired' by the 
   * calling thread.
   */
  virtual bool tryAcquire(unsigned long) 
    throw(Synchronization_Exception) = 0;

  };
  
}; 

#endif
