Skip to content

Commit 96af6f0

Browse files
committed
rt: Randomize port selection
1 parent 601f714 commit 96af6f0

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/rt/rust_port_selector.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ rust_port_selector::select(rust_task *task, rust_port **dptr,
2626
// block the task before any of them can try to send another
2727
// message.
2828

29+
// Start looking for ports from a different index each time.
30+
size_t j = isaac_rand(&task->thread->rctx);
2931
for (size_t i = 0; i < n_ports; i++) {
30-
rust_port *port = ports[i];
32+
size_t k = (i + j) % n_ports;
33+
rust_port *port = ports[k];
3134
I(task->thread, port != NULL);
3235

3336
port->lock.lock();
@@ -50,7 +53,8 @@ rust_port_selector::select(rust_task *task, rust_port **dptr,
5053
}
5154

5255
for (size_t i = 0; i < locks_taken; i++) {
53-
rust_port *port = ports[i];
56+
size_t k = (i + j) % n_ports;
57+
rust_port *port = ports[k];
5458
port->lock.unlock();
5559
}
5660
}

0 commit comments

Comments
 (0)