///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
//
// All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License and GNU Library
// General Public License as published by the Free Software Foundation;
// either version 2, or (at your option) any later version.
//
// This program 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 and GNU Library General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// and GNU Library General Public License along with this program; if
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
// MA 02139, USA.
//
///////////////////////////////////////////////////////////////////////////////
package org.myoodb.util;
public class EventSemaphore
{
private long m_lastTrigger;
private long m_fungeFactorTime;
private long m_fungeFactorHoldDownTime;
private java.util.concurrent.locks.ReentrantLock m_lock;
private java.util.concurrent.locks.Condition m_condition;
public EventSemaphore()
{
m_lastTrigger = 0;
m_fungeFactorTime = 0;
m_fungeFactorHoldDownTime = 1000;
m_lock = new java.util.concurrent.locks.ReentrantLock();
m_condition = m_lock.newCondition();
}
public void yield()
{
m_lock.lock();
try
{
if (m_fungeFactorTime != 0)
{
long ctime = System.currentTimeMillis();
if ((ctime - m_fungeFactorTime) > m_lastTrigger)
{
m_condition.await(m_fungeFactorHoldDownTime, java.util.concurrent.TimeUnit.MILLISECONDS);
}
else
{
m_condition.await();
}
}
else
{
m_condition.await();
}
}
catch (InterruptedException e)
{
// nothing to do
}
finally
{
m_lock.unlock();
}
}
public void yield(long timeout) throws java.lang.InterruptedException
{
m_lock.lock();
try
{
if (m_fungeFactorTime != 0)
{
long ctime = System.currentTimeMillis();
if ((ctime - m_fungeFactorTime) > m_lastTrigger)
{
m_condition.await(m_fungeFactorHoldDownTime, java.util.concurrent.TimeUnit.MILLISECONDS);
}
else
{
m_condition.await(timeout, java.util.concurrent.TimeUnit.MILLISECONDS);
}
}
else
{
m_condition.await(timeout, java.util.concurrent.TimeUnit.MILLISECONDS);
}
}
catch (InterruptedException e)
{
// nothing to do
}
finally
{
m_lock.unlock();
}
}
public void signal()
{
m_lock.lock();
try
{
m_lastTrigger = System.currentTimeMillis();
m_condition.signal();
}
finally
{
m_lock.unlock();
}
}
public void signalAll()
{
m_lock.lock();
try
{
m_lastTrigger = System.currentTimeMillis();
m_condition.signalAll();
}
finally
{
m_lock.unlock();
}
}
public int hashCode()
{
return toString().hashCode();
}
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
else if (obj instanceof String)
{
return toString().equals(obj);
}
else if (obj instanceof LockSemaphore)
{
return toString().equals(((LockSemaphore) obj).toString());
}
else
{
return false;
}
}
public int compareTo(Object obj)
{
if (obj instanceof String)
{
return toString().compareTo((String) obj);
}
else if (obj instanceof LockSemaphore)
{
return toString().compareTo(((LockSemaphore) obj).toString());
}
else
{
return -1;
}
}
public String toString()
{
return m_lock.toString();
}
//
// XXX: advance features
//
public void setFungeFactorTime(long fungeFactorTime)
{
m_fungeFactorTime = fungeFactorTime;
}
public long getFungeFactorTime()
{
return m_fungeFactorTime;
}
public void setFungeFactorHoldDownTime(long fungeFactorHoldDownTime)
{
m_fungeFactorHoldDownTime = fungeFactorHoldDownTime;
}
public long getFungeFactorHoldDownTime()
{
return m_fungeFactorHoldDownTime;
}
}
|