001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.util;
025:
026: public class EventSemaphore {
027: private long m_lastTrigger;
028: private long m_fungeFactorTime;
029: private long m_fungeFactorHoldDownTime;
030: private java.util.concurrent.locks.ReentrantLock m_lock;
031: private java.util.concurrent.locks.Condition m_condition;
032:
033: public EventSemaphore() {
034: m_lastTrigger = 0;
035: m_fungeFactorTime = 0;
036: m_fungeFactorHoldDownTime = 1000;
037:
038: m_lock = new java.util.concurrent.locks.ReentrantLock();
039:
040: m_condition = m_lock.newCondition();
041: }
042:
043: public void yield() {
044: m_lock.lock();
045:
046: try {
047: if (m_fungeFactorTime != 0) {
048: long ctime = System.currentTimeMillis();
049: if ((ctime - m_fungeFactorTime) > m_lastTrigger) {
050: m_condition.await(m_fungeFactorHoldDownTime,
051: java.util.concurrent.TimeUnit.MILLISECONDS);
052: } else {
053: m_condition.await();
054: }
055: } else {
056: m_condition.await();
057: }
058: } catch (InterruptedException e) {
059: // nothing to do
060: } finally {
061: m_lock.unlock();
062: }
063: }
064:
065: public void yield(long timeout)
066: throws java.lang.InterruptedException {
067: m_lock.lock();
068:
069: try {
070: if (m_fungeFactorTime != 0) {
071: long ctime = System.currentTimeMillis();
072: if ((ctime - m_fungeFactorTime) > m_lastTrigger) {
073: m_condition.await(m_fungeFactorHoldDownTime,
074: java.util.concurrent.TimeUnit.MILLISECONDS);
075: } else {
076: m_condition.await(timeout,
077: java.util.concurrent.TimeUnit.MILLISECONDS);
078: }
079: } else {
080: m_condition.await(timeout,
081: java.util.concurrent.TimeUnit.MILLISECONDS);
082: }
083: } catch (InterruptedException e) {
084: // nothing to do
085: } finally {
086: m_lock.unlock();
087: }
088: }
089:
090: public void signal() {
091: m_lock.lock();
092:
093: try {
094: m_lastTrigger = System.currentTimeMillis();
095:
096: m_condition.signal();
097: } finally {
098: m_lock.unlock();
099: }
100: }
101:
102: public void signalAll() {
103: m_lock.lock();
104:
105: try {
106: m_lastTrigger = System.currentTimeMillis();
107:
108: m_condition.signalAll();
109: } finally {
110: m_lock.unlock();
111: }
112: }
113:
114: public int hashCode() {
115: return toString().hashCode();
116: }
117:
118: public boolean equals(Object obj) {
119: if (this == obj) {
120: return true;
121: } else if (obj instanceof String) {
122: return toString().equals(obj);
123: } else if (obj instanceof LockSemaphore) {
124: return toString().equals(((LockSemaphore) obj).toString());
125: } else {
126: return false;
127: }
128: }
129:
130: public int compareTo(Object obj) {
131: if (obj instanceof String) {
132: return toString().compareTo((String) obj);
133: } else if (obj instanceof LockSemaphore) {
134: return toString().compareTo(
135: ((LockSemaphore) obj).toString());
136: } else {
137: return -1;
138: }
139: }
140:
141: public String toString() {
142: return m_lock.toString();
143: }
144:
145: //
146: // XXX: advance features
147: //
148:
149: public void setFungeFactorTime(long fungeFactorTime) {
150: m_fungeFactorTime = fungeFactorTime;
151: }
152:
153: public long getFungeFactorTime() {
154: return m_fungeFactorTime;
155: }
156:
157: public void setFungeFactorHoldDownTime(long fungeFactorHoldDownTime) {
158: m_fungeFactorHoldDownTime = fungeFactorHoldDownTime;
159: }
160:
161: public long getFungeFactorHoldDownTime() {
162: return m_fungeFactorHoldDownTime;
163: }
164: }
|