diff mbox

[v5,05/18] atomics: add atomic_test_and_set_acquire

Message ID 1463196873-17737-6-git-send-email-cota@braap.org
State New
Headers show

Commit Message

Emilio Cota May 14, 2016, 3:34 a.m. UTC
This new helper expands to __atomic_test_and_set with acquire semantics
where available; otherwise it expands to __sync_test_and_set, which
has acquire semantics.

Signed-off-by: Emilio G. Cota <cota@braap.org>
---
 include/qemu/atomic.h | 3 +++
 1 file changed, 3 insertions(+)

Comments

Paolo Bonzini May 16, 2016, 10:05 a.m. UTC | #1
On 14/05/2016 05:34, Emilio G. Cota wrote:
> This new helper expands to __atomic_test_and_set with acquire semantics
> where available; otherwise it expands to __sync_test_and_set, which
> has acquire semantics.
> 
> Signed-off-by: Emilio G. Cota <cota@braap.org>

Non-seqcst read-modify-write operations are beyond what I expected to
have in qemu/atomic.h, but I guess it's okay for test-and-set because of
__sync_test_and_set.

Paolo

> ---
>  include/qemu/atomic.h | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h
> index 5bc4d6c..6061a46 100644
> --- a/include/qemu/atomic.h
> +++ b/include/qemu/atomic.h
> @@ -113,6 +113,7 @@
>  } while(0)
>  #endif
>  
> +#define atomic_test_and_set_acquire(ptr) __atomic_test_and_set(ptr, __ATOMIC_ACQUIRE)
>  
>  /* All the remaining operations are fully sequentially consistent */
>  
> @@ -327,6 +328,8 @@
>  #endif
>  #endif
>  
> +#define atomic_test_and_set_acquire(ptr) __sync_lock_test_and_set(ptr, true)
> +
>  /* Provide shorter names for GCC atomic builtins.  */
>  #define atomic_fetch_inc(ptr)  __sync_fetch_and_add(ptr, 1)
>  #define atomic_fetch_dec(ptr)  __sync_fetch_and_add(ptr, -1)
>
Sergey Fedorov May 17, 2016, 4:15 p.m. UTC | #2
On 14/05/16 06:34, Emilio G. Cota wrote:
> This new helper expands to __atomic_test_and_set with acquire semantics
> where available; otherwise it expands to __sync_test_and_set, which
> has acquire semantics.

Why don't also add atomic_clear_release() for completeness?

Kind regards,
Sergey

>
> Signed-off-by: Emilio G. Cota <cota@braap.org>
> ---
>  include/qemu/atomic.h | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h
> index 5bc4d6c..6061a46 100644
> --- a/include/qemu/atomic.h
> +++ b/include/qemu/atomic.h
> @@ -113,6 +113,7 @@
>  } while(0)
>  #endif
>  
> +#define atomic_test_and_set_acquire(ptr) __atomic_test_and_set(ptr, __ATOMIC_ACQUIRE)
>  
>  /* All the remaining operations are fully sequentially consistent */
>  
> @@ -327,6 +328,8 @@
>  #endif
>  #endif
>  
> +#define atomic_test_and_set_acquire(ptr) __sync_lock_test_and_set(ptr, true)
> +
>  /* Provide shorter names for GCC atomic builtins.  */
>  #define atomic_fetch_inc(ptr)  __sync_fetch_and_add(ptr, 1)
>  #define atomic_fetch_dec(ptr)  __sync_fetch_and_add(ptr, -1)
Paolo Bonzini May 17, 2016, 4:23 p.m. UTC | #3
On 17/05/2016 18:15, Sergey Fedorov wrote:
> On 14/05/16 06:34, Emilio G. Cota wrote:
>> This new helper expands to __atomic_test_and_set with acquire semantics
>> where available; otherwise it expands to __sync_test_and_set, which
>> has acquire semantics.
> 
> Why don't also add atomic_clear_release() for completeness?

