Skip to content

RFC 2: MT safe fiber context switch on ARM#15582

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
ysbaddaden:feature/execution-context-arm-context-switch
Mar 31, 2025
Merged

RFC 2: MT safe fiber context switch on ARM#15582
straight-shoota merged 2 commits intocrystal-lang:masterfrom
ysbaddaden:feature/execution-context-arm-context-switch

Conversation

@ysbaddaden
Copy link
Collaborator

@ysbaddaden ysbaddaden commented Mar 21, 2025

The ARM32 equivalent of #15581.

As stated in a comment the chosen assembly is compatible with armv6, but might not work on older architectures and it doesn't take advantage of armv7 supporting the dmb ish instruction.

We might want to consider to integrate #14524 (comment) into the compiler, so we could add flags based on features, for example armv7 when any feature matches /\+v7(ve|r|m|em|s|k|)/.

Note: I also reduced the duplicated assembly. Let's use the flags to wrap the FPU instructions intead of duplicating the assembly 4 times. Extracted as #15585 but the commit is still in this branch because I need it.

@ysbaddaden ysbaddaden added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:multithreading labels Mar 21, 2025
@ysbaddaden ysbaddaden self-assigned this Mar 21, 2025
@ysbaddaden
Copy link
Collaborator Author

ysbaddaden commented Mar 21, 2025

It's actually a bit more subtle: we must check for FeatureDB (I guess it's +db) to know when data barriers (dmb) is supported, because some ARMv6 support it (the m variant).

@straight-shoota
Copy link
Member

@ysbaddaden This needs a conflict resolution.

We need a store-store barrier between pushing the registers to the
current stack and setting the resumable flag of the current fiber,
otherwise the CPU is allowed to reorder the instructions at runtime and
to store the resumable flag before or while storing the registers.

This can happen for example:

thread 1: enqueues current fiber A
therad 1: swapcontext -> store resumable
thread 1: is preempted
thread 2: steals fiber A
thread 2: resumes fiber A
thread 2: loads registers => reads garbage => segfaults
thread 1: stores registers (too late)
@ysbaddaden ysbaddaden force-pushed the feature/execution-context-arm-context-switch branch from 905e3e3 to 61e888c Compare March 28, 2025 14:15
@ysbaddaden
Copy link
Collaborator Author

ysbaddaden commented Mar 28, 2025

@straight-shoota Rebased to drop the extracted first commit merged with #15585. No more conflicts.

@straight-shoota straight-shoota added this to the 1.16.0 milestone Mar 28, 2025
@straight-shoota straight-shoota merged commit bffefb2 into crystal-lang:master Mar 31, 2025
31 of 32 checks passed
@github-project-automation github-project-automation bot moved this from Review to Done in Multi-threading Mar 31, 2025
@ysbaddaden ysbaddaden deleted the feature/execution-context-arm-context-switch branch March 31, 2025 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:multithreading topic:stdlib:concurrency

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants