Commit a0b13f50 authored by Jonny Schäfer's avatar Jonny Schäfer
Browse files

parallel data acquisition with channels

parent 6be32708
package main
import (
"sync"
)
// lazy filled list
type listLazy struct {
update func(int) []*videoContainer
......@@ -12,23 +8,14 @@ type listLazy struct {
content []*videoContainer
}
// temporary storage for parallel data acquisition
type listChunk struct {
data *[]*videoContainer
avail *sync.Mutex
}
// listUpdateChunk concurrently runs the lists update function and
// returns a listChunk. The listChunk.avail mutex will be unlocked when
// finished.
func (l *listLazy) listUpdateChunk(state int) *listChunk {
cnk := &listChunk{&[]*videoContainer{}, &sync.Mutex{}}
cnk.avail.Lock()
// returns a channel, which sends the data when the update is finished.
func (l *listLazy) listUpdateChunk(state int) chan []*videoContainer {
chn := make(chan []*videoContainer)
go func() {
*cnk.data = l.update(state)
cnk.avail.Unlock()
chn <- l.update(state)
}()
return cnk
return chn
}
// listNew creates a new lazy list with the given update function.
......@@ -43,22 +30,18 @@ func listNew(update func(int) []*videoContainer) *listLazy {
func (l *listLazy) listGet(index int) *videoContainer {
par := 5 // parallel update count
for index >= len(l.content) && !l.dry {
recv := 0
temp := make([]*listChunk, par)
temp := make([]chan []*videoContainer, par)
for i, _ := range temp {
temp[i] = l.listUpdateChunk(l.state + i)
}
for _, c := range temp {
c.avail.Lock()
c.avail.Unlock()
if len(*c.data) == 0 {
data := <-c
if len(data) == 0 {
l.dry = true
}
l.content = append(l.content, *c.data...)
recv += len(*c.data)
l.content = append(l.content, data...)
l.state++
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment