/*******************************************************
 * File: MonitorBoost.cpp
 * Author: Keith Schwarz (htiek@cs.stanford.edu)
 *
 * Implementation of monitors using Boost library.
 */
#include "Monitor.hh"
#include <boost/thread.hpp>
using namespace boost;

namespace synch {
  
  /* Implementation struct is just a wrapper around condition_variable_any
   * and a mutex.
   */
  struct Monitor::Impl {
    mutex mMutex;
    condition_variable_any mCond;
  };
  
  /* Constructor sets up the impl struct. */
  Monitor::Monitor() : mImpl(new Impl) {
    // Handled by initializer list
  }
  
  /* Destructor clears the impl struct. */
  Monitor::~Monitor() {
    delete mImpl;
  }
  
  /* Lock and unlock are forwarded to the underlying mutex. */
  void Monitor::lock() {
    mImpl->mMutex.lock();
  }
  void Monitor::unlock() {
    mImpl->mMutex.unlock();
  }
  
  /* Wait blocks on the raw mutex variable. */
  void Monitor::wait() {
    mImpl->mCond.wait(mImpl->mMutex);
  }
  
  /* notify just signals the underlying condition. */
  void Monitor::notify() {
    mImpl->mCond.notify_one();
  }
  
  /* notifyAll notifies everything. */
  void Monitor::notifyAll() {
    mImpl->mCond.notify_all();
  }
}
