4141#include "platform/iot_clock.h"
4242#include "platform/iot_threads.h"
4343
44+ /* Atomics include. */
45+ #include "iot_atomic.h"
46+
4447/* Validate MQTT configuration settings. */
4548#if IOT_MQTT_ENABLE_ASSERTS != 0 && IOT_MQTT_ENABLE_ASSERTS != 1
4649 #error "IOT_MQTT_ENABLE_ASSERTS must be 0 or 1."
6063
6164/*-----------------------------------------------------------*/
6265
66+ /**
67+ * @brief Uninitialized value for @ref _initCalled.
68+ */
69+ #define MQTT_LIBRARY_UNINITIALIZED ( ( uint32_t ) 0 )
70+
71+ /**
72+ * @brief Initialized value for @ref _initCalled.
73+ */
74+ #define MQTT_LIBRARY_INITIALIZED ( ( uint32_t ) 1 )
75+
76+ /*-----------------------------------------------------------*/
77+
6378/**
6479 * @brief Check if the library is initialized.
6580 *
@@ -243,15 +258,15 @@ static IotMqttError_t _subscriptionCommon( IotMqttOperationType_t operation,
243258 *
244259 * API functions will fail if @ref mqtt_function_init was not called.
245260 */
246- static uint32_t _initCalled = 0 ;
261+ static volatile uint32_t _initCalled = MQTT_LIBRARY_UNINITIALIZED ;
247262
248263/*-----------------------------------------------------------*/
249264
250265static bool _checkInit ( void )
251266{
252267 bool status = true;
253268
254- if ( _initCalled == 0 )
269+ if ( _initCalled == MQTT_LIBRARY_UNINITIALIZED )
255270 {
256271 IotLogError ( "IotMqtt_Init was not called." );
257272
@@ -994,35 +1009,37 @@ void _IotMqtt_DecrementConnectionReferences( _mqttConnection_t * pMqttConnection
9941009IotMqttError_t IotMqtt_Init ( void )
9951010{
9961011 IotMqttError_t status = IOT_MQTT_SUCCESS ;
1012+ uint32_t allowInitialization = Atomic_CompareAndSwap_u32 ( & _initCalled ,
1013+ MQTT_LIBRARY_INITIALIZED ,
1014+ MQTT_LIBRARY_UNINITIALIZED );
9971015
998- if ( _initCalled == 0 )
1016+ if ( allowInitialization == 1 )
9991017 {
10001018 /* Call any additional serializer initialization function if serializer
10011019 * overrides are enabled. */
10021020 #if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1
10031021 #ifdef _IotMqtt_InitSerializeAdditional
10041022 if ( _IotMqtt_InitSerializeAdditional () == false )
10051023 {
1024+ /* Log initialization status. */
1025+ IotLogError ( "Failed to initialize MQTT library serializer. " );
1026+
10061027 status = IOT_MQTT_INIT_FAILED ;
10071028 }
10081029 else
10091030 {
10101031 EMPTY_ELSE_MARKER ;
10111032 }
1012- #endif
1033+ #endif /* ifdef _IotMqtt_InitSerializeAdditional */
10131034 #endif /* if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1 */
10141035
1015- /* Log initialization status. */
1016- if ( status != IOT_MQTT_SUCCESS )
1036+ if ( status == IOT_MQTT_SUCCESS )
10171037 {
1018- IotLogError ( "Failed to initialize MQTT library serializer. " );
1038+ IotLogInfo ( "MQTT library successfully initialized. " );
10191039 }
10201040 else
10211041 {
1022- /* Set the flag that specifies initialization is complete. */
1023- _initCalled = 1 ;
1024-
1025- IotLogInfo ( "MQTT library successfully initialized." );
1042+ EMPTY_ELSE_MARKER ;
10261043 }
10271044 }
10281045 else
@@ -1037,10 +1054,12 @@ IotMqttError_t IotMqtt_Init( void )
10371054
10381055void IotMqtt_Cleanup ( void )
10391056{
1040- if ( _initCalled == 1 )
1041- {
1042- _initCalled = 0 ;
1057+ uint32_t allowCleanup = Atomic_CompareAndSwap_u32 ( & _initCalled ,
1058+ MQTT_LIBRARY_UNINITIALIZED ,
1059+ MQTT_LIBRARY_INITIALIZED ) ;
10431060
1061+ if ( allowCleanup == 1 )
1062+ {
10441063 /* Call any additional serializer cleanup initialization function if serializer
10451064 * overrides are enabled. */
10461065 #if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1
0 commit comments