Skip to content

Commit

Permalink
simplify pool
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiojr committed Oct 17, 2024
1 parent 359c026 commit 3a4d2d0
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions lib/pool.risor
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,34 @@

__workers := []
__chan := chan()
__sent := 0
__done := chan()
__finished := 0

func __worker(c) {
return func() {
for _, work := range c {
work()
__done <- nil
try(work(), print("hello"), func(e) {
print("error: ", e)
})
}
}
}

// Create a new worker pool with n workers
func new(n) {
spawn(func(){
for range __done {
__finished++
if __finished == __sent {
close(__chan)
}
}
})

for i := 0; i < n; i++ {
w := __worker(__chan)
__workers.append(spawn(func() { w() }))
__workers.append(spawn(w))
}
}

// Queue a function to be executed by a worker goroutine
func queue(fn) {
__sent++
__chan <- func() {
fn()
}
__chan <- fn
}

// Wait for all jobs to finish
func wait() {
if __sent == 0 {
return
}
close(__chan)

<-__chan
__workers.each(func(w) { w.wait() })
}

0 comments on commit 3a4d2d0

Please sign in to comment.