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.

        state1 [12]byte

        sema   uint32

}
 

func (wg *WaitGroup) state() *uint64 {

        if uintptr(unsafe.Pointer(&wg.state1))%8 == 0 {

                return (*uint64)(unsafe.Pointer(&wg.state1))

        } else {

                return (*uint64)(unsafe.Pointer(&wg.state1[4]))

        }

}



On Thursday, February 2, 2017 at 2:18:33 AM UTC+8, T L wrote:
>
>
>
> On Thursday, February 2, 2017 at 12:11:39 AM UTC+8, Jan Mercl wrote:
>>
>> On Wed, Feb 1, 2017 at 5:04 PM T L <tapi...@gmail.com> wrote:
>>
>> > But what does an allocated struct or slice means? A struct or slice 
>> allocated on heap, not stack?
>>
>> There's no heap nor stack from the POV of the language spec. Allocated 
>> means appropriate amount of storage is now used by the instance. Everything 
>> else is an implementation detail and has no influence on the semantics.
>>
>
> So allocated means addressable? 
>  
>
>>
>> -- 
>>
>> -j
>>
>

-- 
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.

Reply via email to