* If timeout less than 0, If Queue is empty, return (nil, ErrEmptyQueue). * If timeout equals to 0, block until get a value from Queue. * If timeout greater than 0, wait timeout seconds until get a value from Queue, if timeout passed, return (nil, ErrEmptyQueue).
(timeout float64)
| 114 | // * If timeout greater than 0, wait timeout seconds until get a value from Queue, |
| 115 | // if timeout passed, return (nil, ErrEmptyQueue). |
| 116 | func (q *Queue) Get(timeout float64) (interface{}, error) { |
| 117 | q.mutex.Lock() |
| 118 | q.clearPending() |
| 119 | isempty := q.isempty() |
| 120 | if timeout < 0.0 && isempty { |
| 121 | q.mutex.Unlock() |
| 122 | return nil, ErrEmptyQueue |
| 123 | } |
| 124 | |
| 125 | if !isempty { |
| 126 | v := q.get() |
| 127 | q.notifyPutter(nil) |
| 128 | q.mutex.Unlock() |
| 129 | return v, nil |
| 130 | } |
| 131 | |
| 132 | e := q.newGetter() |
| 133 | q.mutex.Unlock() |
| 134 | w := e.Value.(waiter) |
| 135 | |
| 136 | var v interface{} |
| 137 | if timeout == 0.0 { |
| 138 | v = <-w |
| 139 | } else { |
| 140 | select { |
| 141 | case v = <-w: |
| 142 | case <-time.After(time.Duration(timeout) * time.Second): |
| 143 | return nil, ErrEmptyQueue |
| 144 | } |
| 145 | } |
| 146 | q.mutex.Lock() |
| 147 | q.notifyPutter(e) |
| 148 | q.mutex.Unlock() |
| 149 | return v, nil |
| 150 | } |
| 151 | |
| 152 | // Same as Put(-1). |
| 153 | func (q *Queue) PutNoWait(val interface{}) error { |
no test coverage detected