File: shared.go

package info (click to toggle)
golang-fsnotify 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 716 kB
  • sloc: ansic: 98; makefile: 4
file content (64 lines) | stat: -rw-r--r-- 1,035 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package fsnotify

import "sync"

type shared struct {
	Events chan Event
	Errors chan error
	done   chan struct{}
	mu     sync.Mutex
}

func newShared(ev chan Event, errs chan error) *shared {
	return &shared{
		Events: ev,
		Errors: errs,
		done:   make(chan struct{}),
	}
}

// Returns true if the event was sent, or false if watcher is closed.
func (w *shared) sendEvent(e Event) bool {
	if e.Op == 0 {
		return true
	}
	select {
	case <-w.done:
		return false
	case w.Events <- e:
		return true
	}
}

// Returns true if the error was sent, or false if watcher is closed.
func (w *shared) sendError(err error) bool {
	if err == nil {
		return true
	}
	select {
	case <-w.done:
		return false
	case w.Errors <- err:
		return true
	}
}

func (w *shared) isClosed() bool {
	select {
	case <-w.done:
		return true
	default:
		return false
	}
}

// Mark as closed; returns true if it was already closed.
func (w *shared) close() bool {
	w.mu.Lock()
	defer w.mu.Unlock()
	if w.isClosed() {
		return true
	}
	close(w.done)
	return false
}