Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is “_ = variable” mean in sync/mutex.go

Tags:

go

underline

sync/mutex.go:

func (m *Mutex) Unlock() {
    if race.Enabled {
         _ = m.state
         race.Release(unsafe.Pointer(m))
     }
...

what's the mean of _ = m.state?

I know the var _ interface = Object mean check if Object implemented interface.

like image 395
oohcode Avatar asked Jan 22 '19 08:01

oohcode


1 Answers

From the commit log, the reason is that "_ = m.state" make sure m is not nil.

commit 5bb3a66a973ea87494b9197091e8c1f122080627
Author: Rémy Oudompheng <[email protected]>
Date:   Mon Apr 8 23:46:54 2013 +0200

    sync, sync/atomic: do not corrupt race detector after a nil dereference.

    The race detector uses a global lock to analyze atomic
    operations. A panic in the middle of the code leaves the
    lock acquired.

    Similarly, the sync package may leave the race detectro
    inconsistent when methods are called on nil pointers.

    R=golang-dev, r, minux.ma, dvyukov, rsc, adg
    CC=golang-dev
    https://golang.org/cl/7981043
like image 62
jack Avatar answered Nov 01 '22 14:11

jack