net: lib: coap_client: Remove unnecessary atomic variable

In receiving thread, continuing the loops is based on
has_ongoing_exchanges() so it does not need atomic
coap_client_recv_active variable.

When idling, it wakes from semaphore. But there was potential
deadlock when coap_client_schedule_poll() would not signal the
semaphore, if atomic variable was already showing that it runs.
Removing the atomic variable removes this deadlock.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
This commit is contained in:
Seppo Takalo 2024-10-28 13:29:14 +02:00 committed by Mahesh Mahadevan
parent 9c9dc9f760
commit 1e5a537ade

View file

@ -24,7 +24,6 @@ static K_MUTEX_DEFINE(coap_client_mutex);
static struct coap_client *clients[CONFIG_COAP_CLIENT_MAX_INSTANCES];
static int num_clients;
static K_SEM_DEFINE(coap_client_recv_sem, 0, 1);
static atomic_t coap_client_recv_active;
static bool timeout_expired(struct coap_client_internal_request *internal_req);
static void cancel_requests_with(struct coap_client *client, int error);
@ -82,10 +81,7 @@ static int coap_client_schedule_poll(struct coap_client *client, int sock,
memcpy(&internal_req->coap_request, req, sizeof(struct coap_client_request));
internal_req->request_ongoing = true;
if (!coap_client_recv_active) {
k_sem_give(&coap_client_recv_sem);
}
atomic_set(&coap_client_recv_active, 1);
k_sem_give(&coap_client_recv_sem);
return 0;
}
@ -956,7 +952,6 @@ static void cancel_requests_with(struct coap_client *client, int error)
reset_internal_request(&client->requests[i]);
}
}
atomic_clear(&coap_client_recv_active);
k_mutex_unlock(&client->lock);
}
@ -974,7 +969,6 @@ void coap_client_recv(void *coap_cl, void *a, void *b)
k_sem_take(&coap_client_recv_sem, K_FOREVER);
while (true) {
atomic_set(&coap_client_recv_active, 1);
ret = handle_poll();
if (ret < 0) {
/* Error in polling */
@ -987,7 +981,6 @@ void coap_client_recv(void *coap_cl, void *a, void *b)
continue;
} else {
idle:
atomic_set(&coap_client_recv_active, 0);
k_sem_take(&coap_client_recv_sem, K_FOREVER);
}
}