-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
closes #8895 add std/once #16192
Closed
Closed
closes #8895 add std/once #16192
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
96b385e
add std/once
ringabout 6ed6c72
minor
ringabout 06ff965
small
ringabout e26d200
fix
ringabout ca1d4f7
add tests
ringabout 5922d71
changes
ringabout e3019dc
Update once.nim
ringabout b648d03
Update tonce.nim
ringabout 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
when compileOption("threads"): | ||
import std/[atomics, locks] | ||
|
||
|
||
type | ||
Once* = object ## A object that ensures a block of code is executed once. | ||
when compileOption("threads"): | ||
finished: Atomic[bool] | ||
lock: Lock | ||
else: | ||
finished: bool | ||
|
||
proc `=destroy`*(once: var Once) {.inline.} = | ||
when compileOption("threads"): | ||
deinitLock(once.lock) | ||
|
||
proc init*(once: var Once) = | ||
## Initializes a `Once` object. | ||
when compileOption("threads"): | ||
once.finished.store(false, moRelaxed) | ||
initLock(once.lock) | ||
|
||
template once*(cond: var Once, body: untyped) = | ||
## Executes a block of code only once (the first time the block is reached). | ||
## It is thread-safe. | ||
runnableExamples("--gc:orc --threads:on"): | ||
var block1: Once | ||
var count = 0 | ||
init(block1) | ||
|
||
for i in 1 .. 10: | ||
once(block1): | ||
inc count | ||
|
||
# only the first `block1` is executed | ||
once(block1): | ||
count = 888 | ||
|
||
assert count == 1 | ||
|
||
when compileOption("threads"): | ||
if not cond.finished.load(moAcquire): | ||
withLock cond.lock: | ||
if not cond.finished.load(moRelaxed): | ||
try: | ||
body | ||
finally: | ||
cond.finished.store(true, moRelease) | ||
else: | ||
if not cond.finished: | ||
try: | ||
body | ||
finally: | ||
cond.finished = true | ||
|
||
|
||
## The code block is executed only once among threads. | ||
runnableExamples("--gc:orc --threads:on"): | ||
block: | ||
var thr: array[0..4, Thread[void]] | ||
var block1: Once | ||
var count = 0 | ||
init(block1) | ||
proc threadFunc() {.thread.} = | ||
for i in 1 .. 10: | ||
once(block1): | ||
inc count | ||
for i in 0..high(thr): | ||
createThread(thr[i], threadFunc) | ||
joinThreads(thr) | ||
assert count == 1 | ||
|
||
## The code blocks is executed per thread. | ||
runnableExamples("--gc:orc --threads:on"): | ||
block: | ||
var thr: array[0..4, Thread[void]] | ||
var count = 0 | ||
proc threadFunc() {.thread.} = | ||
var block1: Once | ||
init(block1) | ||
for i in 1 .. 10: | ||
once(block1): | ||
inc count | ||
for i in 0..high(thr): | ||
createThread(thr[i], threadFunc) | ||
joinThreads(thr) | ||
assert count == thr.len |
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,34 @@ | ||
discard """ | ||
matrix: "--gc:orc --threads:on" | ||
""" | ||
import std/once | ||
|
||
|
||
block: | ||
var thr: array[0..4, Thread[void]] | ||
var block1: Once | ||
var count = 0 | ||
initOnce(block1) | ||
proc threadFunc() {.thread.} = | ||
for i in 1 .. 10: | ||
once(block1): | ||
inc count | ||
for i in 0..high(thr): | ||
createThread(thr[i], threadFunc) | ||
joinThreads(thr) | ||
doAssert count == 1 | ||
|
||
|
||
block: | ||
var thr: array[0..4, Thread[void]] | ||
var count = 0 | ||
proc threadFunc() {.thread.} = | ||
var block1: Once | ||
initOnce(block1) | ||
for i in 1 .. 10: | ||
once(block1): | ||
inc count | ||
for i in 0..high(thr): | ||
createThread(thr[i], threadFunc) | ||
joinThreads(thr) | ||
doAssert count == 5 |
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.
js needs to be supported because
system.once
already works in js (not considering edge case of webworkers)(and leave a TODO for supporting VM because VM already doesn't work with system.once)