Skip to content

Commit 0260de2

Browse files
committed
Formatting
1 parent 32217bd commit 0260de2

File tree

19 files changed

+116
-95
lines changed

19 files changed

+116
-95
lines changed

atomicfu/src/androidNativeMain/kotlin/kotlinx/atomicfu/locks/PosixParkingDelegator.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package kotlinx.atomicfu.locks
22

33
import kotlinx.cinterop.*
4-
import kotlinx.cinterop.alloc
5-
import kotlinx.cinterop.free
6-
import kotlinx.cinterop.pointed
7-
import kotlinx.cinterop.ptr
84
import platform.posix.*
95

106
@OptIn(ExperimentalForeignApi::class, UnsafeNumber::class)
@@ -23,15 +19,15 @@ internal actual object ParkingDelegator {
2319
return ParkingData(mut, cond)
2420
}
2521

26-
actual inline fun wait(ref: ParkingData, shouldWait: () -> Boolean){
22+
actual inline fun wait(ref: ParkingData, shouldWait: () -> Boolean) {
2723
callAndVerify { pthread_mutex_lock(ref.mut) }
2824
try {
2925
if (shouldWait()) callAndVerify { pthread_cond_wait(ref.cond, ref.mut) }
3026
} finally {
3127
callAndVerify { pthread_mutex_unlock(ref.mut) }
3228
}
3329
}
34-
30+
3531
actual inline fun timedWait(ref: ParkingData, nanos: Long, shouldWait: () -> Boolean): Unit = memScoped {
3632
val ts = alloc<timespec>().ptr
3733

@@ -68,4 +64,8 @@ internal actual object ParkingDelegator {
6864
nativeHeap.free(ref.cond)
6965
}
7066
}
71-
internal actual class ParkingData @OptIn(UnsafeNumber::class) constructor(val mut: CPointer<pthread_mutex_t>, val cond: CPointer<pthread_cond_t>)
67+
68+
internal actual class ParkingData @OptIn(UnsafeNumber::class) constructor(
69+
val mut: CPointer<pthread_mutex_t>,
70+
val cond: CPointer<pthread_cond_t>
71+
)

atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ParkingDelegator.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package kotlinx.atomicfu.locks
22

33
/**
4-
* Object that stores references that need to be manually destroyed and deallocated,
4+
* Object that stores references that need to be manually destroyed and deallocated,
55
* after native pthread_cond_wait usage.
66
*/
77
internal expect class ParkingData
@@ -12,7 +12,7 @@ internal expect class ParkingData
1212
*/
1313
internal expect object ParkingDelegator {
1414
fun createRef(): ParkingData
15-
fun wait(ref: ParkingData, shouldWait: () -> Boolean)
15+
fun wait(ref: ParkingData, shouldWait: () -> Boolean)
1616
fun timedWait(ref: ParkingData, nanos: Long, shouldWait: () -> Boolean)
1717
fun wake(ref: ParkingData)
1818
fun destroyRef(ref: ParkingData)
@@ -22,18 +22,18 @@ internal expect object ParkingDelegator {
2222
* Adds nano seconds to current time in seconds.
2323
* Clamps for Int.
2424
*/
25-
internal fun Int.addNanosToSeconds(nanos: Long): Int =
25+
internal fun Int.addNanosToSeconds(nanos: Long): Int =
2626
(this + nanos / 1_000_000_000).coerceIn(Int.MIN_VALUE.toLong(), Int.MAX_VALUE.toLong()).toInt()
2727

2828
/**
2929
* Adds nano seconds to current time in seconds.
3030
*/
3131
internal fun Long.addNanosToSeconds(nanos: Long): Long {
32-
32+
3333
// Should never happen as this is checked in `ThreadParker`
3434
check(nanos >= 0) { "Cannot wait for a negative number of nanoseconds" }
35-
val result = this + nanos / 1_000_000_000
36-
35+
val result = this + nanos / 1_000_000_000
36+
3737
// Overflow check: should never happen since this is very far into the future.
3838
check(!(this xor result < 0 && this >= 0)) { "Nano seconds addition overflowed, current time in seconds is $this" }
3939
return result
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package kotlinx.atomicfu.locks
22

3+
import kotlinx.atomicfu.locks.ParkingSupport.currentThreadHandle
4+
import kotlinx.atomicfu.locks.ParkingSupport.park
5+
import kotlinx.atomicfu.locks.ParkingSupport.unpark
36
import kotlin.time.Duration
47
import kotlin.time.TimeMark
58

69
/**
7-
* Parking and unparking support for threads on Kotlin/Native and Kotlin/JVM.
10+
* Parking and unparking support for threads on Kotlin/Native and Kotlin/JVM.
811
* Can be used as a building block to create locks and other synchronization primitives.
912
*
1013
* A call to [ParkingSupport.park] or [ParkingSupport.parkUntil] will pause the current thread.
@@ -14,9 +17,9 @@ import kotlin.time.TimeMark
1417
* - A spurious wakeup
1518
* - (Only on JVM) The thread was interrupted. The interrupted flag stays set after wakeup.
1619
* A future call to [park] this thread will return immediately, unless the `Thread.interrupted` flag is cleared.
17-
*
20+
*
1821
* The caller is responsible for verifying the reason of wakeup and how to respond accordingly.
19-
*
22+
*
2023
* Example usage parking thread:
2124
* ```Kotlin
2225
* // publish my parking handle
@@ -26,22 +29,22 @@ import kotlin.time.TimeMark
2629
* ParkingSupport.park(Duration.INFINITE)
2730
* }
2831
* ```
29-
*
32+
*
3033
* Example usage unparker thread:
3134
* ```Kotlin
3235
* state.value = WAKE
3336
* ParkingSupport.unpark(handleReference.value)
3437
* ```
35-
*
38+
*
3639
* PLEASE NOTE: this is a low-level API and should be used with caution.
3740
* Unless the goal is to create a _synchronization primitive_ like a mutex or semaphore,
3841
* it is advised to a higher level concurrency API like `kotlinx.coroutines`
3942
*/
4043
expect object ParkingSupport {
41-
44+
4245
/**
4346
* Parks the current thread for [timeout] duration.
44-
*
47+
*
4548
* Wakes up in the following cases:
4649
* - A different thread calls [ParkingSupport.unpark].
4750
* - The [timeout] is exceeded.
@@ -50,7 +53,7 @@ expect object ParkingSupport {
5053
* A future call to [park] this thread will return immediately, unless the `Thread.interrupted` flag is cleared.
5154
*/
5255
fun park(timeout: Duration)
53-
56+
5457
/**
5558
* Parks the current thread until [deadline] is reached.
5659
*
@@ -67,21 +70,21 @@ expect object ParkingSupport {
6770
* Unparks the thread corresponding to [handle].
6871
* If [unpark] is called while the corresponding thread is not parked, the next [park] call will return immediately
6972
* — the [ParkingHandle] is unparked ahead of time.
70-
*
71-
* A [ParkingHandle] can only _remember_ one pre-unpark attempt at a time.
72-
* Meaning, when two consecutive [unpark] calls are made while the corresponding thread is not parked,
73+
*
74+
* A [ParkingHandle] can only _remember_ one pre-unpark attempt at a time.
75+
* Meaning, when two consecutive [unpark] calls are made while the corresponding thread is not parked,
7376
* only the next park call will return immediately — [unpark] calls are not accumulated.
7477
*/
7578
fun unpark(handle: ParkingHandle)
7679

7780
/**
78-
* Returns the [ParkingHandle] corresponding to the current thread.
81+
* Returns the [ParkingHandle] corresponding to the current thread.
7982
* This [ParkingHandle] should be shared with other threads which allow them to [unpark] the current thread.
80-
*
83+
*
8184
* A [ParkingHandle] is uniquely associated with a specific thread, maintaining a one-to-one correspondence.
82-
* When the _same_ thread makes multiple calls to [currentThreadHandle],
85+
* When the _same_ thread makes multiple calls to [currentThreadHandle],
8386
* it always returns the _same_ [ParkingHandle].
84-
*
87+
*
8588
* Note: as this function returns a unique [ParkingHandle] for each thread it should not be cached or memoized.
8689
*/
8790
fun currentThreadHandle(): ParkingHandle
@@ -91,7 +94,7 @@ expect object ParkingSupport {
9194
* A handle allowing to unpark a thread of execution using [ParkingSupport.unpark].
9295
* There is a one-to-one mapping between threads and parking handles.
9396
* A handle can be obtained by calling [ParkingSupport.currentThreadHandle].
94-
* Refer to [ParkingSupport] documentation for more details
97+
* Refer to [ParkingSupport] documentation for more details
9598
* on how to use [ParkingHandle] and how parking works in general.
9699
*/
97100
expect class ParkingHandle

atomicfu/src/concurrentMain/kotlin/kotlinx/atomicfu/locks/ThreadParker.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import kotlin.time.TimeSource
66

77
/**
88
* Thread parker for Kotlin/Native based on POSIX calls.
9-
* Resides in a shared sourceSet with JVM, to be testable with Lincheck.
9+
* Resides in a shared sourceSet with JVM, to be testable with Lincheck.
1010
* (Which is part of PR #508)
1111
*/
1212
internal class ThreadParker {
@@ -16,7 +16,7 @@ internal class ThreadParker {
1616
fun park() = parkWith { data ->
1717
delegator.wait(data) { state.value is Parked }
1818
}
19-
19+
2020
fun parkNanos(nanos: Long) {
2121
val mark = TimeSource.Monotonic.markNow()
2222
parkWith { data ->
@@ -35,7 +35,7 @@ internal class ThreadParker {
3535
delegator.destroyRef(pd)
3636
continue
3737
}
38-
38+
3939
invokeWait(pd)
4040

4141
while (true) {
@@ -45,7 +45,7 @@ internal class ThreadParker {
4545
delegator.destroyRef(pd)
4646
return
4747
}
48-
48+
4949
// If other thread is unparking return. Let unparking thread deal with cleanup.
5050
is Unparking -> if (state.compareAndSet(changedState, Free)) return
5151

@@ -64,7 +64,7 @@ internal class ThreadParker {
6464
}
6565
// Parker was pre unparked. Set to free and continue.
6666
Unparked -> if (state.compareAndSet(Unparked, Free)) return
67-
67+
6868
// The states below should only be reachable if parking thread has not yet returned.
6969
is Parked -> throw IllegalStateException("Thread should not be able to call park when it is already parked")
7070
is Unparking -> throw IllegalStateException("Thread should not be able to call park when it is already parked")
@@ -75,14 +75,14 @@ internal class ThreadParker {
7575
fun unpark() {
7676
val myUnparkingState = Unparking()
7777
while (true) {
78-
when (val currentState = state.value) {
79-
78+
when (val currentState = state.value) {
79+
8080
// Is already unparked
8181
Unparked -> return
8282
is Unparking -> return
83-
83+
8484
Free -> if (state.compareAndSet(Free, Unparked)) return
85-
85+
8686
// Is parked -> try unpark
8787
is Parked -> if (state.compareAndSet(currentState, myUnparkingState)) {
8888
delegator.wake(currentState.data)
@@ -97,6 +97,7 @@ internal class ThreadParker {
9797
}
9898

9999
private sealed interface ParkingState
100+
100101
// The Parker is pre-unparked. The next park call will change state to Free and return immediately.
101102
private object Unparked : ParkingState
102103

atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/BarrierTest.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ class BarrierTest {
1616
private class Arrs(numberOfThreads: Int) {
1717
val after = AtomicIntArray(numberOfThreads)
1818
val before = AtomicIntArray(numberOfThreads)
19-
init {repeat(numberOfThreads) {
19+
20+
init {
21+
repeat(numberOfThreads) {
2022
after[it].value = 0
2123
before[it].value = 0
22-
}}
24+
}
25+
}
2326
}
27+
2428
@Test
2529
fun testBarrier() {
2630
repeat(TEST_ITERATIONS) { iteration ->
@@ -60,6 +64,7 @@ private class Barrier(private val parties: Int) {
6064
init {
6165
require(parties > 1)
6266
}
67+
6368
private val count = atomic(0)
6469
private val waiters = atomicArrayOfNulls<Any?>(parties - 1)
6570

atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/CyclicBarrierTest.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ class CyclicBarrierTest {
1616
private class Arrs(numberOfThreads: Int) {
1717
val after = AtomicIntArray(numberOfThreads)
1818
val before = AtomicIntArray(numberOfThreads)
19-
init {repeat(numberOfThreads) {
20-
after[it].value = 0
21-
before[it].value = 0
22-
}}
19+
20+
init {
21+
repeat(numberOfThreads) {
22+
after[it].value = 0
23+
before[it].value = 0
24+
}
25+
}
2326
}
24-
27+
2528
@Test
2629
fun stressCyclicBarrier() {
2730
BARRIER_SIZES.forEach { barrierSize ->
@@ -94,8 +97,7 @@ private class MSQueueCyclicBarrier<E> {
9497
if (curTail.next.compareAndSet(null, node)) {
9598
tail.compareAndSet(curTail, node)
9699
return node.id
97-
}
98-
else tail.compareAndSet(curTail, curTail.next.value!!)
100+
} else tail.compareAndSet(curTail, curTail.next.value!!)
99101
}
100102
}
101103

@@ -111,6 +113,7 @@ private class MSQueueCyclicBarrier<E> {
111113
}
112114
}
113115
}
116+
114117
private class Node<E>(var element: E?, val id: Long) {
115118
val next = atomic<Node<E>?>(null)
116119
}

atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/ExchangerTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import kotlin.time.Duration
88
private const val N_ITEMS_TO_SWAP = 100_000
99

1010
class ExchangerTest {
11-
11+
1212
@Test
1313
fun exchangeTwoLists() {
1414
val aBefore = List(N_ITEMS_TO_SWAP) { 0 }
1515
val bBefore = List(N_ITEMS_TO_SWAP) { 1 }
1616
val aAfter = mutableListOf<Int>()
1717
val bAfter = mutableListOf<Int>()
18-
18+
1919
val exchanger = Exchanger<Int>()
20-
20+
2121
val at = testThread {
2222
aBefore.forEachIndexed { i, v ->
2323
val item = exchanger.exchange(v)

atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/LatchTest.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ class LatchTest {
1515
private class Arrs(numberOfThreads: Int) {
1616
val after = AtomicIntArray(numberOfThreads)
1717
val before = AtomicIntArray(numberOfThreads)
18-
init {repeat(numberOfThreads) {
19-
after[it].value = 0
20-
before[it].value = 0
21-
}}
18+
19+
init {
20+
repeat(numberOfThreads) {
21+
after[it].value = 0
22+
before[it].value = 0
23+
}
24+
}
2225
}
23-
26+
2427
@Test
2528
fun latchTest() {
2629
repeat(TEST_ITERATIONS) { iteration ->
@@ -90,8 +93,7 @@ private class MSQueueLatch<E> {
9093
if (curTail.next.compareAndSet(null, node)) {
9194
tail.compareAndSet(curTail, node)
9295
return
93-
}
94-
else tail.compareAndSet(curTail, curTail.next.value!!)
96+
} else tail.compareAndSet(curTail, curTail.next.value!!)
9597
}
9698
}
9799

@@ -106,6 +108,7 @@ private class MSQueueLatch<E> {
106108
}
107109
}
108110
}
111+
109112
private class Node<E>(var element: E?) {
110113
val next = atomic<Node<E>?>(null)
111114
}

atomicfu/src/concurrentTest/kotlin/kotlinx/atomicfu/locks/TestThread.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package kotlinx.atomicfu.locks
22

3-
internal fun testThread(doConcurrent: () -> Unit): TestThread = TestThread(doConcurrent)
3+
internal fun testThread(doConcurrent: () -> Unit): TestThread = TestThread(doConcurrent)
44

55
internal expect class TestThread(toDo: () -> Unit) {
66
fun join()

0 commit comments

Comments
 (0)