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

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

namespace ZThread { 
  
class MutexImpl;

/**
 * @class RecursiveMutex
 *
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-03-02T20:49:38-0500>
 * @version 2.0.0a
 *
 * A RecursiveMutex can be acquire()d and release()d more than once 
 * by the same thread, instead of causing a Deadlock_Exception. The
 * RecursiveMutex should be release()d as many times as it was acquired().
 */
class ZTHREAD_API RecursiveMutex : public Lockable {
    
  MutexImpl* _impl;
  
  // Disable copy constructors
  RecursiveMutex(const RecursiveMutex&);
  RecursiveMutex& operator=(const RecursiveMutex&);
  
  public:

  /**
   * Create a new RecursiveMutex.
   *
   * @exception Initialization_Exception - thrown if resources
   * could not be allocated
   */
  RecursiveMutex() 
    throw(Synchronization_Exception);

  /**
   * Destroy this RecursiveMutex.
   */
  virtual ~RecursiveMutex()
    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
   *
   * @see Lockable::acquire()
   *
   * @exception Unexpected_Exception - the calling thread was killed.
   * @exception Interrupted_Exception - thrown if the calling thread was
   * interrupt()ed.
   * @exception InvalidOp_Exception - thrown if the calling thread is not
   * in not in a valid state.
   */ 
  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
   *
   * see Lockable::tryAcquire()
   *
   * @exception Unexpected_Exception - the calling thread was killed.
   * @exception Interrupted_Exception - thrown if the calling thread was
   * interrupt()ed.
   * @exception InvalidOp_Exception - thrown if the calling thread is not
   * in not in a valid state.
   *
   * @return bool indicating the success of the lock attempt
   */ 
  virtual bool tryAcquire(unsigned long) 
    throw(Synchronization_Exception);

  
  /**
   * Unlock the mutex and prevent re-entrance on the protected block of code.
   * This needs to be called for each acquire() that succeeded.
   *
   * 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. Or when the 
   * mutex is release()d more times than it has been acquire()d.
   */
  virtual void release() 
    throw(Synchronization_Exception);
  
}; /* RecursiveMutex */
  
};

#endif
