Message ID | af5b9b35eb142b53fb7cb4684b494bc6ccb047f1.1559562608.git.guillaume.tucker@collabora.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | [i-g-t,1/4] tests: add libatomic dependency | expand |
On Mon, 2019-06-03 at 12:54 +0100, Guillaume Tucker wrote: > Replace calls to the older __sync_* functions with the new __atomic_* > standard ones. This fixes builds on some architectures, in particular > MIPS which doesn't have __sync_add_and_fetch_8 and > __sync_val_compare_and_swap_8 for 64-bit variable handling. Can't we use the C11 atomics from stdatomic.h instead? For instance: https://en.cppreference.com/w/c/atomic/atomic_compare_exchange > Signed-off-by: Guillaume Tucker <guillaume.tucker@collabora.com> > --- > tests/Makefile.am | 2 +- > tests/i915/gem_create.c | 12 ++++++++++-- > 2 files changed, 11 insertions(+), 3 deletions(-) > > diff --git a/tests/Makefile.am b/tests/Makefile.am > index 5097debf629c..18a0f1f20592 100644 > --- a/tests/Makefile.am > +++ b/tests/Makefile.am > @@ -90,7 +90,7 @@ AM_LDFLAGS = -Wl,--as-needed > drm_import_export_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) > drm_import_export_LDADD = $(LDADD) -lpthread > gem_create_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) > -gem_create_LDADD = $(LDADD) -lpthread > +gem_create_LDADD = $(LDADD) -lpthread -latomic > gem_close_race_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) > gem_close_race_LDADD = $(LDADD) -lpthread > gem_ctx_thrash_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) > diff --git a/tests/i915/gem_create.c b/tests/i915/gem_create.c > index 43cbf45f289b..a4aeb94b3f93 100644 > --- a/tests/i915/gem_create.c > +++ b/tests/i915/gem_create.c > @@ -156,6 +156,14 @@ static void invalid_nonaligned_size(int fd) > gem_close(fd, create.handle); > } > > +static uint64_t atomic_compare_swap_u64(uint64_t *ptr, uint64_t oldval, > + uint64_t newval) > +{ > + __atomic_compare_exchange_n(ptr, &oldval, newval, 0, > + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); > + return oldval; > +} > + > static uint64_t get_npages(uint64_t *global, uint64_t npages) > { > uint64_t try, old, max; > @@ -165,7 +173,7 @@ static uint64_t get_npages(uint64_t *global, uint64_t npages) > old = max; > try = 1 + npages % (max / 2); > max -= try; > - } while ((max = __sync_val_compare_and_swap(global, old, max)) != old); > + } while ((max = atomic_compare_swap_u64(global, old, max)) != old); > > return try; > } > @@ -202,7 +210,7 @@ static void *thread_clear(void *data) > } > gem_close(i915, create.handle); > > - __sync_add_and_fetch(&arg->max, npages); > + __atomic_add_fetch(&arg->max, npages, __ATOMIC_SEQ_CST); > } > > return NULL;
On 06/06/2019 08:20, Ser, Simon wrote: > On Mon, 2019-06-03 at 12:54 +0100, Guillaume Tucker wrote: >> Replace calls to the older __sync_* functions with the new __atomic_* >> standard ones. This fixes builds on some architectures, in particular >> MIPS which doesn't have __sync_add_and_fetch_8 and >> __sync_val_compare_and_swap_8 for 64-bit variable handling. > > Can't we use the C11 atomics from stdatomic.h instead? > > For instance: > https://en.cppreference.com/w/c/atomic/atomic_compare_exchange Yes, thanks for pointing this out. The stdatomic.h macros also use the __atomic_* functions so the libatomic dependency remains the same, but it's a bit better than using the __atomic_* functions directly. In particular, stdatomic.h enforces the use of the _Atomic type modifier which seems very important esp for pointers to atomic variables. That's also being fixed in the v2. Guillaume >> Signed-off-by: Guillaume Tucker <guillaume.tucker@collabora.com> >> --- >> tests/Makefile.am | 2 +- >> tests/i915/gem_create.c | 12 ++++++++++-- >> 2 files changed, 11 insertions(+), 3 deletions(-) >> >> diff --git a/tests/Makefile.am b/tests/Makefile.am >> index 5097debf629c..18a0f1f20592 100644 >> --- a/tests/Makefile.am >> +++ b/tests/Makefile.am >> @@ -90,7 +90,7 @@ AM_LDFLAGS = -Wl,--as-needed >> drm_import_export_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) >> drm_import_export_LDADD = $(LDADD) -lpthread >> gem_create_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) >> -gem_create_LDADD = $(LDADD) -lpthread >> +gem_create_LDADD = $(LDADD) -lpthread -latomic >> gem_close_race_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) >> gem_close_race_LDADD = $(LDADD) -lpthread >> gem_ctx_thrash_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) >> diff --git a/tests/i915/gem_create.c b/tests/i915/gem_create.c >> index 43cbf45f289b..a4aeb94b3f93 100644 >> --- a/tests/i915/gem_create.c >> +++ b/tests/i915/gem_create.c >> @@ -156,6 +156,14 @@ static void invalid_nonaligned_size(int fd) >> gem_close(fd, create.handle); >> } >> >> +static uint64_t atomic_compare_swap_u64(uint64_t *ptr, uint64_t oldval, >> + uint64_t newval) >> +{ >> + __atomic_compare_exchange_n(ptr, &oldval, newval, 0, >> + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); >> + return oldval; >> +} >> + >> static uint64_t get_npages(uint64_t *global, uint64_t npages) >> { >> uint64_t try, old, max; >> @@ -165,7 +173,7 @@ static uint64_t get_npages(uint64_t *global, uint64_t npages) >> old = max; >> try = 1 + npages % (max / 2); >> max -= try; >> - } while ((max = __sync_val_compare_and_swap(global, old, max)) != old); >> + } while ((max = atomic_compare_swap_u64(global, old, max)) != old); >> >> return try; >> } >> @@ -202,7 +210,7 @@ static void *thread_clear(void *data) >> } >> gem_close(i915, create.handle); >> >> - __sync_add_and_fetch(&arg->max, npages); >> + __atomic_add_fetch(&arg->max, npages, __ATOMIC_SEQ_CST); >> } >> >> return NULL;
diff --git a/tests/Makefile.am b/tests/Makefile.am index 5097debf629c..18a0f1f20592 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -90,7 +90,7 @@ AM_LDFLAGS = -Wl,--as-needed drm_import_export_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) drm_import_export_LDADD = $(LDADD) -lpthread gem_create_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) -gem_create_LDADD = $(LDADD) -lpthread +gem_create_LDADD = $(LDADD) -lpthread -latomic gem_close_race_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) gem_close_race_LDADD = $(LDADD) -lpthread gem_ctx_thrash_CFLAGS = $(AM_CFLAGS) $(THREAD_CFLAGS) diff --git a/tests/i915/gem_create.c b/tests/i915/gem_create.c index 43cbf45f289b..a4aeb94b3f93 100644 --- a/tests/i915/gem_create.c +++ b/tests/i915/gem_create.c @@ -156,6 +156,14 @@ static void invalid_nonaligned_size(int fd) gem_close(fd, create.handle); } +static uint64_t atomic_compare_swap_u64(uint64_t *ptr, uint64_t oldval, + uint64_t newval) +{ + __atomic_compare_exchange_n(ptr, &oldval, newval, 0, + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); + return oldval; +} + static uint64_t get_npages(uint64_t *global, uint64_t npages) { uint64_t try, old, max; @@ -165,7 +173,7 @@ static uint64_t get_npages(uint64_t *global, uint64_t npages) old = max; try = 1 + npages % (max / 2); max -= try; - } while ((max = __sync_val_compare_and_swap(global, old, max)) != old); + } while ((max = atomic_compare_swap_u64(global, old, max)) != old); return try; } @@ -202,7 +210,7 @@ static void *thread_clear(void *data) } gem_close(i915, create.handle); - __sync_add_and_fetch(&arg->max, npages); + __atomic_add_fetch(&arg->max, npages, __ATOMIC_SEQ_CST); } return NULL;
Replace calls to the older __sync_* functions with the new __atomic_* standard ones. This fixes builds on some architectures, in particular MIPS which doesn't have __sync_add_and_fetch_8 and __sync_val_compare_and_swap_8 for 64-bit variable handling. Signed-off-by: Guillaume Tucker <guillaume.tucker@collabora.com> --- tests/Makefile.am | 2 +- tests/i915/gem_create.c | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-)