95 lines
1.7 KiB
C++

//----------------------------------------------------------------------------
//
// Copyright (C) Intel Corporation, 2006 - 2007.
//
// File: GuardedCounter.cpp
//
// Contents: A guarded counter.
//
// Notes:
//----------------------------------------------------------------------------
#pragma once
#ifndef _GuardedCounter_cpp__
#define _GuardedCounter_cpp__
#include <GuardedCounter.h>
#include <ace/Global_Macros.h>
#include <ace/Null_Mutex.h>
#include <ace/Mutex.h>
#include <ace/Guard_T.h>
#define TRUE 1
#define FALSE 0
GuardedCounter::GuardedCounter(void)
{
_counter=0;
_MaxValue=INT_MAX;
}
GuardedCounter::~GuardedCounter(void)
{
}
GuardedCounter::GuardedCounter(ACE_UINT32 MaxVal)
{
_counter=0;
_MaxValue=MaxVal;
}
GuardedCounter::GuardedCounter(ACE_UINT32 InitVal,ACE_UINT32 MaxVal)
{
_counter=InitVal;
_MaxValue=MaxVal;
}
void GuardedCounter::Increment()
{
ACE_GUARD( ACE_Thread_Mutex ,
lock,
_counter_mutex);
_counter++;
}
void GuardedCounter::Decrement()
{
ACE_GUARD( ACE_Thread_Mutex ,
lock,
_counter_mutex);
_counter--;
}
ACE_UINT32 GuardedCounter::GetCounterValue()
{
return _counter;
}
int GuardedCounter::TryIncrementCheckMax()
{
ACE_GUARD_RETURN( ACE_Thread_Mutex ,
lock,
_counter_mutex,FALSE);
if (_counter < _MaxValue){
_counter++;
return TRUE;
}
return FALSE;
}
int GuardedCounter::IncrementCheckMax()
{
ACE_GUARD_RETURN( ACE_Thread_Mutex ,
lock,
_counter_mutex,FALSE);
_counter++;
if (_counter <= _MaxValue){
return TRUE;
}
return FALSE;
}
void GuardedCounter::SetMaxValue(ACE_UINT32 val)
{
ACE_GUARD( ACE_Thread_Mutex ,
lock,
_counter_mutex);
_MaxValue=val;
}
#endif //_GuardedCounter_cpp__