-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver_unix.go
57 lines (51 loc) · 967 Bytes
/
server_unix.go
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
/**
* @Author: llh
* @Date: 2019-06-01 15:08:12
* @Last Modified by: llh
*/
// +build darwin netbsd freebsd openbsd dragonfly linux
package tfg
import (
"golang.org/x/sys/unix"
"runtime"
)
func (s *server) serving() error {
if err := unix.SetNonblock(s.ln.fd, true); err != nil {
return err
}
if s.numPollEvent <= 0 {
s.numPollEvent = runtime.NumCPU()
}
defer func() {
s.waitForShutdown()
s.Stop()
}()
for id := 0; id < s.numPollEvent; id++ {
poll, err := mkPoll()
if err != nil {
return err
}
event := &pollEvent{
id: id,
poll: poll,
s: s,
}
event.poll.addLn(s.ln.fd)
s.pollEvents = append(s.pollEvents, event)
}
s.wg.Add(len(s.pollEvents))
for _, pollEvent := range s.pollEvents {
go pollEvent.run()
}
return nil
}
func (s *server) waitForShutdown() {
s.cond.L.Lock()
s.cond.Wait()
s.cond.L.Unlock()
}
func (s *server) signalShutdown() {
s.cond.L.Lock()
s.cond.Signal()
s.cond.L.Unlock()
}