Source file src/runtime/netpoll.go

     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 aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || windows
     6  
     7  package runtime
     8  
     9  import (
    10  	"runtime/internal/atomic"
    11  	"unsafe"
    12  )
    13  
    14  // Integrated network poller (platform-independent part).
    15  // A particular implementation (epoll/kqueue/port/AIX/Windows)
    16  // must define the following functions:
    17  //
    18  // func netpollinit()
    19  //     Initialize the poller. Only called once.
    20  //
    21  // func netpollopen(fd uintptr, pd *pollDesc) int32
    22  //     Arm edge-triggered notifications for fd. The pd argument is to pass
    23  //     back to netpollready when fd is ready. Return an errno value.
    24  //
    25  // func netpollclose(fd uintptr) int32
    26  //     Disable notifications for fd. Return an errno value.
    27  //
    28  // func netpoll(delta int64) gList
    29  //     Poll the network. If delta < 0, block indefinitely. If delta == 0,
    30  //     poll without blocking. If delta > 0, block for up to delta nanoseconds.
    31  //     Return a list of goroutines built by calling netpollready.
    32  //
    33  // func netpollBreak()
    34  //     Wake up the network poller, assumed to be blocked in netpoll.
    35  //
    36  // func netpollIsPollDescriptor(fd uintptr) bool
    37  //     Reports whether fd is a file descriptor used by the poller.
    38  
    39  // Error codes returned by runtime_pollReset and runtime_pollWait.
    40  // These must match the values in internal/poll/fd_poll_runtime.go.
    41  const (
    42  	pollNoError        = 0 // no error
    43  	pollErrClosing     = 1 // descriptor is closed
    44  	pollErrTimeout     = 2 // I/O timeout
    45  	pollErrNotPollable = 3 // general error polling descriptor
    46  )
    47  
    48  // pollDesc contains 2 binary semaphores, rg and wg, to park reader and writer
    49  // goroutines respectively. The semaphore can be in the following states:
    50  // pdReady - io readiness notification is pending;
    51  //           a goroutine consumes the notification by changing the state to nil.
    52  // pdWait - a goroutine prepares to park on the semaphore, but not yet parked;
    53  //          the goroutine commits to park by changing the state to G pointer,
    54  //          or, alternatively, concurrent io notification changes the state to pdReady,
    55  //          or, alternatively, concurrent timeout/close changes the state to nil.
    56  // G pointer - the goroutine is blocked on the semaphore;
    57  //             io notification or timeout/close changes the state to pdReady or nil respectively
    58  //             and unparks the goroutine.
    59  // nil - none of the above.
    60  const (
    61  	pdReady uintptr = 1
    62  	pdWait  uintptr = 2
    63  )
    64  
    65  const pollBlockSize = 4 * 1024
    66  
    67  // Network poller descriptor.
    68  //
    69  // No heap pointers.
    70  //
    71  //go:notinheap
    72  type pollDesc struct {
    73  	link *pollDesc // in pollcache, protected by pollcache.lock
    74  	fd   uintptr   // constant for pollDesc usage lifetime
    75  
    76  	// atomicInfo holds bits from closing, rd, and wd,
    77  	// which are only ever written while holding the lock,
    78  	// summarized for use by netpollcheckerr,
    79  	// which cannot acquire the lock.
    80  	// After writing these fields under lock in a way that
    81  	// might change the summary, code must call publishInfo
    82  	// before releasing the lock.
    83  	// Code that changes fields and then calls netpollunblock
    84  	// (while still holding the lock) must call publishInfo
    85  	// before calling netpollunblock, because publishInfo is what
    86  	// stops netpollblock from blocking anew
    87  	// (by changing the result of netpollcheckerr).
    88  	// atomicInfo also holds the eventErr bit,
    89  	// recording whether a poll event on the fd got an error;
    90  	// atomicInfo is the only source of truth for that bit.
    91  	atomicInfo atomic.Uint32 // atomic pollInfo
    92  
    93  	// rg, wg are accessed atomically and hold g pointers.
    94  	// (Using atomic.Uintptr here is similar to using guintptr elsewhere.)
    95  	rg atomic.Uintptr // pdReady, pdWait, G waiting for read or nil
    96  	wg atomic.Uintptr // pdReady, pdWait, G waiting for write or nil
    97  
    98  	lock    mutex // protects the following fields
    99  	closing bool
   100  	user    uint32    // user settable cookie
   101  	rseq    uintptr   // protects from stale read timers
   102  	rt      timer     // read deadline timer (set if rt.f != nil)
   103  	rd      int64     // read deadline (a nanotime in the future, -1 when expired)
   104  	wseq    uintptr   // protects from stale write timers
   105  	wt      timer     // write deadline timer
   106  	wd      int64     // write deadline (a nanotime in the future, -1 when expired)
   107  	self    *pollDesc // storage for indirect interface. See (*pollDesc).makeArg.
   108  }
   109  
   110  // pollInfo is the bits needed by netpollcheckerr, stored atomically,
   111  // mostly duplicating state that is manipulated under lock in pollDesc.
   112  // The one exception is the pollEventErr bit, which is maintained only
   113  // in the pollInfo.
   114  type pollInfo uint32
   115  
   116  const (
   117  	pollClosing = 1 << iota
   118  	pollEventErr
   119  	pollExpiredReadDeadline
   120  	pollExpiredWriteDeadline
   121  )
   122  
   123  func (i pollInfo) closing() bool              { return i&pollClosing != 0 }
   124  func (i pollInfo) eventErr() bool             { return i&pollEventErr != 0 }
   125  func (i pollInfo) expiredReadDeadline() bool  { return i&pollExpiredReadDeadline != 0 }
   126  func (i pollInfo) expiredWriteDeadline() bool { return i&pollExpiredWriteDeadline != 0 }
   127  
   128  // info returns the pollInfo corresponding to pd.
   129  func (pd *pollDesc) info() pollInfo {
   130  	return pollInfo(pd.atomicInfo.Load())
   131  }
   132  
   133  // publishInfo updates pd.atomicInfo (returned by pd.info)
   134  // using the other values in pd.
   135  // It must be called while holding pd.lock,
   136  // and it must be called after changing anything
   137  // that might affect the info bits.
   138  // In practice this means after changing closing
   139  // or changing rd or wd from < 0 to >= 0.
   140  func (pd *pollDesc) publishInfo() {
   141  	var info uint32
   142  	if pd.closing {
   143  		info |= pollClosing
   144  	}
   145  	if pd.rd < 0 {
   146  		info |= pollExpiredReadDeadline
   147  	}
   148  	if pd.wd < 0 {
   149  		info |= pollExpiredWriteDeadline
   150  	}
   151  
   152  	// Set all of x except the pollEventErr bit.
   153  	x := pd.atomicInfo.Load()
   154  	for !pd.atomicInfo.CompareAndSwap(x, (x&pollEventErr)|info) {
   155  		x = pd.atomicInfo.Load()
   156  	}
   157  }
   158  
   159  // setEventErr sets the result of pd.info().eventErr() to b.
   160  func (pd *pollDesc) setEventErr(b bool) {
   161  	x := pd.atomicInfo.Load()
   162  	for (x&pollEventErr != 0) != b && !pd.atomicInfo.CompareAndSwap(x, x^pollEventErr) {
   163  		x = pd.atomicInfo.Load()
   164  	}
   165  }
   166  
   167  type pollCache struct {
   168  	lock  mutex
   169  	first *pollDesc
   170  	// PollDesc objects must be type-stable,
   171  	// because we can get ready notification from epoll/kqueue
   172  	// after the descriptor is closed/reused.
   173  	// Stale notifications are detected using seq variable,
   174  	// seq is incremented when deadlines are changed or descriptor is reused.
   175  }
   176  
   177  var (
   178  	netpollInitLock mutex
   179  	netpollInited   uint32
   180  
   181  	pollcache      pollCache
   182  	netpollWaiters uint32
   183  )
   184  
   185  //go:linkname poll_runtime_pollServerInit internal/poll.runtime_pollServerInit
   186  func poll_runtime_pollServerInit() {
   187  	netpollGenericInit()
   188  }
   189  
   190  func netpollGenericInit() {
   191  	if atomic.Load(&netpollInited) == 0 {
   192  		lockInit(&netpollInitLock, lockRankNetpollInit)
   193  		lock(&netpollInitLock)
   194  		if netpollInited == 0 {
   195  			netpollinit()
   196  			atomic.Store(&netpollInited, 1)
   197  		}
   198  		unlock(&netpollInitLock)
   199  	}
   200  }
   201  
   202  func netpollinited() bool {
   203  	return atomic.Load(&netpollInited) != 0
   204  }
   205  
   206  //go:linkname poll_runtime_isPollServerDescriptor internal/poll.runtime_isPollServerDescriptor
   207  
   208  // poll_runtime_isPollServerDescriptor reports whether fd is a
   209  // descriptor being used by netpoll.
   210  func poll_runtime_isPollServerDescriptor(fd uintptr) bool {
   211  	return netpollIsPollDescriptor(fd)
   212  }
   213  
   214  //go:linkname poll_runtime_pollOpen internal/poll.runtime_pollOpen
   215  func poll_runtime_pollOpen(fd uintptr) (*pollDesc, int) {
   216  	pd := pollcache.alloc()
   217  	lock(&pd.lock)
   218  	wg := pd.wg.Load()
   219  	if wg != 0 && wg != pdReady {
   220  		throw("runtime: blocked write on free polldesc")
   221  	}
   222  	rg := pd.rg.Load()
   223  	if rg != 0 && rg != pdReady {
   224  		throw("runtime: blocked read on free polldesc")
   225  	}
   226  	pd.fd = fd
   227  	pd.closing = false
   228  	pd.setEventErr(false)
   229  	pd.rseq++
   230  	pd.rg.Store(0)
   231  	pd.rd = 0
   232  	pd.wseq++
   233  	pd.wg.Store(0)
   234  	pd.wd = 0
   235  	pd.self = pd
   236  	pd.publishInfo()
   237  	unlock(&pd.lock)
   238  
   239  	errno := netpollopen(fd, pd)
   240  	if errno != 0 {
   241  		pollcache.free(pd)
   242  		return nil, int(errno)
   243  	}
   244  	return pd, 0
   245  }
   246  
   247  //go:linkname poll_runtime_pollClose internal/poll.runtime_pollClose
   248  func poll_runtime_pollClose(pd *pollDesc) {
   249  	if !pd.closing {
   250  		throw("runtime: close polldesc w/o unblock")
   251  	}
   252  	wg := pd.wg.Load()
   253  	if wg != 0 && wg != pdReady {
   254  		throw("runtime: blocked write on closing polldesc")
   255  	}
   256  	rg := pd.rg.Load()
   257  	if rg != 0 && rg != pdReady {
   258  		throw("runtime: blocked read on closing polldesc")
   259  	}
   260  	netpollclose(pd.fd)
   261  	pollcache.free(pd)
   262  }
   263  
   264  func (c *pollCache) free(pd *pollDesc) {
   265  	lock(&c.lock)
   266  	pd.link = c.first
   267  	c.first = pd
   268  	unlock(&c.lock)
   269  }
   270  
   271  // poll_runtime_pollReset, which is internal/poll.runtime_pollReset,
   272  // prepares a descriptor for polling in mode, which is 'r' or 'w'.
   273  // This returns an error code; the codes are defined above.
   274  //go:linkname poll_runtime_pollReset internal/poll.runtime_pollReset
   275  func poll_runtime_pollReset(pd *pollDesc, mode int) int {
   276  	errcode := netpollcheckerr(pd, int32(mode))
   277  	if errcode != pollNoError {
   278  		return errcode
   279  	}
   280  	if mode == 'r' {
   281  		pd.rg.Store(0)
   282  	} else if mode == 'w' {
   283  		pd.wg.Store(0)
   284  	}
   285  	return pollNoError
   286  }
   287  
   288  // poll_runtime_pollWait, which is internal/poll.runtime_pollWait,
   289  // waits for a descriptor to be ready for reading or writing,
   290  // according to mode, which is 'r' or 'w'.
   291  // This returns an error code; the codes are defined above.
   292  //go:linkname poll_runtime_pollWait internal/poll.runtime_pollWait
   293  func poll_runtime_pollWait(pd *pollDesc, mode int) int {
   294  	errcode := netpollcheckerr(pd, int32(mode))
   295  	if errcode != pollNoError {
   296  		return errcode
   297  	}
   298  	// As for now only Solaris, illumos, and AIX use level-triggered IO.
   299  	if GOOS == "solaris" || GOOS == "illumos" || GOOS == "aix" {
   300  		netpollarm(pd, mode)
   301  	}
   302  	for !netpollblock(pd, int32(mode), false) {
   303  		errcode = netpollcheckerr(pd, int32(mode))
   304  		if errcode != pollNoError {
   305  			return errcode
   306  		}
   307  		// Can happen if timeout has fired and unblocked us,
   308  		// but before we had a chance to run, timeout has been reset.
   309  		// Pretend it has not happened and retry.
   310  	}
   311  	return pollNoError
   312  }
   313  
   314  //go:linkname poll_runtime_pollWaitCanceled internal/poll.runtime_pollWaitCanceled
   315  func poll_runtime_pollWaitCanceled(pd *pollDesc, mode int) {
   316  	// This function is used only on windows after a failed attempt to cancel
   317  	// a pending async IO operation. Wait for ioready, ignore closing or timeouts.
   318  	for !netpollblock(pd, int32(mode), true) {
   319  	}
   320  }
   321  
   322  //go:linkname poll_runtime_pollSetDeadline internal/poll.runtime_pollSetDeadline
   323  func poll_runtime_pollSetDeadline(pd *pollDesc, d int64, mode int) {
   324  	lock(&pd.lock)
   325  	if pd.closing {
   326  		unlock(&pd.lock)
   327  		return
   328  	}
   329  	rd0, wd0 := pd.rd, pd.wd
   330  	combo0 := rd0 > 0 && rd0 == wd0
   331  	if d > 0 {
   332  		d += nanotime()
   333  		if d <= 0 {
   334  			// If the user has a deadline in the future, but the delay calculation
   335  			// overflows, then set the deadline to the maximum possible value.
   336  			d = 1<<63 - 1
   337  		}
   338  	}
   339  	if mode == 'r' || mode == 'r'+'w' {
   340  		pd.rd = d
   341  	}
   342  	if mode == 'w' || mode == 'r'+'w' {
   343  		pd.wd = d
   344  	}
   345  	pd.publishInfo()
   346  	combo := pd.rd > 0 && pd.rd == pd.wd
   347  	rtf := netpollReadDeadline
   348  	if combo {
   349  		rtf = netpollDeadline
   350  	}
   351  	if pd.rt.f == nil {
   352  		if pd.rd > 0 {
   353  			pd.rt.f = rtf
   354  			// Copy current seq into the timer arg.
   355  			// Timer func will check the seq against current descriptor seq,
   356  			// if they differ the descriptor was reused or timers were reset.
   357  			pd.rt.arg = pd.makeArg()
   358  			pd.rt.seq = pd.rseq
   359  			resettimer(&pd.rt, pd.rd)
   360  		}
   361  	} else if pd.rd != rd0 || combo != combo0 {
   362  		pd.rseq++ // invalidate current timers
   363  		if pd.rd > 0 {
   364  			modtimer(&pd.rt, pd.rd, 0, rtf, pd.makeArg(), pd.rseq)
   365  		} else {
   366  			deltimer(&pd.rt)
   367  			pd.rt.f = nil
   368  		}
   369  	}
   370  	if pd.wt.f == nil {
   371  		if pd.wd > 0 && !combo {
   372  			pd.wt.f = netpollWriteDeadline
   373  			pd.wt.arg = pd.makeArg()
   374  			pd.wt.seq = pd.wseq
   375  			resettimer(&pd.wt, pd.wd)
   376  		}
   377  	} else if pd.wd != wd0 || combo != combo0 {
   378  		pd.wseq++ // invalidate current timers
   379  		if pd.wd > 0 && !combo {
   380  			modtimer(&pd.wt, pd.wd, 0, netpollWriteDeadline, pd.makeArg(), pd.wseq)
   381  		} else {
   382  			deltimer(&pd.wt)
   383  			pd.wt.f = nil
   384  		}
   385  	}
   386  	// If we set the new deadline in the past, unblock currently pending IO if any.
   387  	// Note that pd.publishInfo has already been called, above, immediately after modifying rd and wd.
   388  	var rg, wg *g
   389  	if pd.rd < 0 {
   390  		rg = netpollunblock(pd, 'r', false)
   391  	}
   392  	if pd.wd < 0 {
   393  		wg = netpollunblock(pd, 'w', false)
   394  	}
   395  	unlock(&pd.lock)
   396  	if rg != nil {
   397  		netpollgoready(rg, 3)
   398  	}
   399  	if wg != nil {
   400  		netpollgoready(wg, 3)
   401  	}
   402  }
   403  
   404  //go:linkname poll_runtime_pollUnblock internal/poll.runtime_pollUnblock
   405  func poll_runtime_pollUnblock(pd *pollDesc) {
   406  	lock(&pd.lock)
   407  	if pd.closing {
   408  		throw("runtime: unblock on closing polldesc")
   409  	}
   410  	pd.closing = true
   411  	pd.rseq++
   412  	pd.wseq++
   413  	var rg, wg *g
   414  	pd.publishInfo()
   415  	rg = netpollunblock(pd, 'r', false)
   416  	wg = netpollunblock(pd, 'w', false)
   417  	if pd.rt.f != nil {
   418  		deltimer(&pd.rt)
   419  		pd.rt.f = nil
   420  	}
   421  	if pd.wt.f != nil {
   422  		deltimer(&pd.wt)
   423  		pd.wt.f = nil
   424  	}
   425  	unlock(&pd.lock)
   426  	if rg != nil {
   427  		netpollgoready(rg, 3)
   428  	}
   429  	if wg != nil {
   430  		netpollgoready(wg, 3)
   431  	}
   432  }
   433  
   434  // netpollready is called by the platform-specific netpoll function.
   435  // It declares that the fd associated with pd is ready for I/O.
   436  // The toRun argument is used to build a list of goroutines to return
   437  // from netpoll. The mode argument is 'r', 'w', or 'r'+'w' to indicate
   438  // whether the fd is ready for reading or writing or both.
   439  //
   440  // This may run while the world is stopped, so write barriers are not allowed.
   441  //go:nowritebarrier
   442  func netpollready(toRun *gList, pd *pollDesc, mode int32) {
   443  	var rg, wg *g
   444  	if mode == 'r' || mode == 'r'+'w' {
   445  		rg = netpollunblock(pd, 'r', true)
   446  	}
   447  	if mode == 'w' || mode == 'r'+'w' {
   448  		wg = netpollunblock(pd, 'w', true)
   449  	}
   450  	if rg != nil {
   451  		toRun.push(rg)
   452  	}
   453  	if wg != nil {
   454  		toRun.push(wg)
   455  	}
   456  }
   457  
   458  func netpollcheckerr(pd *pollDesc, mode int32) int {
   459  	info := pd.info()
   460  	if info.closing() {
   461  		return pollErrClosing
   462  	}
   463  	if (mode == 'r' && info.expiredReadDeadline()) || (mode == 'w' && info.expiredWriteDeadline()) {
   464  		return pollErrTimeout
   465  	}
   466  	// Report an event scanning error only on a read event.
   467  	// An error on a write event will be captured in a subsequent
   468  	// write call that is able to report a more specific error.
   469  	if mode == 'r' && info.eventErr() {
   470  		return pollErrNotPollable
   471  	}
   472  	return pollNoError
   473  }
   474  
   475  func netpollblockcommit(gp *g, gpp unsafe.Pointer) bool {
   476  	r := atomic.Casuintptr((*uintptr)(gpp), pdWait, uintptr(unsafe.Pointer(gp)))
   477  	if r {
   478  		// Bump the count of goroutines waiting for the poller.
   479  		// The scheduler uses this to decide whether to block
   480  		// waiting for the poller if there is nothing else to do.
   481  		atomic.Xadd(&netpollWaiters, 1)
   482  	}
   483  	return r
   484  }
   485  
   486  func netpollgoready(gp *g, traceskip int) {
   487  	atomic.Xadd(&netpollWaiters, -1)
   488  	goready(gp, traceskip+1)
   489  }
   490  
   491  // returns true if IO is ready, or false if timedout or closed
   492  // waitio - wait only for completed IO, ignore errors
   493  // Concurrent calls to netpollblock in the same mode are forbidden, as pollDesc
   494  // can hold only a single waiting goroutine for each mode.
   495  func netpollblock(pd *pollDesc, mode int32, waitio bool) bool {
   496  	gpp := &pd.rg
   497  	if mode == 'w' {
   498  		gpp = &pd.wg
   499  	}
   500  
   501  	// set the gpp semaphore to pdWait
   502  	for {
   503  		// Consume notification if already ready.
   504  		if gpp.CompareAndSwap(pdReady, 0) {
   505  			return true
   506  		}
   507  		if gpp.CompareAndSwap(0, pdWait) {
   508  			break
   509  		}
   510  
   511  		// Double check that this isn't corrupt; otherwise we'd loop
   512  		// forever.
   513  		if v := gpp.Load(); v != pdReady && v != 0 {
   514  			throw("runtime: double wait")
   515  		}
   516  	}
   517  
   518  	// need to recheck error states after setting gpp to pdWait
   519  	// this is necessary because runtime_pollUnblock/runtime_pollSetDeadline/deadlineimpl
   520  	// do the opposite: store to closing/rd/wd, publishInfo, load of rg/wg
   521  	if waitio || netpollcheckerr(pd, mode) == pollNoError {
   522  		gopark(netpollblockcommit, unsafe.Pointer(gpp), waitReasonIOWait, traceEvGoBlockNet, 5)
   523  	}
   524  	// be careful to not lose concurrent pdReady notification
   525  	old := gpp.Swap(0)
   526  	if old > pdWait {
   527  		throw("runtime: corrupted polldesc")
   528  	}
   529  	return old == pdReady
   530  }
   531  
   532  func netpollunblock(pd *pollDesc, mode int32, ioready bool) *g {
   533  	gpp := &pd.rg
   534  	if mode == 'w' {
   535  		gpp = &pd.wg
   536  	}
   537  
   538  	for {
   539  		old := gpp.Load()
   540  		if old == pdReady {
   541  			return nil
   542  		}
   543  		if old == 0 && !ioready {
   544  			// Only set pdReady for ioready. runtime_pollWait
   545  			// will check for timeout/cancel before waiting.
   546  			return nil
   547  		}
   548  		var new uintptr
   549  		if ioready {
   550  			new = pdReady
   551  		}
   552  		if gpp.CompareAndSwap(old, new) {
   553  			if old == pdWait {
   554  				old = 0
   555  			}
   556  			return (*g)(unsafe.Pointer(old))
   557  		}
   558  	}
   559  }
   560  
   561  func netpolldeadlineimpl(pd *pollDesc, seq uintptr, read, write bool) {
   562  	lock(&pd.lock)
   563  	// Seq arg is seq when the timer was set.
   564  	// If it's stale, ignore the timer event.
   565  	currentSeq := pd.rseq
   566  	if !read {
   567  		currentSeq = pd.wseq
   568  	}
   569  	if seq != currentSeq {
   570  		// The descriptor was reused or timers were reset.
   571  		unlock(&pd.lock)
   572  		return
   573  	}
   574  	var rg *g
   575  	if read {
   576  		if pd.rd <= 0 || pd.rt.f == nil {
   577  			throw("runtime: inconsistent read deadline")
   578  		}
   579  		pd.rd = -1
   580  		pd.publishInfo()
   581  		rg = netpollunblock(pd, 'r', false)
   582  	}
   583  	var wg *g
   584  	if write {
   585  		if pd.wd <= 0 || pd.wt.f == nil && !read {
   586  			throw("runtime: inconsistent write deadline")
   587  		}
   588  		pd.wd = -1
   589  		pd.publishInfo()
   590  		wg = netpollunblock(pd, 'w', false)
   591  	}
   592  	unlock(&pd.lock)
   593  	if rg != nil {
   594  		netpollgoready(rg, 0)
   595  	}
   596  	if wg != nil {
   597  		netpollgoready(wg, 0)
   598  	}
   599  }
   600  
   601  func netpollDeadline(arg any, seq uintptr) {
   602  	netpolldeadlineimpl(arg.(*pollDesc), seq, true, true)
   603  }
   604  
   605  func netpollReadDeadline(arg any, seq uintptr) {
   606  	netpolldeadlineimpl(arg.(*pollDesc), seq, true, false)
   607  }
   608  
   609  func netpollWriteDeadline(arg any, seq uintptr) {
   610  	netpolldeadlineimpl(arg.(*pollDesc), seq, false, true)
   611  }
   612  
   613  func (c *pollCache) alloc() *pollDesc {
   614  	lock(&c.lock)
   615  	if c.first == nil {
   616  		const pdSize = unsafe.Sizeof(pollDesc{})
   617  		n := pollBlockSize / pdSize
   618  		if n == 0 {
   619  			n = 1
   620  		}
   621  		// Must be in non-GC memory because can be referenced
   622  		// only from epoll/kqueue internals.
   623  		mem := persistentalloc(n*pdSize, 0, &memstats.other_sys)
   624  		for i := uintptr(0); i < n; i++ {
   625  			pd := (*pollDesc)(add(mem, i*pdSize))
   626  			pd.link = c.first
   627  			c.first = pd
   628  		}
   629  	}
   630  	pd := c.first
   631  	c.first = pd.link
   632  	lockInit(&pd.lock, lockRankPollDesc)
   633  	unlock(&c.lock)
   634  	return pd
   635  }
   636  
   637  // makeArg converts pd to an interface{}.
   638  // makeArg does not do any allocation. Normally, such
   639  // a conversion requires an allocation because pointers to
   640  // go:notinheap types (which pollDesc is) must be stored
   641  // in interfaces indirectly. See issue 42076.
   642  func (pd *pollDesc) makeArg() (i any) {
   643  	x := (*eface)(unsafe.Pointer(&i))
   644  	x._type = pdType
   645  	x.data = unsafe.Pointer(&pd.self)
   646  	return
   647  }
   648  
   649  var (
   650  	pdEface any    = (*pollDesc)(nil)
   651  	pdType  *_type = efaceOf(&pdEface)._type
   652  )
   653  

View as plain text