-
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.
- Loading branch information
Showing
3 changed files
with
97 additions
and
39 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
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,48 @@ | ||
package czar | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// Priority implement a lock with three priorities. | ||
type Priority struct { | ||
l sync.Mutex | ||
m sync.Mutex | ||
h sync.Mutex | ||
} | ||
|
||
// H executes function f with 0 priority. | ||
func (p *Priority) H(f func() error) error { | ||
p.h.Lock() | ||
defer p.h.Unlock() | ||
return f() | ||
} | ||
|
||
// H executes function f with 1 priority. | ||
func (p *Priority) M(f func() error) error { | ||
p.m.Lock() | ||
defer p.m.Unlock() | ||
p.h.Lock() | ||
defer p.h.Unlock() | ||
return f() | ||
} | ||
|
||
// H executes function f with 2 priority. | ||
func (p *Priority) L(f func() error) error { | ||
p.l.Lock() | ||
defer p.l.Unlock() | ||
p.m.Lock() | ||
defer p.m.Unlock() | ||
p.h.Lock() | ||
defer p.h.Unlock() | ||
return f() | ||
} | ||
|
||
// NewPriority returns a new Priority. | ||
func NewPriority() *Priority { | ||
return &Priority{ | ||
l: sync.Mutex{}, | ||
m: sync.Mutex{}, | ||
h: sync.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,18 @@ | ||
package czar | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPriority(t *testing.T) { | ||
pri := NewPriority() | ||
pri.H(func() error { | ||
return nil | ||
}) | ||
pri.M(func() error { | ||
return nil | ||
}) | ||
pri.L(func() error { | ||
return nil | ||
}) | ||
} |