Skip to content

Commit

Permalink
Extract priority code into a library
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Sep 11, 2024
1 parent dd35519 commit 183f2be
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 73 deletions.
2 changes: 1 addition & 1 deletion lib/lru/README.md
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.
1 change: 1 addition & 0 deletions lib/lru/lru.go
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 (
Expand Down
3 changes: 3 additions & 0 deletions lib/priority/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Priority

Package priority implements a priority mutex.
29 changes: 29 additions & 0 deletions lib/priority/priority.go
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),
}
}
27 changes: 27 additions & 0 deletions lib/priority/priority_test.go
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
})
}
13 changes: 7 additions & 6 deletions protocol/czar/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"

"github.com/mohanson/daze/lib/doa"
"github.com/mohanson/daze/lib/priority"
)

// A Stream managed by the multiplexer.
Expand All @@ -26,7 +27,7 @@ func (s *Stream) Close() error {
s.rer.Put(io.ErrClosedPipe)
s.wer.Put(io.ErrClosedPipe)
s.zo0.Do(func() {
s.mux.pri.H(func() error {
s.mux.pri.Pri(0, func() error {
s.mux.con.Write([]byte{s.idx, 0x02, 0x00, 0x00})
return nil
})
Expand All @@ -39,7 +40,7 @@ func (s *Stream) Esolc() error {
s.rer.Put(io.EOF)
s.wer.Put(io.ErrClosedPipe)
s.zo0.Do(func() {
s.mux.pri.H(func() error {
s.mux.pri.Pri(0, func() error {
s.mux.con.Write([]byte{s.idx, 0x02, 0x01, 0x00})
return nil
})
Expand Down Expand Up @@ -98,7 +99,7 @@ func (s *Stream) Write(p []byte) (int, error) {
binary.BigEndian.PutUint16(b[2:4], uint16(l))
copy(b[4:], p[:l])
p = p[l:]
err := s.mux.pri.M(func() error {
err := s.mux.pri.Pri(1, func() error {
if err := s.wer.Get(); err != nil {
return err
}
Expand Down Expand Up @@ -144,7 +145,7 @@ type Mux struct {
ach chan *Stream
con net.Conn
idp *Sip
pri *Priority
pri *priority.Priority
rer *Err
usb []*Stream
}
Expand All @@ -171,7 +172,7 @@ func (m *Mux) Open() (*Stream, error) {
if err != nil {
return nil, err
}
err = m.pri.H(func() error {
err = m.pri.Pri(0, func() error {
return doa.Err(m.con.Write([]byte{idx, 0x00, 0x00, 0x00}))
})
if err != nil {
Expand Down Expand Up @@ -250,7 +251,7 @@ func NewMux(conn net.Conn) *Mux {
ach: make(chan *Stream),
con: conn,
idp: NewSip(),
pri: NewPriority(),
pri: priority.NewPriority(2),
rer: NewErr(),
usb: make([]*Stream, 256),
}
Expand Down
48 changes: 0 additions & 48 deletions protocol/czar/priority.go

This file was deleted.

18 changes: 0 additions & 18 deletions protocol/czar/priority_test.go

This file was deleted.

0 comments on commit 183f2be

Please sign in to comment.