paradigm_3/html/
/*! \file eventqueue.cpp
  This is the eventqueue implementation.

  \author Jon A. Lambert
  \date 05/02/2003
  \version 0.30

  \note
  Commented out semaphore code as we don't want to wait on both ends of
  the queue.  The Server code would block on messages from main thread
  instead of select().  In the main thread, we will sleep \see Chat().
 */
#include "sysconfig.h"
#include "eventqueue.h"
#include "guard.h"

/*!
  Constructor for EventQueue.

  \param num_threads The maximum number of threads allowed access
         to the queue.

  \note One should check to see if this was properly initialized via IsOk.
 */
#pragma argsused
EventQueue::EventQueue(int num_threads) {
//  mFree = CreateSemaphore(0, num_threads, num_threads, 0);
//  mUsed = CreateSemaphore(0, 0, num_threads, 0);
  InitializeCriticalSection(&mMutex);
//  if (mFree == FALSE || mUsed == FALSE)
//    mOk = false;
//  else
//    mOk = true;
}

/*!
  Destructor for EventQueue.
 */
EventQueue::~EventQueue() {
  DeleteCriticalSection(&mMutex);
//  if (mUsed != FALSE)
//    CloseHandle(mUsed);
//  if (mFree != FALSE)
//    CloseHandle(mFree);
}

/*!
  Pushes and Event onto the EventQueue.  Client must create Event.

  \param event The Event object to be pushed onto the queue
 */
void EventQueue::Push(Event * event) {
//  if (!mOk)
//    return;
//  WaitForSingleObject(mFree, INFINITE);
  {
    Guard g(mMutex);
    mQue.push(event);
  }  
//  ReleaseSemaphore(mUsed, 1, 0);
}

/*!
  Pops an Event onto the EventQueue.  Client must destroy Event.

  \return The Event object on the top of the queue or NULL
 */
Event* EventQueue::Pop() {
  Event * event = NULL;
//  if (!mOk)
//    return NULL;
//  WaitForSingleObject(mUsed, INFINITE);
  {
    Guard g(mMutex);
    if (!mQue.empty()) {
      event = mQue.front();
      mQue.pop();
    }
  }
//  ReleaseSemaphore(mFree, 1, 0);
  return event;
}

/*!
  Size returns the number of Events contained in the EventQueue

  \return number of Events in the EventQueue
 */
int EventQueue::Size() {
  Guard g(mMutex);
  return mQue.size();
}

/*!
  IsOk checks to see if the EventQueue was properly initialized.

  \return true if initialied or false if there was an error.
 */
bool EventQueue::IsOk() {
//  return mOk;
  return true;
}