Fix a deadlock problem created when ws initiated on linux #847 #730

This commit is contained in:
John M. Penn 2019-08-22 15:35:17 -05:00
parent 2afa2b3e38
commit d0cbd1f2b5
4 changed files with 11 additions and 4 deletions

View File

@ -30,6 +30,7 @@ class HTTP_Server {
pthread_mutex_t WebSocketSessionMakerMapLock; /* ** */
std::map<mg_connection*, WebSocketSession*> sessionMap; /* ** */
pthread_mutex_t sessionMapLock; /* ** */
pthread_mutex_t serviceLock; /* ** */
struct mg_serve_http_opts http_server_options; /* ** mongoose*/
struct mg_bind_opts bind_opts; /* ** mongoose*/
pthread_cond_t serviceConnections; /* ** */

View File

@ -14,6 +14,7 @@ class HttpSimObject : public Trick::SimObject {
HttpSimObject() {
("default_data") server.http_default_data() ;
("initialization") server.http_init() ;
("freeze") server.http_top_of_frame() ;
("top_of_frame") server.http_top_of_frame() ;
("shutdown") server.http_shutdown() ;
}

View File

@ -8,6 +8,7 @@ LIBRARY DEPENDENCIES:
#include <string>
#include <sstream>
#include <iomanip> // for setprecision
#include <algorithm>
#include "trick/memorymanager_c_intf.h"
#include "trick/input_processor_proto.h"
#include "trick/exec_proto.h"

View File

@ -34,6 +34,8 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
nc->flags |= MG_F_SEND_AND_CLOSE;
std::cout << "ERROR: No such web socket interface: \"" << uri << "\"." << std::endl;
}
} else {
std::cout << "ERROR: WEBSOCKET_REQUEST URI does not start with API prefix." << std::endl;
}
} break;
case MG_EV_WEBSOCKET_FRAME: { // Process websocket messages from the client (web browser).
@ -87,16 +89,17 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
static void* connectionAttendant (void* arg) {
HTTP_Server *S = (HTTP_Server*)arg;
while(1) {
pthread_mutex_lock(&S->sessionMapLock);
pthread_mutex_lock(&S->serviceLock);
// Wait here until the serviceConnections condition is signaled by the top_of_frame job.
pthread_cond_wait(&S->serviceConnections, &S->sessionMapLock);
pthread_cond_wait(&S->serviceConnections, &S->serviceLock);
if (S->shutting_down) {
pthread_mutex_unlock(&S->sessionMapLock);
pthread_mutex_unlock(&S->serviceLock);
return NULL;
} else {
mg_mgr_poll(&S->mgr, 50);
}
pthread_mutex_unlock(&S->sessionMapLock);
pthread_mutex_unlock(&S->serviceLock);
}
return NULL;
}
@ -180,6 +183,7 @@ void HTTP_Server::handleWebSocketClientMessage(struct mg_connection *nc, std::st
void HTTP_Server::addWebSocketSession(struct mg_connection *nc, WebSocketSession* session) {
pthread_mutex_lock(&sessionMapLock);
sessionMap.insert( std::pair<mg_connection*, WebSocketSession*>(nc, session) );
pthread_mutex_unlock(&sessionMapLock);
}