~rogpeppe/mgo/eliminate-acquire-race

« back to all changes in this revision

Viewing changes to session.go

  • Committer: Roger Peppe
  • Date: 2013-04-05 14:05:14 UTC
  • Revision ID: roger.peppe@canonical.com-20130405140514-vynpw59pm6js9ak6
eliminate the possibility of a race in session.acquireSocket

Show diffs side-by-side

added added

removed removed

Lines of Context:
2894
2894
        // Read-only lock to check for previously reserved socket.
2895
2895
        s.m.RLock()
2896
2896
        if s.masterSocket != nil {
 
2897
                sock := s.masterSocket
 
2898
                sock.Acquire()
2897
2899
                s.m.RUnlock()
2898
 
                s.masterSocket.Acquire()
2899
 
                return s.masterSocket, nil
 
2900
                return sock, nil
2900
2901
        }
2901
2902
        if s.slaveSocket != nil && s.slaveOk && slaveOk {
 
2903
                sock := s.slaveSocket
 
2904
                sock.Acquire()
2902
2905
                s.m.RUnlock()
2903
 
                s.slaveSocket.Acquire()
2904
 
                return s.slaveSocket, nil
 
2906
                return sock, nil
2905
2907
        }
2906
2908
        s.m.RUnlock()
2907
2909