File tree Expand file tree Collapse file tree 2 files changed +16
-2
lines changed
fiber/execution_context/multi_threaded Expand file tree Collapse file tree 2 files changed +16
-2
lines changed Original file line number Diff line number Diff line change @@ -179,6 +179,18 @@ class Thread
179
179
Crystal ::System ::Thread .sleep(time)
180
180
end
181
181
182
+ # Delays execution for a brief moment.
183
+ @[NoInline ]
184
+ def self.delay (backoff : Int32 ) : Int32
185
+ if backoff < 7
186
+ backoff.times { Intrinsics .pause }
187
+ backoff &+ 1
188
+ else
189
+ Thread .yield
190
+ 0
191
+ end
192
+ end
193
+
182
194
# Returns the Thread object associated to the running system thread.
183
195
def self.current : Thread
184
196
Crystal ::System ::Thread .current_thread
Original file line number Diff line number Diff line change @@ -66,15 +66,17 @@ module Fiber::ExecutionContext
66
66
# fiber but thread B didn't saved its context yet); we must wait until
67
67
# the context switch assembly saved all registers on the stack and set
68
68
# the fiber as resumable.
69
+ attempts = 0
70
+
69
71
until fiber.resumable?
70
72
if fiber.dead?
71
73
raise " BUG: tried to resume dead fiber #{ fiber } (#{ inspect } )"
72
74
end
73
75
74
76
# OPTIMIZE: if the thread saving the fiber context has been preempted,
75
77
# this will block the current thread from progressing... shall we
76
- # abort and reenqueue the fiber after MAX iterations ?
77
- Intrinsics .pause
78
+ # abort and reenqueue the fiber after MAX attempts ?
79
+ attempts = Thread .delay(attempts)
78
80
end
79
81
80
82
swapcontext(fiber)
You can’t perform that action at this time.
0 commit comments