Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep server instance in a shared_ptr #1166

Merged
merged 3 commits into from
Aug 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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()
Expand Down
43 changes: 25 additions & 18 deletions Server/ServerFactory.h → Util/make_unique.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
namespace osrm
{
// Taken from http://msdn.microsoft.com/en-us/library/dn439780.asp
// Note, that the snippet was broken there and needed minor massaging

// 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)...)));
}

struct ServerFactory
// make_unique<T[]>
template <class T> std::unique_ptr<T> make_unique(std::size_t Size)
{
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);
}
};

#endif // SERVER_FACTORY_H
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
8 changes: 4 additions & 4 deletions routed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down