Mercurial Hosting > luan
changeset 941:c948f674a2d5
simplify Timeout
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 11 Oct 2016 00:34:19 -0600 |
parents | b77d631b9e28 |
children | c157a786ed0b |
files | src/org/eclipse/jetty/util/thread/Timeout.java |
diffstat | 1 files changed, 132 insertions(+), 329 deletions(-) [+] |
line wrap: on
line diff
diff -r b77d631b9e28 -r c948f674a2d5 src/org/eclipse/jetty/util/thread/Timeout.java --- a/src/org/eclipse/jetty/util/thread/Timeout.java Tue Oct 11 00:13:30 2016 -0600 +++ b/src/org/eclipse/jetty/util/thread/Timeout.java Tue Oct 11 00:34:19 2016 -0600 @@ -33,348 +33,151 @@ */ public class Timeout { - private static final Logger LOG = LoggerFactory.getLogger(Timeout.class); - private Object _lock; - private long _duration; - private volatile long _now=System.currentTimeMillis(); - private Task _head=new Task(); - - /* ------------------------------------------------------------ */ - public Timeout() - { - _lock=new Object(); - _head._timeout=this; - } + private static final Logger LOG = LoggerFactory.getLogger(Timeout.class); + private Object _lock; + private long _duration; + private volatile long _now=System.currentTimeMillis(); + private Task _head=new Task(); - /* ------------------------------------------------------------ */ - public Timeout(Object lock) - { - _lock=lock; - _head._timeout=this; - } - - /* ------------------------------------------------------------ */ - /** - * @return Returns the duration. - */ - public long getDuration() - { - return _duration; - } - - /* ------------------------------------------------------------ */ - /** - * @param duration The duration to set. - */ - public void setDuration(long duration) - { - _duration = duration; - } + /* ------------------------------------------------------------ */ + public Timeout(Object lock) + { + _lock=lock; + _head._timeout=this; + } - /* ------------------------------------------------------------ */ - public long setNow() - { - return _now=System.currentTimeMillis(); - } - - /* ------------------------------------------------------------ */ - public long getNow() - { - return _now; - } - - /* ------------------------------------------------------------ */ - public void setNow(long now) - { - _now=now; - } + /* ------------------------------------------------------------ */ + /** + * @return Returns the duration. + */ + public long getDuration() + { + return _duration; + } - /* ------------------------------------------------------------ */ - /** Get an expired tasks. - * This is called instead of {@link #tick()} to obtain the next - * expired Task, but without calling it's {@link Task#expire()} or - * {@link Task#expired()} methods. - * - * @return the next expired task or null. - */ - public Task expired() - { - synchronized (_lock) - { - long _expiry = _now-_duration; - - if (_head._next!=_head) - { - Task task = _head._next; - if (task._timestamp>_expiry) - return null; + /* ------------------------------------------------------------ */ + /** + * @param duration The duration to set. + */ + public void setDuration(long duration) + { + _duration = duration; + } - task.unlink(); - task._expired=true; - return task; - } - return null; - } - } - - /* ------------------------------------------------------------ */ - public void tick() - { - final long expiry = _now-_duration; + /* ------------------------------------------------------------ */ + public long setNow() + { + return _now=System.currentTimeMillis(); + } + + /* ------------------------------------------------------------ */ + public long getNow() + { + return _now; + } - Task task=null; - while (true) - { - try - { - synchronized (_lock) - { - task= _head._next; - if (task==_head || task._timestamp>expiry) - break; - task.unlink(); - task._expired=true; - task.expire(); - } - - task.expired(); - } - catch(Throwable th) - { - LOG.warn("EXCEPTION",th); - } - } - } - - /* ------------------------------------------------------------ */ - public void tick(long now) - { - _now=now; - tick(); - } + /* ------------------------------------------------------------ */ + public void setNow(long now) + { + _now=now; + } - /* ------------------------------------------------------------ */ - public void schedule(Task task) - { - schedule(task,0L); - } - - /* ------------------------------------------------------------ */ - /** - * @param task - * @param delay A delay in addition to the default duration of the timeout - */ - public void schedule(Task task,long delay) - { - synchronized (_lock) - { - if (task._timestamp!=0) - { - task.unlink(); - task._timestamp=0; - } - task._timeout=this; - task._expired=false; - task._delay=delay; - task._timestamp = _now+delay; + /* ------------------------------------------------------------ */ + /** Get an expired tasks. + * This is called instead of {@link #tick()} to obtain the next + * expired Task, but without calling it's {@link Task#expire()} or + * {@link Task#expired()} methods. + * + * @return the next expired task or null. + */ - Task last=_head._prev; - while (last!=_head) - { - if (last._timestamp <= task._timestamp) - break; - last=last._prev; - } - last.link(task); - } - } - - - /* ------------------------------------------------------------ */ - public void cancelAll() - { - synchronized (_lock) - { - _head._next=_head._prev=_head; - } - } + public Task expired() + { + synchronized (_lock) + { + long _expiry = _now-_duration; + if (_head._next!=_head) + { + Task task = _head._next; + if (task._timestamp>_expiry) + return null; - /* ------------------------------------------------------------ */ - public boolean isEmpty() - { - synchronized (_lock) - { - return _head._next==_head; - } - } - - /* ------------------------------------------------------------ */ - public long getTimeToNext() - { - synchronized (_lock) - { - if (_head._next==_head) - return -1; - long to_next = _duration+_head._next._timestamp-_now; - return to_next<0?0:to_next; - } - } - - /* ------------------------------------------------------------ */ - @Override - public String toString() - { - StringBuffer buf = new StringBuffer(); - buf.append(super.toString()); - - Task task = _head._next; - while (task!=_head) - { - buf.append("-->"); - buf.append(task); - task=task._next; - } - - return buf.toString(); - } + task.unlink(); + task._expired=true; + return task; + } + return null; + } + } - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - /* ------------------------------------------------------------ */ - /** Task. - * The base class for scheduled timeouts. This class should be - * extended to implement the expire() method, which is called if the - * timeout expires. - * - * - * - */ - public static class Task - { - Task _next; - Task _prev; - Timeout _timeout; - long _delay; - long _timestamp=0; - boolean _expired=false; + public void cancelAll() + { + synchronized (_lock) + { + _head._next=_head._prev=_head; + } + } - /* ------------------------------------------------------------ */ - protected Task() - { - _next=_prev=this; - } - - /* ------------------------------------------------------------ */ - public long getTimestamp() - { - return _timestamp; - } - - /* ------------------------------------------------------------ */ - public long getAge() - { - final Timeout t = _timeout; - if (t!=null) - { - final long now=t._now; - if (now!=0 && _timestamp!=0) - return now-_timestamp; - } - return 0; - } + public long getTimeToNext() + { + synchronized (_lock) + { + if (_head._next==_head) + return -1; + long to_next = _duration+_head._next._timestamp-_now; + return to_next<0?0:to_next; + } + } - /* ------------------------------------------------------------ */ - private void unlink() - { - _next._prev=_prev; - _prev._next=_next; - _next=_prev=this; - _expired=false; - } + @Override + public String toString() + { + StringBuffer buf = new StringBuffer(); + buf.append(super.toString()); + + Task task = _head._next; + while (task!=_head) + { + buf.append("-->"); + buf.append(task); + task=task._next; + } + + return buf.toString(); + } - /* ------------------------------------------------------------ */ - private void link(Task task) - { - Task next_next = _next; - _next._prev=task; - _next=task; - _next._next=next_next; - _next._prev=this; - } - - /* ------------------------------------------------------------ */ - /** Schedule the task on the given timeout. - * The task exiry will be called after the timeout duration. - * @param timer - */ - public void schedule(Timeout timer) - { - timer.schedule(this); - } - - /* ------------------------------------------------------------ */ - /** Schedule the task on the given timeout. - * The task exiry will be called after the timeout duration. - * @param timer - */ - public void schedule(Timeout timer, long delay) - { - timer.schedule(this,delay); - } - - /* ------------------------------------------------------------ */ - /** Reschedule the task on the current timeout. - * The task timeout is rescheduled as if it had been cancelled and - * scheduled on the current timeout. - */ - public void reschedule() - { - Timeout timeout = _timeout; - if (timeout!=null) - timeout.schedule(this,_delay); - } - - /* ------------------------------------------------------------ */ - /** Cancel the task. - * Remove the task from the timeout. - */ - public void cancel() - { - Timeout timeout = _timeout; - if (timeout!=null) - { - synchronized (timeout._lock) - { - unlink(); - _timestamp=0; - } - } - } - - /* ------------------------------------------------------------ */ - public boolean isExpired() { return _expired; } + /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ + /* ------------------------------------------------------------ */ + /** Task. + * The base class for scheduled timeouts. This class should be + * extended to implement the expire() method, which is called if the + * timeout expires. + * + * + * + */ + public static class Task + { + Task _next; + Task _prev; + Timeout _timeout; + long _timestamp=0; + boolean _expired=false; - /* ------------------------------------------------------------ */ - public boolean isScheduled() { return _next!=this; } - - /* ------------------------------------------------------------ */ - /** Expire task. - * This method is called when the timeout expires. It is called - * in the scope of the synchronize block (on this) that sets - * the {@link #isExpired()} state to true. - * @see #expired() For an unsynchronized callback. - */ - protected void expire(){} + protected Task() + { + _next=_prev=this; + } - /* ------------------------------------------------------------ */ - /** Expire task. - * This method is called when the timeout expires. It is called - * outside of any synchronization scope and may be delayed. - * - */ - public void expired(){} - - } + private void unlink() + { + _next._prev=_prev; + _prev._next=_next; + _next=_prev=this; + _expired=false; + } + } }