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 plan9
6// +build plan9
7
8package runtime
9
10import "runtime/internal/atomic"
11
12var netpollInited uint32
13var netpollWaiters uint32
14
15var netpollStubLock mutex
16var netpollNote note
17
18// netpollBroken, protected by netpollBrokenLock, avoids a double notewakeup.
19var netpollBrokenLock mutex
20var netpollBroken bool
21
22func netpollGenericInit() {
23	atomic.Store(&netpollInited, 1)
24}
25
26func netpollBreak() {
27	lock(&netpollBrokenLock)
28	broken := netpollBroken
29	netpollBroken = true
30	if !broken {
31		notewakeup(&netpollNote)
32	}
33	unlock(&netpollBrokenLock)
34}
35
36// Polls for ready network connections.
37// Returns list of goroutines that become runnable.
38func netpoll(delay int64) gList {
39	// Implementation for platforms that do not support
40	// integrated network poller.
41	if delay != 0 {
42		// This lock ensures that only one goroutine tries to use
43		// the note. It should normally be completely uncontended.
44		lock(&netpollStubLock)
45
46		lock(&netpollBrokenLock)
47		noteclear(&netpollNote)
48		netpollBroken = false
49		unlock(&netpollBrokenLock)
50
51		notetsleep(&netpollNote, delay)
52		unlock(&netpollStubLock)
53		// Guard against starvation in case the lock is contended
54		// (eg when running TestNetpollBreak).
55		osyield()
56	}
57	return gList{}
58}
59
60func netpollinited() bool {
61	return atomic.Load(&netpollInited) != 0
62}
63