How does golang io.pipe write() and read() functions work?

By reading golang src pipe.go to figure out how pipe works, I ran into these two write() & read() functions. What confuses me is that if reader calls read() func and hold l.lock and then waiting for data, how does writer call write() func and acquire l.lock to write data in?

func (p *pipe) write(b []byte) (n int, err error) {
// pipe uses nil to mean not available
if b == nil {
b = zero[:]
}

// One writer at a time.
p.wl.Lock()
defer p.wl.Unlock()

p.l.Lock()
defer p.l.Unlock()
if p.werr != nil {
    err = ErrClosedPipe
    return
}
p.data = b
p.rwait.Signal()
for {
    if p.data == nil {
        break
    }
    if p.rerr != nil {
        err = p.rerr
        break
    }
    if p.werr != nil {
        err = ErrClosedPipe
        break
    }
    p.wwait.Wait()
}
n = len(b) - len(p.data)
p.data = nil // in case of rerr or werr
return

}

func (p *pipe) read(b []byte) (n int, err error) {

// One reader at a time.
p.rl.Lock()
defer p.rl.Unlock()

p.l.Lock()
defer p.l.Unlock()
for {
    if p.rerr != nil {
        return 0, ErrClosedPipe
    }
    if p.data != nil {
        break
    }
    if p.werr != nil {
        return 0, p.werr
    }
    p.rwait.Wait()
}
n = copy(b, p.data)
p.data = p.data[n:]
if len(p.data) == 0 {
    p.data = nil
    p.wwait.Signal()
}
return

}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.