/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM 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 General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */


#ifndef NMM_THREADCONDITION_HPP
#define NMM_THREADCONDITION_HPP

#include "nmm/nmm_prefix.hpp"


#include "nmm/Result.hpp"
#include "visibility.hpp"

#include <stdexcept>
#include <pthread.h>

namespace NMM{
  class ThreadMutex;
  
  /** Wrapper for the pthread_cond_t variables.
   */
  class DLLEXPORT ThreadCondition {
  public:

    /** Constructor
     */
    ThreadCondition(const ThreadMutex&);

    /** Destructor
     */
    ~ThreadCondition();

    /** Atomically unlocks mutex and  waits, but it also bounds the
     * duration of the wait. If notify has not been signaled within
     * the  amount  of time specified by abstime, the mutex is  re-acquired.
     * This method expect the absolut time.
     * 
     * @param sec specifies the absolut time in seconds to wait 
     * @param nsec specifies the absolute time nano seconds to wait
     * @return SUCESS if the ThreadCondition is notified before the specified time, otherwise TIME_OUT
     */
    Result timedWait(long sec = 0, long nsec = 0) const;

    /** Atomically unlocks mutex and  waits, but it also bounds the
     * duration of the wait. If notify has not been signaled within
     * the  amount  of time specified by abstime, the mutex is  re-acquired.
     * 
     * @param sec specifies the relative time in seconds to wait 
     * @param nsec specifies the relative time nano seconds to wait
     * @return SUCESS if the ThreadCondition is notified before the specified time, otherwise TIME_OUT
     */
    Result relativeTimedWait(long sec , long nsec = 0) const;

    /** Atomically unlocks the mutex and waits for the condition variable
     * to be signaled.
     */
    void wait()              const ;

    /** Restarts one of the threads that  are waiting on this condition variable.
     * If several threads are waiting, exactly one is restarted, but it is not
     * specified which thread is restarted.
     */
    inline void notify()     const {pthread_cond_signal(&cond);}

    /** Restarts all the threads that are waiting on the condition variable.
     */
    inline void notifyAll()  const {pthread_cond_broadcast(&cond);}
  private:
    /** Disabled Copy-Constructor
     */
    ThreadCondition(const ThreadCondition&);    

    /** Disabled assignement operator
     */
    void operator=(const ThreadCondition&); 

    mutable pthread_cond_t cond;
    const ThreadMutex& mutex;
  };
}//end namespace NMM

#include "nmm/nmm_suffix.hpp"

#endif

