worker.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright (c) 2014 Ashley Jeffs
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. package tunny
  20. import (
  21. "sync/atomic"
  22. "time"
  23. )
  24. type workerWrapper struct {
  25. readyChan chan int
  26. jobChan chan interface{}
  27. outputChan chan interface{}
  28. poolOpen uint32
  29. worker TunnyWorker
  30. }
  31. func (wrapper *workerWrapper) Loop() {
  32. // TODO: Configure?
  33. tout := time.Duration(5)
  34. for !wrapper.worker.TunnyReady() {
  35. // It's sad that we can't simply check if jobChan is closed here.
  36. if atomic.LoadUint32(&wrapper.poolOpen) == 0 {
  37. break
  38. }
  39. time.Sleep(tout * time.Millisecond)
  40. }
  41. wrapper.readyChan <- 1
  42. for data := range wrapper.jobChan {
  43. wrapper.outputChan <- wrapper.worker.TunnyJob(data)
  44. for !wrapper.worker.TunnyReady() {
  45. if atomic.LoadUint32(&wrapper.poolOpen) == 0 {
  46. break
  47. }
  48. time.Sleep(tout * time.Millisecond)
  49. }
  50. wrapper.readyChan <- 1
  51. }
  52. close(wrapper.readyChan)
  53. close(wrapper.outputChan)
  54. }
  55. func (wrapper *workerWrapper) Open() {
  56. if extWorker, ok := wrapper.worker.(TunnyExtendedWorker); ok {
  57. extWorker.TunnyInitialize()
  58. }
  59. wrapper.readyChan = make(chan int)
  60. wrapper.jobChan = make(chan interface{})
  61. wrapper.outputChan = make(chan interface{})
  62. atomic.SwapUint32(&wrapper.poolOpen, uint32(1))
  63. go wrapper.Loop()
  64. }
  65. // Follow this with Join(), otherwise terminate isn't called on the worker
  66. func (wrapper *workerWrapper) Close() {
  67. close(wrapper.jobChan)
  68. // Breaks the worker out of a Ready() -> false loop
  69. atomic.SwapUint32(&wrapper.poolOpen, uint32(0))
  70. }
  71. func (wrapper *workerWrapper) Join() {
  72. // Ensure that both the ready and output channels are closed
  73. for {
  74. _, readyOpen := <-wrapper.readyChan
  75. _, outputOpen := <-wrapper.outputChan
  76. if !readyOpen && !outputOpen {
  77. break
  78. }
  79. }
  80. if extWorker, ok := wrapper.worker.(TunnyExtendedWorker); ok {
  81. extWorker.TunnyTerminate()
  82. }
  83. }
  84. func (wrapper *workerWrapper) Interrupt() {
  85. if extWorker, ok := wrapper.worker.(TunnyInterruptable); ok {
  86. extWorker.TunnyInterrupt()
  87. }
  88. }