Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-bitswap
Commits
061f0d39
Commit
061f0d39
authored
Dec 14, 2014
by
Jeromy
Committed by
Juan Batiz-Benet
Dec 17, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add locks to wantlist to avoid race condition
parent
4b4958e3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
0 deletions
+10
-0
wantlist/wantlist.go
wantlist/wantlist.go
+10
-0
No files found.
wantlist/wantlist.go
View file @
061f0d39
...
...
@@ -3,9 +3,11 @@ package wantlist
import
(
u
"github.com/jbenet/go-ipfs/util"
"sort"
"sync"
)
type
Wantlist
struct
{
lk
sync
.
RWMutex
set
map
[
u
.
Key
]
*
Entry
}
...
...
@@ -21,6 +23,8 @@ type Entry struct {
}
func
(
w
*
Wantlist
)
Add
(
k
u
.
Key
,
priority
int
)
{
w
.
lk
.
Lock
()
defer
w
.
lk
.
Unlock
()
if
_
,
ok
:=
w
.
set
[
k
];
ok
{
return
}
...
...
@@ -31,10 +35,14 @@ func (w *Wantlist) Add(k u.Key, priority int) {
}
func
(
w
*
Wantlist
)
Remove
(
k
u
.
Key
)
{
w
.
lk
.
Lock
()
defer
w
.
lk
.
Unlock
()
delete
(
w
.
set
,
k
)
}
func
(
w
*
Wantlist
)
Contains
(
k
u
.
Key
)
bool
{
w
.
lk
.
RLock
()
defer
w
.
lk
.
RUnlock
()
_
,
ok
:=
w
.
set
[
k
]
return
ok
}
...
...
@@ -46,6 +54,8 @@ func (es entrySlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
func
(
es
entrySlice
)
Less
(
i
,
j
int
)
bool
{
return
es
[
i
]
.
Priority
>
es
[
j
]
.
Priority
}
func
(
w
*
Wantlist
)
Entries
()
[]
*
Entry
{
w
.
lk
.
RLock
()
defer
w
.
lk
.
RUnlock
()
var
es
entrySlice
for
_
,
e
:=
range
w
.
set
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment