mirror of
https://github.com/jeremyd/ergo.git
synced 2026-07-26 00:58:11 -07:00
refactor channel registration
This commit is contained in:
@@ -9,17 +9,6 @@ import "sync/atomic"
|
||||
// For examples of use, see caps.Set and modes.ModeSet; the array has to be converted to a
|
||||
// slice to use these functions.
|
||||
|
||||
// BitsetInitialize initializes a bitset.
|
||||
func BitsetInitialize(set []uint64) {
|
||||
// XXX re-zero the bitset using atomic stores. it's unclear whether this is required,
|
||||
// however, golang issue #5045 suggests that you shouldn't mix atomic operations
|
||||
// with non-atomic operations (such as the runtime's automatic zero-initialization) on
|
||||
// the same word
|
||||
for i := 0; i < len(set); i++ {
|
||||
atomic.StoreUint64(&set[i], 0)
|
||||
}
|
||||
}
|
||||
|
||||
// BitsetGet returns whether a given bit of the bitset is set.
|
||||
func BitsetGet(set []uint64, position uint) bool {
|
||||
idx := position / 64
|
||||
|
||||
@@ -10,7 +10,6 @@ type testBitset [2]uint64
|
||||
func TestSets(t *testing.T) {
|
||||
var t1 testBitset
|
||||
t1s := t1[:]
|
||||
BitsetInitialize(t1s)
|
||||
|
||||
if BitsetGet(t1s, 0) || BitsetGet(t1s, 63) || BitsetGet(t1s, 64) || BitsetGet(t1s, 127) {
|
||||
t.Error("no bits should be set in a newly initialized bitset")
|
||||
@@ -47,7 +46,6 @@ func TestSets(t *testing.T) {
|
||||
|
||||
var t2 testBitset
|
||||
t2s := t2[:]
|
||||
BitsetInitialize(t2s)
|
||||
|
||||
for i = 0; i < 128; i++ {
|
||||
if i%2 == 1 {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Once is a fork of sync.Once to expose a Done() method.
|
||||
type Once struct {
|
||||
done uint32
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
func (o *Once) Do(f func()) {
|
||||
if atomic.LoadUint32(&o.done) == 0 {
|
||||
o.doSlow(f)
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Once) doSlow(f func()) {
|
||||
o.m.Lock()
|
||||
defer o.m.Unlock()
|
||||
if o.done == 0 {
|
||||
defer atomic.StoreUint32(&o.done, 1)
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Once) Done() bool {
|
||||
return atomic.LoadUint32(&o.done) == 1
|
||||
}
|
||||
Reference in New Issue
Block a user