Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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 | |
| 18 | import org.jboss.netty.util.HashedWheelTimer; |
| 19 | |
| 20 | /** |
| 21 | * Hashed-wheel timer singleton. Care must be taken to shutdown the timer |
| 22 | * only when the VM is ready to exit. |
| 23 | */ |
| 24 | public final class Timer { |
| 25 | |
Yuta HIGUCHI | f9ce497 | 2014-10-04 22:13:47 -0700 | [diff] [blame] | 26 | private static volatile HashedWheelTimer timer; |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 27 | |
| 28 | // Ban public construction |
| 29 | private Timer() { |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Returns the singleton hashed-wheel timer. |
| 34 | * |
| 35 | * @return hashed-wheel timer |
| 36 | */ |
| 37 | public static HashedWheelTimer getTimer() { |
| 38 | if (Timer.timer == null) { |
Yuta HIGUCHI | f9ce497 | 2014-10-04 22:13:47 -0700 | [diff] [blame] | 39 | initTimer(); |
| 40 | } |
| 41 | return Timer.timer; |
| 42 | } |
| 43 | |
| 44 | private static synchronized void initTimer() { |
| 45 | if (Timer.timer == null) { |
Yuta HIGUCHI | a546afc | 2014-10-06 21:44:48 -0700 | [diff] [blame] | 46 | HashedWheelTimer hwTimer = new HashedWheelTimer(); |
| 47 | hwTimer.start(); |
| 48 | Timer.timer = hwTimer; |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 49 | } |
tom | 8bf2e6b | 2014-09-10 20:53:54 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | } |