Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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 implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 18 | import java.util.concurrent.TimeUnit; |
| 19 | import java.util.function.Supplier; |
| 20 | |
| 21 | import com.google.common.base.Suppliers; |
| 22 | |
| 23 | import io.netty.util.HashedWheelTimer; |
| 24 | import io.netty.util.Timeout; |
| 25 | import io.netty.util.TimerTask; |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Hashed-wheel timer singleton. Care must be taken to shutdown the timer |
| 29 | * only when the VM is ready to exit. |
| 30 | */ |
| 31 | public final class Timer { |
| 32 | |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 33 | private static volatile org.jboss.netty.util.HashedWheelTimer timer; |
| 34 | |
| 35 | private static final Supplier<HashedWheelTimer> TIMER = |
| 36 | Suppliers.memoize(HashedWheelTimer::new); |
| 37 | |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 38 | |
| 39 | // Ban public construction |
| 40 | private Timer() { |
| 41 | } |
| 42 | |
| 43 | /** |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 44 | * Executes one-shot timer task on shared thread pool. |
| 45 | * |
| 46 | * @param task timer task to execute |
| 47 | * @param delay before executing the task |
| 48 | * @param unit of delay |
| 49 | * @return a handle which is associated with the specified task |
| 50 | */ |
| 51 | public static Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) { |
| 52 | return TIMER.get().newTimeout(task, delay, unit); |
| 53 | } |
| 54 | |
| 55 | /** |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 56 | * Returns the singleton hashed-wheel timer. |
| 57 | * |
| 58 | * @return hashed-wheel timer |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 59 | * |
| 60 | * @deprecated in 1.11.0 |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 61 | */ |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 62 | @Deprecated |
| 63 | public static org.jboss.netty.util.HashedWheelTimer getTimer() { |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 64 | if (Timer.timer == null) { |
Yuta HIGUCHI | f9ce497 | 2014-10-04 22:13:47 -0700 | [diff] [blame] | 65 | initTimer(); |
| 66 | } |
| 67 | return Timer.timer; |
| 68 | } |
| 69 | |
| 70 | private static synchronized void initTimer() { |
| 71 | if (Timer.timer == null) { |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 72 | org.jboss.netty.util.HashedWheelTimer hwTimer = |
| 73 | new org.jboss.netty.util.HashedWheelTimer(); |
Yuta HIGUCHI | a546afc | 2014-10-06 21:44:48 -0700 | [diff] [blame] | 74 | hwTimer.start(); |
| 75 | Timer.timer = hwTimer; |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 76 | } |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } |