Re: [Mesa-dev] [PATCH 03/21] glsl: protect anonymous struct id with a mutex
On Sat, May 3, 2014 at 1:33 AM, Ian Romanick i...@freedesktop.org wrote: 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); } I could not find any portable stdatomic.h implementation from a quick search. One thing we may do is to have $ cat $mesa/include/c11/stdatomic.h #if __STDC_VERSION__ = 201112L !defined(__STDC_NO_ATOMICS__) #include stdatomic.h #else /* a implementation that is good enough for us */ #include threads.h #define ATOMIC_VAR_INIT(val) { _MTX_INITIALIZER_NP, (val) } typedef struct atomic_uint_t { mtx_t mutex; unsigned int val; } atomic_uint_t; static inline unsigned int atomic_fetch_add(volatile atomic_uint_t *a) { unsigned int val; mtx_lock(a-mutex); val = a-val++; mtx_unlock(a-mutex); return val; } #endif Whoever needs more than reference counting with unsigned int will need to extend it a bit on systems where there is no stdatomic.h. And maybe we are better off without naming it stdatomic.h. Ideas? Signed-off-by: Chia-I Wu o...@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); -- o...@lunarg.com ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev
Re: [Mesa-dev] [PATCH 03/21] glsl: protect anonymous struct id with a mutex
On Fri, May 2, 2014 at 10:33 AM, Ian Romanick i...@freedesktop.org wrote: 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); } Note that gcc-4.9 is the first version to have stdatomic.h. gcc has supported atomic built-ins for a long time though: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev
Re: [Mesa-dev] [PATCH 03/21] glsl: protect anonymous struct id with a mutex
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 o...@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); ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev
[Mesa-dev] [PATCH 03/21] glsl: protect anonymous struct id with a mutex
There may be two contexts compiling shaders at the same time, and we want the anonymous struct id to be globally unique. Signed-off-by: Chia-I Wu o...@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); -- 1.8.5.3 ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev