github.com/hoop33/elvish@v0.0.0-20160801152013-6d25485beab4/sys/select.go (about)

     1  package sys
     2  
     3  /*
     4  #include <sys/select.h>
     5  
     6  void fdclr(int fd, fd_set *set) {
     7  	FD_CLR(fd, set);
     8  }
     9  
    10  int fdisset(int fd, fd_set *set) {
    11  	return FD_ISSET(fd, set);
    12  }
    13  
    14  void fdset(int fd, fd_set *set) {
    15  	FD_SET(fd, set);
    16  }
    17  
    18  void fdzero(fd_set *set) {
    19  	FD_ZERO(set);
    20  }
    21  */
    22  import "C"
    23  
    24  import (
    25  	"syscall"
    26  	"unsafe"
    27  )
    28  
    29  type FdSet syscall.FdSet
    30  
    31  func (fs *FdSet) c() *C.fd_set {
    32  	return (*C.fd_set)(unsafe.Pointer(fs))
    33  }
    34  
    35  func (fs *FdSet) s() *syscall.FdSet {
    36  	return (*syscall.FdSet)(fs)
    37  }
    38  
    39  func NewFdSet(fds ...int) *FdSet {
    40  	fs := &FdSet{}
    41  	fs.Set(fds...)
    42  	return fs
    43  }
    44  
    45  func (fs *FdSet) Clear(fds ...int) {
    46  	for _, fd := range fds {
    47  		C.fdclr(C.int(fd), fs.c())
    48  	}
    49  }
    50  
    51  func (fs *FdSet) IsSet(fd int) bool {
    52  	return C.fdisset(C.int(fd), fs.c()) != 0
    53  }
    54  
    55  func (fs *FdSet) Set(fds ...int) {
    56  	for _, fd := range fds {
    57  		C.fdset(C.int(fd), fs.c())
    58  	}
    59  }
    60  
    61  func (fs *FdSet) Zero() {
    62  	C.fdzero(fs.c())
    63  }