This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 564
Store initializing singletons in a separate thread local small map #4019
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a5abaae
Add a test
projedi d28f122
Add another test
projedi 39d678b
Use a specialized storage for shared shadow
projedi 783e052
Switch to std::map
projedi b52658e
Try using stack
projedi 7295cc0
Remove unneeded addHeapRef
projedi b419101
Fix initializer6 test
projedi 4d63969
Do not use random in initializers7
projedi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add a test
- Loading branch information
commit a5abaae70f4b4b783e5319f306ad3638120d8e4a
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license | ||
* that can be found in the LICENSE file. | ||
*/ | ||
|
||
package runtime.basic.initializers6 | ||
|
||
import kotlin.test.* | ||
|
||
import kotlin.native.concurrent.* | ||
|
||
val aWorkerId = 2 | ||
val bWorkersCount = 3 | ||
|
||
val aWorkerUnlocker = AtomicInt(0) | ||
val bWorkerUnlocker = AtomicInt(0) | ||
|
||
object A { | ||
init { | ||
// Must be called by aWorker only. | ||
assertEquals(aWorkerId, Worker.current.id) | ||
// Only allow b workers to run, when a worker has started initialization. | ||
bWorkerUnlocker.increment() | ||
// Only proceed with initialization, when all b workers have started executing. | ||
while (aWorkerUnlocker.value < bWorkersCount) {} | ||
// And now wait a bit, to increase probability of races. | ||
Worker.current.park(1000 * 1000L) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 100 ms would be enough :) |
||
} | ||
val a = produceA() | ||
val b = produceB() | ||
} | ||
|
||
fun produceA(): String { | ||
// Must've been called by aWorker only. | ||
assertEquals(aWorkerId, Worker.current.id) | ||
return "A" | ||
} | ||
|
||
fun produceB(): String { | ||
// Must've been called by aWorker only. | ||
assertEquals(aWorkerId, Worker.current.id) | ||
// Also check that it's ok to get A.a while initializing A.b. | ||
return "B+${A.a}" | ||
} | ||
|
||
@Test fun runTest() { | ||
val aWorker = Worker.start() | ||
// This test relies on aWorkerId value. | ||
assertEquals(aWorkerId, aWorker.id) | ||
val bWorkers = Array(bWorkersCount, { _ -> Worker.start() }) | ||
|
||
val aFuture = aWorker.execute(TransferMode.SAFE, {}, { | ||
A.b | ||
}) | ||
val bFutures = Array(bWorkers.size, { | ||
bWorkers[it].execute(TransferMode.SAFE, {}, { | ||
// Wait until A has started to initialize. | ||
while (bWorkerUnlocker.value < 1) {} | ||
// Now allow A initialization to continue. | ||
aWorkerUnlocker.increment() | ||
// And this should not've tried to init A itself. | ||
A.a + A.b | ||
}) | ||
}) | ||
|
||
for (future in bFutures) { | ||
assertEquals("AB+A", future.result) | ||
} | ||
assertEquals("B+A", aFuture.result) | ||
|
||
for (worker in bWorkers) { | ||
worker.requestTermination().result | ||
} | ||
aWorker.requestTermination().result | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is always correct.