-
-
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
Fixes for typedthreads data race issues #24612
Open
mk1nz
wants to merge
7
commits into
nim-lang:devel
Choose a base branch
from
mk1nz:devel
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−41
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d555881
Fixes for typedthreads data race issues
mk1nz 2686229
Update typedthreads.nim
mk1nz d0b1070
fixed type mismatch errors to work with Boehm
mk1nz 621c77e
Update threadimpl.nim
mk1nz 5d21a31
Update
mk1nz af0d68f
Update typedthreads.nim
mk1nz 17e7e9a
Update typedthreads.nim
mk1nz 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
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 |
---|---|---|
|
@@ -75,7 +75,7 @@ deinitLock(l) | |
``` | ||
]## | ||
|
||
|
||
import std/atomics | ||
import std/private/[threadtypes] | ||
export Thread | ||
|
||
|
@@ -149,9 +149,9 @@ else: | |
nimThreadProcWrapperBody(closure) | ||
{.pop.} | ||
|
||
proc running*[TArg](t: Thread[TArg]): bool {.inline.} = | ||
proc running*[TArg](t: var Thread[TArg]): bool {.inline.} = | ||
## Returns true if `t` is running. | ||
result = t.dataFn != nil | ||
result = t.dataFn.load() != nil | ||
|
||
proc handle*[TArg](t: Thread[TArg]): SysThread {.inline.} = | ||
## Returns the thread handle of `t`. | ||
|
@@ -202,7 +202,7 @@ when false: | |
else: | ||
discard pthread_cancel(t.sys) | ||
when declared(registerThread): unregisterThread(addr(t)) | ||
t.dataFn = nil | ||
t.dataFn.store nil | ||
## if thread `t` already exited, `t.core` will be `null`. | ||
if not isNil(t.core): | ||
deallocThreadStorage(t.core) | ||
|
@@ -217,10 +217,10 @@ when hostOS == "windows": | |
## Entry point is the proc `tp`. | ||
## `param` is passed to `tp`. `TArg` can be `void` if you | ||
## don't need to pass any data to the thread. | ||
t.core = cast[PGcThread](allocThreadStorage(sizeof(GcThread))) | ||
t.core.store cast[PGcThread](allocThreadStorage(sizeof(GcThread))) | ||
|
||
when TArg isnot void: t.data = param | ||
t.dataFn = tp | ||
when TArg isnot void: t.data.store param | ||
t.dataFn.store tp | ||
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. these loads/stores can use relaxed memory ordering 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. I set moAcquireRelease, and if I'm right, this will work just like withLock |
||
when hasSharedHeap: t.core.stackSize = ThreadStackSize | ||
var dummyThreadId: int32 = 0'i32 | ||
t.sys = createThread(nil, ThreadStackSize, threadProcWrapper[TArg], | ||
|
@@ -242,10 +242,10 @@ elif defined(genode): | |
proc createThread*[TArg](t: var Thread[TArg], | ||
tp: proc (arg: TArg) {.thread, nimcall.}, | ||
param: TArg) = | ||
t.core = cast[PGcThread](allocThreadStorage(sizeof(GcThread))) | ||
t.core.store cast[PGcThread](allocThreadStorage(sizeof(GcThread))) | ||
|
||
when TArg isnot void: t.data = param | ||
t.dataFn = tp | ||
when TArg isnot void: t.data.store param | ||
t.dataFn.store tp | ||
when hasSharedHeap: t.stackSize = ThreadStackSize | ||
t.sys.initThread( | ||
runtimeEnv, | ||
|
@@ -266,11 +266,14 @@ else: | |
## Entry point is the proc `tp`. `param` is passed to `tp`. | ||
## `TArg` can be `void` if you | ||
## don't need to pass any data to the thread. | ||
t.core = cast[PGcThread](allocThreadStorage(sizeof(GcThread))) | ||
|
||
when TArg isnot void: t.data = param | ||
t.dataFn = tp | ||
when hasSharedHeap: t.core.stackSize = ThreadStackSize | ||
t.core.store cast[PGcThread](allocThreadStorage(sizeof(GcThread))) | ||
|
||
when TArg isnot void: t.data.store param | ||
t.dataFn.store tp | ||
when hasSharedHeap: | ||
var stackSize: PGcThread = t.core.load() | ||
stacksize.stackSize = ThreadStackSize | ||
t.core.store stacksize | ||
var a {.noinit.}: Pthread_attr | ||
doAssert pthread_attr_init(a) == 0 | ||
when hasAllocStack: | ||
|
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
Oops, something went wrong.
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.
the
var
here is a breaking change, why is it needed?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.
please ignore. If I manage to fix other issues that arise, this change will be removed with the next commit.
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.
nim c --styleCheck:usages --styleCheck:error --verbosity:0 --hints:off --skipParentCfg --skipUserCfg --outdir:build '--nimcache:build/nimcache/$projectName' -d:metricsTest -d:metrics --threads:on -d:nimTypeNames --mm:refc -r tests/chronos_server_tests
/Users/runner/work/Nim/Nim/pkgstemp/metrics/metrics/chronos_httpserver.nim(400, 6) template/generic instantiation of
async
from here/Users/runner/work/Nim/Nim/pkgstemp/metrics/metrics/chronos_httpserver.nim(403, 26) template/generic instantiation of
running
from here/Users/runner/work/Nim/Nim/lib/std/typedthreads.nim(155, 22) Error: type mismatch
Expression: load(t.dataFn, moAcquireRelease)
[1] t.dataFn: Atomic[proc (m: MetricsServerData){.nimcall, gcsafe.}]
[2] moAcquireRelease: MemoryOrder
Expected one of (first mismatch at [position]):
[1] proc load[T: Trivial](location: var Atomic[T];
order: MemoryOrder = moSequentiallyConsistent): T
[1] proc load[T: not Trivial](location: var Atomic[T];
order: MemoryOrder = moSequentiallyConsistent): T
expression 't.dataFn' is immutable, not 'var'