@@ -101,40 +101,37 @@ internal class WorkQueue {
101
101
}
102
102
103
103
/* *
104
- * Tries stealing from [victim] queue into the [stolenTaskRef] argument.
104
+ * Tries stealing from this queue into the [stolenTaskRef] argument.
105
105
*
106
106
* Returns [NOTHING_TO_STEAL] if queue has nothing to steal, [TASK_STOLEN] if at least task was stolen
107
107
* or positive value of how many nanoseconds should pass until the head of this queue will be available to steal.
108
108
*/
109
- fun tryStealFrom (victim : WorkQueue , stolenTaskRef : ObjectRef <Task ?>): Long {
110
- assert { bufferSize == 0 }
111
- val task = victim.pollBuffer()
109
+ fun trySteal (stolenTaskRef : ObjectRef <Task ?>): Long {
110
+ val task = pollBuffer()
112
111
if (task != null ) {
113
112
stolenTaskRef.element = task
114
113
return TASK_STOLEN
115
114
}
116
- return tryStealLastScheduled(victim, stolenTaskRef, blockingOnly = false )
115
+ return tryStealLastScheduled(stolenTaskRef, blockingOnly = false )
117
116
}
118
117
119
- fun tryStealBlockingFrom (victim : WorkQueue , stolenTaskRef : ObjectRef <Task ?>): Long {
120
- assert { bufferSize == 0 }
121
- var start = victim.consumerIndex.value
122
- val end = victim.producerIndex.value
123
- val buffer = victim.buffer
118
+ fun tryStealBlocking (stolenTaskRef : ObjectRef <Task ?>): Long {
119
+ var start = consumerIndex.value
120
+ val end = producerIndex.value
124
121
125
122
while (start != end) {
126
123
val index = start and MASK
127
- if (victim. blockingTasksInBuffer.value == 0 ) break
124
+ if (blockingTasksInBuffer.value == 0 ) break
128
125
val value = buffer[index]
129
126
if (value != null && value.isBlocking && buffer.compareAndSet(index, value, null )) {
130
- victim. blockingTasksInBuffer.decrementAndGet()
127
+ blockingTasksInBuffer.decrementAndGet()
131
128
stolenTaskRef.element = value
132
129
return TASK_STOLEN
133
130
} else {
134
131
++ start
135
132
}
136
133
}
137
- return tryStealLastScheduled(victim, stolenTaskRef, blockingOnly = true )
134
+ return tryStealLastScheduled(stolenTaskRef, blockingOnly = true )
138
135
}
139
136
140
137
fun offloadAllWorkTo (globalQueue : GlobalQueue ) {
@@ -145,11 +142,11 @@ internal class WorkQueue {
145
142
}
146
143
147
144
/* *
148
- * Contract on return value is the same as for [tryStealFrom ]
145
+ * Contract on return value is the same as for [trySteal ]
149
146
*/
150
- private fun tryStealLastScheduled (victim : WorkQueue , stolenTaskRef : ObjectRef <Task ?>, blockingOnly : Boolean ): Long {
147
+ private fun tryStealLastScheduled (stolenTaskRef : ObjectRef <Task ?>, blockingOnly : Boolean ): Long {
151
148
while (true ) {
152
- val lastScheduled = victim. lastScheduledTask.value ? : return NOTHING_TO_STEAL
149
+ val lastScheduled = lastScheduledTask.value ? : return NOTHING_TO_STEAL
153
150
if (blockingOnly && ! lastScheduled.isBlocking) return NOTHING_TO_STEAL
154
151
155
152
// TODO time wraparound ?
@@ -163,7 +160,7 @@ internal class WorkQueue {
163
160
* If CAS has failed, either someone else had stolen this task or the owner executed this task
164
161
* and dispatched another one. In the latter case we should retry to avoid missing task.
165
162
*/
166
- if (victim. lastScheduledTask.compareAndSet(lastScheduled, null )) {
163
+ if (lastScheduledTask.compareAndSet(lastScheduled, null )) {
167
164
stolenTaskRef.element = lastScheduled
168
165
return TASK_STOLEN
169
166
}
0 commit comments