qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Wei Yang <richard.weiyang@gmail.com>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>
Cc: qemu-devel@nongnu.org, Wei Yang <richardw.yang@linux.intel.com>,
	quintela@redhat.com
Subject: Re: [Qemu-devel] [PATCH] migrtion: define MigrationState/MigrationIncomingState.state as MigrationStatus
Date: Fri, 23 Aug 2019 11:21:50 -0500	[thread overview]
Message-ID: <12c6eab2-7988-cdb9-ab54-ae052cd45188@redhat.com> (raw)
In-Reply-To: <20190819140828.otv7vq5lahvquczl@master>


[-- Attachment #1.1: Type: text/plain, Size: 2430 bytes --]

On 8/19/19 9:08 AM, Wei Yang wrote:
> On Mon, Aug 19, 2019 at 12:26:32PM +0100, Dr. David Alan Gilbert wrote:
>> * Wei Yang (richardw.yang@linux.intel.com) wrote:

Typo in the subject line: migrtion should be migration

>>> No functional change. Add default case to fix warning.
>>
>> I think the problem with this is that migrate_set_state uses an
>> atomic_cmpxchg and so we have to be careful that the type we use
>> is compatible with that.
>> MigrationStatus is an enum and I think compilers are allowed to
>> choose the types of that;  so I'm not sure we're guaranteed
>> that an enum is always OK for the atomic_cmpxchg, and if it is
> 
> Took a look into the definition of atomic_cmpxchg, which finally calls
> 
>   * __atomic_compare_exchange_n for c++11
>   * __sync_val_compare_and_swap

Those are compiler-defined macros, so you have to consult the compiler
documentation to see if they state what happens when invoked on an enum
type.  You also have to check whether our macro
typeof_strip_qual(enum_type) produces 'int' or something else.

C99 doesn't specify _Atomic at all (which is why we handrolled our own
atomic.h built on top of compiler primitives, instead of using
<stdatomic.h>).  But reading C11, I see that 6.7.2.4 states that
_Atomic(type) is okay except for:

"The type name in an atomic type specifier shall not refer to an array
type, a function type, an atomic type, or a qualified type."

which does NOT preclude the use of _Atomic(enum_type), so presumably
compilers have to be prepared to handle an atomic enum type.  Still,
it's rather shaky ground if you can't prove compilers handle it correctly.


> 
> Both of them take two pointers to compare and exchange its content.
> 
> Per C99 standard, http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf,
> it mentioned:
> 
>   Each enumerated type shall be compatible with char, a signed integer type,
>   or an unsigned integer type. The choice of type is implementation-defined,
>   but shall be capable of representing the values of all the members of the
>   enumeration.
> 
> Based on this, I think atomic_cmpxchg should work fine with enum.

What C99 says is rather weak; you really want to be basing your
decisions on atomics based on C11 or later.


-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  parent reply	other threads:[~2019-08-23 16:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-21 14:27 [Qemu-devel] [PATCH] migrtion: define MigrationState/MigrationIncomingState.state as MigrationStatus Wei Yang
2019-08-19  3:29 ` Wei Yang
2019-08-19 11:26   ` Dr. David Alan Gilbert
2019-08-19 13:32     ` Wei Yang
2019-08-21  8:51     ` Wei Yang
2019-08-19 11:26 ` Dr. David Alan Gilbert
2019-08-19 14:08   ` Wei Yang
2019-08-23 15:29     ` Dr. David Alan Gilbert
2019-08-23 16:18       ` Dr. David Alan Gilbert
2019-08-23 16:21     ` Eric Blake [this message]
2019-08-23 23:49       ` Wei Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12c6eab2-7988-cdb9-ab54-ae052cd45188@redhat.com \
    --to=eblake@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=richard.weiyang@gmail.com \
    --cc=richardw.yang@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).