2018-05-24 02:49:45 +03:00
|
|
|
// Copyright 2018 Joshua J Baker. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
2020-01-07 19:33:49 +03:00
|
|
|
import "sync"
|
2018-05-24 02:49:45 +03:00
|
|
|
|
|
|
|
// this is a good candiate for a lock-free structure.
|
|
|
|
|
|
|
|
type noteQueue struct {
|
2020-01-07 19:33:49 +03:00
|
|
|
mu sync.Mutex
|
2018-05-24 02:49:45 +03:00
|
|
|
notes []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *noteQueue) Add(note interface{}) (one bool) {
|
|
|
|
q.mu.Lock()
|
|
|
|
q.notes = append(q.notes, note)
|
|
|
|
n := len(q.notes)
|
|
|
|
q.mu.Unlock()
|
|
|
|
return n == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *noteQueue) ForEach(iter func(note interface{}) error) error {
|
|
|
|
q.mu.Lock()
|
|
|
|
if len(q.notes) == 0 {
|
|
|
|
q.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
notes := q.notes
|
|
|
|
q.notes = nil
|
|
|
|
q.mu.Unlock()
|
|
|
|
for _, note := range notes {
|
|
|
|
if err := iter(note); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-01-07 19:33:49 +03:00
|
|
|
q.mu.Lock()
|
|
|
|
if q.notes == nil {
|
|
|
|
for i := range notes {
|
|
|
|
notes[i] = nil
|
|
|
|
}
|
|
|
|
q.notes = notes[:0]
|
|
|
|
}
|
|
|
|
q.mu.Unlock()
|
2018-05-24 02:49:45 +03:00
|
|
|
return nil
|
|
|
|
}
|