...

Source file src/internal/poll/fd_poll_js.go

Documentation: internal/poll

     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  //go:build js && wasm
     6  
     7  package poll
     8  
     9  import (
    10  	"syscall"
    11  	"time"
    12  )
    13  
    14  type pollDesc struct {
    15  	fd      *FD
    16  	closing bool
    17  }
    18  
    19  func (pd *pollDesc) init(fd *FD) error { pd.fd = fd; return nil }
    20  
    21  func (pd *pollDesc) close() {}
    22  
    23  func (pd *pollDesc) evict() {
    24  	pd.closing = true
    25  	if pd.fd != nil {
    26  		syscall.StopIO(pd.fd.Sysfd)
    27  	}
    28  }
    29  
    30  func (pd *pollDesc) prepare(mode int, isFile bool) error {
    31  	if pd.closing {
    32  		return errClosing(isFile)
    33  	}
    34  	return nil
    35  }
    36  
    37  func (pd *pollDesc) prepareRead(isFile bool) error { return pd.prepare('r', isFile) }
    38  
    39  func (pd *pollDesc) prepareWrite(isFile bool) error { return pd.prepare('w', isFile) }
    40  
    41  func (pd *pollDesc) wait(mode int, isFile bool) error {
    42  	if pd.closing {
    43  		return errClosing(isFile)
    44  	}
    45  	if isFile { // TODO(neelance): js/wasm: Use callbacks from JS to block until the read/write finished.
    46  		return nil
    47  	}
    48  	return ErrDeadlineExceeded
    49  }
    50  
    51  func (pd *pollDesc) waitRead(isFile bool) error { return pd.wait('r', isFile) }
    52  
    53  func (pd *pollDesc) waitWrite(isFile bool) error { return pd.wait('w', isFile) }
    54  
    55  func (pd *pollDesc) waitCanceled(mode int) {}
    56  
    57  func (pd *pollDesc) pollable() bool { return true }
    58  
    59  // SetDeadline sets the read and write deadlines associated with fd.
    60  func (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.
    65  func (fd *FD) SetReadDeadline(t time.Time) error {
    66  	return setDeadlineImpl(fd, t, 'r')
    67  }
    68  
    69  // SetWriteDeadline sets the write deadline associated with fd.
    70  func (fd *FD) SetWriteDeadline(t time.Time) error {
    71  	return setDeadlineImpl(fd, t, 'w')
    72  }
    73  
    74  func 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.
    97  func IsPollDescriptor(fd uintptr) bool {
    98  	return false
    99  }
   100  

View as plain text