[Mesa-dev] [PATCH 03/21] glsl: protect anonymous struct id with a mutex

Ian Romanick idr at freedesktop.org
Fri May 2 10:33:29 PDT 2014


On 04/22/2014 01:58 AM, Chia-I Wu wrote:
> There may be two contexts compiling shaders at the same time, and we want the
> anonymous struct id to be globally unique.

I am not very excited about this.

Is there any chance of getting stdatomic.h for the MSVC compilers that
people care about?  I'd much rather have this code be...

   if (identifier == NULL) {
      static volatile atomic_uint_t anon_count = ATOMIC_VAR_INIT(1);
      unsigned count;

      count = atomic_fetch_add(&anon_count, 1);
      identifier = ralloc_asprintf(this, "#anon_struct_%04x", count);
   }

> Signed-off-by: Chia-I Wu <olv at lunarg.com>
> ---
>  src/glsl/glsl_parser_extras.cpp | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/src/glsl/glsl_parser_extras.cpp b/src/glsl/glsl_parser_extras.cpp
> index 91c9285..30e284b 100644
> --- a/src/glsl/glsl_parser_extras.cpp
> +++ b/src/glsl/glsl_parser_extras.cpp
> @@ -1332,9 +1332,15 @@ ast_struct_specifier::ast_struct_specifier(const char *identifier,
>  					   ast_declarator_list *declarator_list)
>  {
>     if (identifier == NULL) {
> +      static mtx_t mutex = _MTX_INITIALIZER_NP;
>        static unsigned anon_count = 1;
> -      identifier = ralloc_asprintf(this, "#anon_struct_%04x", anon_count);
> -      anon_count++;
> +      unsigned count;
> +
> +      mtx_lock(&mutex);
> +      count = anon_count++;
> +      mtx_unlock(&mutex);
> +
> +      identifier = ralloc_asprintf(this, "#anon_struct_%04x", count);
>     }
>     name = identifier;
>     this->declarations.push_degenerate_list_at_head(&declarator_list->link);
> 



More information about the mesa-dev mailing list