-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixing broadcaster to broadcast atomically, and close to be goroutine…
… safe with Write()
- Loading branch information
Showing
3 changed files
with
16 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,34 @@ | ||
package stream | ||
|
||
import "sync" | ||
|
||
type closer chan struct{} | ||
|
||
func (a closer) Close() error { | ||
close(a) | ||
return nil | ||
} | ||
|
||
func (a closer) IsOpen() bool { | ||
select { | ||
case <-a: | ||
return false | ||
default: | ||
return true | ||
} | ||
} | ||
|
||
type noLock struct{} | ||
|
||
func (l noLock) Lock() {} | ||
func (l noLock) Unlock() {} | ||
import ( | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
type broadcaster struct { | ||
sync.RWMutex | ||
closer | ||
closed uint32 | ||
*sync.Cond | ||
} | ||
|
||
func newBroadcaster() *broadcaster { | ||
return &broadcaster{ | ||
Cond: sync.NewCond(noLock{}), | ||
closer: make(closer), | ||
} | ||
var b broadcaster | ||
b.Cond = sync.NewCond(b.RWMutex.RLocker()) | ||
return &b | ||
} | ||
|
||
func (b *broadcaster) Wait() { | ||
if b.closer.IsOpen() { | ||
if b.IsOpen() { | ||
b.Cond.Wait() | ||
} | ||
} | ||
|
||
func (b *broadcaster) IsOpen() bool { | ||
return atomic.LoadUint32(&b.closed) == 0 | ||
} | ||
|
||
func (b *broadcaster) Close() error { | ||
b.closer.Close() | ||
atomic.StoreUint32(&b.closed, 1) | ||
b.Cond.Broadcast() | ||
return nil | ||
} |