File: chancond.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 836 kB
  • sloc: makefile: 4
file content (36 lines) | stat: -rw-r--r-- 493 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package missinggo

import "sync"

type ChanCond struct {
	mu sync.Mutex
	ch chan struct{}
}

func (me *ChanCond) Wait() <-chan struct{} {
	me.mu.Lock()
	defer me.mu.Unlock()
	if me.ch == nil {
		me.ch = make(chan struct{})
	}
	return me.ch
}

func (me *ChanCond) Signal() {
	me.mu.Lock()
	defer me.mu.Unlock()
	select {
	case me.ch <- struct{}{}:
	default:
	}
}

func (me *ChanCond) Broadcast() {
	me.mu.Lock()
	defer me.mu.Unlock()
	if me.ch == nil {
		return
	}
	close(me.ch)
	me.ch = nil
}