forked from cometbft/cometbft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
8 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,14 @@ | ||
package consensus | ||
|
||
import "sync" | ||
|
||
type TaskRunner struct { | ||
wg sync.WaitGroup | ||
taskCh chan func() | ||
} | ||
|
||
func StartTaskRunner(buf int) *TaskRunner { | ||
tr := &TaskRunner{ | ||
taskCh: make(chan func(), buf), | ||
} | ||
tr.wg.Add(1) | ||
// StartTaskRunner spawn a single goroutine to run tasks in FIFO order. | ||
func StartTaskRunner(buf int) func(func()) { | ||
taskCh := make(chan func(), buf) | ||
go func() { | ||
defer tr.wg.Done() | ||
for f := range tr.taskCh { | ||
for f := range taskCh { | ||
f() | ||
} | ||
}() | ||
return tr | ||
} | ||
|
||
func (tr *TaskRunner) StopAndWait() { | ||
close(tr.taskCh) | ||
tr.wg.Wait() | ||
} | ||
|
||
func (tr *TaskRunner) RunTask(f func()) { | ||
tr.taskCh <- f | ||
return func(f func()) { | ||
taskCh <- f | ||
} | ||
} |