diff options
| author | ckrinitsin <101062646+ckrinitsin@users.noreply.github.com> | 2025-03-23 20:55:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-23 20:55:14 +0100 |
| commit | 66d87280633590818261b89c50fe1830092174e4 (patch) | |
| tree | d9231a5f2d52249657c7523af9c2ff11bdbe4196 /src/client/client.cpp | |
| parent | 3939238f3fe46ed36919f29cbebe824341689960 (diff) | |
| parent | d6ea41e1bef4a312a8a1d6683bb29aad777ec9ae (diff) | |
| download | BT-Programming-Assignment-66d87280633590818261b89c50fe1830092174e4.tar.gz BT-Programming-Assignment-66d87280633590818261b89c50fe1830092174e4.zip | |
Fixes
Diffstat (limited to 'src/client/client.cpp')
| -rw-r--r-- | src/client/client.cpp | 24 |
1 files changed, 6 insertions, 18 deletions
diff --git a/src/client/client.cpp b/src/client/client.cpp index cbaddeb..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 - 1; i != shared_memory->tail; i = (i - 1) % QUEUE_SIZE) { - if (i == index) { - return false; - } - } - - return shared_memory->tail != index; + 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->head; + 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->head = (1 + shared_memory->head) % QUEUE_SIZE; - shared_memory->full = shared_memory->head == shared_memory->tail; + shared_memory->tail = (1 + shared_memory->tail) % QUEUE_SIZE; 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; } |