From cf9cc9b59586bcdb410d03c6b26408eb4eb75414 Mon Sep 17 00:00:00 2001 From: Mark Tomlinson Date: Mon, 29 Feb 2016 13:21:57 +1300 Subject: [PATCH] Ensure sockets are closed when sessions are closed The cSession class had a per-thread variable m_sockets. This allowed different threads to share the same session, but each would have a different socket to communicate with the client. This was implemented using GStaticPrivate, but as this had been deprecated, changed to GPrivate. The documentation for GPrivate states: "It is not possible to destroy a GPrivate after it has been used. As such, it is only ever acceptable to use GPrivate in static scope, and even then sparingly so." This is not true for the m_sockets variable. To achieve the same thing, a hash table is used to allow a different socket per thread. When the cSession class is deleted, all sockets are closed as the hash table is freed up. Signed-off-by: Mark Tomlinson --- baselib/session.cpp | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/baselib/session.cpp b/baselib/session.cpp index c5edfc8..ca9c2d7 100644 --- a/baselib/session.cpp +++ b/baselib/session.cpp @@ -103,11 +103,7 @@ private: SaHpiDomainIdT m_did; SaHpiSessionIdT m_sid; SaHpiSessionIdT m_remote_sid; -#if GLIB_CHECK_VERSION (2, 32, 0) - GPrivate m_sockets; -#else - GStaticPrivate m_sockets; -#endif + GHashTable * m_sockets; }; @@ -117,16 +113,12 @@ cSession::cSession() m_sid( 0 ), m_remote_sid( 0 ) { - #if GLIB_CHECK_VERSION (2, 32, 0) - m_sockets = G_PRIVATE_INIT (g_free); - #else - wrap_g_static_private_init( &m_sockets ); - #endif + m_sockets = g_hash_table_new_full( g_direct_hash, g_direct_equal, 0, DeleteSock ); } cSession::~cSession() { - wrap_g_static_private_free( &m_sockets ); + g_hash_table_destroy( m_sockets ); } SaErrorT cSession::GetEntityRoot( SaHpiEntityPathT& entity_root ) const @@ -213,11 +205,9 @@ SaErrorT cSession::DoRpc( uint32_t id, } } - #if GLIB_CHECK_VERSION (2, 32, 0) - wrap_g_static_private_set( &m_sockets, 0);// close socket - #else - wrap_g_static_private_set( &m_sockets, 0, 0 ); // close socket - #endif + ohc_lock(); + g_hash_table_remove( m_sockets, (void *)pthread_self() ); // close socket + ohc_unlock(); g_usleep( NEXT_RPC_ATTEMPT_TIMEOUT ); } if ( !rc ) { @@ -240,7 +230,9 @@ SaErrorT cSession::DoRpc( uint32_t id, SaErrorT cSession::GetSock( cClientStreamSock * & sock ) { - gpointer ptr = wrap_g_static_private_get( &m_sockets ); + ohc_lock(); + gpointer ptr = g_hash_table_lookup( m_sockets, (void *)pthread_self() ); + ohc_unlock(); if ( ptr ) { sock = reinterpret_cast(ptr); } else { @@ -266,11 +258,9 @@ SaErrorT cSession::GetSock( cClientStreamSock * & sock ) /* keepalive_intvl */ 1, /* keepalive_probes */ 3 ); - #if GLIB_CHECK_VERSION (2, 32, 0) - wrap_g_static_private_set( &m_sockets, sock ); - #else - wrap_g_static_private_set( &m_sockets, sock, DeleteSock ); - #endif + ohc_lock(); + g_hash_table_insert ( m_sockets, (void *)pthread_self(), sock ); + ohc_unlock(); } return SA_OK; -- 2.7.2