Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concern here is
attr
is declared in the scope ofmqtt_init()
ormqtt_reconnect()
. I thinkpthread_mutexattr_t
might need to come from the user's program's scope (i.e., the main function's scope, or the same scope asmqtt_client
's definition) for this to be safe.Edit: see my third suggestion below (the third comment)
What about if the UNIX/APPLE PAL defines a function like
mqtt_pal_init_pthread_mutex_recursive(struct mqtt_client *, pthread_mutexattr_t*)
that:mqtt_init()
(i.e., the one initialized here)&client->mutex
and thepthread_mutexattr_t*
passed from the user? This new mutex would be initialized as the recursive mutex.Then users that want this functionality can do
Open to suggestions and ideas! Happy to iterate a few times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative would be changing the
MQTT_PAL_MUTEX_INIT()
interface. It could take two arguments: amqtt_pal_mutex_t*
and amqtt_pal_mutex_attr_t*
. All the PAL's would need to add the new mutex attr type. Since theirMQTT_PAL_MUTEX_INIT
wouldn't use it, the other PALs that don't have a similar attribute type could assign it a dummy type (e.g.void*
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A third option came to mind. What about (in the UNIX/APPLE PAL):
The
MQTT_PAL_MUTEX_INIT()
macro is passed a pointer tomqtt_pal_mutex_t
, soMQTT_PAL_MUTEX_INIT()
could then beThis is the cleanest solution IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That last option does look like a very clean solution.
Is it worth including an option on whether to use recursive mutexes at all?
Or alternatively the flag could be the other way round, e.g. MQTT_USE_MUTEX_DEFAULT, if the recursive mutex is the default. (Possibly a better name for the flag is needed if the default is not going to be PTHREAD_MUTEX_DEFAULT.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a reason to not fully switch to recursive mutexes for UNIX/APPLE. Do you have a concern about it?
I supose it could lead to confusion if people share their UNIX/APPLE message callback with a different platform. That said, I think this could be considered a "feature" of the UNIX/APPLE PAL, but not something MQTT-C supports in general.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No objections from me. It was just my own relative lack of experience with these kind of mutexes.