mirror of
https://github.com/jeremyd/ergo.git
synced 2026-07-27 01:18:10 -07:00
atomic bitset implementations of caps.Set and modes.ModeSet
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
// Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
|
||||
// released under the MIT license
|
||||
|
||||
package utils
|
||||
|
||||
import "sync/atomic"
|
||||
|
||||
// Library functions for lock-free bitsets, typically (constant-sized) arrays of uint64.
|
||||
// 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
|
||||
bit := position % 64
|
||||
block := atomic.LoadUint64(&set[idx])
|
||||
return (block & (1 << bit)) != 0
|
||||
}
|
||||
|
||||
// BitsetSet sets a given bit of the bitset to 0 or 1, returning whether it changed.
|
||||
func BitsetSet(set []uint64, position uint, on bool) (changed bool) {
|
||||
idx := position / 64
|
||||
bit := position % 64
|
||||
addr := &set[idx]
|
||||
var mask uint64
|
||||
mask = 1 << bit
|
||||
for {
|
||||
current := atomic.LoadUint64(addr)
|
||||
previouslyOn := (current & mask) != 0
|
||||
if on == previouslyOn {
|
||||
return false
|
||||
}
|
||||
var desired uint64
|
||||
if on {
|
||||
desired = current | mask
|
||||
} else {
|
||||
desired = current & (^mask)
|
||||
}
|
||||
if atomic.CompareAndSwapUint64(addr, current, desired) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BitsetEmpty returns whether the bitset is empty.
|
||||
// Right now, this is technically free of race conditions because we don't
|
||||
// have a method that can simultaneously modify two bits separated by a word boundary
|
||||
// such that one of those modifications is an unset. If we did, there would be a race
|
||||
// that could produce false positives. It's probably better to assume that they are
|
||||
// already possible under concurrent modification (which is not how we're using this).
|
||||
func BitsetEmpty(set []uint64) (empty bool) {
|
||||
for i := 0; i < len(set); i++ {
|
||||
if atomic.LoadUint64(&set[i]) != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// BitsetUnion modifies `set` to be the union of `set` and `other`.
|
||||
// This has race conditions in that we don't necessarily get a single
|
||||
// consistent view of `other` across word boundaries.
|
||||
func BitsetUnion(set []uint64, other []uint64) {
|
||||
for i := 0; i < len(set); i++ {
|
||||
for {
|
||||
ourAddr := &set[i]
|
||||
ourBlock := atomic.LoadUint64(ourAddr)
|
||||
otherBlock := atomic.LoadUint64(&other[i])
|
||||
newBlock := ourBlock | otherBlock
|
||||
if atomic.CompareAndSwapUint64(ourAddr, ourBlock, newBlock) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
|
||||
// released under the MIT license
|
||||
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
var i uint
|
||||
for i = 0; i < 128; i++ {
|
||||
if i%2 == 0 {
|
||||
BitsetSet(t1s, i, true)
|
||||
}
|
||||
}
|
||||
|
||||
if !(BitsetGet(t1s, 0) && !BitsetGet(t1s, 1) && BitsetGet(t1s, 64) && BitsetGet(t1s, 72) && !BitsetGet(t1s, 127)) {
|
||||
t.Error("exactly the even-numbered bits should be set")
|
||||
}
|
||||
|
||||
BitsetSet(t1s, 72, false)
|
||||
if BitsetGet(t1s, 72) {
|
||||
t.Error("remove doesn't work")
|
||||
}
|
||||
|
||||
var t2 testBitset
|
||||
t2s := t2[:]
|
||||
BitsetInitialize(t2s)
|
||||
|
||||
for i = 0; i < 128; i++ {
|
||||
if i%2 == 1 {
|
||||
BitsetSet(t2s, i, true)
|
||||
}
|
||||
}
|
||||
|
||||
BitsetUnion(t1s, t2s)
|
||||
for i = 0; i < 128; i++ {
|
||||
expected := (i != 72)
|
||||
if BitsetGet(t1s, i) != expected {
|
||||
t.Error("all bits should be set except 72")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user