Debian Codegraph @

libgo/go/runtime/os_hurd.go

raw
1 // Copyright 2019 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 // This file is derived from os_aix.go.
6
7 package runtime
8
9 import "unsafe"
10
11 //extern sysconf
12 func sysconf(int32) _C_long
13
14 //extern getpagesize
15 func getPageSize() int32
16
17 type mOS struct {
18 waitsema uintptr // semaphore for parking on locks
19 }
20
21 func getProcID() uint64 {
22 return uint64(gettid())
23 }
24
25 //extern malloc
26 func libc_malloc(uintptr) unsafe.Pointer
27
28 //go:noescape
29 //extern sem_init
30 func sem_init(sem *semt, pshared int32, value uint32) int32
31
32 //go:noescape
33 //extern sem_wait
34 func sem_wait(sem *semt) int32
35
36 //go:noescape
37 //extern sem_post
38 func sem_post(sem *semt) int32
39
40 //go:noescape
41 //extern sem_timedwait
42 func sem_timedwait(sem *semt, timeout *timespec) int32
43
44 //go:noescape
45 //extern clock_gettime
46 func clock_gettime(clock_id int32, timeout *timespec) int32
47
48 //go:nosplit
49 func semacreate(mp *m) {
50 if mp.waitsema != 0 {
51 return
52 }
53
54 var sem *semt
55
56 // Call libc's malloc rather than malloc. This will
57 // allocate space on the C heap. We can't call malloc
58 // here because it could cause a deadlock.
59 sem = (*semt)(libc_malloc(unsafe.Sizeof(*sem)))
60 if sem_init(sem, 0, 0) != 0 {
61 throw("sem_init")
62 }
63 mp.waitsema = uintptr(unsafe.Pointer(sem))
64 }
65
66 //go:nosplit
67 func semasleep(ns int64) int32 {
68 _m_ := getg().m
69 if ns >= 0 {
70 var ts timespec
71
72 if clock_gettime(_CLOCK_REALTIME, &ts) != 0 {
73 throw("clock_gettime")
74 }
75
76 sec := int64(ts.tv_sec) + ns/1e9
77 nsec := int64(ts.tv_nsec) + ns%1e9
78 if nsec >= 1e9 {
79 sec++
80 nsec -= 1e9
81 }
82 if sec != int64(timespec_sec_t(sec)) {
83 // Handle overflows (timespec_sec_t is 32-bit in 32-bit applications)
84 sec = 1<<31 - 1
85 }
86 ts.tv_sec = timespec_sec_t(sec)
87 ts.tv_nsec = timespec_nsec_t(nsec)
88
89 if sem_timedwait((*semt)(unsafe.Pointer(_m_.waitsema)), &ts) != 0 {
90 err := errno()
91 if err == _ETIMEDOUT || err == _EAGAIN || err == _EINTR {
92 return -1
93 }
94 throw("sem_timedwait")
95 }
96 return 0
97 }
98 for {
99 r1 := sem_wait((*semt)(unsafe.Pointer(_m_.waitsema)))
100 if r1 == 0 {
101 break
102 }
103 if errno() == _EINTR {
104 continue
105 }
106 throw("sem_wait")
107 }
108 return 0
109 }
110
111 //go:nosplit
112 func semawakeup(mp *m) {
113 if sem_post((*semt)(unsafe.Pointer(mp.waitsema))) != 0 {
114 throw("sem_post")
115 }
116 }
117
118 func getncpu() int32 {
119 n := int32(sysconf(__SC_NPROCESSORS_ONLN))
120 if n < 1 {
121 return 1
122 }
123 return n
124 }
125
126 func osinit() {
127 ncpu = getncpu()
128 if physPageSize == 0 {
129 physPageSize = uintptr(getPageSize())
130 }
131 }
132
133 func setProcessCPUProfiler(hz int32) {
134 setProcessCPUProfilerTimer(hz)
135 }
136
137 func setThreadCPUProfiler(hz int32) {
138 setThreadCPUProfilerHz(hz)
139 }
140
141 //go:nosplit
142 func validSIGPROF(mp *m, c *sigctxt) bool {
143 return true
144 }