This commit is contained in:
tursom 2023-03-15 13:59:26 +08:00
parent f72933280c
commit 693d0a45c0
8 changed files with 106 additions and 12 deletions

Binary file not shown.

51
concurrent/lock/CLH.go Normal file
View File

@ -0,0 +1,51 @@
package lock
import (
"github.com/petermattis/goid"
"github.com/tursom/GoCollections/exceptions"
"github.com/tursom/GoCollections/lang/atomic"
)
type (
CLH struct {
tail atomic.Reference[clhNode]
state atomic.Reference[clhNode]
}
clhNode struct {
prev *clhNode
locked atomic.Bool
gid int64
}
)
func (c *CLH) Lock() {
node := &clhNode{
prev: c.tail.Load(),
locked: 1, // true
gid: goid.Get(),
}
for !c.tail.CompareAndSwap(node.prev, node) {
node.prev = c.tail.Load()
}
if node.prev == nil {
return
}
for node.prev.locked.Load() {
}
c.state.Store(node)
}
func (c *CLH) Unlock() {
node := c.state.Load()
if node.gid != goid.Get() {
panic(exceptions.NewIllegalAccessException("unlock with wrong goroutine", nil))
}
node.locked.Store(false)
}

View File

@ -0,0 +1,18 @@
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
package exceptions
type IllegalAccessException struct {
RuntimeException
}
func NewIllegalAccessException(message string, config *ExceptionConfig) *IllegalAccessException {
return &IllegalAccessException{
*NewRuntimeException(message, config.AddSkipStack(1).
SetExceptionName("github.com.tursom.GoCollections.exceptions.IllegalParameterException")),
}
}

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/tursom/GoCollections
go 1.19
go 1.20
require (
github.com/petermattis/goid v0.0.0-20220302125637-5f11c28912df

View File

@ -3,8 +3,8 @@ package lang
type (
Array[T any] []T
Int8Array Array[Int32]
Int16Array Array[Int32]
Int8Array Array[Int8]
Int16Array Array[Int16]
Int32Array Array[Int32]
Int64Array Array[Int64]
UInt8Array Array[UInt8]
@ -25,54 +25,54 @@ func (a Int8Array) SetBit(bit int, up bool) (old bool) {
arrIndex := bit / 8
i := &a[arrIndex]
return a[arrIndex].SwapBit(bit%8, up)
return SwapBit[int8]((*int8)(i), bit%8, up)
}
func (a Int16Array) SetBit(bit int, up bool) (old bool) {
arrIndex := bit / 16
i := &a[arrIndex]
return SwapBit(i, bit%16, up)
return SwapBit[int16]((*int16)(i), bit%16, up)
}
func (a Int32Array) SetBit(bit int, up bool) (old bool) {
arrIndex := bit / 32
i := &a[arrIndex]
return SwapBit(i, bit%32, up)
return SwapBit[int32]((*int32)(i), bit%32, up)
}
func (a Int64Array) SetBit(bit int, up bool) (old bool) {
arrIndex := bit / 64
i := &a[arrIndex]
return SwapBit(i, bit%64, up)
return SwapBit[int64]((*int64)(i), bit%64, up)
}
func (a UInt8Array) SetBit(bit int, up bool) (old bool) {
arrIndex := bit / 8
i := &a[arrIndex]
return SwapBit(i, bit%8, up)
return SwapBit[uint8]((*uint8)(i), bit%8, up)
}
func (a UInt16Array) SetBit(bit int, up bool) (old bool) {
arrIndex := bit / 16
i := &a[arrIndex]
return SwapBit(i, bit%16, up)
return SwapBit[uint16]((*uint16)(i), bit%16, up)
}
func (a UInt32Array) SetBit(bit int, up bool) (old bool) {
arrIndex := bit / 32
i := &a[arrIndex]
return SwapBit(i, bit%32, up)
return SwapBit[uint32]((*uint32)(i), bit%32, up)
}
func (a UInt64Array) SetBit(bit int, up bool) (old bool) {
arrIndex := bit / 64
i := &a[arrIndex]
return SwapBit(i, bit%64, up)
return SwapBit[uint64]((*uint64)(i), bit%64, up)
}

View File

@ -13,7 +13,7 @@ type Number interface {
ToFloat64() Float64
}
func SwapBit[T int32 | int64 | uint32 | uint64](p *T, bit int, new bool) (old bool) {
func SwapBit[T int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64](p *T, bit int, new bool) (old bool) {
location := T(1) << bit
oldValue := *p
var newValue T

View File

@ -0,0 +1,19 @@
package functional
type (
Function interface {
invoke()
}
FunctionErred interface {
invoke() error
}
Function1[A1 any] interface {
invoke(a1 A1)
}
Function1Erred[A1 any] interface {
invoke(a1 A1) error
}
)

View File

@ -0,0 +1,6 @@
package functional
type (
F = Function
FE = FunctionErred
)