1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // asyncIO implements asynchronous cancelable I/O.
15 // An asyncIO represents a single asynchronous Read or Write
16 // operation. The result is returned on the result channel.
17 // The undergoing I/O system call can either complete or be
18 // interrupted by a note.
22 // mu guards the pid field.
25 // pid holds the process id of
26 // the process running the IO operation.
30 // result is the return value of a Read or Write operation.
36 // newAsyncIO returns a new asyncIO that performs an I/O
37 // operation by calling fn, which must do one and only one
38 // interruptible system call.
39 func newAsyncIO(fn
func([]byte) (int, error
), b
[]byte) *asyncIO
{
41 res
: make(chan result
, 0),
45 // Lock the current goroutine to its process
46 // and store the pid in io so that Cancel can
47 // interrupt it. We ignore the "hangup" signal,
48 // so the signal does not take down the entire
50 runtime
.LockOSThread()
51 runtime_ignoreHangup()
52 aio
.pid
= syscall
.Getpid()
59 runtime_unignoreHangup()
62 aio
.res
<- result
{n
, err
}
67 // Cancel interrupts the I/O operation, causing
68 // the Wait function to return.
69 func (aio
*asyncIO
) Cancel() {
75 f
, e
:= syscall
.Open("/proc/"+itoa
.Itoa(aio
.pid
)+"/note", syscall
.O_WRONLY
)
79 syscall
.Write(f
, []byte("hangup"))
83 // Wait for the I/O operation to complete.
84 func (aio
*asyncIO
) Wait() (int, error
) {
89 // The following functions, provided by the runtime, are used to
90 // ignore and unignore the "hangup" signal received by the process.
91 func runtime_ignoreHangup()
92 func runtime_unignoreHangup()