Corrected incorrect thread exiting in server.c

This commit is contained in:
Adrien Bourmault 2021-09-29 17:53:01 +02:00
parent 0b0ecbff6b
commit 942c2db6ba
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
3 changed files with 30 additions and 16 deletions

View File

@ -96,7 +96,7 @@ int ModelUnload(int id)
if (id-1 < loadedModelSize) { if (id-1 < loadedModelSize) {
memmove(&loadedModel[id-1], memmove(&loadedModel[id-1],
&loadedModel[id-1] + sizeof(Model_t*), &loadedModel[id-1] + sizeof(Model_t*),
loadedModelSize - (id-1)); loadedModelSize - id);
} }
// Decrement loaded model index // Decrement loaded model index
@ -113,13 +113,13 @@ int ModelUnload(int id)
int ModelRun(int id) int ModelRun(int id)
{ {
if (id <= 0 || id > loadedModelSize) if (id <= 0 || id > loadedModelSize)
return 0; return -1;
if (!loadedModel[id-1]->scheduler) if (!loadedModel[id-1]->scheduler)
return 0; return -2;
if (loadedModel[id-1]->isRunning) if (loadedModel[id-1]->isRunning)
return 0; return -3;
loadedModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread; loadedModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread;
loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles; loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles;
@ -129,16 +129,19 @@ int ModelRun(int id)
SchedInit(loadedModel[id-1]->scheduler); SchedInit(loadedModel[id-1]->scheduler);
printLog("Model %d launched\n", id); printLog("Model %d launched\n", id);
return 1; return 0;
} }
int ModelStop(int id) int ModelStop(int id)
{ {
if (id <= 0 || id > loadedModelSize) { if (id <= 0 || id > loadedModelSize) {
return 0; return -1;
} }
if (!loadedModel[id-1]->scheduler) { if (!loadedModel[id-1]->scheduler) {
return 0; return -2;
}
if (!loadedModel[id-1]->isRunning) {
return -3;
} }
// Stop model scheduler // Stop model scheduler
@ -150,12 +153,12 @@ int ModelStop(int id)
// Disable running bit // Disable running bit
loadedModel[id-1]->isRunning = false; loadedModel[id-1]->isRunning = false;
return 1; return 0;
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
void ModelAddToKnown(Model_t **newModel) // TODO manage deletion and empty slots void ModelAddToKnown(Model_t **newModel)
{ {
// increment index // increment index
knownModelSize++; knownModelSize++;
@ -192,7 +195,7 @@ void ModelAddToKnown(Model_t **newModel) // TODO manage deletion and empty slots
knownModel[knownModelSize-1]->siteNumber = 0; knownModel[knownModelSize-1]->siteNumber = 0;
} }
void ModelDelete(int id) //XXX void ModelDelete(int id) //XXX fragmentation
{ {
// Free a model structure // Free a model structure
free(knownModel[id-1]->name); free(knownModel[id-1]->name);
@ -209,8 +212,6 @@ void ModelDelete(int id) //XXX
free(knownModel[id-1]); free(knownModel[id-1]);
knownModel[id-1] = NULL; knownModel[id-1] = NULL;
knownModelSize--;
} }
void ModelShutdown(void) void ModelShutdown(void)

View File

@ -295,6 +295,11 @@ int parseParentFieldPropsXML (xmlDocPtr doc,
} }
currentNode = currentNode->next; currentNode = currentNode->next;
} }
free(id);
free(date);
free(author);
return 0; return 0;
} }
@ -353,7 +358,7 @@ int ParseModelIdentityXML(Model_t *model, Parameters_t *params)
printLog("Preparsing model %s\n", model->name); printLog("Preparsing model %s\n", model->name);
// Opening document // Opening document
xmlDocument = xmlParseFile(model->filename); xmlDocument = xmlReadFile(model->filename, NULL, 0);
if (xmlDocument == NULL) { if (xmlDocument == NULL) {
printErr("Can't parse model file at '%s'.\n", model->filename); printErr("Can't parse model file at '%s'.\n", model->filename);
@ -468,6 +473,7 @@ int ParseModelIdentityXML(Model_t *model, Parameters_t *params)
free(version); free(version);
xmlFreeDoc(xmlDocument); xmlFreeDoc(xmlDocument);
free(schemPath); free(schemPath);
xmlCleanupParser();
return 0; return 0;
} }
@ -517,7 +523,7 @@ int ParseModelXML(Model_t *model)
printLog("Parsing model %s\n", model->name); printLog("Parsing model %s\n", model->name);
xmlDocument = xmlParseFile(model->filename); xmlDocument = xmlReadFile(model->filename, NULL, 0);
if (xmlDocument == NULL) { if (xmlDocument == NULL) {
printErr("Can't parse model file at '%s'.\n", model->filename); printErr("Can't parse model file at '%s'.\n", model->filename);
@ -557,6 +563,7 @@ int ParseModelXML(Model_t *model)
} }
xmlFreeDoc(xmlDocument); xmlFreeDoc(xmlDocument);
xmlCleanupParser();
return 0; return 0;
} }

View File

@ -45,7 +45,7 @@ void ServerInit(Server_t *server)
#define SEND_BUFFER_SIZE 80 * 24 #define SEND_BUFFER_SIZE 80 * 24
#define RECEIVE_BUFFER_SIZE 80 #define RECEIVE_BUFFER_SIZE 80
void *serverCommunicationInstance(void *server) void *serverCommunicationInstance(void *serverCom)
{ {
ServerCommunication_t *args; ServerCommunication_t *args;
char **argv = NULL; char **argv = NULL;
@ -54,7 +54,7 @@ void *serverCommunicationInstance(void *server)
int tokenIndex, bytesReceived, clientPort; int tokenIndex, bytesReceived, clientPort;
char clientIP[16]; char clientIP[16];
args = (ServerCommunication_t*) server; args = (ServerCommunication_t*) serverCom;
// Get ip address from client // Get ip address from client
inet_ntop(AF_INET, inet_ntop(AF_INET,
@ -249,6 +249,12 @@ static void *serverMain(void *server)
} }
serverExiting: serverExiting:
for (int i; i < serverSlotIndex; i++) {
serverSlots[i].pleaseStop = true;
usleep(10000);
pthread_cancel(serverSlots[i].id);
pthread_join(serverSlots[i].id, NULL);
}
close(args->sockfd); close(args->sockfd);
printLog("Server #%lu offline\n", args->id); printLog("Server #%lu offline\n", args->id);
return NULL; return NULL;