This website works better with JavaScript.
Home
Issues
Pull Requests
Milestones
AI流水线
Repositories
Datasets
Forum
实训
竞赛
大数据
AI开发
Register
Sign In
JointCloud
/
common
Not watched
Unwatch
Watch all
Watch but not notify
1
Star
0
Fork
0
Code
Releases
0
Wiki
evaluate
Activity
Issues
0
Pull Requests
0
Datasets
Model
Cloudbrain
HPC
Browse Source
增加工具类
pull/47/head
Sydonian
1 year ago
parent
922ffc8693
commit
3978d820a8
1 changed files
with
48 additions
and
0 deletions
Split View
Diff Options
Show Stats
Download Patch File
Download Diff File
+48
-0
utils/sync2/event.go
+ 48
- 0
utils/sync2/event.go
View File
@@ -0,0 +1,48 @@
package sync2
import (
"context"
"errors"
"sync"
)
var ErrEventClosed = errors.New("event is closed")
var ErrContextCanceled = errors.New("context canceled")
type Event struct {
ch chan any
closeOnce sync.Once
}
func NewEvent() Event {
return Event{
ch: make(chan any, 1),
}
}
func (e *Event) Set() {
select {
case e.ch <- nil:
default:
}
}
func (e *Event) Wait(ctx context.Context) error {
select {
case _, ok := <-e.ch:
if ok {
return nil
}
return ErrEventClosed
case <-ctx.Done():
return ErrContextCanceled
}
}
func (e *Event) Close() {
e.closeOnce.Do(func() {
close(e.ch)
})
}
Write
Preview
Loading…
Cancel
Save