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

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

namespace ZThread { 
  
class MutexImpl;

/**
 * @class Mutex
 *
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-17T22:50:15-0500>
 * @version 2.0.0a
 *
 * A Mutex is a simple lock that can be held by one thread at any given time. If
 * two or more threads attempt to acquire() this Mutex, only one will be successful.
 * The others will be blocked until the thread that acquire()d the Mutex release()es
 * it.
 */
class ZTHREAD_API Mutex : public Lockable {
  
  MutexImpl* _impl;
  
  // Disable copy constructors
  Mutex(const Mutex&);
  Mutex& operator = (const Mutex&);
  
  public:

  /**
   * Create a new Mutex.
   *
   * @exception Initialization_Exception - thrown if there are not
   * enough available resources
   */
  Mutex() 
    throw(Synchronization_Exception);

  /**
   * Destroy this Mutex.
   */
  virtual ~Mutex()
    throw();
  
  /**
   * Lock the mutex and prevent re-entrance on the protected block of code.
   * Calling thread is blocked until the Mutex is released by the thread 
   * currently holding its lock. Only one thread may acquire() the lock at 
   * any time.
   *
   * Acquiring this mutex twice will result in deadlock. 
   *
   * see Lockable::acquire()
   *
   * @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);

  /**
   * Lock the mutex and prevent re-entrance on the protected block of code.
   * If the given amount of time expires or the mutex is locked this function
   * returns.
   *
   * Acquiring this mutex twice will result in deadlock. 
   *
   * see Lockable::tryAcquire()
   *
   * @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
   *
   * @return bool indicating the success of the lock attempt
   */
  virtual bool tryAcquire(unsigned long) 
    throw(Synchronization_Exception);
  
  /**
   * Unlock the mutex allowing another thread to acquire() it.
   *
   * see Lockable::release()
   *
   * @exception InvalidOp_Exception - thrown if there is an attempt to release this
   * mutex from a thread that had not previously acquire()d the mutex. 
   */
  virtual void release() 
    throw(Synchronization_Exception);
  
}; /* Mutex */


};

#endif
