github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/queues/link_chan.go (about) 1 // implements locking queues, using list and mutex 2 package goqueuestest 3 4 import "sync" 5 6 type pcLifoElement struct { 7 value interface{} 8 prev *pcLifoElement 9 } 10 11 type PcLifo struct { 12 head *pcLifoElement 13 mutex sync.Mutex 14 } 15 16 func NewPcLifo() *PcLifo { 17 q := &PcLifo{} 18 q.head = nil 19 return q 20 } 21 22 func (q *PcLifo) Enqueue(value interface{}) { 23 q.mutex.Lock() 24 q.head = &pcLifoElement{value, q.head} 25 q.mutex.Unlock() 26 } 27 28 func (q *PcLifo) Dequeue() (interface{}, bool) { 29 q.mutex.Lock() 30 if q.head == nil { 31 q.mutex.Unlock() 32 return nil, false 33 } 34 value := q.head.value 35 q.head = q.head.prev 36 q.mutex.Unlock() 37 return value, true 38 } 39 40 type pcFifoElement struct { 41 value interface{} 42 next *pcFifoElement 43 } 44 45 type PcFifo struct { 46 front *pcFifoElement 47 back *pcFifoElement 48 mutex sync.Mutex 49 } 50 51 func NewPcFifo() *PcFifo { 52 q := &PcFifo{} 53 q.front = nil 54 q.back = nil 55 return q 56 } 57 58 func (q *PcFifo) Enqueue(value interface{}) { 59 q.mutex.Lock() 60 tmp := &pcFifoElement{value, nil} 61 if q.front == nil { 62 q.front = tmp 63 q.back = tmp 64 } else { 65 q.back.next = tmp 66 q.back = tmp 67 } 68 q.mutex.Unlock() 69 } 70 71 func (q *PcFifo) Dequeue() (value interface{}, ok bool) { 72 q.mutex.Lock() 73 tmp := q.front 74 if q.front != nil { 75 q.front = q.front.next 76 } else { 77 q.back = nil 78 } 79 q.mutex.Unlock() 80 if tmp != nil { 81 return tmp.value, true 82 } 83 return nil, false 84 }