I am using the cleanup variable attribute in order to free a mutex using this macro [...]
I can't say I think very highly of that idea. It makes your code mysterious to anyone reading it (maybe even future you), and it risks producing undefined behavior in the event that the proxy variable goes out of scope in a different manner than you expect, so that perhaps the mutex is already (or still) unlocked or maybe even uninitialized.
Not to mention that using language extensions, especially such idiosyncratic ones as GCC attributes, ties you very tightly to a particular C implementation. Maybe you don't care about that, but if it's me, I at least want to get something very valuable in return, and I don't see that here.
After learning about compound literal's, I wanted to get rid of the
tmpv_
variable, since its not needed in this context.
But it is needed, or at least something is needed to hang the attribute on. I guess you are trying to hang the attribute on a compound literal, but GCC does not document any support for that:
The keyword __attribute__
allows you to specify special properties of
variables, function parameters, or structure, union, and, in C++,
class members. [...] Other attributes are available for functions (see Function Attributes), labels (see Label Attributes), enumerators (see Enumerator Attributes), statements (see Statement Attributes), and for types (see Type Attributes).
(GCC online documentation)
A compound literal is not any of the items documented to support GCC attributes.
You continue,
#define LOCK() \
pthread_mutex_lock(&mutex); \
(void)(int32_t __attribute__((cleanup(mutexUnlock)))){0}
But the compiler gives me a warning for each place the macro is used:
warning: 'cleanup' attribute does not apply to types [-Wattributes]
I am not sure how to interpret this.
The only syntactic construct in that code to which the attribute could conceivably be attached is the type name int32_t
. As GCC warns you, the cleanup
attribute does not apply to types (though there are other attributes that do apply to types). The upshot is that that use of the attribute will have no effect, as indeed you observed (another reason to avoid trying to be cute / magical). GCC chooses to warn you about this issue instead of either failing compilation altogether or silently ignoring the attribute.
What is the problem with this code?
If you could attach attributes to compound literals, I would expect GCC to require them to precede or follow the literal, not appear inside it. But again, as far as I can tell, GCC does not support attributes for compound literals.
More generally, the only remotely reasonable way to use the __cleanup__
attribute is to attach it to the object you want cleaned up -- mutex
in this case. Attaching it to a proxy object so as to use the scope of that object to trigger random behavior involving a different object is a deplorable abuse of the feature. I would recommend discarding the whole idea and instead unlocking the mutex explicitly.