fifo.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package lockman
  15. import (
  16. "container/list"
  17. "yunion.io/x/log"
  18. )
  19. type FIFO struct {
  20. fifo *list.List
  21. }
  22. func NewFIFO() *FIFO {
  23. return &FIFO{fifo: list.New()}
  24. }
  25. func (f *FIFO) Push(ele interface{}) {
  26. f.fifo.PushBack(ele)
  27. }
  28. func (f *FIFO) Pop(ele interface{}) interface{} {
  29. e := f.fifo.Front()
  30. for e != nil && e.Value != ele {
  31. e = e.Next()
  32. }
  33. if e != nil {
  34. v := f.fifo.Remove(e)
  35. if v != ele {
  36. log.Fatalf("remove element not identical!!")
  37. }
  38. }
  39. return nil
  40. }
  41. func (f *FIFO) Len() int {
  42. return f.fifo.Len()
  43. }
  44. type ElementInspectFunc func(ele interface{})
  45. func (f *FIFO) Enum(eif ElementInspectFunc) {
  46. e := f.fifo.Front()
  47. for e != nil {
  48. eif(e.Value)
  49. e = e.Next()
  50. }
  51. }