Discussion:
glthread_cond_timedwait errors on C99 code
(too old to reply)
Tim Rühsen
2017-09-30 13:33:30 UTC
Permalink
Raw Message
Hi,

just seeking for clarification...

The line

return glthread_cond_timedwait(&cond->cond, &mutex->mutex, &(struct
timespec){ .tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000 });

errors with

thread.c:155:136: error: macro "glthread_cond_timedwait" passed 4 arguments,
but takes just 3
return glthread_cond_timedwait(&cond->cond, &mutex->mutex, &(struct
timespec){ .tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000 });

This is with gcc 7.2.0 in C99 (default) mode. Is it the preprocessor, my code
or the macro broken ? Or a general C99 flaw ?
BTW, putting brackets () around the last argument makes it compile.

Anybody with some insights ?

Regards, Tim
Bruno Haible
2017-09-30 14:31:22 UTC
Permalink
Raw Message
Post by Tim Rühsen
The line
return glthread_cond_timedwait(&cond->cond, &mutex->mutex, &(struct
timespec){ .tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000 });
errors with
thread.c:155:136: error: macro "glthread_cond_timedwait" passed 4 arguments,
but takes just 3
return glthread_cond_timedwait(&cond->cond, &mutex->mutex, &(struct
timespec){ .tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000 });
That's because glthread_cond_timedwait is a macro, and you are passing it
the arguments
&cond->cond
&mutex->mutex
&(struct timespec){ .tv_sec = ms / 1000
.tv_nsec = (ms % 1000) * 1000000 }
Post by Tim Rühsen
This is with gcc 7.2.0 in C99 (default) mode. Is it the preprocessor, my code
or the macro broken ? Or a general C99 flaw ?
BTW, putting brackets () around the last argument makes it compile.
It's a general C flaw: The C preprocessor considers only opening parentheses,
closing parentheses, and commas as syntactically relevant. Everything else are
"other tokens". A consequence of this is that you need to use extra parentheses
when passing comma-expressions or struct initializers to macros.

Bruno
Tim Rühsen
2017-09-30 16:58:51 UTC
Permalink
Raw Message
Post by Bruno Haible
Post by Tim Rühsen
The line
return glthread_cond_timedwait(&cond->cond, &mutex->mutex, &(struct
timespec){ .tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000 });
errors with
thread.c:155:136: error: macro "glthread_cond_timedwait" passed 4
arguments, but takes just 3
return glthread_cond_timedwait(&cond->cond, &mutex->mutex, &(struct
timespec){ .tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000 });
That's because glthread_cond_timedwait is a macro, and you are passing it
the arguments
&cond->cond
&mutex->mutex
&(struct timespec){ .tv_sec = ms / 1000
.tv_nsec = (ms % 1000) * 1000000 }
Post by Tim Rühsen
This is with gcc 7.2.0 in C99 (default) mode. Is it the preprocessor, my
code or the macro broken ? Or a general C99 flaw ?
BTW, putting brackets () around the last argument makes it compile.
It's a general C flaw: The C preprocessor considers only opening
parentheses, closing parentheses, and commas as syntactically relevant.
Everything else are "other tokens". A consequence of this is that you need
to use extra parentheses when passing comma-expressions or struct
initializers to macros.
Thanks for your explanation, Bruno :-)

Regards, Tim

Loading...