Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
8c58b83e
Commit
8c58b83e
authored
Dec 14, 2019
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enhance LockFreeLinkedList
parent
a1d3cf0f
Changes
2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
262 additions
and
88 deletions
+262
-88
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
...onMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
+171
-68
mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/utils/LockFreeLinkedListTest.kt
...st/kotlin/net/mamoe/mirai/utils/LockFreeLinkedListTest.kt
+91
-20
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/LockFreeLinkedList.kt
View file @
8c58b83e
This diff is collapsed.
Click to expand it.
mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/utils/LockFreeLinkedListTest.kt
View file @
8c58b83e
...
@@ -23,10 +23,10 @@ internal class LockFreeLinkedListTest {
...
@@ -23,10 +23,10 @@ internal class LockFreeLinkedListTest {
@Test
@Test
fun
addAndGetSingleThreaded
()
{
fun
addAndGetSingleThreaded
()
{
val
list
=
LockFreeLinkedList
<
Int
>()
val
list
=
LockFreeLinkedList
<
Int
>()
list
.
add
(
1
)
list
.
add
Last
(
1
)
list
.
add
(
2
)
list
.
add
Last
(
2
)
list
.
add
(
3
)
list
.
add
Last
(
3
)
list
.
add
(
4
)
list
.
add
Last
(
4
)
list
.
size
shouldBeEqualTo
4
list
.
size
shouldBeEqualTo
4
}
}
...
@@ -36,7 +36,7 @@ internal class LockFreeLinkedListTest {
...
@@ -36,7 +36,7 @@ internal class LockFreeLinkedListTest {
//withContext(Dispatchers.Default){
//withContext(Dispatchers.Default){
val
list
=
LockFreeLinkedList
<
Int
>()
val
list
=
LockFreeLinkedList
<
Int
>()
list
.
concurrent
Add
(
1000
,
10
,
1
)
list
.
concurrent
Do
(
1000
,
10
)
{
addLast
(
1
)
}
list
.
size
shouldBeEqualTo
1000
*
10
list
.
size
shouldBeEqualTo
1000
*
10
list
.
concurrentDo
(
100
,
10
)
{
list
.
concurrentDo
(
100
,
10
)
{
...
@@ -51,18 +51,55 @@ internal class LockFreeLinkedListTest {
...
@@ -51,18 +51,55 @@ internal class LockFreeLinkedListTest {
fun
addAndGetMassConcurrentAccess
()
=
runBlocking
{
fun
addAndGetMassConcurrentAccess
()
=
runBlocking
{
val
list
=
LockFreeLinkedList
<
Int
>()
val
list
=
LockFreeLinkedList
<
Int
>()
val
addJob
=
async
{
list
.
concurrent
Add
(
5000
,
10
,
1
)
}
val
addJob
=
async
{
list
.
concurrent
Do
(
2
,
30000
)
{
addLast
(
1
)
}
}
delay
(
10
)
// let addJob fly
//delay(1
) // let addJob fly
if
(
addJob
.
isCompleted
)
{
if
(
addJob
.
isCompleted
)
{
error
(
"Number of elements are not enough"
)
error
(
"Number of elements are not enough"
)
}
}
list
.
concurrentDo
(
1000
,
10
)
{
val
foreachJob
=
async
{
list
.
concurrentDo
(
1
,
10000
)
{
forEach
{
it
+
it
}
}
}
val
removeLastJob
=
async
{
list
.
concurrentDo
(
1
,
15000
)
{
removeLast
()
shouldBeEqualTo
1
}
}
val
removeFirstJob
=
async
{
list
.
concurrentDo
(
1
,
10000
)
{
removeFirst
()
shouldBeEqualTo
1
}
}
val
addJob2
=
async
{
list
.
concurrentDo
(
1
,
5000
)
{
addLast
(
1
)
}
}
val
removeExactJob
=
launch
{
list
.
concurrentDo
(
3
,
1000
)
{
remove
(
1
).
shouldBeTrue
()
remove
(
1
).
shouldBeTrue
()
}
}
addJob
.
join
()
}
val
filteringGetOrAddJob
=
launch
{
list
.
concurrentDo
(
1
,
10000
)
{
filteringGetOrAdd
({
it
==
2
},
{
1
})
}
}
joinAll
(
addJob
,
addJob2
,
foreachJob
,
removeLastJob
,
removeFirstJob
,
removeExactJob
,
filteringGetOrAddJob
)
list
.
size
shouldBeEqualTo
5000
*
10
-
1000
*
10
list
.
size
shouldBeEqualTo
2
*
30000
-
1
*
15000
-
1
*
10000
+
1
*
5000
-
3
*
1000
+
1
*
10000
}
@Test
fun
removeWhileForeach
()
{
val
list
=
LockFreeLinkedList
<
Int
>()
repeat
(
10
)
{
list
.
addLast
(
it
)
}
list
.
forEach
{
list
.
remove
(
it
+
1
)
}
list
.
peekFirst
()
shouldBeEqualTo
0
}
}
@Test
@Test
...
@@ -72,11 +109,11 @@ internal class LockFreeLinkedListTest {
...
@@ -72,11 +109,11 @@ internal class LockFreeLinkedListTest {
assertFalse
{
list
.
remove
(
1
)
}
assertFalse
{
list
.
remove
(
1
)
}
assertEquals
(
0
,
list
.
size
)
assertEquals
(
0
,
list
.
size
)
list
.
add
(
1
)
list
.
add
Last
(
1
)
assertTrue
{
list
.
remove
(
1
)
}
assertTrue
{
list
.
remove
(
1
)
}
assertEquals
(
0
,
list
.
size
)
assertEquals
(
0
,
list
.
size
)
list
.
add
(
2
)
list
.
add
Last
(
2
)
assertFalse
{
list
.
remove
(
1
)
}
assertFalse
{
list
.
remove
(
1
)
}
assertEquals
(
1
,
list
.
size
)
assertEquals
(
1
,
list
.
size
)
}
}
...
@@ -107,6 +144,43 @@ internal class LockFreeLinkedListTest {
...
@@ -107,6 +144,43 @@ internal class LockFreeLinkedListTest {
list
.
toString
()
shouldBeEqualTo
"[1, 2, 3, 4, 5]"
list
.
toString
()
shouldBeEqualTo
"[1, 2, 3, 4, 5]"
}
}
@Test
fun
`filteringGetOrAdd
when
add`
()
{
val
list
=
LockFreeLinkedList
<
Int
>()
list
.
addAll
(
listOf
(
1
,
2
,
3
,
4
,
5
))
val
value
=
list
.
filteringGetOrAdd
({
it
==
6
},
{
6
})
println
(
"Check value"
)
value
shouldBeEqualTo
6
println
(
"Check size"
)
println
(
list
.
getLinkStructure
())
list
.
size
shouldBeEqualTo
6
}
@Test
fun
`filteringGetOrAdd
when
get
`
()
{
val
list
=
LockFreeLinkedList
<
Int
>()
list
.
addAll
(
listOf
(
1
,
2
,
3
,
4
,
5
))
val
value
=
list
.
filteringGetOrAdd
({
it
==
2
},
{
2
})
println
(
"Check value"
)
value
shouldBeEqualTo
2
println
(
"Check size"
)
println
(
list
.
getLinkStructure
())
list
.
size
shouldBeEqualTo
5
}
@Test
fun
`filteringGetOrAdd
when
empty`
()
{
val
list
=
LockFreeLinkedList
<
Int
>()
val
value
=
list
.
filteringGetOrAdd
({
it
==
2
},
{
2
})
println
(
"Check value"
)
value
shouldBeEqualTo
2
println
(
"Check size"
)
println
(
list
.
getLinkStructure
())
list
.
size
shouldBeEqualTo
1
}
/*
/*
@Test
@Test
fun indexOf() {
fun indexOf() {
...
@@ -176,16 +250,13 @@ internal class LockFreeLinkedListTest {
...
@@ -176,16 +250,13 @@ internal class LockFreeLinkedListTest {
*/
*/
}
}
@UseExperimental
(
ExperimentalCoroutinesApi
::
class
)
@MiraiExperimentalAPI
@MiraiExperimentalAPI
internal
suspend
inline
fun
<
E
>
LockFreeLinkedList
<
E
>.
concurrentAdd
(
numberOfCoroutines
:
Int
,
timesOfAdd
:
Int
,
element
:
E
)
=
internal
suspend
inline
fun
<
E
:
LockFreeLinkedList
<
*
>>
E
.
concurrentDo
(
numberOfCoroutines
:
Int
,
times
:
Int
,
crossinline
todo
:
E
.()
->
Unit
)
=
concurrentDo
(
numberOfCoroutines
,
timesOfAdd
)
{
add
(
element
)
}
@MiraiExperimentalAPI
internal
suspend
inline
fun
<
E
:
LockFreeLinkedList
<
*
>>
E
.
concurrentDo
(
numberOfCoroutines
:
Int
,
timesOfAdd
:
Int
,
crossinline
todo
:
E
.()
->
Unit
)
=
coroutineScope
{
coroutineScope
{
repeat
(
numberOfCoroutines
)
{
repeat
(
numberOfCoroutines
)
{
launch
{
launch
(
start
=
CoroutineStart
.
UNDISPATCHED
)
{
repeat
(
times
OfAdd
)
{
repeat
(
times
)
{
todo
()
todo
()
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment