From 492e9afa4c32be278b61498be20ae0afc3ef3188 Mon Sep 17 00:00:00 2001 From: flywind <43030857+xflywind@users.noreply.github.com> Date: Fri, 13 Nov 2020 15:11:00 +0800 Subject: [PATCH] fix #15941 (#15948) * fix #15941 * add testcase * update --- lib/pure/collections/sharedlist.nim | 11 +++++++---- tests/stdlib/tsharedlist.nim | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 tests/stdlib/tsharedlist.nim diff --git a/lib/pure/collections/sharedlist.nim b/lib/pure/collections/sharedlist.nim index 790529b79a..c72477675b 100644 --- a/lib/pure/collections/sharedlist.nim +++ b/lib/pure/collections/sharedlist.nim @@ -65,11 +65,14 @@ iterator items*[A](x: var SharedList[A]): A = proc add*[A](x: var SharedList[A]; y: A) = withLock(x): var node: SharedListNode[A] - if x.tail == nil or x.tail.dataLen == ElemsPerNode: - node = cast[type node](allocShared0(sizeof(node[]))) - node.next = x.tail + if x.tail == nil: + node = cast[typeof node](allocShared0(sizeof(node[]))) + x.tail = node + x.head = node + elif x.tail.dataLen == ElemsPerNode: + node = cast[typeof node](allocShared0(sizeof(node[]))) + x.tail.next = node x.tail = node - if x.head == nil: x.head = node else: node = x.tail node.d[node.dataLen] = y diff --git a/tests/stdlib/tsharedlist.nim b/tests/stdlib/tsharedlist.nim new file mode 100644 index 0000000000..a795be0f3a --- /dev/null +++ b/tests/stdlib/tsharedlist.nim @@ -0,0 +1,17 @@ +import sharedlist + +var + list: SharedList[int] + count: int + +init(list) + +for i in 1 .. 250: + list.add i + +for i in list: + inc count + +doAssert count == 250 + +deinitSharedList(list)