gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/internal/poll/fd_poll_js.go (about)

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