Skip to content

Commit 675bdd5

Browse files
committed
Prevent overflow of t to be an issue
1 parent 6477530 commit 675bdd5

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/work-stealing-queue.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ static inline ws_array_t *ws_queue_push(ws_queue_t *q, void *elt, int32_t eltsz)
5555
int64_t t = jl_atomic_load_acquire(&q->top);
5656
ws_array_t *ary = jl_atomic_load_relaxed(&q->array);
5757
ws_array_t *old_ary = NULL;
58-
if (__unlikely(b - t > ary->capacity - 1)) {
58+
int64_t size = b - t;
59+
if (__unlikely(size > ary->capacity - 1)) {
5960
ws_array_t *new_ary = create_ws_array(2 * ary->capacity, eltsz);
6061
for (int i = 0; i < ary->capacity; i++) {
6162
memcpy(new_ary->buffer + ((t + i) & new_ary->mask) * eltsz, ary->buffer + ((t + i) & ary->mask) * eltsz, eltsz);
@@ -77,9 +78,10 @@ static inline void ws_queue_pop(ws_queue_t *q, void *dest, int32_t eltsz) JL_NOT
7778
jl_atomic_store_relaxed(&q->bottom, b);
7879
jl_fence();
7980
int64_t t = jl_atomic_load_relaxed(&q->top);
80-
if (__likely(t <= b)) {
81+
int64_t size = b - t + 1;
82+
if (__likely(size > 0)) {
8183
memcpy(dest, ary->buffer + (b & ary->mask) * eltsz, eltsz);
82-
if (t == b) {
84+
if (size == 1) {
8385
if (!jl_atomic_cmpswap(&q->top, &t, t + 1))
8486
memset(dest, 0, eltsz);
8587
jl_atomic_store_relaxed(&q->bottom, b + 1);
@@ -96,7 +98,8 @@ static inline void ws_queue_steal_from(ws_queue_t *q, void *dest, int32_t eltsz)
9698
int64_t t = jl_atomic_load_acquire(&q->top);
9799
jl_fence();
98100
int64_t b = jl_atomic_load_acquire(&q->bottom);
99-
if (t < b) {
101+
int64_t size = b - t;
102+
if (size > 0) {
100103
ws_array_t *ary = jl_atomic_load_relaxed(&q->array);
101104
memcpy(dest, ary->buffer + (t & ary->mask) * eltsz, eltsz);
102105
if (!jl_atomic_cmpswap(&q->top, &t, t + 1))

0 commit comments

Comments
 (0)