Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
1634b423
Commit
1634b423
authored
15 years ago
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
time: make tick.Stop a little more robust
R=r CC=golang-dev, jackpal
https://golang.org/cl/186228
parent
fe01d4c8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
6 deletions
+30
-6
src/pkg/time/tick.go
src/pkg/time/tick.go
+30
-6
No files found.
src/pkg/time/tick.go
View file @
1634b423
...
...
@@ -24,12 +24,26 @@ package time
// at intervals.
type
Ticker
struct
{
C
<-
chan
int64
// The channel on which the ticks are delivered.
done
chan
bool
ns
int64
shutdown
bool
}
// Stop turns off a ticker. After Stop, no more ticks will be delivered.
func
(
t
*
Ticker
)
Stop
()
{
t
.
shutdown
=
true
}
// Stop turns off a ticker. After Stop, no more ticks will be sent.
func
(
t
*
Ticker
)
Stop
()
{
t
.
shutdown
=
true
go
t
.
drain
()
}
func
(
t
*
Ticker
)
drain
()
{
for
{
select
{
case
<-
t
.
C
:
case
<-
t
.
done
:
return
}
}
}
func
(
t
*
Ticker
)
ticker
(
c
chan
<-
int64
)
{
now
:=
Nanoseconds
()
...
...
@@ -47,13 +61,23 @@ func (t *Ticker) ticker(c chan<- int64) {
when
+=
t
.
ns
}
Sleep
(
when
-
now
)
now
=
Nanoseconds
()
for
!
t
.
shutdown
&&
when
>
now
{
// limit individual sleeps so that stopped
// long-term tickers don't pile up.
const
maxSleep
=
1e9
if
when
-
now
>
maxSleep
{
Sleep
(
maxSleep
)
}
else
{
Sleep
(
when
-
now
)
}
now
=
Nanoseconds
()
}
if
t
.
shutdown
{
return
break
}
c
<-
now
}
t
.
done
<-
true
}
// Tick is a convenience wrapper for NewTicker providing access to the ticking
...
...
@@ -73,7 +97,7 @@ func NewTicker(ns int64) *Ticker {
return
nil
}
c
:=
make
(
chan
int64
)
t
:=
&
Ticker
{
c
,
ns
,
false
}
t
:=
&
Ticker
{
c
,
make
(
chan
bool
),
ns
,
false
}
go
t
.
ticker
(
c
)
return
t
}
This diff is collapsed.
Click to expand it.
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