diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/client.cpp | 20 | ||||
| -rw-r--r-- | src/common/shared_memory.h | 4 | ||||
| -rw-r--r-- | src/server/shared_memory_server.h | 5 |
3 files changed, 9 insertions, 20 deletions
diff --git a/src/client/client.cpp b/src/client/client.cpp index e7853a8..1c0727f 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -91,20 +91,7 @@ bool Client::request_processed(SharedMemory* shared_memory, int index) return false; } - if (shared_memory->full) { - return false; - } - if (shared_memory->tail == shared_memory->head && !shared_memory->full) { - return true; - } - - for (int i = shared_memory->head; i != shared_memory->tail; i = (i + 1) % QUEUE_SIZE) { - if (i == index) { - return false; - } - } - - return true; + return shared_memory->request[index].status == PROCESSED; } int Client::send_request( @@ -117,17 +104,17 @@ int Client::send_request( pthread_mutex_lock(&shared_memory->mutex); - while (shared_memory->full) { + while (shared_memory->request[shared_memory->tail].status != FREE) { pthread_cond_wait(&shared_memory->cond_var, &shared_memory->mutex); } index = shared_memory->tail; Request* request = &shared_memory->request[index]; request->type = type; + request->status = SENT; strncpy(request->key, k.value_or("null").c_str(), MAX_KEY_SIZE); strncpy(request->value, v.value_or("null").c_str(), MAX_VALUE_SIZE); shared_memory->tail = (1 + shared_memory->tail) % QUEUE_SIZE; - shared_memory->full = shared_memory->head == shared_memory->tail; pthread_cond_signal(&shared_memory->cond_var); pthread_mutex_unlock(&shared_memory->mutex); @@ -147,6 +134,7 @@ std::string Client::process_respond(SharedMemory* shared_memory, int index) pthread_cond_wait(&shared_memory->cond_var, &shared_memory->mutex); } std::string result(shared_memory->request[index].response); + shared_memory->request[index].status = FREE; pthread_mutex_unlock(&shared_memory->mutex); return result; } diff --git a/src/common/shared_memory.h b/src/common/shared_memory.h index fe101ea..1798ad1 100644 --- a/src/common/shared_memory.h +++ b/src/common/shared_memory.h @@ -14,11 +14,14 @@ */ enum Operations { INSERT, DELETE, GET, PRINT }; +enum Status { FREE, SENT, PROCESSED}; + /** * @brief One request constists out of the operation, the arguments and the response. */ struct Request { Operations type; + Status status = FREE; char key[MAX_KEY_SIZE]; char value[MAX_VALUE_SIZE]; char response[MAX_VALUE_SIZE]; @@ -38,7 +41,6 @@ struct SharedMemory { int tail; int head; - bool full; }; /** diff --git a/src/server/shared_memory_server.h b/src/server/shared_memory_server.h index d4be69f..e8e5c38 100644 --- a/src/server/shared_memory_server.h +++ b/src/server/shared_memory_server.h @@ -34,7 +34,6 @@ public: mmap(0, sizeof(SharedMemory), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); shared_memory->tail = shared_memory->head = 0; - shared_memory->full = false; pthread_mutexattr_t mutex_attr; pthread_condattr_t cond_attr; @@ -74,7 +73,7 @@ public: while (true) { pthread_mutex_lock(&shared_memory->mutex); - if (shared_memory->tail == shared_memory->head && !shared_memory->full) { + if (shared_memory->request[shared_memory->head].status != SENT) { pthread_cond_wait(&shared_memory->cond_var, &shared_memory->mutex); } @@ -140,8 +139,8 @@ public: default: break; } + shared_memory->request[shared_memory->head].status = PROCESSED; shared_memory->head = (1 + shared_memory->head) % QUEUE_SIZE; - shared_memory->full = false; pthread_cond_signal(&shared_memory->cond_var); pthread_mutex_unlock(&shared_memory->mutex); } |