-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract priority code into a library
- Loading branch information
Showing
8 changed files
with
68 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Lru | ||
|
||
Package lru implements an LRU cache. | ||
Package lru implements an lru cache. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// A data structure that follows the constraints of a least recently used (lru) cache. | ||
package lru | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Priority | ||
|
||
Package priority implements a priority mutex. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package priority | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// Priority implement a lock with priorities. | ||
type Priority struct { | ||
l []sync.Mutex | ||
} | ||
|
||
// Call the function f with priority. | ||
func (p *Priority) Pri(n int, f func() error) error { | ||
for i := n; i >= 0; i-- { | ||
p.l[i].Lock() | ||
} | ||
err := f() | ||
for i := n; i >= 0; i-- { | ||
p.l[i].Unlock() | ||
} | ||
return err | ||
} | ||
|
||
// NewPriority returns a new Priority with n priority levels. | ||
func NewPriority(n int) *Priority { | ||
return &Priority{ | ||
l: make([]sync.Mutex, n), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package priority | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func BenchmarkPriority(b *testing.B) { | ||
pri := NewPriority(3) | ||
for range b.N { | ||
pri.Pri(2, func() error { | ||
return nil | ||
}) | ||
} | ||
} | ||
|
||
func TestPriority(t *testing.T) { | ||
pri := NewPriority(3) | ||
pri.Pri(0, func() error { | ||
return nil | ||
}) | ||
pri.Pri(1, func() error { | ||
return nil | ||
}) | ||
pri.Pri(2, func() error { | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.