Skip to content

Commit 891380b

Browse files
feat: Make state and size atomic
This makes the mqtt client state atomic to reduce the scope of locking in some parts of the code.
1 parent fa88da5 commit 891380b

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

lib/include/mqtt_client_priv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ struct esp_mqtt_client {
114114
esp_transport_handle_t transport;
115115
mqtt_config_storage_t *config;
116116
mqtt_state_t mqtt_state;
117-
mqtt_client_state_t state;
117+
_Atomic mqtt_client_state_t state;
118118
uint64_t refresh_connection_tick;
119119
int64_t keepalive_tick;
120120
uint64_t reconnect_tick;

lib/mqtt_outbox.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typedef struct outbox_item {
2424
STAILQ_HEAD(outbox_list_t, outbox_item);
2525

2626
struct outbox_t {
27-
uint64_t size;
27+
_Atomic uint64_t size;
2828
struct outbox_list_t *list;
2929
};
3030

mqtt_client.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,8 @@ static void esp_mqtt_task(void *pv)
15851585
while (client->run) {
15861586
MQTT_API_LOCK(client);
15871587
run_event_loop(client);
1588-
switch (client->state) {
1588+
mqtt_client_state_t state = client->state;
1589+
switch (state) {
15891590
case MQTT_STATE_DISCONNECTED:
15901591
break;
15911592
case MQTT_STATE_INIT:
@@ -1874,12 +1875,13 @@ int esp_mqtt_client_subscribe_multiple(esp_mqtt_client_handle_t client,
18741875
if (client->config->outbox_limit > 0 && outbox_get_size(client->outbox) > client->config->outbox_limit) {
18751876
return -2;
18761877
}
1877-
MQTT_API_LOCK(client);
1878+
18781879
if (client->state != MQTT_STATE_CONNECTED) {
18791880
ESP_LOGE(TAG, "Client has not connected");
1880-
MQTT_API_UNLOCK(client);
18811881
return -1;
18821882
}
1883+
1884+
MQTT_API_LOCK(client);
18831885
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
18841886
#ifdef MQTT_PROTOCOL_5
18851887
int max_qos = topic_list[0].qos;
@@ -1942,12 +1944,11 @@ int esp_mqtt_client_unsubscribe(esp_mqtt_client_handle_t client, const char *top
19421944
ESP_LOGE(TAG, "Client was not initialized");
19431945
return -1;
19441946
}
1945-
MQTT_API_LOCK(client);
19461947
if (client->state != MQTT_STATE_CONNECTED) {
1947-
MQTT_API_UNLOCK(client);
19481948
ESP_LOGE(TAG, "Client has not connected");
19491949
return -1;
19501950
}
1951+
MQTT_API_LOCK(client);
19511952
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
19521953
#ifdef MQTT_PROTOCOL_5
19531954
mqtt5_msg_unsubscribe(&client->mqtt_state.connection,
@@ -2049,15 +2050,14 @@ int esp_mqtt_client_publish(esp_mqtt_client_handle_t client, const char *topic,
20492050
ESP_LOGE(TAG, "Client was not initialized");
20502051
return -1;
20512052
}
2052-
MQTT_API_LOCK(client);
20532053
#if MQTT_SKIP_PUBLISH_IF_DISCONNECTED
20542054
if (client->state != MQTT_STATE_CONNECTED) {
20552055
ESP_LOGI(TAG, "Publishing skipped: client is not connected");
2056-
MQTT_API_UNLOCK(client);
20572056
return -1;
20582057
}
20592058
#endif
20602059

2060+
MQTT_API_LOCK(client);
20612061
#ifdef MQTT_PROTOCOL_5
20622062
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
20632063
if (esp_mqtt5_client_publish_check(client, qos, retain) != ESP_OK) {

0 commit comments

Comments
 (0)