Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
69069048
Commit
69069048
authored
Oct 16, 2014
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
event: make TypeMux zero value ready to use
parent
10bbf265
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
13 deletions
+12
-13
event.go
event/event.go
+6
-7
event_test.go
event/event_test.go
+6
-6
No files found.
event/event.go
View file @
69069048
...
...
@@ -23,6 +23,8 @@ type Subscription interface {
// A TypeMux dispatches events to registered receivers. Receivers can be
// registered to handle events of certain type. Any operation
// called after mux is stopped will return ErrMuxClosed.
//
// The zero value is ready to use.
type
TypeMux
struct
{
mutex
sync
.
RWMutex
subm
map
[
reflect
.
Type
][]
*
muxsub
...
...
@@ -32,11 +34,6 @@ type TypeMux struct {
// ErrMuxClosed is returned when Posting on a closed TypeMux.
var
ErrMuxClosed
=
errors
.
New
(
"event: mux closed"
)
// NewTypeMux creates a running mux.
func
NewTypeMux
()
*
TypeMux
{
return
&
TypeMux
{
subm
:
make
(
map
[
reflect
.
Type
][]
*
muxsub
)}
}
// Subscribe creates a subscription for events of the given types. The
// subscription's channel is closed when it is unsubscribed
// or the mux is closed.
...
...
@@ -44,9 +41,11 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
sub
:=
newsub
(
mux
)
mux
.
mutex
.
Lock
()
if
mux
.
stopped
{
mux
.
mutex
.
Unlock
()
close
(
sub
.
postC
)
}
else
{
if
mux
.
subm
==
nil
{
mux
.
subm
=
make
(
map
[
reflect
.
Type
][]
*
muxsub
)
}
for
_
,
t
:=
range
types
{
rtyp
:=
reflect
.
TypeOf
(
t
)
oldsubs
:=
mux
.
subm
[
rtyp
]
...
...
@@ -55,8 +54,8 @@ func (mux *TypeMux) Subscribe(types ...interface{}) Subscription {
subs
[
len
(
oldsubs
)]
=
sub
mux
.
subm
[
rtyp
]
=
subs
}
mux
.
mutex
.
Unlock
()
}
mux
.
mutex
.
Unlock
()
return
sub
}
...
...
event/event_test.go
View file @
69069048
...
...
@@ -10,7 +10,7 @@ import (
type
testEvent
int
func
TestSub
(
t
*
testing
.
T
)
{
mux
:=
NewTypeMux
(
)
mux
:=
new
(
TypeMux
)
defer
mux
.
Stop
()
sub
:=
mux
.
Subscribe
(
testEvent
(
0
))
...
...
@@ -28,7 +28,7 @@ func TestSub(t *testing.T) {
}
func
TestMuxErrorAfterStop
(
t
*
testing
.
T
)
{
mux
:=
NewTypeMux
(
)
mux
:=
new
(
TypeMux
)
mux
.
Stop
()
sub
:=
mux
.
Subscribe
(
testEvent
(
0
))
...
...
@@ -41,7 +41,7 @@ func TestMuxErrorAfterStop(t *testing.T) {
}
func
TestUnsubscribeUnblockPost
(
t
*
testing
.
T
)
{
mux
:=
NewTypeMux
(
)
mux
:=
new
(
TypeMux
)
defer
mux
.
Stop
()
sub
:=
mux
.
Subscribe
(
testEvent
(
0
))
...
...
@@ -62,7 +62,7 @@ func TestUnsubscribeUnblockPost(t *testing.T) {
func
TestMuxConcurrent
(
t
*
testing
.
T
)
{
rand
.
Seed
(
time
.
Now
()
.
Unix
())
mux
:=
NewTypeMux
(
)
mux
:=
new
(
TypeMux
)
defer
mux
.
Stop
()
recv
:=
make
(
chan
int
)
...
...
@@ -111,7 +111,7 @@ func emptySubscriber(mux *TypeMux, types ...interface{}) {
}
func
BenchmarkPost3
(
b
*
testing
.
B
)
{
var
mux
=
NewTypeMux
(
)
var
mux
=
new
(
TypeMux
)
defer
mux
.
Stop
()
emptySubscriber
(
mux
,
testEvent
(
0
))
emptySubscriber
(
mux
,
testEvent
(
0
))
...
...
@@ -123,7 +123,7 @@ func BenchmarkPost3(b *testing.B) {
}
func
BenchmarkPostConcurrent
(
b
*
testing
.
B
)
{
var
mux
=
NewTypeMux
(
)
var
mux
=
new
(
TypeMux
)
defer
mux
.
Stop
()
emptySubscriber
(
mux
,
testEvent
(
0
))
emptySubscriber
(
mux
,
testEvent
(
0
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment