golang源码学习之sync.WaitGroup

527 阅读3分钟

一、核心数据结构


// A WaitGroup must not be copied after first use.
// 使用noCopy保证WaitGroup在首次使用之后,不能被复制
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, and the other 4 as storage
	// for the sema.
	state1 [3]uint32
}

这个是go的WaitGroup的数据结构,里面维护了两个变量noCopy和state1,其中noCopy的作用是保证WaitGroup在首次使用之后,不能被复制,避免指针field带来的不安全操作,state1用来维护goroutine counter和goroutine wait数量和一个信号量
根据源码中的注释 state1这个数据接占有3个unit32,相当与32bit*3=96bit=12bytes

二、核心函数

1、state函数

// state returns pointers to the state and sema fields stored within wg.state1.
// 
func (wg *WaitGroup) state() (statep *uint64, semap *uint32) {
	if uintptr(unsafe.Pointer(&wg.state1))%8 == 0 {
		return (*uint64)(unsafe.Pointer(&wg.state1)), &wg.state1[2]
	} else {
		return (*uint64)(unsafe.Pointer(&wg.state1[1])), &wg.state1[0]
	}
}

这个函数的主要功能是根据操作系统的位数来返回state(goroutine add数量和等待的goroutine数量)和sema(信号量)

image.png

2、Add函数

func (wg *WaitGroup) Add(delta int) {
        // 获取state (goroutine counter 和 goroutine waiter)
	statep, semap := wg.state()
        // 静态检查
	if race.Enabled {
		_ = *statep // trigger nil deref early
		if delta < 0 {
			// Synchronize decrements with Wait.
			race.ReleaseMerge(unsafe.Pointer(wg))
		}
		race.Disable()
		defer race.Enable()
	}
        // 这里相当于将两个32的uint转成64位的uint,高32位代表goroutine counter,所以后面的delta要左移32位,这样才能把增量值加到goroutine counter
	state := atomic.AddUint64(statep, uint64(delta)<<32)
        // 将state左移得到高32位的goroutine counter数量
	v := int32(state >> 32)
        // 64位取低32位,就是goroutine waiter的数量
	w := uint32(state)
	if race.Enabled && delta > 0 && v == int32(delta) {
		// The first increment must be synchronized with Wait.
		// Need to model this as a read, because there can be
		// several concurrent wg.counter transitions from 0.
		race.Read(unsafe.Pointer(semap))
	}
	if v < 0 {
		panic("sync: negative WaitGroup counter")
	}
	if w != 0 && delta > 0 && v == int32(delta) {
		panic("sync: WaitGroup misuse: Add called concurrently with Wait")
	}
        // 如果goroutine counter大于0或者goroutine waiter等于0
	if v > 0 || w == 0 {
		return
	}
        // 能进入这里的逻辑是(v<=0&&w!=0)
        // 所有在执行的goroutine已经Done并且有goroutine正在等待
	// This goroutine has set counter to 0 when waiters > 0.
	// Now there can't be concurrent mutations of state:
	// - Adds must not happen concurrently with Wait,
	// - Wait does not increment waiters if it sees counter == 0.
	// Still do a cheap sanity check to detect WaitGroup misuse.
	if *statep != state {
		panic("sync: WaitGroup misuse: Add called concurrently with Wait")
	}
	// Reset waiters count to 0.
	*statep = 0
	for ; w != 0; w-- {
                // 通知所有正在等待的协程
		runtime_Semrelease(semap, false, 0)
	}
}

3、Wait函数

func (wg *WaitGroup) Wait() {
         // 获取state (goroutine counter 和 goroutine waiter)
	statep, semap := wg.state()
	if race.Enabled {
		_ = *statep // trigger nil deref early
		race.Disable()
	}
	for {
                // 从内存中加载goroutine counter 和 goroutine waiter
		state := atomic.LoadUint64(statep)
		// goroutine counter 高32位
		v := int32(state >> 32)
		// waiter num 低32位
		w := uint32(state)
                // 说明没有goroutine需要等待
		if v == 0 {
			// Counter is 0, no need to wait.
			if race.Enabled {
				race.Enable()
				race.Acquire(unsafe.Pointer(wg))
			}
			return
		}
		// Increment waiters count.
		// 低32位 waiter num + 1
		if atomic.CompareAndSwapUint64(statep, state, state+1) {
			if race.Enabled && w == 0 {
				// Wait must be synchronized with the first Add.
				// Need to model this is as a write to race with the read in Add.
				// As a consequence, can do the write only for the first waiter,
				// otherwise concurrent Waits will race with each other.
				race.Write(unsafe.Pointer(semap))
			}
			// 等待信号量>0,然后将其减一
			// 阻塞
			runtime_Semacquire(semap)
			if *statep != 0 {
				panic("sync: WaitGroup is reused before previous Wait has returned")
			}
			if race.Enabled {
				race.Enable()
				race.Acquire(unsafe.Pointer(wg))
			}
			return
		}
	}
}

4、在Add和Wait函数中涉及到两个并发原语
runtime_Semrelease(释放信号量)和runtime_Semacquire(申请信号量)

// Semacquire waits until *s > 0 and then atomically decrements it.
// It is intended as a simple sleep primitive for use by the synchronization
// library and should not be used directly.

// 等待(阻塞)直到s>0被唤醒,并且将s的值原子的减1(表示已经消耗了一次信号量,所以要减1)
func runtime_Semacquire(s *uint32)

// Semrelease atomically increments *s and notifies a waiting goroutine
// if one is blocked in Semacquire.
// It is intended as a simple wakeup primitive for use by the synchronization
// library and should not be used directly.
// If handoff is true, pass count directly to the first waiter.
// skipframes is the number of frames to omit during tracing, counting from
// runtime_Semrelease's caller.
// 以原子方式递增*s(加1)并通知等待的goroutine
func runtime_Semrelease(s *uint32, handoff bool, skipframes int)

三、总结

1、考虑了操作系统32位和64位的差异
2、利用原子操作避免并发带来的问题
3、利用信号量操作,传递通知
4、WaitGroup成员state1变量不是指针,需要使用指针作为入参(因为go是值传递,如果直接传入WaitGroup的值,相当于把成员复制了,地址不一样了),不然可能会死锁

func T(s *sync.WaitGroup) {
	defer s.Done()
}