diff options
author | Jan Huwald <jh@sotun.de> | 2013-06-04 10:08:48 (GMT) |
---|---|---|
committer | Jan Huwald <jh@sotun.de> | 2013-06-04 10:08:48 (GMT) |
commit | 4bccf8848a3410795ab66d7f615fa5b98417afc6 (patch) | |
tree | ec0d37993f325825c8308585200f47c7394660cb | |
parent | f6e02882fb9899eca36d389ee55fdaf0ec5cc5b3 (diff) |
packed_array: add a little atomic access logic
-rw-r--r-- | packed_array.hpp | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/packed_array.hpp b/packed_array.hpp index 9fb2c59..f4fa041 100644 --- a/packed_array.hpp +++ b/packed_array.hpp @@ -18,9 +18,12 @@ struct packed_array { element(word_t *base, uint shift) : base(base), shift(shift) {} T operator= (T val) { - word_t mask = ~(((((word_t) 1) << bit_sz) - 1) << shift); - *base &= mask; - *base ^= val << shift; + word_t old_val, new_val; + do { + word_t mask = ~(((((word_t) 1) << bit_sz) - 1) << shift); + old_val = *((volatile word_t*) base); + new_val = (old_val & mask) ^ (val << shift); + } while (!__sync_bool_compare_and_swap(base, old_val, new_val)); return val; } |