Changed overly complicated HTTP proxy thread handling to automatic, more sane, handling.

This commit is contained in:
Thulinma 2013-04-06 21:38:41 +02:00
parent c1844e2ee9
commit a0b1b1a8b3

View file

@ -56,9 +56,6 @@ namespace Connector_HTTP {
}; };
std::map<std::string, ConnConn *> connectorConnections; ///< Connections to connectors std::map<std::string, ConnConn *> connectorConnections; ///< Connections to connectors
std::set<tthread::thread *> activeThreads; ///< Holds currently active threads
std::set<tthread::thread *> doneThreads; ///< Holds threads that are done and ready to be joined.
tthread::mutex threadMutex; ///< Mutex for adding/removing threads.
tthread::mutex connMutex; ///< Mutex for adding/removing connector connections. tthread::mutex connMutex; ///< Mutex for adding/removing connector connections.
tthread::mutex timeoutMutex; ///< Mutex for timeout thread. tthread::mutex timeoutMutex; ///< Mutex for timeout thread.
tthread::thread * timeouter = 0; ///< Thread that times out connections to connectors. tthread::thread * timeouter = 0; ///< Thread that times out connections to connectors.
@ -539,17 +536,6 @@ namespace Connector_HTTP {
//close and remove the connection //close and remove the connection
conn->close(); conn->close();
delete conn; delete conn;
//remove this thread from activeThreads and add it to doneThreads.
threadMutex.lock();
for (std::set<tthread::thread *>::iterator it = activeThreads.begin(); it != activeThreads.end(); it++){
if (( *it)->get_id() == tthread::this_thread::get_id()){
tthread::thread * T = ( *it);
activeThreads.erase(T);
doneThreads.insert(T);
break;
}
}
threadMutex.unlock();
} }
} //Connector_HTTP namespace } //Connector_HTTP namespace
@ -567,41 +553,14 @@ int main(int argc, char ** argv){
while (server_socket.connected() && conf.is_active){ while (server_socket.connected() && conf.is_active){
Socket::Connection S = server_socket.accept(); Socket::Connection S = server_socket.accept();
if (S.connected()){ //check if the new connection is valid if (S.connected()){ //check if the new connection is valid
//lock the thread mutex and spawn a new thread for this connection //spawn a new thread for this connection
Connector_HTTP::threadMutex.lock();
tthread::thread * T = new tthread::thread(Connector_HTTP::proxyHandleHTTPConnection, (void *)(new Socket::Connection(S))); tthread::thread * T = new tthread::thread(Connector_HTTP::proxyHandleHTTPConnection, (void *)(new Socket::Connection(S)));
Connector_HTTP::activeThreads.insert(T); //detach it, no need to keep track of it anymore
//clean up any threads that may have finished T->detach();
while ( !Connector_HTTP::doneThreads.empty()){
T = *Connector_HTTP::doneThreads.begin();
T->join();
Connector_HTTP::doneThreads.erase(T);
delete T;
}
Connector_HTTP::threadMutex.unlock();
}else{ }else{
Util::sleep(10); //sleep 10ms Util::sleep(10); //sleep 10ms
} }
} //while connected and not requested to stop } //while connected and not requested to stop
server_socket.close(); server_socket.close();
//wait for existing connections to drop
bool repeat = true;
while (repeat){
Connector_HTTP::threadMutex.lock();
repeat = !Connector_HTTP::activeThreads.empty();
//clean up any threads that may have finished
while ( !Connector_HTTP::doneThreads.empty()){
tthread::thread * T = *Connector_HTTP::doneThreads.begin();
T->join();
Connector_HTTP::doneThreads.erase(T);
delete T;
}
Connector_HTTP::threadMutex.unlock();
if (repeat){
Util::sleep(100); //sleep 100ms
}
}
return 0; return 0;
} //main } //main