~ubuntu-branches/ubuntu/saucy/golang/saucy

« back to all changes in this revision

Viewing changes to src/pkg/sync/rwmutex.go

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2013-07-08 05:52:37 UTC
  • mfrom: (29.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130708055237-at01839e0hp8z3ni
Tags: 2:1.1-1ubuntu1
016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
package sync
6
6
 
7
 
import "sync/atomic"
 
7
import (
 
8
        "sync/atomic"
 
9
        "unsafe"
 
10
)
8
11
 
9
12
// An RWMutex is a reader/writer mutual exclusion lock.
10
13
// The lock can be held by an arbitrary number of readers
24
27
 
25
28
// RLock locks rw for reading.
26
29
func (rw *RWMutex) RLock() {
 
30
        if raceenabled {
 
31
                _ = rw.w.state
 
32
                raceDisable()
 
33
        }
27
34
        if atomic.AddInt32(&rw.readerCount, 1) < 0 {
28
35
                // A writer is pending, wait for it.
29
36
                runtime_Semacquire(&rw.readerSem)
30
37
        }
 
38
        if raceenabled {
 
39
                raceEnable()
 
40
                raceAcquire(unsafe.Pointer(&rw.readerSem))
 
41
        }
31
42
}
32
43
 
33
44
// RUnlock undoes a single RLock call;
35
46
// It is a run-time error if rw is not locked for reading
36
47
// on entry to RUnlock.
37
48
func (rw *RWMutex) RUnlock() {
 
49
        if raceenabled {
 
50
                _ = rw.w.state
 
51
                raceReleaseMerge(unsafe.Pointer(&rw.writerSem))
 
52
                raceDisable()
 
53
        }
38
54
        if atomic.AddInt32(&rw.readerCount, -1) < 0 {
39
55
                // A writer is pending.
40
56
                if atomic.AddInt32(&rw.readerWait, -1) == 0 {
42
58
                        runtime_Semrelease(&rw.writerSem)
43
59
                }
44
60
        }
 
61
        if raceenabled {
 
62
                raceEnable()
 
63
        }
45
64
}
46
65
 
47
66
// Lock locks rw for writing.
51
70
// a blocked Lock call excludes new readers from acquiring
52
71
// the lock.
53
72
func (rw *RWMutex) Lock() {
 
73
        if raceenabled {
 
74
                _ = rw.w.state
 
75
                raceDisable()
 
76
        }
54
77
        // First, resolve competition with other writers.
55
78
        rw.w.Lock()
56
79
        // Announce to readers there is a pending writer.
59
82
        if r != 0 && atomic.AddInt32(&rw.readerWait, r) != 0 {
60
83
                runtime_Semacquire(&rw.writerSem)
61
84
        }
 
85
        if raceenabled {
 
86
                raceEnable()
 
87
                raceAcquire(unsafe.Pointer(&rw.readerSem))
 
88
                raceAcquire(unsafe.Pointer(&rw.writerSem))
 
89
        }
62
90
}
63
91
 
64
92
// Unlock unlocks rw for writing.  It is a run-time error if rw is
68
96
// goroutine.  One goroutine may RLock (Lock) an RWMutex and then
69
97
// arrange for another goroutine to RUnlock (Unlock) it.
70
98
func (rw *RWMutex) Unlock() {
 
99
        if raceenabled {
 
100
                _ = rw.w.state
 
101
                raceRelease(unsafe.Pointer(&rw.readerSem))
 
102
                raceRelease(unsafe.Pointer(&rw.writerSem))
 
103
                raceDisable()
 
104
        }
 
105
 
71
106
        // Announce to readers there is no active writer.
72
107
        r := atomic.AddInt32(&rw.readerCount, rwmutexMaxReaders)
73
108
        // Unblock blocked readers, if any.
76
111
        }
77
112
        // Allow other writers to proceed.
78
113
        rw.w.Unlock()
 
114
        if raceenabled {
 
115
                raceEnable()
 
116
        }
79
117
}
80
118
 
81
119
// RLocker returns a Locker interface that implements