Merge pull request #1166 from Project-OSRM/server_instance_unique_ptr
keep server instance in a shared_ptr
This commit is contained in:
		
						commit
						85c244ed08
					
				| @ -28,14 +28,18 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
| #ifndef SERVER_H | ||||
| #define SERVER_H | ||||
| 
 | ||||
| #include "../Util/StringUtil.h" | ||||
| 
 | ||||
| #include "Connection.h" | ||||
| #include "RequestHandler.h" | ||||
| 
 | ||||
| #include "../Util/make_unique.hpp" | ||||
| #include "../Util/SimpleLogger.h" | ||||
| #include "../Util/StringUtil.h" | ||||
| 
 | ||||
| #include <boost/asio.hpp> | ||||
| #include <boost/bind.hpp> | ||||
| 
 | ||||
| #include <zlib.h> | ||||
| 
 | ||||
| #include <functional> | ||||
| #include <memory> | ||||
| #include <thread> | ||||
| @ -44,6 +48,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
| class Server | ||||
| { | ||||
|   public: | ||||
| 
 | ||||
|     // Note: returns a shared instead of a unique ptr as it is captured in a lambda somewhere else
 | ||||
|     static std::shared_ptr<Server> CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads) | ||||
|     { | ||||
|         SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion(); | ||||
|         const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency()); | ||||
|         const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads); | ||||
|         return std::make_shared<Server>(ip_address, ip_port, real_num_threads); | ||||
|     } | ||||
| 
 | ||||
|     explicit Server(const std::string &address, const int port, const unsigned thread_pool_size) | ||||
|         : thread_pool_size(thread_pool_size), acceptor(io_service), | ||||
|           new_connection(new http::Connection(io_service, request_handler)), request_handler() | ||||
|  | ||||
| @ -25,25 +25,32 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
| 
 | ||||
| */ | ||||
| 
 | ||||
| #ifndef SERVER_FACTORY_H | ||||
| #define SERVER_FACTORY_H | ||||
| #ifndef MAKE_UNIQUE_H__ | ||||
| #define MAKE_UNIQUE_H__ | ||||
| 
 | ||||
| #include "Server.h" | ||||
| #include "../Util/SimpleLogger.h" | ||||
| #include <cstdlib> | ||||
| #include <memory> | ||||
| #include <type_traits> | ||||
| 
 | ||||
| #include <zlib.h> | ||||
| 
 | ||||
| struct ServerFactory | ||||
| namespace osrm | ||||
| { | ||||
|     ServerFactory() = delete; | ||||
|     ServerFactory(const ServerFactory &) = delete; | ||||
|     static Server *CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads) | ||||
|     { | ||||
|         SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion(); | ||||
|         const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency()); | ||||
|         const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads); | ||||
|         return new Server(ip_address, ip_port, real_num_threads); | ||||
|     } | ||||
| }; | ||||
| // Taken from http://msdn.microsoft.com/en-us/library/dn439780.asp
 | ||||
| // Note, that the snippet was broken there and needed minor massaging
 | ||||
| 
 | ||||
| #endif // SERVER_FACTORY_H
 | ||||
| // make_unique<T>
 | ||||
| template <class T, class... Types> std::unique_ptr<T> make_unique(Types &&... Args) | ||||
| { | ||||
|     return (std::unique_ptr<T>(new T(std::forward<Types>(Args)...))); | ||||
| } | ||||
| 
 | ||||
| // make_unique<T[]>
 | ||||
| template <class T> std::unique_ptr<T> make_unique(std::size_t Size) | ||||
| { | ||||
|     return (std::unique_ptr<T>(new T[Size]())); | ||||
| } | ||||
| 
 | ||||
| // make_unique<T[N]> disallowed
 | ||||
| template <class T, class... Types> | ||||
| typename std::enable_if<std::extent<T>::value != 0, void>::type make_unique(Types &&...) = delete; | ||||
| } | ||||
| #endif | ||||
| @ -26,7 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
| */ | ||||
| 
 | ||||
| #include "Library/OSRM.h" | ||||
| #include "Server/ServerFactory.h" | ||||
| #include "Server/Server.h" | ||||
| #include "Util/GitDescription.h" | ||||
| #include "Util/ProgramOptions.h" | ||||
| #include "Util/SimpleLogger.h" | ||||
| @ -131,8 +131,8 @@ int main(int argc, const char *argv[]) | ||||
| #endif | ||||
| 
 | ||||
|         OSRM osrm_lib(server_paths, use_shared_memory); | ||||
|         Server *routing_server = | ||||
|             ServerFactory::CreateServer(ip_address, ip_port, requested_thread_num); | ||||
|         auto routing_server = | ||||
|             Server::CreateServer(ip_address, ip_port, requested_thread_num); | ||||
| 
 | ||||
|         routing_server->GetRequestHandlerPtr().RegisterRoutingMachine(&osrm_lib); | ||||
| 
 | ||||
| @ -181,7 +181,7 @@ int main(int argc, const char *argv[]) | ||||
|         } | ||||
| 
 | ||||
|         SimpleLogger().Write() << "freeing objects"; | ||||
|         delete routing_server; | ||||
|         routing_server.reset(); | ||||
|         SimpleLogger().Write() << "shutdown completed"; | ||||
|     } | ||||
|     catch (const std::exception &e) | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user