Use Synchronised instead of mutex for next protocol id.

This commit is contained in:
hiker
2016-02-05 08:24:28 +11:00
parent 348a300c09
commit 0d488b916d
2 changed files with 4 additions and 10 deletions

View File

@@ -35,7 +35,6 @@
ProtocolManager::ProtocolManager()
{
pthread_mutex_init(&m_asynchronous_protocols_mutex, NULL);
pthread_mutex_init(&m_id_mutex, NULL);
pthread_mutex_init(&m_exit_mutex, NULL);
m_next_protocol_id = 0;
@@ -77,7 +76,6 @@ void ProtocolManager::abort()
pthread_mutex_unlock(&m_exit_mutex); // will stop the update function
pthread_join(*m_asynchronous_update_thread, NULL); // wait the thread to finish
pthread_mutex_lock(&m_asynchronous_protocols_mutex);
pthread_mutex_lock(&m_id_mutex);
m_protocols.lock();
for (unsigned int i = 0; i < m_protocols.getData().size() ; i++)
@@ -97,10 +95,8 @@ void ProtocolManager::abort()
m_requests.unlock();
pthread_mutex_unlock(&m_asynchronous_protocols_mutex);
pthread_mutex_unlock(&m_id_mutex);
pthread_mutex_destroy(&m_asynchronous_protocols_mutex);
pthread_mutex_destroy(&m_id_mutex);
pthread_mutex_destroy(&m_exit_mutex);
} // abort
@@ -602,10 +598,10 @@ int ProtocolManager::exit()
*/
uint32_t ProtocolManager::getNextProtocolId()
{
pthread_mutex_lock(&m_id_mutex);
m_next_protocol_id.lock();
uint32_t id = m_next_protocol_id;
m_next_protocol_id++;
pthread_mutex_unlock(&m_id_mutex);
m_next_protocol_id.unlock();
return id;
} // getNextProtocolId

View File

@@ -179,13 +179,11 @@ class ProtocolManager : public AbstractSingleton<ProtocolManager>,
* If a protocol has an id lower than this value, it means that it has
* been formerly started.
*/
uint32_t m_next_protocol_id;
Synchronised<uint32_t> m_next_protocol_id;
// mutexes:
/*! Used to ensure that the protocol vector is used thread-safely. */
pthread_mutex_t m_asynchronous_protocols_mutex;
/*! Used to ensure that the protocol id is used in a thread-safe way.*/
pthread_mutex_t m_id_mutex;
/*! Used when need to quit.*/
pthread_mutex_t m_exit_mutex;
@@ -196,6 +194,6 @@ class ProtocolManager : public AbstractSingleton<ProtocolManager>,
/*! True if the thread is running. */
bool m_asynchronous_thread_running;
};
}; // class ProtocolManager
#endif // PROTOCOL_MANAGER_HPP