/
sync.go
89 lines (73 loc) · 1.46 KB
/
sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sync
import (
"sync"
"v.io/x/ref/runtime/internal/testing/concurrency"
)
// Mutex is a wrapper around the Go implementation of Mutex.
type Mutex struct {
m sync.Mutex
}
// MUTEX INTERFACE IMPLEMENTATION
func (m *Mutex) Lock() {
if t := concurrency.T(); t != nil {
t.MutexLock(&m.m)
} else {
m.m.Lock()
}
}
func (m *Mutex) Unlock() {
if t := concurrency.T(); t != nil {
t.MutexUnlock(&m.m)
} else {
m.m.Unlock()
}
}
// RWMutex is a wrapper around the Go implementation of RWMutex.
type RWMutex struct {
m sync.RWMutex
}
// RWMUTEX INTERFACE IMPLEMENTATION
func (m *RWMutex) Lock() {
if t := concurrency.T(); t != nil {
t.RWMutexLock(&m.m)
} else {
m.m.Lock()
}
}
func (m *RWMutex) RLock() {
if t := concurrency.T(); t != nil {
t.RWMutexRLock(&m.m)
} else {
m.m.RLock()
}
}
func (m *RWMutex) RLocker() sync.Locker {
if t := concurrency.T(); t != nil {
return (*rlocker)(m)
}
return m.m.RLocker()
}
func (m *RWMutex) RUnlock() {
if t := concurrency.T(); t != nil {
t.RWMutexRUnlock(&m.m)
} else {
m.m.RUnlock()
}
}
func (m *RWMutex) Unlock() {
if t := concurrency.T(); t != nil {
t.RWMutexUnlock(&m.m)
} else {
m.m.Unlock()
}
}
type rlocker RWMutex
func (r *rlocker) Lock() {
(*RWMutex)(r).RLock()
}
func (r *rlocker) Unlock() {
(*RWMutex)(r).RUnlock()
}