Skip to content

Commit

Permalink
Merge pull request #25 from paullouisageneau/synchronize-client-start
Browse files Browse the repository at this point in the history
Fix synchronization of client_start()
  • Loading branch information
paullouisageneau authored Aug 26, 2024
2 parents ca2fa31 + 1764044 commit 9d9628d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,22 @@ void client_destroy(client_t *client) {
}

int client_start(client_t *client) {
mutex_lock(&client->protocol_mutex);
if (client->is_started) {
PLUM_LOG_DEBUG("Client is already started");
mutex_unlock(&client->protocol_mutex);
return 0;
}

int ret = thread_init(&client->thread, client_thread_entry, client);
if (ret) {
PLUM_LOG_FATAL("Thread creation failed, error=%d", ret);
mutex_unlock(&client->protocol_mutex);
return -1;
}

client->is_started = true;
mutex_unlock(&client->protocol_mutex);
return 0;
}

Expand Down Expand Up @@ -474,12 +478,14 @@ int client_run_protocol(client_t *client, const protocol_t *protocol,
}

int client_interrupt(client_t *client, bool stop) {
if (!client->is_started)
return PROTOCOL_ERR_SUCCESS;

PLUM_LOG_DEBUG("Interrupting protocol");
mutex_lock(&client->protocol_mutex);

if (!client->is_started) {
mutex_unlock(&client->protocol_mutex);
return PROTOCOL_ERR_SUCCESS;
}

if (stop)
atomic_store(&client->is_stopping, true);

Expand Down

0 comments on commit 9d9628d

Please sign in to comment.