001/* 002 * Trident - A Multithreaded Server Alternative 003 * Copyright 2014 The TridentSDK Team 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package net.tridentsdk.server.concurrent; 019 020import net.tridentsdk.Trident; 021import net.tridentsdk.concurrent.SelectableThreadPool; 022import net.tridentsdk.config.Config; 023import net.tridentsdk.config.ConfigSection; 024import net.tridentsdk.docs.InternalUseOnly; 025import net.tridentsdk.server.TridentServer; 026import net.tridentsdk.util.TridentLogger; 027 028import javax.annotation.concurrent.ThreadSafe; 029 030/** 031 * Handles the majority of the lifecycle for the concurrent 032 * 033 * @author The TridentSDK Team 034 */ 035@ThreadSafe 036public final class ThreadsHandler { 037 private static final Config cfg = new Config(Trident.fileContainer().resolve("server.json")); // Initialization code can't use factory 038 private static final ConfigSection section = cfg.getConfigSection("performance"); 039 040 private static final SelectableThreadPool entities = configure("Entities"); 041 // private static final ExecutorFactory entities = configure("Tile-Entities"); not needed yet 042 private static final SelectableThreadPool players = configure("Players"); 043 private static final SelectableThreadPool worlds = configure("Worlds"); 044 045 // These 2 were originally placed together but livelock concerns have partitioned them 046 private static final SelectableThreadPool chunks = configure("Chunks"); 047 private static final SelectableThreadPool generator = configure("Generator"); 048 049 private ThreadsHandler() { 050 } 051 052 public static SelectableThreadPool configure(String uppercaseName) { 053 String tagName = uppercaseName.toLowerCase() + "-threads"; 054 int threads = section.getInt(tagName); 055 if (threads == 0) { 056 threads = 2; 057 TridentLogger.get().warn("Could not find config field for " + tagName + ", using 2 threads instead."); 058 } 059 060 return ConcurrentTaskExecutor.create(threads, uppercaseName); 061 } 062 063 /** 064 * Creates the concurrent handler for internal use 065 * 066 * @return the new thread handler 067 */ 068 @InternalUseOnly 069 public static ThreadsHandler create() { 070 return new ThreadsHandler(); 071 } 072 073 /** 074 * Stops all the executors and clears all caches of concurrent concurrent 075 */ 076 @InternalUseOnly 077 public static void shutdownAll() { 078 TridentServer.instance().mainThread().interrupt(); 079 } 080 081 /** 082 * Gets the executor for the world thread pool 083 * 084 * @return the executor 085 */ 086 @InternalUseOnly 087 public static SelectableThreadPool worldExecutor() { 088 return worlds; 089 } 090 091 /** 092 * Gets the executor for the chunk thread pool 093 * 094 * @return the executor 095 */ 096 @InternalUseOnly 097 public static SelectableThreadPool chunkExecutor() { 098 return chunks; 099 } 100 101 /** 102 * Gets the executor for the generator thread pool 103 * 104 * @return the executor 105 */ 106 @InternalUseOnly 107 public static SelectableThreadPool genExecutor() { 108 return generator; 109 } 110 111 /** 112 * Gets the executor for the entity thread pool 113 * 114 * @return the executor 115 */ 116 @InternalUseOnly 117 public static SelectableThreadPool entityExecutor() { 118 return entities; 119 } 120 121 /** 122 * Gets the executor for the player thread pool 123 * 124 * @return the executor 125 */ 126 @InternalUseOnly 127 public static SelectableThreadPool playerExecutor() { 128 return players; 129 } 130}