Commit e52d2f77 authored by Sonja Happ's avatar Sonja Happ Committed by Steffen Vogel

improve unthreaded implementation of MQTT node type

- set cancel type of MQTT communication management thread to asynchronous
- fix removing node from list in mqtt_stop
- add a debug output upon invocation of pthread cancel in mqtt_type_stop
- contributes to #248
parent 3624cb9f
......@@ -37,6 +37,13 @@ static pthread_t thread;
static void * mosquitto_loop_thread(void *ctx)
{
int ret;
// set the cancel type of this thread to async
ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, nullptr);
if (ret != 0) {
error("Unable to set cancel type of MQTT communication thread to asynchronous.");
return nullptr;
}
while(true){
for (unsigned i = 0; i < vlist_length(&clients); i++) {
struct node *node = (struct node *) vlist_at(&clients, i);
......@@ -382,14 +389,15 @@ int mqtt_stop(struct node *n)
int ret;
struct mqtt *m = (struct mqtt *) n->_vd;
// unregister client from global MQTT client list
// so that mosquitto loop is no longer invoked for this client
// important to do that before disconnecting from broker, otherwise, mosquitto thread will attempt to reconnect
vlist_remove(&clients, vlist_index(&clients, n));
ret = mosquitto_disconnect(m->client);
if (ret)
goto mosquitto_error;
// unregister client from global MQTT client list
// so that mosquitto loop is no longer invoked for this client
vlist_remove_all(&clients, n);
ret = io_destroy(&m->io);
if (ret)
return ret;
......@@ -437,6 +445,7 @@ int mqtt_type_stop()
ret = pthread_cancel(thread);
if (ret)
return ret;
debug( 3, "Called pthread_cancel() on MQTT communication management thread.");
ret = pthread_join(thread, nullptr);
if (ret) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment