Skip to content

Commit

Permalink
Merge pull request #1 from wuhunyu/main
Browse files Browse the repository at this point in the history
修复 linklist 部分方法死锁问题
  • Loading branch information
shenghui0779 authored Nov 15, 2024
2 parents a83aaba + b59b7b7 commit 798346f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
27 changes: 21 additions & 6 deletions linklist/doublylinklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type element[T comparable] struct {
func New[T comparable](values ...T) *DoublyLinkList[T] {
list := &DoublyLinkList[T]{}
if len(values) > 0 {
list.Append(values...)
list.appendWithoutLock(values...)
}
return list
}
Expand All @@ -35,6 +35,11 @@ func (list *DoublyLinkList[T]) Append(values ...T) {
list.mutex.Lock()
defer list.mutex.Unlock()

list.appendWithoutLock(values...)
}

// appendWithoutLock 向链表尾部追加值(不持有锁)
func (list *DoublyLinkList[T]) appendWithoutLock(values ...T) {
for _, value := range values {
newElement := &element[T]{value: value, prev: list.last}
if list.size == 0 {
Expand Down Expand Up @@ -172,6 +177,11 @@ func (list *DoublyLinkList[T]) Values() []T {
list.mutex.RLock()
defer list.mutex.RUnlock()

return list.valuesWithoutLock()
}

// valuesWithoutLock 返回链表所有元素的值(不持有锁)
func (list *DoublyLinkList[T]) valuesWithoutLock() []T {
values := make([]T, 0, list.size)
for e := list.first; e != nil; e = e.next {
values = append(values, e.value)
Expand Down Expand Up @@ -210,6 +220,11 @@ func (list *DoublyLinkList[T]) Clear() {
list.mutex.Lock()
defer list.mutex.Unlock()

list.clearWithoutLock()
}

// clearWithoutLock 清空链表(不持有锁)
func (list *DoublyLinkList[T]) clearWithoutLock() {
list.size = 0
list.first = nil
list.last = nil
Expand All @@ -224,11 +239,11 @@ func (list *DoublyLinkList[T]) Sort(comparator func(x, y T) int) {
return
}

values := list.Values()
values := list.valuesWithoutLock()
slices.SortFunc(values, comparator)

list.Clear()
list.Append(values...)
list.clearWithoutLock()
list.appendWithoutLock(values...)
}

// Swap 交换链表中两个指定索引的元素
Expand Down Expand Up @@ -258,7 +273,7 @@ func (list *DoublyLinkList[T]) Insert(index int, values ...T) {
if !list.withinRange(index) {
// Append
if index == list.size {
list.Append(values...)
list.appendWithoutLock(values...)
}
return
}
Expand Down Expand Up @@ -316,7 +331,7 @@ func (list *DoublyLinkList[T]) Set(index int, value T) {
if !list.withinRange(index) {
// Append
if index == list.size {
list.Append(value)
list.appendWithoutLock(value)
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion linklist/doublylinklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func TestSet(t *testing.T) {
func TestString(t *testing.T) {
c := New[int]()
c.Append(1)
if !strings.HasPrefix(c.String(), "DoublyLinkedList") {
if !strings.HasPrefix(c.String(), "DoublyLinkList") {
t.Errorf("String should start with container name")
}
}
Expand Down

0 comments on commit 798346f

Please sign in to comment.