[Mesa-dev] [PATCH 1/5] intel: Add a macro for printing a debug warning once.

Eric Anholt eric at anholt.net
Tue Oct 16 13:02:45 PDT 2012


Jason Wood <sandain at hotmail.com> writes:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 10/15/2012 10:41 AM, Eric Anholt wrote:
>> Jason Wood <sandain at hotmail.com> writes:
>> 
>>> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
>>> 
>>> On 10/12/2012 04:30 PM, Eric Anholt wrote:
>>>> There are a number of places where some obscure piece of the
>>>> code is not currently worth fixing, and we have some workaround
>>>> behavior available.  It's nicer for users to do some lame
>>>> workaround than to just assert, but without asserts we never
>>>> knew when the workaround was at fault.
>>>> 
>>>> This should give us a nice compromise: Execute the workaround,
>>>> but mention that the obscure workaround was hit. --- 
>>>> src/mesa/drivers/dri/intel/intel_context.h |   11 +++++++++++
>>>> 1 file changed, 11 insertions(+)
>>>> 
>>>> diff --git a/src/mesa/drivers/dri/intel/intel_context.h 
>>>> b/src/mesa/drivers/dri/intel/intel_context.h index
>>>> b732696..eeefadf 100644 ---
>>>> a/src/mesa/drivers/dri/intel/intel_context.h +++ 
>>>> b/src/mesa/drivers/dri/intel/intel_context.h @@ -483,6 +483,17
>>>> @@ extern int INTEL_DEBUG; dbg_printf(__VA_ARGS__);			\ }
>>>> while(0)
>>>> 
>>>> +#define WARN_ONCE(cond, fmt...) do {
>>>> \ +   if (unlikely(cond)) {
>>>> \ +      static bool _warned = false;
>>>> \ +      if (!_warned) {
>>>> \
>>> 
>>> I could be wrong here because I'm not super familiar with how
>>> macros work..
>>> 
>>> It looks like this if statement will _always_ evaluate to true
>>> given that you just initialized the value in the previous line.
>>> I assume this is not the behavior that you were after.
>> 
>> The "static" keyword on _warned means that it will have persistant 
>> storage that contains the initializer value on library load.
>> 
>
> Have you actually tried this to see if your code only warns once?
> When I write a little test program to see how this code actually
> functions, I get multiple warnings unless I move the
> definition/initialization of _warned outside of the WARN_ONCE define
> block:
>
> static bool _warned = false;
>
> #define WARN_ONCE(cond, fmt...) do {                            \
>    if (unlikely(cond)) {                                        \
>       if (!_warned) {                                           \
>          fprintf(stderr, "WARNING: ");                          \
>          fprintf(stderr, fmt);                                  \
>          _warned = true;                                        \
>       }                                                         \
>    }                                                            \
> } while (0)
>
>
> I admit that as a Perl programmer my C fu is a little weak, so I may
> just be adding noise to this list.  If this is the case, I apologize.
>
> On a side note, the "do { ... } while(0)" portion of the code looks
> funny to me, and I have to assume that it is some oddball compiler
> workaround.  GCC works just fine when this code is wrapped in just
> curly braces.

No, really, it works:

diff --git a/src/mesa/drivers/dri/intel/intel_context.c b/src/mesa/drivers/dri/intel/intel_conte
index 204609e..a67d35b 100644
--- a/src/mesa/drivers/dri/intel/intel_context.c
+++ b/src/mesa/drivers/dri/intel/intel_context.c
@@ -707,6 +707,10 @@ intelInitContext(struct intel_context *intel,
       _swrast_CreateContext(ctx);
    }
 
+   for (int i = 0; i < 3; i++) {
+      WARN_ONCE(true, "asdfasdfasdf\n");
+   }
+
    _vbo_CreateContext(ctx);
    if (ctx->swrast_context) {
       _tnl_CreateContext(ctx);

anholt at eliezer:anholt/src/mesa% mesagl glxgears
libGL: OpenDriver: trying /home/anholt/src/mesa/lib/tls/i965_dri.so
libGL: OpenDriver: trying /home/anholt/src/mesa/lib/i965_dri.so
WARNING: asdfasdfasdf
Running synchronized to the vertical refresh.  The framerate should be
approximately the same as the monitor refresh rate.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20121016/80d5732f/attachment.pgp>


More information about the mesa-dev mailing list