|
| 1 | +// |
| 2 | +// queue.go |
| 3 | +// |
| 4 | +// Created by Hicham Bouabdallah |
| 5 | +// Copyright (c) 2012 SimpleRocket LLC |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person |
| 8 | +// obtaining a copy of this software and associated documentation |
| 9 | +// files (the "Software"), to deal in the Software without |
| 10 | +// restriction, including without limitation the rights to use, |
| 11 | +// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the |
| 13 | +// Software is furnished to do so, subject to the following |
| 14 | +// conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be |
| 17 | +// included in all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 21 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 23 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 24 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 26 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 27 | +// |
| 28 | + |
| 29 | +package main |
| 30 | + |
| 31 | +import "sync" |
| 32 | + |
| 33 | +type queuenode struct { |
| 34 | + data string |
| 35 | + id string |
| 36 | + next *queuenode |
| 37 | +} |
| 38 | + |
| 39 | +// A go-routine safe FIFO (first in first out) data stucture. |
| 40 | +type Queue struct { |
| 41 | + head *queuenode |
| 42 | + tail *queuenode |
| 43 | + count int |
| 44 | + lock *sync.Mutex |
| 45 | + lenOfCmds int |
| 46 | +} |
| 47 | + |
| 48 | +// Creates a new pointer to a new queue. |
| 49 | +func NewQueue() *Queue { |
| 50 | + q := &Queue{} |
| 51 | + q.lock = &sync.Mutex{} |
| 52 | + return q |
| 53 | +} |
| 54 | + |
| 55 | +// Returns the number of elements in the queue (i.e. size/length) |
| 56 | +// go-routine safe. |
| 57 | +func (q *Queue) Len() int { |
| 58 | + q.lock.Lock() |
| 59 | + defer q.lock.Unlock() |
| 60 | + return q.count |
| 61 | +} |
| 62 | + |
| 63 | +// Returns the length of the data (gcode cmd) in the queue (i.e. size/length) |
| 64 | +// go-routine safe. |
| 65 | +func (q *Queue) LenOfCmds() int { |
| 66 | + q.lock.Lock() |
| 67 | + defer q.lock.Unlock() |
| 68 | + return q.lenOfCmds |
| 69 | +} |
| 70 | + |
| 71 | +// Pushes/inserts a value at the end/tail of the queue. |
| 72 | +// Note: this function does mutate the queue. |
| 73 | +// go-routine safe. |
| 74 | +func (q *Queue) Push(item string, id string) { |
| 75 | + q.lock.Lock() |
| 76 | + defer q.lock.Unlock() |
| 77 | + |
| 78 | + n := &queuenode{data: item, id: id} |
| 79 | + |
| 80 | + if q.tail == nil { |
| 81 | + q.tail = n |
| 82 | + q.head = n |
| 83 | + } else { |
| 84 | + q.tail.next = n |
| 85 | + q.tail = n |
| 86 | + } |
| 87 | + q.count++ |
| 88 | + q.lenOfCmds += len(item) |
| 89 | +} |
| 90 | + |
| 91 | +// Returns the value at the front of the queue. |
| 92 | +// i.e. the oldest value in the queue. |
| 93 | +// Note: this function does mutate the queue. |
| 94 | +// go-routine safe. |
| 95 | +func (q *Queue) Poll() (string, string) { |
| 96 | + q.lock.Lock() |
| 97 | + defer q.lock.Unlock() |
| 98 | + |
| 99 | + if q.head == nil { |
| 100 | + return "", "" |
| 101 | + } |
| 102 | + |
| 103 | + n := q.head |
| 104 | + q.head = n.next |
| 105 | + |
| 106 | + if q.head == nil { |
| 107 | + q.tail = nil |
| 108 | + } |
| 109 | + q.count-- |
| 110 | + q.lenOfCmds -= len(n.data) |
| 111 | + |
| 112 | + return n.data, n.id |
| 113 | +} |
| 114 | + |
| 115 | +// Returns a read value at the front of the queue. |
| 116 | +// i.e. the oldest value in the queue. |
| 117 | +// Note: this function does NOT mutate the queue. |
| 118 | +// go-routine safe. |
| 119 | +func (q *Queue) Peek() (string, string) { |
| 120 | + q.lock.Lock() |
| 121 | + defer q.lock.Unlock() |
| 122 | + |
| 123 | + n := q.head |
| 124 | + if n == nil || n.data == "" { |
| 125 | + return "", "" |
| 126 | + } |
| 127 | + |
| 128 | + return n.data, n.id |
| 129 | +} |
| 130 | + |
| 131 | +// Returns a read value at the front of the queue. |
| 132 | +// i.e. the oldest value in the queue. |
| 133 | +// Note: this function does NOT mutate the queue. |
| 134 | +// go-routine safe. |
| 135 | +func (q *Queue) Delete() { |
| 136 | + q.lock.Lock() |
| 137 | + defer q.lock.Unlock() |
| 138 | + |
| 139 | + q.head = nil |
| 140 | + q.tail = nil |
| 141 | + q.count = 0 |
| 142 | + q.lenOfCmds = 0 |
| 143 | +} |
0 commit comments