Skip to content

Commit efa0219

Browse files
committed
runtime: use FIFO queue instead of LIFO queue for blocking thread pool
1 parent dc75c72 commit efa0219

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

std/runtime/blocking.jule

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ struct blockingenv {
1616
maxWorkers: i32 // Constant once initialized.
1717

1818
mu: mutex
19-
jobs: &blockingJob
19+
head: &blockingJob
20+
tail: &blockingJob
2021
nw: i32 // Count of the created worker threads.
2122
widle: &thread // Parkers of the idle workers waiting for work.
2223
}
@@ -26,10 +27,10 @@ impl blockingenv {
2627
#disable nilptr boundary
2728
fn getjob(mut *self, mut &job: *blockingJob): (ok: bool) {
2829
self.mu.lock()
29-
if self.jobs != nil {
30-
*job = *self.jobs
30+
if self.head != nil {
31+
*job = *self.head
32+
self.head = self.head.link
3133
job.link = nil
32-
self.jobs = self.jobs.link
3334
ok = true
3435
}
3536
self.mu.unlock()
@@ -91,7 +92,7 @@ fn blockingWorker() {
9192
}
9293
blocking.mu.lock()
9394
// A job is enqueued, try again.
94-
if blocking.jobs != nil {
95+
if blocking.head != nil {
9596
blocking.mu.unlock()
9697
continue
9798
}
@@ -112,7 +113,7 @@ fn blockingWorker() {
112113

113114
fn blockinginit() {
114115
blocking = blockingenv{}
115-
blocking.maxWorkers = 1 //i32(NumCPU())
116+
blocking.maxWorkers = i32(NumCPU())
116117
}
117118

118119
// Executes the given job on the blocking thread pool,
@@ -131,12 +132,14 @@ async fn Blocking(job: fn()) {
131132
mut m := gett()
132133
mc := m.c
133134
mut bj := blockingJob{job: job, cp: unsafe { (&c)(&mc) }}
135+
mut bjp := unsafe { (&blockingJob)(&bj) }
134136
blocking.mu.lock()
135-
if blocking.jobs == nil {
136-
blocking.jobs = unsafe { (&blockingJob)(&bj) }
137+
if blocking.head == nil {
138+
blocking.head = bjp
139+
blocking.tail = blocking.head
137140
} else {
138-
bj.link = blocking.jobs
139-
blocking.jobs = unsafe { (&blockingJob)(&bj) }
141+
blocking.tail.link = bjp
142+
blocking.tail = bjp
140143
}
141144
// We use the tagged pointer to call `blocking.wakeworker` after adding
142145
// a new job to the queue. It is done by mutex-unlock algorithm.

0 commit comments

Comments
 (0)