You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.9 KiB

9 years ago
9 years ago
9 years ago
  1. package common
  2. import (
  3. "sync/atomic"
  4. )
  5. //----------------------------------------
  6. // Task
  7. // val: the value returned after task execution.
  8. // err: the error returned during task completion.
  9. // abort: tells Parallel to return, whether or not all tasks have completed.
  10. type Task func(i int) (val interface{}, err error, abort bool)
  11. type TaskResult struct {
  12. Value interface{}
  13. Error error
  14. }
  15. type TaskResultCh <-chan TaskResult
  16. type taskResultOK struct {
  17. TaskResult
  18. OK bool
  19. }
  20. type TaskResultSet struct {
  21. chz []TaskResultCh
  22. results []taskResultOK
  23. }
  24. func newTaskResultSet(chz []TaskResultCh) *TaskResultSet {
  25. return &TaskResultSet{
  26. chz: chz,
  27. results: nil,
  28. }
  29. }
  30. func (trs *TaskResultSet) Channels() []TaskResultCh {
  31. return trs.chz
  32. }
  33. func (trs *TaskResultSet) LatestResult(index int) (TaskResult, bool) {
  34. if len(trs.results) <= index {
  35. return TaskResult{}, false
  36. }
  37. resultOK := trs.results[index]
  38. return resultOK.TaskResult, resultOK.OK
  39. }
  40. // NOTE: Not concurrency safe.
  41. func (trs *TaskResultSet) Reap() *TaskResultSet {
  42. if trs.results == nil {
  43. trs.results = make([]taskResultOK, len(trs.chz))
  44. }
  45. for i := 0; i < len(trs.results); i++ {
  46. var trch = trs.chz[i]
  47. select {
  48. case result := <-trch:
  49. // Overwrite result.
  50. trs.results[i] = taskResultOK{
  51. TaskResult: result,
  52. OK: true,
  53. }
  54. default:
  55. // Do nothing.
  56. }
  57. }
  58. return trs
  59. }
  60. // Returns the firstmost (by task index) error as
  61. // discovered by all previous Reap() calls.
  62. func (trs *TaskResultSet) FirstValue() interface{} {
  63. for _, result := range trs.results {
  64. if result.Value != nil {
  65. return result.Value
  66. }
  67. }
  68. return nil
  69. }
  70. // Returns the firstmost (by task index) error as
  71. // discovered by all previous Reap() calls.
  72. func (trs *TaskResultSet) FirstError() error {
  73. for _, result := range trs.results {
  74. if result.Error != nil {
  75. return result.Error
  76. }
  77. }
  78. return nil
  79. }
  80. //----------------------------------------
  81. // Parallel
  82. // Run tasks in parallel, with ability to abort early.
  83. // Returns ok=false iff any of the tasks returned abort=true.
  84. // NOTE: Do not implement quit features here. Instead, provide convenient
  85. // concurrent quit-like primitives, passed implicitly via Task closures. (e.g.
  86. // it's not Parallel's concern how you quit/abort your tasks).
  87. func Parallel(tasks ...Task) (trs *TaskResultSet, ok bool) {
  88. var taskResultChz = make([]TaskResultCh, len(tasks)) // To return.
  89. var taskDoneCh = make(chan bool, len(tasks)) // A "wait group" channel, early abort if any true received.
  90. var numPanics = new(int32) // Keep track of panics to set ok=false later.
  91. ok = true // We will set it to false iff any tasks panic'd or returned abort.
  92. // Start all tasks in parallel in separate goroutines.
  93. // When the task is complete, it will appear in the
  94. // respective taskResultCh (associated by task index).
  95. for i, task := range tasks {
  96. var taskResultCh = make(chan TaskResult, 1) // Capacity for 1 result.
  97. taskResultChz[i] = taskResultCh
  98. go func(i int, task Task, taskResultCh chan TaskResult) {
  99. // Recovery
  100. defer func() {
  101. if pnk := recover(); pnk != nil {
  102. atomic.AddInt32(numPanics, 1)
  103. taskResultCh <- TaskResult{nil, ErrorWrap(pnk, "Panic in task")}
  104. taskDoneCh <- false
  105. }
  106. }()
  107. // Run the task.
  108. var val, err, abort = task(i)
  109. // Send val/err to taskResultCh.
  110. // NOTE: Below this line, nothing must panic/
  111. taskResultCh <- TaskResult{val, err}
  112. // Decrement waitgroup.
  113. taskDoneCh <- abort
  114. }(i, task, taskResultCh)
  115. }
  116. // Wait until all tasks are done, or until abort.
  117. // DONE_LOOP:
  118. for i := 0; i < len(tasks); i++ {
  119. abort := <-taskDoneCh
  120. if abort {
  121. ok = false
  122. break
  123. }
  124. }
  125. // Ok is also false if there were any panics.
  126. // We must do this check here (after DONE_LOOP).
  127. ok = ok && (atomic.LoadInt32(numPanics) == 0)
  128. return newTaskResultSet(taskResultChz).Reap(), ok
  129. }