2 * Copyright (c) 2011,2013 Big Switch Networks, Inc.
4 * Licensed under the Eclipse Public License, Version 1.0 (the
5 * "License"); you may not use this file except in compliance with the
6 * License. You may obtain a copy of the License at
8 * http://www.eclipse.org/legal/epl-v10.html
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 * implied. See the License for the specific language governing
14 * permissions and limitations under the License.
16 * This file incorporates work covered by the following copyright and
19 * Originally created by David Erickson, Stanford University
21 * Licensed under the Apache License, Version 2.0 (the "License");
22 * you may not use this file except in compliance with the
23 * License. You may obtain a copy of the License at
25 * http://www.apache.org/licenses/LICENSE-2.0
27 * Unless required by applicable law or agreed to in writing,
28 * software distributed under the License is distributed on an "AS
29 * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
30 * express or implied. See the License for the specific language
31 * governing permissions and limitations under the License.
34 package org.opendaylight.controller.sal.utils;
36 import java.util.concurrent.ScheduledExecutorService;
37 import java.util.concurrent.TimeUnit;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * This allows you to represent a task that should be queued for future
44 * execution but where you only want the task to complete once in response to
45 * some sequence of events. For example, if you get a change notification and
46 * want to reload state, you only want to reload the state once, at the end, and
47 * don't want to queue an update for every notification that might come in.
49 * The semantics are as follows: * If the task hasn't begun yet, do not queue a
50 * new task * If the task has begun, set a bit to restart it after the current
53 public class SingletonTask {
54 protected static Logger logger = LoggerFactory
55 .getLogger(SingletonTask.class);
57 protected static class SingletonTaskContext {
58 protected boolean taskShouldRun = false;
59 protected boolean taskRunning = false;
61 protected SingletonTaskWorker waitingTask = null;
64 protected static class SingletonTaskWorker implements Runnable {
66 boolean canceled = false;
67 long nextschedule = 0;
69 public SingletonTaskWorker(SingletonTask parent) {
76 synchronized (parent.context) {
77 if (canceled || !parent.context.taskShouldRun)
80 parent.context.taskRunning = true;
81 parent.context.taskShouldRun = false;
86 } catch (Exception e) {
87 logger.error("Exception while executing task", e);
90 synchronized (parent.context) {
91 parent.context.taskRunning = false;
93 if (parent.context.taskShouldRun) {
94 long now = System.nanoTime();
95 if ((nextschedule <= 0 || (nextschedule - now) <= 0)) {
96 parent.ses.execute(this);
98 parent.ses.schedule(this, nextschedule - now,
99 TimeUnit.NANOSECONDS);
106 protected SingletonTaskContext context = new SingletonTaskContext();
107 protected Runnable task;
108 protected ScheduledExecutorService ses;
111 * Construct a new SingletonTask for the given runnable. The context is used
112 * to manage the state of the task execution and can be shared by more than
113 * one instance of the runnable.
118 public SingletonTask(ScheduledExecutorService ses, Runnable task) {
125 * Schedule the task to run if there's not already a task scheduled If there
126 * is such a task waiting that has not already started, it cancel that task
127 * and reschedule it to run at the given time. If the task is already
128 * started, it will cause the task to be rescheduled once it completes to
129 * run after delay from the time of reschedule.
132 * the delay in scheduling
134 * the timeunit of the delay
136 public void reschedule(long delay, TimeUnit unit) {
137 boolean needQueue = true;
138 SingletonTaskWorker stw = null;
140 synchronized (context) {
141 if (context.taskRunning || context.taskShouldRun) {
142 if (context.taskRunning) {
143 // schedule to restart at the right time
145 long now = System.nanoTime();
147 + TimeUnit.NANOSECONDS.convert(delay, unit);
148 context.waitingTask.nextschedule = then;
150 context.waitingTask.nextschedule = 0;
154 // cancel and requeue
155 context.waitingTask.canceled = true;
156 context.waitingTask = null;
160 context.taskShouldRun = true;
163 stw = context.waitingTask = new SingletonTaskWorker(this);
172 ses.schedule(stw, delay, unit);