...
Source file src/internal/poll/fd_io_plan9.go
1
2
3
4
5 package poll
6
7 import (
8 "runtime"
9 "sync"
10 "syscall"
11 )
12
13
14
15
16
17
18 type asyncIO struct {
19 res chan result
20
21
22 mu sync.Mutex
23
24
25
26 pid int
27 }
28
29
30 type result struct {
31 n int
32 err error
33 }
34
35
36
37
38 func newAsyncIO(fn func([]byte) (int, error), b []byte) *asyncIO {
39 aio := &asyncIO{
40 res: make(chan result, 0),
41 }
42 aio.mu.Lock()
43 go func() {
44
45
46
47
48
49 runtime.LockOSThread()
50 runtime_ignoreHangup()
51 aio.pid = syscall.Getpid()
52 aio.mu.Unlock()
53
54 n, err := fn(b)
55
56 aio.mu.Lock()
57 aio.pid = -1
58 runtime_unignoreHangup()
59 aio.mu.Unlock()
60
61 aio.res <- result{n, err}
62 }()
63 return aio
64 }
65
66
67
68 func (aio *asyncIO) Cancel() {
69 aio.mu.Lock()
70 defer aio.mu.Unlock()
71 if aio.pid == -1 {
72 return
73 }
74 f, e := syscall.Open("/proc/"+itoa(aio.pid)+"/note", syscall.O_WRONLY)
75 if e != nil {
76 return
77 }
78 syscall.Write(f, []byte("hangup"))
79 syscall.Close(f)
80 }
81
82
83 func (aio *asyncIO) Wait() (int, error) {
84 res := <-aio.res
85 return res.n, res.err
86 }
87
88
89
90 func runtime_ignoreHangup()
91 func runtime_unignoreHangup()
92
View as plain text