-
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
4 changed files
with
83 additions
and
15 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
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,49 @@ | ||
package czar | ||
|
||
import ( | ||
"errors" | ||
"math/big" | ||
"sync" | ||
) | ||
|
||
// A stream id generator. Stream id can be reused, and the smallest available stream id is guaranteed to be generated | ||
// each time. | ||
type Sip struct { | ||
i *big.Int | ||
m *sync.Mutex | ||
} | ||
|
||
// Get selects an stream id from the pool, removes it from the pool, and returns it to the caller. | ||
func (s *Sip) Get() (uint8, error) { | ||
s.m.Lock() | ||
defer s.m.Unlock() | ||
n := big.NewInt(0).Not(s.i) | ||
m := n.TrailingZeroBits() | ||
if m == 256 { | ||
return 0, errors.New("daze: out of stream") | ||
} | ||
s.i.SetBit(s.i, int(m), 1) | ||
return uint8(m), nil | ||
} | ||
|
||
// Put adds x to the pool. | ||
func (s *Sip) Put(x uint8) { | ||
s.m.Lock() | ||
defer s.m.Unlock() | ||
s.i = s.i.SetBit(s.i, int(x), 0) | ||
} | ||
|
||
// Set removes x from the pool. | ||
func (s *Sip) Set(x uint8) { | ||
s.m.Lock() | ||
defer s.m.Unlock() | ||
s.i = s.i.SetBit(s.i, int(x), 1) | ||
} | ||
|
||
// NewSip returns a new sid. | ||
func NewSip() *Sip { | ||
return &Sip{ | ||
i: big.NewInt(0), | ||
m: &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,19 @@ | ||
package czar | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mohanson/daze/lib/doa" | ||
) | ||
|
||
func TestSip(t *testing.T) { | ||
sid := NewSip() | ||
for i := range 256 { | ||
doa.Doa(doa.Try(sid.Get()) == uint8(i)) | ||
} | ||
doa.Doa(doa.Err(sid.Get()) != nil) | ||
sid.Put(65) | ||
sid.Put(15) | ||
doa.Doa(doa.Try(sid.Get()) == 15) | ||
doa.Doa(doa.Try(sid.Get()) == 65) | ||
} |