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
|
#ifndef SERVER_H
|
||||||
#define SERVER_H
|
#define SERVER_H
|
||||||
|
|
||||||
#include "../Util/StringUtil.h"
|
|
||||||
|
|
||||||
#include "Connection.h"
|
#include "Connection.h"
|
||||||
#include "RequestHandler.h"
|
#include "RequestHandler.h"
|
||||||
|
|
||||||
|
#include "../Util/make_unique.hpp"
|
||||||
|
#include "../Util/SimpleLogger.h"
|
||||||
|
#include "../Util/StringUtil.h"
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
|
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@ -44,6 +48,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
class Server
|
class Server
|
||||||
{
|
{
|
||||||
public:
|
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)
|
explicit Server(const std::string &address, const int port, const unsigned thread_pool_size)
|
||||||
: thread_pool_size(thread_pool_size), acceptor(io_service),
|
: thread_pool_size(thread_pool_size), acceptor(io_service),
|
||||||
new_connection(new http::Connection(io_service, request_handler)), request_handler()
|
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
|
#ifndef MAKE_UNIQUE_H__
|
||||||
#define SERVER_FACTORY_H
|
#define MAKE_UNIQUE_H__
|
||||||
|
|
||||||
#include "Server.h"
|
#include <cstdlib>
|
||||||
#include "../Util/SimpleLogger.h"
|
#include <memory>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include <zlib.h>
|
namespace osrm
|
||||||
|
|
||||||
struct ServerFactory
|
|
||||||
{
|
{
|
||||||
ServerFactory() = delete;
|
// Taken from http://msdn.microsoft.com/en-us/library/dn439780.asp
|
||||||
ServerFactory(const ServerFactory &) = delete;
|
// Note, that the snippet was broken there and needed minor massaging
|
||||||
static Server *CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads)
|
|
||||||
|
// make_unique<T>
|
||||||
|
template <class T, class... Types> std::unique_ptr<T> make_unique(Types &&... Args)
|
||||||
{
|
{
|
||||||
SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion();
|
return (std::unique_ptr<T>(new T(std::forward<Types>(Args)...)));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
#endif // SERVER_FACTORY_H
|
// 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 "Library/OSRM.h"
|
||||||
#include "Server/ServerFactory.h"
|
#include "Server/Server.h"
|
||||||
#include "Util/GitDescription.h"
|
#include "Util/GitDescription.h"
|
||||||
#include "Util/ProgramOptions.h"
|
#include "Util/ProgramOptions.h"
|
||||||
#include "Util/SimpleLogger.h"
|
#include "Util/SimpleLogger.h"
|
||||||
@ -131,8 +131,8 @@ int main(int argc, const char *argv[])
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
OSRM osrm_lib(server_paths, use_shared_memory);
|
OSRM osrm_lib(server_paths, use_shared_memory);
|
||||||
Server *routing_server =
|
auto routing_server =
|
||||||
ServerFactory::CreateServer(ip_address, ip_port, requested_thread_num);
|
Server::CreateServer(ip_address, ip_port, requested_thread_num);
|
||||||
|
|
||||||
routing_server->GetRequestHandlerPtr().RegisterRoutingMachine(&osrm_lib);
|
routing_server->GetRequestHandlerPtr().RegisterRoutingMachine(&osrm_lib);
|
||||||
|
|
||||||
@ -181,7 +181,7 @@ int main(int argc, const char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
SimpleLogger().Write() << "freeing objects";
|
SimpleLogger().Write() << "freeing objects";
|
||||||
delete routing_server;
|
routing_server.reset();
|
||||||
SimpleLogger().Write() << "shutdown completed";
|
SimpleLogger().Write() << "shutdown completed";
|
||||||
}
|
}
|
||||||
catch (const std::exception &e)
|
catch (const std::exception &e)
|
||||||
|
Loading…
Reference in New Issue
Block a user