3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00
Use []uint32 in bitset instead of []uint64, because it's harder to guarantee
64-bit alignment of []uint64 than I had realized:

https://go101.org/article/memory-layout.html
This commit is contained in:
Shivaram Lingamneni 2019-05-27 15:17:28 -04:00
parent bc2e546de9
commit 9fe65223db
5 changed files with 37 additions and 38 deletions

View File

@ -204,8 +204,8 @@ package caps
numCapabs = len(CAPDEFS) numCapabs = len(CAPDEFS)
bitsetLen = numCapabs // 64 bitsetLen = numCapabs // 32
if numCapabs % 64 > 0: if numCapabs % 32 > 0:
bitsetLen += 1 bitsetLen += 1
print (""" print ("""
const ( const (

View File

@ -11,7 +11,7 @@ import (
) )
// Set holds a set of enabled capabilities. // Set holds a set of enabled capabilities.
type Set [bitsetLen]uint64 type Set [bitsetLen]uint32
// NewSet returns a new Set, with the given capabilities enabled. // NewSet returns a new Set, with the given capabilities enabled.
func NewSet(capabs ...Capability) *Set { func NewSet(capabs ...Capability) *Set {

View File

@ -7,7 +7,6 @@ package modes
import ( import (
"strings" "strings"
"sync/atomic"
"github.com/oragono/oragono/irc/utils" "github.com/oragono/oragono/irc/utils"
) )
@ -318,12 +317,13 @@ func ParseChannelModeChanges(params ...string) (ModeChanges, map[rune]bool) {
} }
// ModeSet holds a set of modes. // ModeSet holds a set of modes.
type ModeSet [1]uint64 type ModeSet [2]uint32
// valid modes go from 65 ('A') to 122 ('z'), making at most 58 possible values; // valid modes go from 65 ('A') to 122 ('z'), making at most 58 possible values;
// subtract 65 from the mode value and use that bit of the uint64 to represent it // subtract 65 from the mode value and use that bit of the uint32 to represent it
const ( const (
minMode = 65 // 'A' minMode = 65 // 'A'
maxMode = 122 // 'z'
) )
// returns a pointer to a new ModeSet // returns a pointer to a new ModeSet
@ -357,11 +357,10 @@ func (set *ModeSet) AllModes() (result []Mode) {
return return
} }
block := atomic.LoadUint64(&set[0]) var i Mode
var i uint for i = minMode; i <= maxMode; i++ {
for i = 0; i < 64; i++ { if set.HasMode(i) {
if block&(1<<i) != 0 { result = append(result, i)
result = append(result, Mode(minMode+i))
} }
} }
return return

View File

@ -5,28 +5,28 @@ package utils
import "sync/atomic" import "sync/atomic"
// Library functions for lock-free bitsets, typically (constant-sized) arrays of uint64. // Library functions for lock-free bitsets, typically (constant-sized) arrays of uint32.
// For examples of use, see caps.Set and modes.ModeSet; the array has to be converted to a // For examples of use, see caps.Set and modes.ModeSet; the array has to be converted to a
// slice to use these functions. // slice to use these functions.
// BitsetGet returns whether a given bit of the bitset is set. // BitsetGet returns whether a given bit of the bitset is set.
func BitsetGet(set []uint64, position uint) bool { func BitsetGet(set []uint32, position uint) bool {
idx := position / 64 idx := position / 32
bit := position % 64 bit := position % 32
block := atomic.LoadUint64(&set[idx]) block := atomic.LoadUint32(&set[idx])
return (block & (1 << bit)) != 0 return (block & (1 << bit)) != 0
} }
// BitsetSet sets a given bit of the bitset to 0 or 1, returning whether it changed. // 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) { func BitsetSet(set []uint32, position uint, on bool) (changed bool) {
idx := position / 64 idx := position / 32
bit := position % 64 bit := position % 32
addr := &set[idx] addr := &set[idx]
var mask uint64 var mask uint32
mask = 1 << bit mask = 1 << bit
for { for {
current := atomic.LoadUint64(addr) current := atomic.LoadUint32(addr)
var desired uint64 var desired uint32
if on { if on {
desired = current | mask desired = current | mask
} else { } else {
@ -34,7 +34,7 @@ func BitsetSet(set []uint64, position uint, on bool) (changed bool) {
} }
if current == desired { if current == desired {
return false return false
} else if atomic.CompareAndSwapUint64(addr, current, desired) { } else if atomic.CompareAndSwapUint32(addr, current, desired) {
return true return true
} }
} }
@ -44,9 +44,9 @@ func BitsetSet(set []uint64, position uint, on bool) (changed bool) {
// This has false positives under concurrent modification (i.e., it can return true // This has false positives under concurrent modification (i.e., it can return true
// even though w.r.t. the sequence of atomic modifications, there was no point at // even though w.r.t. the sequence of atomic modifications, there was no point at
// which the bitset was completely empty), but that's not how we're using this method. // which the bitset was completely empty), but that's not how we're using this method.
func BitsetEmpty(set []uint64) (empty bool) { func BitsetEmpty(set []uint32) (empty bool) {
for i := 0; i < len(set); i++ { for i := 0; i < len(set); i++ {
if atomic.LoadUint64(&set[i]) != 0 { if atomic.LoadUint32(&set[i]) != 0 {
return false return false
} }
} }
@ -56,14 +56,14 @@ func BitsetEmpty(set []uint64) (empty bool) {
// BitsetUnion modifies `set` to be the union of `set` and `other`. // BitsetUnion modifies `set` to be the union of `set` and `other`.
// This has race conditions in that we don't necessarily get a single // This has race conditions in that we don't necessarily get a single
// consistent view of `other` across word boundaries. // consistent view of `other` across word boundaries.
func BitsetUnion(set []uint64, other []uint64) { func BitsetUnion(set []uint32, other []uint32) {
for i := 0; i < len(set); i++ { for i := 0; i < len(set); i++ {
for { for {
ourAddr := &set[i] ourAddr := &set[i]
ourBlock := atomic.LoadUint64(ourAddr) ourBlock := atomic.LoadUint32(ourAddr)
otherBlock := atomic.LoadUint64(&other[i]) otherBlock := atomic.LoadUint32(&other[i])
newBlock := ourBlock | otherBlock newBlock := ourBlock | otherBlock
if atomic.CompareAndSwapUint64(ourAddr, ourBlock, newBlock) { if atomic.CompareAndSwapUint32(ourAddr, ourBlock, newBlock) {
break break
} }
} }
@ -72,23 +72,23 @@ func BitsetUnion(set []uint64, other []uint64) {
// BitsetCopy copies the contents of `other` over `set`. // BitsetCopy copies the contents of `other` over `set`.
// Similar caveats about race conditions as with `BitsetUnion` apply. // Similar caveats about race conditions as with `BitsetUnion` apply.
func BitsetCopy(set []uint64, other []uint64) { func BitsetCopy(set []uint32, other []uint32) {
for i := 0; i < len(set); i++ { for i := 0; i < len(set); i++ {
data := atomic.LoadUint64(&other[i]) data := atomic.LoadUint32(&other[i])
atomic.StoreUint64(&set[i], data) atomic.StoreUint32(&set[i], data)
} }
} }
// BitsetSubtract modifies `set` to subtract the contents of `other`. // BitsetSubtract modifies `set` to subtract the contents of `other`.
// Similar caveats about race conditions as with `BitsetUnion` apply. // Similar caveats about race conditions as with `BitsetUnion` apply.
func BitsetSubtract(set []uint64, other []uint64) { func BitsetSubtract(set []uint32, other []uint32) {
for i := 0; i < len(set); i++ { for i := 0; i < len(set); i++ {
for { for {
ourAddr := &set[i] ourAddr := &set[i]
ourBlock := atomic.LoadUint64(ourAddr) ourBlock := atomic.LoadUint32(ourAddr)
otherBlock := atomic.LoadUint64(&other[i]) otherBlock := atomic.LoadUint32(&other[i])
newBlock := ourBlock & (^otherBlock) newBlock := ourBlock & (^otherBlock)
if atomic.CompareAndSwapUint64(ourAddr, ourBlock, newBlock) { if atomic.CompareAndSwapUint32(ourAddr, ourBlock, newBlock) {
break break
} }
} }

View File

@ -5,7 +5,7 @@ package utils
import "testing" import "testing"
type testBitset [2]uint64 type testBitset [4]uint32
func TestSets(t *testing.T) { func TestSets(t *testing.T) {
var t1 testBitset var t1 testBitset