1// Copyright 2013 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.
4
5// +build js,wasm
6
7package poll
8
9import (
10	"syscall"
11	"time"
12)
13
14type pollDesc struct {
15	fd      *FD
16	closing bool
17}
18
19func (pd *pollDesc) init(fd *FD) error { pd.fd = fd; return nil }
20
21func (pd *pollDesc) close() {}
22
23func (pd *pollDesc) evict() {
24	pd.closing = true
25	if pd.fd != nil {
26		syscall.StopIO(pd.fd.Sysfd)
27	}
28}
29
30func (pd *pollDesc) prepare(mode int, isFile bool) error {
31	if pd.closing {
32		return errClosing(isFile)
33	}
34	return nil
35}
36
37func (pd *pollDesc) prepareRead(isFile bool) error { return pd.prepare('r', isFile) }
38
39func (pd *pollDesc) prepareWrite(isFile bool) error { return pd.prepare('w', isFile) }
40
41func (pd *pollDesc) wait(mode int, isFile bool) error {
42	if pd.closing {
43		return errClosing(isFile)
44	}
45	if isFile { // TODO(neelance): wasm: Use callbacks from JS to block until the read/write finished.
46		return nil
47	}
48	return ErrDeadlineExceeded
49}
50
51func (pd *pollDesc) waitRead(isFile bool) error { return pd.wait('r', isFile) }
52
53func (pd *pollDesc) waitWrite(isFile bool) error { return pd.wait('w', isFile) }
54
55func (pd *pollDesc) waitCanceled(mode int) {}
56
57func (pd *pollDesc) pollable() bool { return true }
58
59// SetDeadline sets the read and write deadlines associated with fd.
60func (fd *FD) SetDeadline(t time.Time) error {
61	return setDeadlineImpl(fd, t, 'r'+'w')
62}
63
64// SetReadDeadline sets the read deadline associated with fd.
65func (fd *FD) SetReadDeadline(t time.Time) error {
66	return setDeadlineImpl(fd, t, 'r')
67}
68
69// SetWriteDeadline sets the write deadline associated with fd.
70func (fd *FD) SetWriteDeadline(t time.Time) error {
71	return setDeadlineImpl(fd, t, 'w')
72}
73
74func setDeadlineImpl(fd *FD, t time.Time, mode int) error {
75	d := t.UnixNano()
76	if t.IsZero() {
77		d = 0
78	}
79	if err := fd.incref(); err != nil {
80		return err
81	}
82	switch mode {
83	case 'r':
84		syscall.SetReadDeadline(fd.Sysfd, d)
85	case 'w':
86		syscall.SetWriteDeadline(fd.Sysfd, d)
87	case 'r' + 'w':
88		syscall.SetReadDeadline(fd.Sysfd, d)
89		syscall.SetWriteDeadline(fd.Sysfd, d)
90	}
91	fd.decref()
92	return nil
93}
94
95// IsPollDescriptor reports whether fd is the descriptor being used by the poller.
96// This is only used for testing.
97func IsPollDescriptor(fd uintptr) bool {
98	return false
99}
100