• Keith Randall's avatar
    runtime: simplify chan ops, take 2 · e410a527
    Keith Randall authored
    This change is the same as CL #9345 which was reverted,
    except for a small bug fix.
    
    The only change is to the body of sendDirect and its callsite.
    Also added a test.
    
    The problem was during a channel send operation.  The target
    of the send was a sleeping goroutine waiting to receive.  We
    basically do:
    1) Read the destination pointer out of the sudog structure
    2) Copy the value we're sending to that destination pointer
    Unfortunately, the previous change had a goroutine suspend
    point between 1 & 2 (the call to sendDirect).  At that point
    the destination goroutine's stack could be copied (shrunk).
    The pointer we read in step 1 is no longer valid for step 2.
    
    Fixed by not allowing any suspension points between 1 & 2.
    I suspect the old code worked correctly basically by accident.
    
    Fixes #13169
    
    The original 9345:
    
    This change removes the retry mechanism we use for buffered channels.
    Instead, any sender waking up a receiver or vice versa completes the
    full protocol with its counterpart.  This means the counterpart does
    not need to relock the channel when it wakes up.  (Currently
    buffered channels need to relock on wakeup.)
    
    For sends on a channel with waiting receivers, this change replaces
    two copies (sender->queue, queue->receiver) with one (sender->receiver).
    For receives on channels with a waiting sender, two copies are still required.
    
    This change unifies to a large degree the algorithm for buffered
    and unbuffered channels, simplifying the overall implementation.
    
    Fixes #11506
    
    Change-Id: I57dfa3fc219cffa4d48301ee15fe5479299efa09
    Reviewed-on: https://go-review.googlesource.com/16740Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
    e410a527
issue13169.go 704 Bytes