From 91ede87e8c913b5141812d84b339c219d83327bd Mon Sep 17 00:00:00 2001 From: overflowerror Date: Thu, 29 Apr 2021 14:52:11 +0200 Subject: [PATCH] removed unnecessary helper threads and pipes --- src/networking.c | 111 +++-------------------------------------------- src/networking.h | 5 --- 2 files changed, 6 insertions(+), 110 deletions(-) diff --git a/src/networking.c b/src/networking.c index 2ccb0db..c47fbf8 100644 --- a/src/networking.c +++ b/src/networking.c @@ -130,15 +130,6 @@ void cleanup() { pthread_cancel(connection->threads.response); pthread_join(connection->threads.response, NULL); } - if (connection->threads.helper[0] != PTHREAD_NULL) { - pthread_cancel(connection->threads.helper[0]); - pthread_join(connection->threads.helper[0], NULL); - } - if (connection->threads.helper[1] != PTHREAD_NULL) { - pthread_cancel(connection->threads.helper[1]); - pthread_join(connection->threads.helper[1], NULL); - } - if (connection->peer.name != NULL) free(connection->peer.name); @@ -247,9 +238,8 @@ void safeEndConnection(struct connection* connection, bool force) { stopThread(self, &(connection->threads.request), true); stopThread(self, &(connection->threads.response), force); - stopThread(self, &(connection->threads.helper[0]), force); - stopThread(self, &(connection->threads.helper[1]), force); + // close socket close(connection->readfd); close(connection->writefd); @@ -268,7 +258,7 @@ int sendHeader(int statusCode, struct headers* headers, struct request* request) } struct connection* connection = (struct connection*) request->_private; - int fd = connection->threads.responseFd; + int fd = connection->writefd; FILE* stream = fdopen(dup(fd), "w"); if (stream == NULL) { @@ -284,7 +274,6 @@ int sendHeader(int statusCode, struct headers* headers, struct request* request) headers_dump(headers, stream); - fprintf(stream, "\r\n"); fclose(stream); @@ -302,7 +291,7 @@ void* responseThread(void* data) { connection->threads.handler.handler((struct request) { .metaData = connection->metaData, .headers = &(connection->headers), - .fd = connection->threads.requestFd, + .fd = connection->readfd, .peer = connection->peer, .userData = connection->threads.handler.data, ._private = connection @@ -313,18 +302,12 @@ void* responseThread(void* data) { debug("networking: response handler returned"); safeEndConnection(connection, false); - - close(connection->threads.requestFd); - close(connection->threads._responseFd); - - close(connection->threads._requestFd); - close(connection->threads.responseFd); return NULL; } /* - * This thread handles finding a handler and handles pipes + * This thread handles finding a handler and handler timeout */ void* requestThread(void* data) { struct connection* connection = (struct connection*) data; @@ -340,80 +323,9 @@ void* requestThread(void* data) { connection->threads.handler = handler; - int pipefd[2]; - if (pipe(&(pipefd[0])) < 0) { - error("networking: Couldn't create reponse pipe: %s", strerror(errno)); - warn("Aborting request."); - - pthread_mutex_lock(&(connection->lock)); - connection->state = ABORTED; - connection->inUse--; - pthread_mutex_unlock(&(connection->lock)); - - return NULL; - } - - int request = pipefd[1]; - connection->threads._requestFd = request; - connection->threads.requestFd = pipefd[0]; - - if (pipe(&(pipefd[0])) < 0) { - close(request); - close(connection->threads.requestFd); - - error("networking: Couldn't create reponse pipe: %s", strerror(errno)); - warn("Aborting request."); - - pthread_mutex_lock(&(connection->lock)); - connection->state = ABORTED; - connection->inUse--; - pthread_mutex_unlock(&(connection->lock)); - - return NULL; - } - - int response = pipefd[0]; - connection->threads._responseFd = response; - connection->threads.responseFd = pipefd[1]; - - if (startCopyThread(connection->readfd, request, true, &(connection->threads.helper[0])) < 0) { - close(request); - close(connection->threads.requestFd); - close(response); - close(connection->threads.responseFd); - - error("networking: Couldn't start helper thread."); - warn("networking: Aborting request."); - - pthread_mutex_lock(&(connection->lock)); - connection->state = ABORTED; - connection->inUse--; - pthread_mutex_unlock(&(connection->lock)); - - return NULL; - } - if (startCopyThread(response, connection->writefd, false, &(connection->threads.helper[1])) < 0) { - close(request); - close(connection->threads.requestFd); - close(response); - close(connection->threads.responseFd); - - error("networking: Couldn't start helper thread."); - warn("networking: Aborting request."); - - pthread_mutex_lock(&(connection->lock)); - connection->state = ABORTED; - connection->inUse--; - pthread_mutex_unlock(&(connection->lock)); - - return NULL; - } - if (pthread_create(&(connection->threads.response), NULL, &responseThread, connection) < 0) { - close(request); - close(connection->threads.requestFd); - close(response); - close(connection->threads.responseFd); + close(connection->readfd); + close(connection->writefd); error("networking: Couldn't start response thread."); warn("networking: Aborting request."); @@ -433,10 +345,6 @@ void* requestThread(void* data) { error("networking: Timeout of handler."); error("networking: Aborting"); - close(request); - close(connection->threads.requestFd); - close(response); - close(connection->threads.responseFd); safeEndConnection(connection, true); @@ -816,14 +724,7 @@ void* listenThread(void* _bind) { .request = PTHREAD_NULL, .response = PTHREAD_NULL, .handler = {}, - .requestFd = -1, - .responseFd = -1, - ._requestFd = -1, - ._responseFd = -1 }; - // TODO see above - connection->threads.helper[0] = PTHREAD_NULL; - connection->threads.helper[1] = PTHREAD_NULL; connection->currentHeaderLength = 0; connection->currentHeader = NULL; connection->inUse = 0; diff --git a/src/networking.h b/src/networking.h index 7e963eb..18bcb6e 100644 --- a/src/networking.h +++ b/src/networking.h @@ -35,12 +35,7 @@ typedef struct handler (*handlerGetter_t)(struct metaData metaData, const char* struct threads { pthread_t request; pthread_t response; - pthread_t helper[2]; struct handler handler; - int requestFd; - int responseFd; - int _requestFd; - int _responseFd; }; struct connection {