Message ID | 1417101181-13500-4-git-send-email-ola.liljedahl@linaro.org |
---|---|
State | Accepted |
Commit | 846b445b1234dcee7df8807c7fff459f080de7a0 |
Headers | show |
On Thu, Nov 27, 2014 at 10:12 AM, Ola Liljedahl <ola.liljedahl@linaro.org> wrote: > Signed-off-by: Ola Liljedahl <ola.liljedahl@linaro.org> > Reviewed-by: Bill Fischofer <bill.fischofer@linaro.org> > --- > (This document/code contribution attached is provided under the terms of > agreement LES-LTM-21309) > > Use definitions from odp_atomic_internal.h. > > platform/linux-generic/include/api/odp_rwlock.h | 4 ++- > platform/linux-generic/odp_rwlock.c | 35 > ++++++++++++------------- > 2 files changed, 20 insertions(+), 19 deletions(-) > > diff --git a/platform/linux-generic/include/api/odp_rwlock.h > b/platform/linux-generic/include/api/odp_rwlock.h > index a880f92..59cf9cc 100644 > --- a/platform/linux-generic/include/api/odp_rwlock.h > +++ b/platform/linux-generic/include/api/odp_rwlock.h > @@ -13,6 +13,8 @@ > * ODP RW Locks > */ > > +#include <odp_atomic.h> > + > #ifdef __cplusplus > extern "C" { > #endif > @@ -28,7 +30,7 @@ extern "C" { > * read lock count > 0 > */ > typedef struct { > - volatile int32_t cnt; /**< -1 Write lock, > + odp_atomic_u32_t cnt; /**< -1 Write lock, > > 0 for Read lock. */ > } odp_rwlock_t; > > diff --git a/platform/linux-generic/odp_rwlock.c > b/platform/linux-generic/odp_rwlock.c > index 2f6a255..01a3b4e 100644 > --- a/platform/linux-generic/odp_rwlock.c > +++ b/platform/linux-generic/odp_rwlock.c > @@ -6,64 +6,63 @@ > > #include <stdbool.h> > #include <odp_atomic.h> > +#include <odp_atomic_internal.h> > #include <odp_rwlock.h> > > #include <odp_spin_internal.h> > > void odp_rwlock_init(odp_rwlock_t *rwlock) > { > - rwlock->cnt = 0; > + odp_atomic_init_u32(&rwlock->cnt, 0); > } > > void odp_rwlock_read_lock(odp_rwlock_t *rwlock) > { > - int32_t cnt; > + uint32_t cnt; > int is_locked = 0; > > while (is_locked == 0) { > - cnt = rwlock->cnt; > + cnt = _odp_atomic_u32_load_mm(&rwlock->cnt, > _ODP_MEMMODEL_RLX); > /* waiting for read lock */ > - if (cnt < 0) { > + if ((int32_t)cnt < 0) { > odp_spin(); > continue; > } > - is_locked = __atomic_compare_exchange_n(&rwlock->cnt, > + is_locked = > _odp_atomic_u32_cmp_xchg_strong_mm(&rwlock->cnt, > &cnt, > cnt + 1, > - false/*strong*/, > - __ATOMIC_ACQUIRE, > - __ATOMIC_RELAXED); > + _ODP_MEMMODEL_ACQ, > + _ODP_MEMMODEL_RLX); > } > } > > void odp_rwlock_read_unlock(odp_rwlock_t *rwlock) > { > - (void)__atomic_sub_fetch(&rwlock->cnt, 1, __ATOMIC_RELEASE); > + _odp_atomic_u32_sub_mm(&rwlock->cnt, 1, _ODP_MEMMODEL_RLS); > } > > void odp_rwlock_write_lock(odp_rwlock_t *rwlock) > { > - int32_t cnt; > + uint32_t cnt; > int is_locked = 0; > > while (is_locked == 0) { > - int32_t zero = 0; > - cnt = rwlock->cnt; > + uint32_t zero = 0; > + cnt = _odp_atomic_u32_load_mm(&rwlock->cnt, > _ODP_MEMMODEL_RLX); > /* lock aquired, wait */ > if (cnt != 0) { > odp_spin(); > continue; > } > - is_locked = __atomic_compare_exchange_n(&rwlock->cnt, > + is_locked = > _odp_atomic_u32_cmp_xchg_strong_mm(&rwlock->cnt, > &zero, > - -1, > - false/*strong*/, > - __ATOMIC_ACQUIRE, > - __ATOMIC_RELAXED); > + (uint32_t)-1, > + _ODP_MEMMODEL_ACQ, > + _ODP_MEMMODEL_RLX); > } > } > > void odp_rwlock_write_unlock(odp_rwlock_t *rwlock) > { > - (void)__atomic_add_fetch(&rwlock->cnt, 1, __ATOMIC_RELEASE); > + _odp_atomic_u32_store_mm(&rwlock->cnt, 0, _ODP_MEMMODEL_RLS); > } > -- > 1.9.1 > > > _______________________________________________ > lng-odp mailing list > lng-odp@lists.linaro.org > http://lists.linaro.org/mailman/listinfo/lng-odp >
diff --git a/platform/linux-generic/include/api/odp_rwlock.h b/platform/linux-generic/include/api/odp_rwlock.h index a880f92..59cf9cc 100644 --- a/platform/linux-generic/include/api/odp_rwlock.h +++ b/platform/linux-generic/include/api/odp_rwlock.h @@ -13,6 +13,8 @@ * ODP RW Locks */ +#include <odp_atomic.h> + #ifdef __cplusplus extern "C" { #endif @@ -28,7 +30,7 @@ extern "C" { * read lock count > 0 */ typedef struct { - volatile int32_t cnt; /**< -1 Write lock, + odp_atomic_u32_t cnt; /**< -1 Write lock, > 0 for Read lock. */ } odp_rwlock_t; diff --git a/platform/linux-generic/odp_rwlock.c b/platform/linux-generic/odp_rwlock.c index 2f6a255..01a3b4e 100644 --- a/platform/linux-generic/odp_rwlock.c +++ b/platform/linux-generic/odp_rwlock.c @@ -6,64 +6,63 @@ #include <stdbool.h> #include <odp_atomic.h> +#include <odp_atomic_internal.h> #include <odp_rwlock.h> #include <odp_spin_internal.h> void odp_rwlock_init(odp_rwlock_t *rwlock) { - rwlock->cnt = 0; + odp_atomic_init_u32(&rwlock->cnt, 0); } void odp_rwlock_read_lock(odp_rwlock_t *rwlock) { - int32_t cnt; + uint32_t cnt; int is_locked = 0; while (is_locked == 0) { - cnt = rwlock->cnt; + cnt = _odp_atomic_u32_load_mm(&rwlock->cnt, _ODP_MEMMODEL_RLX); /* waiting for read lock */ - if (cnt < 0) { + if ((int32_t)cnt < 0) { odp_spin(); continue; } - is_locked = __atomic_compare_exchange_n(&rwlock->cnt, + is_locked = _odp_atomic_u32_cmp_xchg_strong_mm(&rwlock->cnt, &cnt, cnt + 1, - false/*strong*/, - __ATOMIC_ACQUIRE, - __ATOMIC_RELAXED); + _ODP_MEMMODEL_ACQ, + _ODP_MEMMODEL_RLX); } } void odp_rwlock_read_unlock(odp_rwlock_t *rwlock) { - (void)__atomic_sub_fetch(&rwlock->cnt, 1, __ATOMIC_RELEASE); + _odp_atomic_u32_sub_mm(&rwlock->cnt, 1, _ODP_MEMMODEL_RLS); } void odp_rwlock_write_lock(odp_rwlock_t *rwlock) { - int32_t cnt; + uint32_t cnt; int is_locked = 0; while (is_locked == 0) { - int32_t zero = 0; - cnt = rwlock->cnt; + uint32_t zero = 0; + cnt = _odp_atomic_u32_load_mm(&rwlock->cnt, _ODP_MEMMODEL_RLX); /* lock aquired, wait */ if (cnt != 0) { odp_spin(); continue; } - is_locked = __atomic_compare_exchange_n(&rwlock->cnt, + is_locked = _odp_atomic_u32_cmp_xchg_strong_mm(&rwlock->cnt, &zero, - -1, - false/*strong*/, - __ATOMIC_ACQUIRE, - __ATOMIC_RELAXED); + (uint32_t)-1, + _ODP_MEMMODEL_ACQ, + _ODP_MEMMODEL_RLX); } } void odp_rwlock_write_unlock(odp_rwlock_t *rwlock) { - (void)__atomic_add_fetch(&rwlock->cnt, 1, __ATOMIC_RELEASE); + _odp_atomic_u32_store_mm(&rwlock->cnt, 0, _ODP_MEMMODEL_RLS); }
Signed-off-by: Ola Liljedahl <ola.liljedahl@linaro.org> --- (This document/code contribution attached is provided under the terms of agreement LES-LTM-21309) Use definitions from odp_atomic_internal.h. platform/linux-generic/include/api/odp_rwlock.h | 4 ++- platform/linux-generic/odp_rwlock.c | 35 ++++++++++++------------- 2 files changed, 20 insertions(+), 19 deletions(-)