Remove race conditions when accessing the last chunk

This commit is contained in:
Junegunn Choi
2015-01-04 05:01:13 +09:00
parent 0dd024a09f
commit d2f7acbc69
3 changed files with 36 additions and 20 deletions

View File

@@ -42,14 +42,6 @@ func CountItems(cs []*Chunk) int {
return CHUNK_SIZE*(len(cs)-1) + len(*(cs[len(cs)-1]))
}
func (cl *ChunkList) Count() int {
return cl.count
}
func (cl *ChunkList) Chunks() []*Chunk {
return cl.chunks
}
func (cl *ChunkList) Push(data string) {
cl.mutex.Lock()
defer cl.mutex.Unlock()
@@ -63,11 +55,24 @@ func (cl *ChunkList) Push(data string) {
cl.count += 1
}
func (cl *ChunkList) Snapshot() []*Chunk {
func (cl *ChunkList) Snapshot() ([]*Chunk, int) {
cl.mutex.Lock()
defer cl.mutex.Unlock()
ret := make([]*Chunk, len(cl.chunks))
copy(ret, cl.chunks)
return ret
// Duplicate the last chunk
if cnt := len(ret); cnt > 0 {
ret[cnt-1] = ret[cnt-1].dupe()
}
return ret, cl.count
}
func (c *Chunk) dupe() *Chunk {
newChunk := make(Chunk, len(*c))
for idx, ptr := range *c {
newChunk[idx] = ptr
}
return &newChunk
}