Update clist.go

Add more justification of synchrony primitives in documentation.
This commit is contained in:
Jae Kwon 2017-12-25 22:28:15 -08:00 committed by GitHub
parent 2fd8f35b74
commit 0f8ebd024d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 2 deletions

View File

@ -1,20 +1,40 @@
package clist package clist
/* /*
The purpose of CList is to provide a goroutine-safe linked-list. The purpose of CList is to provide a goroutine-safe linked-list.
This list can be traversed concurrently by any number of goroutines. This list can be traversed concurrently by any number of goroutines.
However, removed CElements cannot be added back. However, removed CElements cannot be added back.
NOTE: Not all methods of container/list are (yet) implemented. NOTE: Not all methods of container/list are (yet) implemented.
NOTE: Removed elements need to DetachPrev or DetachNext consistently NOTE: Removed elements need to DetachPrev or DetachNext consistently
to ensure garbage collection of removed elements. to ensure garbage collection of removed elements.
*/ */
import ( import (
"sync" "sync"
) )
// CElement is an element of a linked-list /*
// Traversal from a CElement are goroutine-safe.
CElement is an element of a linked-list
Traversal from a CElement are goroutine-safe.
We can't avoid using WaitGroups or for-loops given the documentation
spec without re-implementing the primitives that already exist in
golang/sync. Notice that WaitGroup allows many go-routines to be
simultaneously released, which is what we want. Mutex doesn't do
this. RWMutex does this, but it's clumsy to use in the way that a
WaitGroup would be used -- and we'd end up having two RWMutex's for
prev/next each, which is doubly confusing.
sync.Cond would be sort-of useful, but we don't need a write-lock in
the for-loop. Use sync.Cond when you need serial access to the
"condition". In our case our condition is if `next != nil || removed`,
and there's no reason to serialize that condition for goroutines
waiting on NextWait() (since it's just a read operation).
*/
type CElement struct { type CElement struct {
mtx sync.RWMutex mtx sync.RWMutex
prev *CElement prev *CElement