MXS-2196: Use smart pointers for lifetime management

The listeners no longer internally track whether they are active or
not. All of the heavy lifting is now done by std::shared_ptr.
This commit is contained in:
Markus Mäkelä
2018-11-30 11:53:17 +02:00
parent a10b6c2e89
commit c858f7f080
3 changed files with 20 additions and 60 deletions

View File

@ -63,6 +63,16 @@ public:
const std::string& auth_options, const std::string& auth_options,
SSL_LISTENER* ssl); SSL_LISTENER* ssl);
/**
* Destroy a listener
*
* This removes the listener from the global list of active listeners. Once destroyed, the port used
* by a listener is open for immediate reuse.
*
* @param listener Listener to destroy
*/
static void destroy(const SListener& listener);
/** /**
* Start listening on the configured port * Start listening on the configured port
* *
@ -84,14 +94,6 @@ public:
*/ */
bool start(); bool start();
/**
* Closes a listener
*
* This closes the network socket the listener listens on to allow immediate reuse of it. The listener
* instance can remain if there are open sessions for it.
*/
void close();
/** /**
* Listener name * Listener name
*/ */
@ -132,18 +134,6 @@ public:
*/ */
const char* state() const; const char* state() const;
/**
* Whether the listener is active
*/
bool is_active() const;
/**
* Deactivate the listener
*
* TODO: Remove and deactivate via removal from global listener list
*/
void deactivate();
/** /**
* The SSL_LISTENER object * The SSL_LISTENER object
*/ */
@ -221,15 +211,6 @@ private:
*/ */
bool listener_serialize(const SListener& listener); bool listener_serialize(const SListener& listener);
/**
* @brief Free a listener
*
* The listener must be destroyed before it can be freed.
*
* @param listener Listener to free
*/
void listener_free(const SListener& listener);
/** /**
* Find a listener * Find a listener
* *

View File

@ -57,7 +57,6 @@ Listener::Listener(SERVICE* service, const std::string& name, const std::string&
, m_listener(nullptr) , m_listener(nullptr)
, m_users(nullptr) , m_users(nullptr)
, m_service(service) , m_service(service)
, m_active(1)
{ {
} }
@ -116,21 +115,17 @@ SListener Listener::create(SERVICE* service,
return listener; return listener;
} }
void listener_free(const SListener& listener) void Listener::destroy(const SListener& listener)
{ {
std::lock_guard<std::mutex> guard(listener_lock); listener->stop();
all_listeners.remove(listener);
}
void Listener::close()
{
deactivate();
stop();
// TODO: This is not pretty but it works, revise when listeners are refactored. This is // TODO: This is not pretty but it works, revise when listeners are refactored. This is
// thread-safe as the listener is freed on the same thread that closes the socket. // thread-safe as the listener is freed on the same thread that closes the socket.
::close(m_listener->fd); ::close(listener->m_listener->fd);
m_listener->fd = -1; listener->m_listener->fd = -1;
std::lock_guard<std::mutex> guard(listener_lock);
all_listeners.remove(listener);
} }
bool Listener::stop() bool Listener::stop()
@ -170,7 +165,7 @@ SListener listener_find(const std::string& name)
for (const auto& a : all_listeners) for (const auto& a : all_listeners)
{ {
if (a->is_active() && a->name() == name) if (a->name() == name)
{ {
rval = a; rval = a;
break; break;
@ -187,7 +182,7 @@ std::vector<SListener> listener_find_by_service(const SERVICE* service)
for (const auto& a : all_listeners) for (const auto& a : all_listeners)
{ {
if (a->is_active() && a->service() == service) if (a->service() == service)
{ {
rval.push_back(a); rval.push_back(a);
} }
@ -616,16 +611,6 @@ json_t* Listener::to_json() const
return rval; return rval;
} }
void Listener::deactivate()
{
m_active = false;
}
bool Listener::is_active() const
{
return m_active;
}
const char* Listener::name() const const char* Listener::name() const
{ {
return m_name.c_str(); return m_name.c_str();

View File

@ -236,12 +236,6 @@ Service::~Service()
{ {
mxs_rworker_delete_data(m_wkey); mxs_rworker_delete_data(m_wkey);
for (const auto& tmp : listener_find_by_service(this))
{
mxb_assert(!tmp->is_active() || maxscale_teardown_in_progress());
listener_free(listener_find(tmp->name()));
}
if (router && router_instance && router->destroyInstance) if (router && router_instance && router->destroyInstance)
{ {
router->destroyInstance(router_instance); router->destroyInstance(router_instance);
@ -538,7 +532,7 @@ bool service_remove_listener(Service* service, const char* target)
if (listener && listener->service() == service) if (listener && listener->service() == service)
{ {
listener->close(); Listener::destroy(listener);
rval = true; rval = true;
} }