OSDN Git Service

Cross build (#203)
[bytom/bytom.git] / blockchain / pseudohsm / watch.go
1 // Copyright 2016 The go-ethereum Authors
2 // This file is part of the go-ethereum library.
3 //
4 // The go-ethereum library is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // The go-ethereum library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16
17 // +build darwin,!ios freebsd linux,!arm64 netbsd solaris windows
18
19 package pseudohsm
20
21 import (
22         //"fmt"
23         //"github.com/rjeczalik/notify"
24         //"time"
25 )
26
27 type watcher struct {
28         kc       *keyCache
29         starting bool
30         running  bool
31         //ev       chan notify.EventInfo
32         quit     chan struct{}
33 }
34
35 func newWatcher(kc *keyCache) *watcher {
36         return &watcher{
37                 kc:   kc,
38                 //ev:   make(chan notify.EventInfo, 10),
39                 quit: make(chan struct{}),
40         }
41 }
42
43 // starts the watcher loop in the background.
44 // Start a watcher in the background if that's not already in progress.
45 // The caller must hold w.kc.mu.
46 func (w *watcher) start() {
47         if w.starting || w.running {
48                 return
49         }
50         w.starting = true
51         go w.loop()
52 }
53
54 func (w *watcher) close() {
55         close(w.quit)
56 }
57
58 func (w *watcher) loop() {
59         defer func() {
60                 w.kc.mu.Lock()
61                 w.running = false
62                 w.starting = false
63                 w.kc.mu.Unlock()
64         }()
65
66 /*
67         err := notify.Watch(w.kc.keydir, w.ev, notify.All)
68         if err != nil {
69                 fmt.Printf("can't watch %s: %v", w.kc.keydir, err)
70                 return
71         }
72         defer notify.Stop(w.ev)
73         fmt.Printf("now watching %s", w.kc.keydir)
74         defer fmt.Printf("no longer watching %s", w.kc.keydir)
75
76         w.kc.mu.Lock()
77         w.running = true
78         w.kc.mu.Unlock()
79
80         // Wait for file system events and reload.
81         // When an event occurs, the reload call is delayed a bit so that
82         // multiple events arriving quickly only cause a single reload.
83         var (
84                 debounce          = time.NewTimer(0)
85                 debounceDuration  = 500 * time.Millisecond
86                 inCycle, hadEvent bool
87         )
88         defer debounce.Stop()
89         for {
90                 select {
91                 case <-w.quit:
92                         return
93                 case <-w.ev:
94                         if !inCycle {
95                                 debounce.Reset(debounceDuration)
96                                 inCycle = true
97                         } else {
98                                 hadEvent = true
99                         }
100                 case <-debounce.C:
101                         w.kc.mu.Lock()
102                         w.kc.reload()
103                         w.kc.mu.Unlock()
104                         if hadEvent {
105                                 debounce.Reset(debounceDuration)
106                                 inCycle, hadEvent = true, false
107                         } else {
108                                 inCycle, hadEvent = false, false
109                         }
110                 }
111         }
112         */
113 }