2012-04-21 14:07:07 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2012 The Perkeep Authors.
|
2012-04-21 14:07:07 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
package chanworker // import "perkeep.org/internal/chanworker"
|
2012-04-21 14:07:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2013-09-11 03:07:20 +00:00
|
|
|
"sync"
|
2012-04-21 14:07:07 +00:00
|
|
|
)
|
|
|
|
|
2015-03-20 19:39:09 +00:00
|
|
|
type chanWorker struct {
|
|
|
|
c chan interface{}
|
2012-04-21 14:07:07 +00:00
|
|
|
|
|
|
|
donec chan bool
|
2015-03-20 19:39:09 +00:00
|
|
|
workc chan interface{}
|
|
|
|
fn func(n interface{}, ok bool)
|
2012-04-21 14:07:07 +00:00
|
|
|
buf *list.List
|
|
|
|
}
|
|
|
|
|
2015-03-20 19:39:09 +00:00
|
|
|
// TODO: make it configurable if need be. Although so far in camput it wasn't.
|
|
|
|
const buffered = 16
|
|
|
|
|
|
|
|
// NewWorker starts nWorkers goroutines running fn on incoming
|
|
|
|
// items sent on the returned channel. fn may block; writes to the
|
2012-04-21 14:07:07 +00:00
|
|
|
// channel will buffer.
|
2013-09-11 03:07:20 +00:00
|
|
|
// If nWorkers is negative, a new goroutine running fn is called for each
|
|
|
|
// item sent on the returned channel.
|
|
|
|
// When the returned channel is closed, fn is called with (nil, false)
|
|
|
|
// after all other calls to fn have completed.
|
2015-03-20 19:39:09 +00:00
|
|
|
// If nWorkers is zero, NewWorker will panic.
|
|
|
|
func NewWorker(nWorkers int, fn func(el interface{}, ok bool)) chan<- interface{} {
|
2013-09-11 03:07:20 +00:00
|
|
|
if nWorkers == 0 {
|
2015-03-20 19:39:09 +00:00
|
|
|
panic("NewChanWorker: invalid value of 0 for nWorkers")
|
2013-09-11 03:07:20 +00:00
|
|
|
}
|
2015-03-20 19:39:09 +00:00
|
|
|
retc := make(chan interface{}, buffered)
|
2013-09-11 03:07:20 +00:00
|
|
|
if nWorkers < 0 {
|
|
|
|
// Unbounded number of workers.
|
|
|
|
go func() {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for w := range retc {
|
|
|
|
wg.Add(1)
|
2015-03-20 19:39:09 +00:00
|
|
|
go func(w interface{}) {
|
2013-09-11 03:07:20 +00:00
|
|
|
fn(w, true)
|
|
|
|
wg.Done()
|
|
|
|
}(w)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
fn(nil, false)
|
|
|
|
}()
|
|
|
|
return retc
|
|
|
|
}
|
2015-03-20 19:39:09 +00:00
|
|
|
w := &chanWorker{
|
2013-09-11 03:07:20 +00:00
|
|
|
c: retc,
|
2015-03-20 19:39:09 +00:00
|
|
|
workc: make(chan interface{}, buffered),
|
2012-04-21 14:07:07 +00:00
|
|
|
donec: make(chan bool), // when workers finish
|
|
|
|
fn: fn,
|
|
|
|
buf: list.New(),
|
|
|
|
}
|
|
|
|
go w.pump()
|
|
|
|
for i := 0; i < nWorkers; i++ {
|
|
|
|
go w.work()
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < nWorkers; i++ {
|
|
|
|
<-w.donec
|
|
|
|
}
|
|
|
|
fn(nil, false) // final sentinel
|
|
|
|
}()
|
2013-09-11 03:07:20 +00:00
|
|
|
return retc
|
2012-04-21 14:07:07 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 19:39:09 +00:00
|
|
|
func (w *chanWorker) pump() {
|
2012-04-21 14:07:07 +00:00
|
|
|
inc := w.c
|
|
|
|
for inc != nil || w.buf.Len() > 0 {
|
|
|
|
outc := w.workc
|
2015-03-20 19:39:09 +00:00
|
|
|
var frontNode interface{}
|
2012-04-21 14:07:07 +00:00
|
|
|
if e := w.buf.Front(); e != nil {
|
2015-03-20 19:39:09 +00:00
|
|
|
frontNode = e.Value
|
2012-04-21 14:07:07 +00:00
|
|
|
} else {
|
|
|
|
outc = nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case outc <- frontNode:
|
|
|
|
w.buf.Remove(w.buf.Front())
|
2015-03-20 19:39:09 +00:00
|
|
|
case el, ok := <-inc:
|
2012-04-21 14:07:07 +00:00
|
|
|
if !ok {
|
|
|
|
inc = nil
|
|
|
|
continue
|
|
|
|
}
|
2015-03-20 19:39:09 +00:00
|
|
|
w.buf.PushBack(el)
|
2012-04-21 14:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(w.workc)
|
|
|
|
}
|
|
|
|
|
2015-03-20 19:39:09 +00:00
|
|
|
func (w *chanWorker) work() {
|
2012-04-21 14:07:07 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case n, ok := <-w.workc:
|
|
|
|
if !ok {
|
|
|
|
w.donec <- true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.fn(n, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|