Go schedules a task to be run in the stream's pool. All submitted tasks will be executed concurrently in worker goroutines. Then, the callbacks returned by the tasks will be executed in the order that the tasks were submitted. All callbacks will be executed by the same goroutine, so no synchronizati
(f Task)
| 60 | // stream's pool are busy, a call to Go() will block until the task can be |
| 61 | // started. |
| 62 | func (s *Stream) Go(f Task) { |
| 63 | s.init() |
| 64 | |
| 65 | // Get a channel from the cache. |
| 66 | ch := getCh() |
| 67 | |
| 68 | // Queue the channel for the callbacker. |
| 69 | s.queue <- ch |
| 70 | |
| 71 | // Submit the task for execution. |
| 72 | s.pool.Go(func() { |
| 73 | defer func() { |
| 74 | // In the case of a panic from f, we don't want the callbacker to |
| 75 | // starve waiting for a callback from this channel, so give it an |
| 76 | // empty callback. |
| 77 | if r := recover(); r != nil { |
| 78 | ch <- func() {} |
| 79 | panic(r) |
| 80 | } |
| 81 | }() |
| 82 | |
| 83 | // Run the task, sending its callback down this task's channel. |
| 84 | callback := f() |
| 85 | ch <- callback |
| 86 | }) |
| 87 | } |
| 88 | |
| 89 | // Wait signals to the stream that all tasks have been submitted. Wait will |
| 90 | // not return until all tasks and callbacks have been run. |