The spec says this: https://golang.org/ref/spec#Size_and_alignment_guarantees Now, I can't really read from this whether this applies to fields or not. If it does, it would seem to me, that, indeed, WaitGroup wouldn't need to use the trick it does.
For the example you name: It isn't particularly telling, as on nacl (at least on x86{,-64}, don't know a lot about ARM) pointers have 32 bits and so does int, meaning both rddeadline and wrdeadline are indeed 64-bit aligned. On Sat, Feb 4, 2017 at 11:12 AM, T L <tapir....@gmail.com> wrote: > > > On Saturday, February 4, 2017 at 5:56:30 PM UTC+8, T L wrote: >> >> >> >> On Saturday, February 4, 2017 at 5:40:25 PM UTC+8, T L wrote: >>> >>> >>> >>> On Friday, February 3, 2017 at 10:44:26 PM UTC+8, Ian Lance Taylor wrote: >>>> >>>> On Fri, Feb 3, 2017 at 5:38 AM, T L <tapi...@gmail.com> wrote: >>>> > Why does WaitGroup.state method check 64-bit alignment at run time? >>>> > Why not make the state field as the first word of WaitGroup struct? >>>> > >>>> > // https://golang.org/src/sync/waitgroup.go?s=1857:1892#L20 >>>> > >>>> > type WaitGroup struct { >>>> > >>>> > noCopy noCopy >>>> > >>>> > // 64-bit value: high 32 bits are counter, low 32 bits are waiter >>>> count. >>>> > >>>> > // 64-bit atomic operations require 64-bit alignment, but 32-bit >>>> > >>>> > // compilers do not ensure it. So we allocate 12 bytes and then use >>>> > >>>> > // the aligned 8 bytes in them as state. >>>> >>>> Doesn't this comment explain the problem? >>>> >>>> Ian >>>> >>> >>> Part of. Ok I get it now. >>> >>> BTW, I have another question, can an allocated local int64 value be >>> relied upon to be 64-bit aligned? >>> >>> >> By search the usages of 64bit functions in atomic package in go source, I >> think the answer should be yes. >> And the go source also imply any 8-byte sized fields, and the fields >> followed 8-byte sized fields, in any struct are also relied upon to be >> 64-bit aligned. >> > > > If this is true, why not define WaitGroup.state as uint64 directly? > > The following is the code in go src which thinks 8-byte sized fields in > structs are also 64-bit aligned. > > // syscall/net_nacl.go > > type netFile struct { > defaultFileImpl > proto *netproto > sotype int > listener *msgq > packet *msgq > rd *byteq > wr *byteq > > * rddeadline int64 wrdeadline int64* > addr Sockaddr > raddr Sockaddr > } > ... > > func SetReadDeadline(fd int, t int64) error { > f, err := fdToNetFile(fd) > if err != nil { > return err > } > *atomic.StoreInt64(&f.rddeadline, t)* > return nil > } > ... > > func SetWriteDeadline(fd int, t int64) error { > f, err := fdToNetFile(fd) > if err != nil { > return err > } > * atomic.StoreInt64(&f.wrdeadline, t)* > return nil > } > > > > >> >> >>> >>> >> -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.