Fixed HTTP proxy race condition.

This commit is contained in:
Thulinma 2013-09-30 16:04:06 +02:00
parent e06ac66757
commit 898cc961ad

View file

@ -58,6 +58,8 @@ namespace Connector_HTTP {
std::map<std::string, ConnConn *> connectorConnections; ///< Connections to connectors
tthread::mutex connMutex; ///< Mutex for adding/removing connector connections.
bool timeoutThreadStarted = false;
tthread::mutex timeoutStartMutex; ///< Mutex for starting timeout thread.
tthread::mutex timeoutMutex; ///< Mutex for timeout thread.
tthread::thread * timeouter = 0; ///< Thread that times out connections to connectors.
JSON::Value capabilities; ///< Holds a list of all HTTP connectors and their properties
@ -67,6 +69,7 @@ namespace Connector_HTTP {
void proxyTimeoutThread(void * n){
n = 0; //prevent unused variable warning
tthread::lock_guard<tthread::mutex> guard(timeoutMutex);
timeoutThreadStarted = true;
while (true){
{
tthread::lock_guard<tthread::mutex> guard(connMutex);
@ -414,14 +417,18 @@ namespace Connector_HTTP {
std::cout << "Re-using connection " << uid << std::endl;
#endif
}
//start a new timeout thread, if neccesary
{//start a new timeout thread, if neccesary
tthread::lock_guard<tthread::mutex> guard(timeoutStartMutex);
if (timeoutMutex.try_lock()){
if (timeouter){
timeouter->join();
delete timeouter;
}
timeoutThreadStarted = false;
timeouter = new tthread::thread(Connector_HTTP::proxyTimeoutThread, 0);
timeoutMutex.unlock();
while (!timeoutThreadStarted){Util::sleep(10);}
}
}
//lock the mutex for this connection, and handle the request