chan_canvasobject.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Code generated by go run gen.go; DO NOT EDIT.
  2. package async
  3. import "fyne.io/fyne/v2"
  4. // UnboundedCanvasObjectChan is a channel with an unbounded buffer for caching
  5. // CanvasObject objects. A channel must be closed via Close method.
  6. type UnboundedCanvasObjectChan struct {
  7. in, out chan fyne.CanvasObject
  8. close chan struct{}
  9. q []fyne.CanvasObject
  10. }
  11. // NewUnboundedCanvasObjectChan returns a unbounded channel with unlimited capacity.
  12. func NewUnboundedCanvasObjectChan() *UnboundedCanvasObjectChan {
  13. ch := &UnboundedCanvasObjectChan{
  14. // The size of CanvasObject is less than 16 bytes, we use 16 to fit
  15. // a CPU cache line (L2, 256 Bytes), which may reduce cache misses.
  16. in: make(chan fyne.CanvasObject, 16),
  17. out: make(chan fyne.CanvasObject, 16),
  18. close: make(chan struct{}),
  19. }
  20. go ch.processing()
  21. return ch
  22. }
  23. // In returns the send channel of the given channel, which can be used to
  24. // send values to the channel.
  25. func (ch *UnboundedCanvasObjectChan) In() chan<- fyne.CanvasObject { return ch.in }
  26. // Out returns the receive channel of the given channel, which can be used
  27. // to receive values from the channel.
  28. func (ch *UnboundedCanvasObjectChan) Out() <-chan fyne.CanvasObject { return ch.out }
  29. // Close closes the channel.
  30. func (ch *UnboundedCanvasObjectChan) Close() { ch.close <- struct{}{} }
  31. func (ch *UnboundedCanvasObjectChan) processing() {
  32. // This is a preallocation of the internal unbounded buffer.
  33. // The size is randomly picked. But if one changes the size, the
  34. // reallocation size at the subsequent for loop should also be
  35. // changed too. Furthermore, there is no memory leak since the
  36. // queue is garbage collected.
  37. ch.q = make([]fyne.CanvasObject, 0, 1<<10)
  38. for {
  39. select {
  40. case e, ok := <-ch.in:
  41. if !ok {
  42. // We don't want the input channel be accidentally closed
  43. // via close() instead of Close(). If that happens, it is
  44. // a misuse, do a panic as warning.
  45. panic("async: misuse of unbounded channel, In() was closed")
  46. }
  47. ch.q = append(ch.q, e)
  48. case <-ch.close:
  49. ch.closed()
  50. return
  51. }
  52. for len(ch.q) > 0 {
  53. select {
  54. case ch.out <- ch.q[0]:
  55. ch.q[0] = nil // de-reference earlier to help GC
  56. ch.q = ch.q[1:]
  57. case e, ok := <-ch.in:
  58. if !ok {
  59. // We don't want the input channel be accidentally closed
  60. // via close() instead of Close(). If that happens, it is
  61. // a misuse, do a panic as warning.
  62. panic("async: misuse of unbounded channel, In() was closed")
  63. }
  64. ch.q = append(ch.q, e)
  65. case <-ch.close:
  66. ch.closed()
  67. return
  68. }
  69. }
  70. // If the remaining capacity is too small, we prefer to
  71. // reallocate the entire buffer.
  72. if cap(ch.q) < 1<<5 {
  73. ch.q = make([]fyne.CanvasObject, 0, 1<<10)
  74. }
  75. }
  76. }
  77. func (ch *UnboundedCanvasObjectChan) closed() {
  78. close(ch.in)
  79. for e := range ch.in {
  80. ch.q = append(ch.q, e)
  81. }
  82. for len(ch.q) > 0 {
  83. select {
  84. case ch.out <- ch.q[0]:
  85. ch.q[0] = nil // de-reference earlier to help GC
  86. ch.q = ch.q[1:]
  87. default:
  88. }
  89. }
  90. close(ch.out)
  91. close(ch.close)
  92. }