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

View File

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

View File

@ -45,7 +45,7 @@ void ServerInit(Server_t *server)
#define SEND_BUFFER_SIZE 80 * 24
#define RECEIVE_BUFFER_SIZE 80
void *serverCommunicationInstance(void *server)
void *serverCommunicationInstance(void *serverCom)
{
ServerCommunication_t *args;
char **argv = NULL;
@ -54,7 +54,7 @@ void *serverCommunicationInstance(void *server)
int tokenIndex, bytesReceived, clientPort;
char clientIP[16];
args = (ServerCommunication_t*) server;
args = (ServerCommunication_t*) serverCom;
// Get ip address from client
inet_ntop(AF_INET,
@ -249,6 +249,12 @@ static void *serverMain(void *server)
}
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);
printLog("Server #%lu offline\n", args->id);
return NULL;