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

#if !defined(_POSIX)
#error This implementation is specific to POSIX
#endif

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

#include <pthread.h>

namespace ZThread {

/**
 * @class FastRecursiveMutex
 *
 * @author Eric Crahen <crahen@cse.buffalo.edu>
 * @date <2002-02-24T09:14:52-0500>
 * @version 2.0.0a
 *
 * This is the smaller and faster implementation of a RecursiveMutex. 
 * A single thread can acquire the mutex any number of times, but it
 * must perform a release for each acquire(). Other threads are blocked
 * until a thread has released all of its locks on the mutex.
 * 
 * This particular implementation performs fewer safety checks. Like 
 * the FastMutex implementation, any waiting caused by an acquire() request 
 * is not interruptable. This is so that the mutex can have the fastest
 * response time for a time critical application while still having a good
 * degree of reliability.
 *
 * Linux will use a a native recursive mutex, other systems will use the
 * 2 mutex scheme
 */ 
class FastRecursiveMutex : public Lockable {
    
  //! Lock
  pthread_mutex_t _mtx;

#ifndef __linux__

  //! Serialize state
  pthread_mutex_t _lock;

  //! Owner
  pthread_t _owner;

  //! Count
  int _count;

#else

  //! Static mutex attribute initializer
  static const pthread_mutexattr_t _attr;

#endif

  // Disable copy constructors
  FastRecursiveMutex(const FastRecursiveMutex&);
  FastRecursiveMutex& operator=(const FastRecursiveMutex&);

  public:
  
  /**
   * Create a new FastRecursiveMutex
   */
  FastRecursiveMutex() 
    throw(Synchronization_Exception);

  /**
   * Destroy this FastRecursiveMutex
   */
  virtual ~FastRecursiveMutex() throw();

  /**
   * Acquire an exclusive lock. No safety or state checks are performed.
   *
   * @exception Synchronization_Exception - not thrown
   */
  virtual void acquire() 
    throw(Synchronization_Exception);

  
  /**
   * Release an exclusive lock. No safety or state checks are performed.
   * The caller should have already acquired the lock, and release it 
   * only once.
   * 
   * @exception Synchronization_Exception - not thrown
   */
  virtual void release() 
    throw(Synchronization_Exception);
  
  /**
   * Try to acquire an exclusive lock. No safety or state checks are performed.
   * This function returns immediately regardless of the value of the timeout.
   *
   * @param timeout Unused
   * @return bool
   * @exception Synchronization_Exception - not thrown
   */
  virtual bool tryAcquire(unsigned long timeout)
    throw(Synchronization_Exception);
  
}; /* FastRecursiveMutex */


};

#endif
