Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onlab.util; |
| 17 | |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 20 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 21 | import static org.onlab.util.Tools.groupedThreads; |
| 22 | |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 23 | import java.util.concurrent.ScheduledFuture; |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Utility for managing a set of shared execution resources, such as a single |
| 28 | * thread scheduled executor and thread pool scheduled executor for use by |
| 29 | * various parts of the platform or by applications. |
| 30 | * <p> |
| 31 | * Whenever possible, use of these shared resources is encouraged over creating |
| 32 | * separate ones. |
| 33 | * </p> |
| 34 | */ |
| 35 | public final class SharedScheduledExecutors { |
| 36 | |
| 37 | public static final int DEFAULT_POOL_SIZE = 30; |
| 38 | |
Ray Milkey | 7fd9ae2 | 2019-04-02 09:34:23 -0700 | [diff] [blame] | 39 | private static final Object EXECUTORS_LOCK = new Object(); |
Ray Milkey | 397caca | 2019-04-01 16:27:50 -0700 | [diff] [blame] | 40 | private static SharedScheduledExecutorService singleThreadExecutor = null; |
Ray Milkey | 397caca | 2019-04-01 16:27:50 -0700 | [diff] [blame] | 41 | private static SharedScheduledExecutorService poolThreadExecutor = null; |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 42 | |
| 43 | // Ban public construction |
| 44 | private SharedScheduledExecutors() { |
| 45 | } |
| 46 | |
Ray Milkey | 397caca | 2019-04-01 16:27:50 -0700 | [diff] [blame] | 47 | private static void setup() { |
Ray Milkey | 7fd9ae2 | 2019-04-02 09:34:23 -0700 | [diff] [blame] | 48 | synchronized (EXECUTORS_LOCK) { |
| 49 | if (singleThreadExecutor == null) { |
| 50 | singleThreadExecutor = |
| 51 | new SharedScheduledExecutorService( |
| 52 | newSingleThreadScheduledExecutor( |
| 53 | groupedThreads("onos/shared/scheduled", |
| 54 | "onos-single-executor"))); |
Ray Milkey | 397caca | 2019-04-01 16:27:50 -0700 | [diff] [blame] | 55 | |
Ray Milkey | 7fd9ae2 | 2019-04-02 09:34:23 -0700 | [diff] [blame] | 56 | poolThreadExecutor = |
| 57 | new SharedScheduledExecutorService( |
| 58 | newScheduledThreadPool(DEFAULT_POOL_SIZE, |
| 59 | groupedThreads("onos/shared/scheduled", |
| 60 | "onos-pool-executor-%d"))); |
| 61 | } |
Ray Milkey | 397caca | 2019-04-01 16:27:50 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Returns the shared scheduled single thread executor. |
| 67 | * |
| 68 | * @return shared scheduled single thread executor |
| 69 | */ |
Jian Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 70 | public static SharedScheduledExecutorService getSingleThreadExecutor() { |
Ray Milkey | 397caca | 2019-04-01 16:27:50 -0700 | [diff] [blame] | 71 | setup(); |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 72 | return singleThreadExecutor; |
| 73 | } |
| 74 | |
| 75 | /** |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 76 | * Executes one-shot timer task on shared thread pool. |
| 77 | * |
| 78 | * @param task timer task to execute |
| 79 | * @param delay before executing the task |
| 80 | * @param unit of delay |
| 81 | * @return a ScheduledFuture representing pending completion of the task |
| 82 | * and whose get() method will return null upon completion |
| 83 | */ |
| 84 | public static ScheduledFuture<?> newTimeout(Runnable task, long delay, TimeUnit unit) { |
Ray Milkey | 397caca | 2019-04-01 16:27:50 -0700 | [diff] [blame] | 85 | setup(); |
Yuta HIGUCHI | 19afc03 | 2017-05-20 23:44:17 -0700 | [diff] [blame] | 86 | return SharedScheduledExecutors.getPoolThreadExecutor() |
| 87 | .schedule(task, delay, unit); |
| 88 | } |
| 89 | |
| 90 | /** |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 91 | * Returns the shared scheduled thread pool executor. |
| 92 | * |
| 93 | * @return shared scheduled executor pool |
| 94 | */ |
Jian Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 95 | public static SharedScheduledExecutorService getPoolThreadExecutor() { |
Ray Milkey | 397caca | 2019-04-01 16:27:50 -0700 | [diff] [blame] | 96 | setup(); |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 97 | return poolThreadExecutor; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Configures the shared scheduled thread pool size. |
| 102 | * |
| 103 | * @param poolSize new pool size |
| 104 | */ |
| 105 | public static void setPoolSize(int poolSize) { |
Ray Milkey | 397caca | 2019-04-01 16:27:50 -0700 | [diff] [blame] | 106 | setup(); |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 107 | checkArgument(poolSize > 0, "Shared pool size size must be greater than 0"); |
| 108 | poolThreadExecutor.setBackingExecutor( |
| 109 | newScheduledThreadPool(poolSize, groupedThreads("onos/shared/scheduled", |
| 110 | "onos-pool-executor-%d"))); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Shuts down all shared scheduled executors. |
| 115 | * This is not intended to be called by application directly. |
| 116 | */ |
| 117 | public static void shutdown() { |
Ray Milkey | 7fd9ae2 | 2019-04-02 09:34:23 -0700 | [diff] [blame] | 118 | synchronized (EXECUTORS_LOCK) { |
Carmelo Cascone | cb2cc1d | 2021-01-28 18:25:59 -0800 | [diff] [blame^] | 119 | if (singleThreadExecutor != null) { |
| 120 | singleThreadExecutor.backingExecutor().shutdown(); |
| 121 | singleThreadExecutor = null; |
| 122 | poolThreadExecutor.backingExecutor().shutdown(); |
| 123 | poolThreadExecutor = null; |
| 124 | } |
Ray Milkey | 7fd9ae2 | 2019-04-02 09:34:23 -0700 | [diff] [blame] | 125 | } |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 126 | } |
| 127 | } |