The previous patch adds atomic_set_release.

Paolo

> Kind regards,
> Sergey
> 
>>
>> Signed-off-by: Emilio G. Cota <cota@braap.org>
>> ---
>>  include/qemu/atomic.h | 3 +++
>>  1 file changed, 3 insertions(+)
>>
>> diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h
>> index 5bc4d6c..6061a46 100644
>> --- a/include/qemu/atomic.h
>> +++ b/include/qemu/atomic.h
>> @@ -113,6 +113,7 @@
>>  } while(0)
>>  #endif
>>  
>> +#define atomic_test_and_set_acquire(ptr) __atomic_test_and_set(ptr, __ATOMIC_ACQUIRE)
>>  
>>  /* All the remaining operations are fully sequentially consistent */
>>  
>> @@ -327,6 +328,8 @@
>>  #endif
>>  #endif
>>  
>> +#define atomic_test_and_set_acquire(ptr) __sync_lock_test_and_set(ptr, true)
>> +
>>  /* Provide shorter names for GCC atomic builtins.  */
>>  #define atomic_fetch_inc(ptr)  __sync_fetch_and_add(ptr, 1)
>>  #define atomic_fetch_dec(ptr)  __sync_fetch_and_add(ptr, -1)
>
Sergey Fedorov May 17, 2016, 4:47 p.m. UTC | #4
On 17/05/16 19:23, Paolo Bonzini wrote:
>
> On 17/05/2016 18:15, Sergey Fedorov wrote:
>> On 14/05/16 06:34, Emilio G. Cota wrote:
>>> This new helper expands to __atomic_test_and_set with acquire semantics
>>> where available; otherwise it expands to __sync_test_and_set, which
>>> has acquire semantics.
>> Why don't also add atomic_clear_release() for completeness?
> The previous patch adds atomic_set_release.

Yes, but it would take the advantage of __sync_lock_release() being just
a release barrier rather than a full barrier of smp_mb() in
atomic_set_release(). But that's only the case for legacy __sync
built-ins (before GCC 4.7.0), though.

Kind regards,
Sergey
Paolo Bonzini May 17, 2016, 5:08 p.m. UTC | #5
On 17/05/2016 18:47, Sergey Fedorov wrote:
>>> >> On 14/05/16 06:34, Emilio G. Cota wrote:
>>>> >>> This new helper expands to __atomic_test_and_set with acquire semantics
>>>> >>> where available; otherwise it expands to __sync_test_and_set, which
>>>> >>> has acquire semantics.
>>> >> Why don't also add atomic_clear_release() for completeness?
>> > The previous patch adds atomic_set_release.
> Yes, but it would take the advantage of __sync_lock_release() being just
> a release barrier rather than a full barrier of smp_mb() in
> atomic_set_release(). But that's only the case for legacy __sync
> built-ins (before GCC 4.7.0), though.

That will be fixed soon by adding smp_mb_acquire() and smp_mb_release(). :)

Paolo
diff mbox

Patch

diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h
index 5bc4d6c..6061a46 100644
--- a/include/qemu/atomic.h
+++ b/include/qemu/atomic.h
@@ -113,6 +113,7 @@ 
 } while(0)
 #endif
 
+#define atomic_test_and_set_acquire(ptr) __atomic_test_and_set(ptr, __ATOMIC_ACQUIRE)
 
 /* All the remaining operations are fully sequentially consistent */
 
@@ -327,6 +328,8 @@ 
 #endif
 #endif
 
+#define atomic_test_and_set_acquire(ptr) __sync_lock_test_and_set(ptr, true)
+
 /* Provide shorter names for GCC atomic builtins.  */
 #define atomic_fetch_inc(ptr)  __sync_fetch_and_add(ptr, 1)
 #define atomic_fetch_dec(ptr)  __sync_fetch_and_add(ptr, -1)