pax_global_header00006660000000000000000000000064146571445540014531gustar00rootroot0000000000000052 comment=609dd7658a06e0109f1d87cad2892a81a1aa9707 golang-github-petermattis-goid-0.0~git20240813.4fcff4a/000077500000000000000000000000001465714455400223405ustar00rootroot00000000000000golang-github-petermattis-goid-0.0~git20240813.4fcff4a/.github/000077500000000000000000000000001465714455400237005ustar00rootroot00000000000000golang-github-petermattis-goid-0.0~git20240813.4fcff4a/.github/dependabot.yml000066400000000000000000000001651465714455400265320ustar00rootroot00000000000000version: 2 updates: - package-ecosystem: "github-actions" directory: "/" schedule: interval: "daily" golang-github-petermattis-goid-0.0~git20240813.4fcff4a/.github/workflows/000077500000000000000000000000001465714455400257355ustar00rootroot00000000000000golang-github-petermattis-goid-0.0~git20240813.4fcff4a/.github/workflows/go.yml000066400000000000000000000212041465714455400270640ustar00rootroot00000000000000name: Go on: push: branches: - master pull_request: branches: - master schedule: - cron: 00 4 * * * jobs: build: runs-on: ubuntu-latest strategy: fail-fast: false matrix: include: # Cross-compilation became possible in go1.5 with the removal of C # code from the compiler. See https://go.dev/doc/go1.5#c. # # Cross-compilation isn't supported in gccgo. - arch: x64 go: '1.3' - arch: x64 go: '1.4' - arch: x64 go: gccgo-9 - arch: x64 go: gccgo-10 - arch: x64 go: gccgo-11 - arch: x64 go: gccgo-12 # NB: gccgo-13 and gccgo-14 are not available in the Ubuntu 22.04. # # TODO(https://github.com/actions/runner-images/issues/9848): Add gccgo-13 and gccgo-14 # when Ubuntu 24.04 is GA on GitHub Actions. arch: - armv6 - armv7 - aarch64 - s390x - 386 - x64 go: - '1.5' - '1.6' - '1.7' - '1.8' - '1.9' - '1.10' - '1.11' - '1.12' - '1.13' - '1.14' - '1.15' - '1.16' - '1.17' - '1.18' - '1.19' - '1.20' - '1.21' - '1.22' - '1.23' exclude: # Support for s390x was added in Go1.7. See https://go.dev/doc/go1.7#ports. - arch: s390x go: '1.5' - arch: s390x go: '1.6' steps: - uses: actions/checkout@v4 - name: Set up Go if: ${{ !startsWith(matrix.go, 'gccgo-') }} uses: actions/setup-go@v5.0.2 with: go-version: ${{ matrix.go }} - name: Set up gccgo if: ${{ startsWith(matrix.go, 'gccgo-') }} shell: bash run: | set -euxo pipefail sudo apt update sudo apt install -y ${{ matrix.go }} echo ${{ matrix.go }} | sed 's/^gcc//' | xargs -I % ln -s /usr/bin/% /usr/local/bin/go go version - name: Configure environment id: configure_environment shell: bash run: | set -euxo pipefail case ${{ matrix.arch }} in armv6) echo GOARCH=arm >> "$GITHUB_ENV" echo GOARM=6 >> "$GITHUB_ENV" ;; armv7) echo GOARCH=arm >> "$GITHUB_ENV" echo GOARM=7 >> "$GITHUB_ENV" ;; aarch64) echo GOARCH=arm64 >> "$GITHUB_ENV" ;; s390x) echo GOARCH=s390x >> "$GITHUB_ENV" ;; 386) echo GOARCH=386 >> "$GITHUB_ENV" ;; x64) echo GOARCH=amd64 >> "$GITHUB_ENV" ;; *) echo "Unknown architecture: ${{ matrix.arch }}" exit 1 ;; esac version_ge() { version=$1 printf "$version\n%s\n" ${{ matrix.go }} | sort -V | head -n1 | xargs test "$version" = } # Race builds are pretty much busted on Go 1.4 and below on systems with newer C # compilers. A number of fixes were backported to go1.4-bootstrap but not any released # version. See https://github.com/golang/go/compare/go1.4.3...release-branch.go1.4. if ! version_ge 1.5; then echo "RACE_BUILDS_BROKEN=1" >> "$GITHUB_OUTPUT" fi # Go binaries built with Go 1.8 and below are incompatible with QEMU user-level emulation. # See https://github.com/golang/go/commit/2673f9e. if version_ge 1.9; then echo "QEMU_EMULATION_WORKS=1" >> "$GITHUB_OUTPUT" fi if version_ge 1.12; then # Better inlining in Go 1.12. See https://go.dev/doc/go1.12#compiler. echo "BETTER_INLINING_AVAILABLE=1" >> "$GITHUB_OUTPUT" # Race detector support on linux/arm64 was added in go1.12. See # https://go.dev/doc/go1.12. echo "ARM64_RACE_SUPPORTED=1" >> "$GITHUB_OUTPUT" fi if version_ge 1.19; then # Race detector support on linux/s390x was added in go1.19. See # https://go.dev/doc/go1.19. echo "S390X_RACE_SUPPORTED=1" >> "$GITHUB_OUTPUT" fi # Race detector binaries crash with: # # FATAL: ThreadSanitizer: unsupported VMA range # # See https://github.com/golang/go/issues/29948. echo "ARM64_UNSUPPORTED_VMA_RANGE=1" >> "$GITHUB_OUTPUT" # Race detector binaries crash with: # # ==17==ERROR: ThreadSanitizer failed to allocate 0x7f0000 (8323072) bytes at address 9000001a0000 (errno: 12) # # See https://github.com/golang/go/issues/67881. echo "S390X_THREAD_SANITIZER_FAILED_TO_ALLOCATE=1" >> "$GITHUB_OUTPUT" - name: Check that Get is inlined if: | !startsWith(matrix.go, 'gccgo-') && steps.configure_environment.outputs.BETTER_INLINING_AVAILABLE shell: bash run: | set -euxo pipefail go build -gcflags='-m' 2>&1 | grep 'can inline Get$' > /dev/null - name: go build & go test -c shell: bash run: | set -euxo pipefail go build -v ./... go test -c -o goid.test ./... - name: go build & go test -c (race) # Race builds aren't supported on linux/386. # # Race builds aren't supported in gccgo. id: build_goid_race_test if: | !cancelled() && matrix.arch == 'x64' && !startsWith(matrix.go, 'gccgo-') && !steps.configure_environment.outputs.RACE_BUILDS_BROKEN shell: bash run: | set -euxo pipefail go build -v -race ./... go test -c -race -o goid.race.test ./... - name: go build & go test -c (race) id: build_goid_race_test_cross if: | !cancelled() && matrix.arch == 'aarch64' && steps.configure_environment.outputs.ARM64_RACE_SUPPORTED || matrix.arch == 's390x' && steps.configure_environment.outputs.S390X_RACE_SUPPORTED shell: bash run: | set -euxo pipefail # Non-host *.syso files are missing from the Go toolchains provided # by setup-go. See https://github.com/actions/setup-go/issues/181. curl --location --output "$(go env GOROOT)"/src/runtime/race/race_linux_"$GOARCH".syso \ https://github.com/golang/go/raw/release-branch.go${{ matrix.go }}/src/runtime/race/race_linux_"$GOARCH".syso sudo apt update sudo apt install -y gcc-${{ matrix.arch }}-linux-gnu export CGO_ENABLED=1 export CC=${{ matrix.arch }}-linux-gnu-gcc export CC_FOR_TARGET=gcc-${{ matrix.arch }}-linux-gnu go build -v -race ./... go test -c -race -o goid.race.test ./... - run: rm goid.race.test if: | !cancelled() && (steps.build_goid_race_test.outcome == 'success' || steps.build_goid_race_test_cross.outcome == 'success') && ( matrix.arch == 'aarch64' && steps.configure_environment.outputs.ARM64_RACE_SUPPORTED && steps.configure_environment.outputs.ARM64_UNSUPPORTED_VMA_RANGE ) || ( matrix.arch == 's390x' && steps.configure_environment.outputs.S390X_RACE_SUPPORTED && steps.configure_environment.outputs.S390X_THREAD_SANITIZER_FAILED_TO_ALLOCATE ) - name: Run tests if: | !cancelled() && (matrix.arch == '386' || matrix.arch == 'x64') shell: bash run: | set -euxo pipefail find . -name '*.test' -type f -executable -print0 | \ xargs -t -0 -I '{}' sh -c '{} -test.v && {} -test.bench=. -test.benchmem -test.v' - name: Run tests if: | !cancelled() && matrix.arch != '386' && matrix.arch != 'x64' && steps.configure_environment.outputs.QEMU_EMULATION_WORKS uses: uraimo/run-on-arch-action@v2 with: arch: ${{ matrix.arch }} distro: bookworm dockerRunArgs: --mount type=bind,source="$(pwd)",target=/checkout,readonly shell: /bin/bash run: | set -euxo pipefail find /checkout -name '*.test' -type f -executable -print0 | \ xargs -t -0 -I '{}' sh -c '{} -test.v && {} -test.bench=. -test.benchmem -test.v' golang-github-petermattis-goid-0.0~git20240813.4fcff4a/.gitignore000066400000000000000000000000331465714455400243240ustar00rootroot00000000000000*~ *.test .*.swp .DS_Store golang-github-petermattis-goid-0.0~git20240813.4fcff4a/LICENSE000066400000000000000000000260751465714455400233570ustar00rootroot00000000000000Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. golang-github-petermattis-goid-0.0~git20240813.4fcff4a/README.md000066400000000000000000000003451465714455400236210ustar00rootroot00000000000000# goid ![Build Status](https://github.com/petermattis/goid/actions/workflows/go.yml/badge.svg) Programatically retrieve the current goroutine's ID. See [the CI configuration](.github/workflows/go.yml) for supported Go versions. golang-github-petermattis-goid-0.0~git20240813.4fcff4a/go.mod000066400000000000000000000000541465714455400234450ustar00rootroot00000000000000module github.com/petermattis/goid go 1.17 golang-github-petermattis-goid-0.0~git20240813.4fcff4a/goid.go000066400000000000000000000017741465714455400236220ustar00rootroot00000000000000// Copyright 2016 Peter Mattis. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. package goid import ( "bytes" "runtime" "strconv" ) func ExtractGID(s []byte) int64 { s = s[len("goroutine "):] s = s[:bytes.IndexByte(s, ' ')] gid, _ := strconv.ParseInt(string(s), 10, 64) return gid } // Parse the goid from runtime.Stack() output. Slow, but it works. func getSlow() int64 { var buf [64]byte return ExtractGID(buf[:runtime.Stack(buf[:], false)]) } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/goid_gccgo.go000066400000000000000000000014011465714455400247470ustar00rootroot00000000000000// Copyright 2018 Peter Mattis. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. //go:build gccgo // +build gccgo package goid //extern runtime.getg func getg() *g func Get() int64 { return getg().goid } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/goid_go1.3.c000066400000000000000000000013431465714455400243360ustar00rootroot00000000000000// Copyright 2015 Peter Mattis. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. // +build !go1.4 #include void ·Get(int64 ret) { ret = g->goid; USED(&ret); } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/goid_go1.3.go000066400000000000000000000013651465714455400245250ustar00rootroot00000000000000// Copyright 2015 Peter Mattis. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. //go:build !go1.4 // +build !go1.4 package goid // Get returns the id of the current goroutine. func Get() int64 golang-github-petermattis-goid-0.0~git20240813.4fcff4a/goid_go1.4.go000066400000000000000000000022451465714455400245240ustar00rootroot00000000000000// Copyright 2015 Peter Mattis. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. //go:build go1.4 && !go1.5 // +build go1.4,!go1.5 package goid import "unsafe" var pointerSize = unsafe.Sizeof(uintptr(0)) // Backdoor access to runtime·getg(). func getg() uintptr // in goid_go1.4.s // Get returns the id of the current goroutine. func Get() int64 { // The goid is the 16th field in the G struct where each field is a // pointer, uintptr or padded to that size. See runtime.h from the // Go sources. I'm not aware of a cleaner way to determine the // offset. return *(*int64)(unsafe.Pointer(getg() + 16*pointerSize)) } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/goid_go1.4.s000066400000000000000000000007621465714455400243630ustar00rootroot00000000000000// Copyright 2014 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Assembly to get into package runtime without using exported symbols. // See https://github.com/golang/go/blob/release-branch.go1.4/misc/cgo/test/backdoor/thunk.s // +build amd64 amd64p32 arm 386 // +build go1.4,!go1.5 #include "textflag.h" #ifdef GOARCH_arm #define JMP B #endif TEXT ·getg(SB),NOSPLIT,$0-0 JMP runtime·getg(SB) golang-github-petermattis-goid-0.0~git20240813.4fcff4a/goid_go1.5.go000066400000000000000000000015761465714455400245330ustar00rootroot00000000000000// Copyright 2016 Peter Mattis. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. //go:build (386 || amd64 || amd64p32 || arm || arm64 || s390x) && gc && go1.5 // +build 386 amd64 amd64p32 arm arm64 s390x // +build gc // +build go1.5 package goid // Defined in goid_go1.5.s. func getg() *g func Get() int64 { return getg().goid } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/goid_go1.5.s000066400000000000000000000022161465714455400243600ustar00rootroot00000000000000// Copyright 2021 Peter Mattis. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. // Assembly to mimic runtime.getg. //go:build (386 || amd64 || amd64p32 || arm || arm64 || s390x) && gc && go1.5 // +build 386 amd64 amd64p32 arm arm64 s390x // +build gc // +build go1.5 #include "textflag.h" // func getg() *g TEXT ·getg(SB),NOSPLIT,$0-8 #ifdef GOARCH_386 MOVL (TLS), AX MOVL AX, ret+0(FP) #endif #ifdef GOARCH_amd64 MOVQ (TLS), AX MOVQ AX, ret+0(FP) #endif #ifdef GOARCH_arm MOVW g, ret+0(FP) #endif #ifdef GOARCH_arm64 MOVD g, ret+0(FP) #endif #ifdef GOARCH_s390x MOVD g, ret+0(FP) #endif RET golang-github-petermattis-goid-0.0~git20240813.4fcff4a/goid_slow.go000066400000000000000000000017231465714455400246600ustar00rootroot00000000000000// Copyright 2016 Peter Mattis. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. //go:build (go1.4 && !go1.5 && !amd64 && !amd64p32 && !arm && !386) || (go1.5 && !386 && !amd64 && !amd64p32 && !arm && !arm64 && !s390x) // +build go1.4,!go1.5,!amd64,!amd64p32,!arm,!386 go1.5,!386,!amd64,!amd64p32,!arm,!arm64,!s390x package goid // Get returns the id of the current goroutine. func Get() int64 { return getSlow() } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/goid_test.go000066400000000000000000000021721465714455400246520ustar00rootroot00000000000000// Copyright 2015 Peter Mattis. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. package goid import ( "fmt" "testing" ) func TestGet(t *testing.T) { ch := make(chan *string, 100) for i := 0; i < cap(ch); i++ { go func(i int) { goid := Get() expected := getSlow() if goid == expected { ch <- nil return } s := fmt.Sprintf("Expected %d, but got %d", expected, goid) ch <- &s }(i) } for i := 0; i < cap(ch); i++ { val := <-ch if val != nil { t.Fatal(*val) } } } func BenchmarkGet(b *testing.B) { for i := 0; i < b.N; i++ { Get() } } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/runtime_gccgo_go1.8.go000066400000000000000000000005561465714455400264360ustar00rootroot00000000000000//go:build gccgo && go1.8 // +build gccgo,go1.8 package goid // https://github.com/gcc-mirror/gcc/blob/releases/gcc-7/libgo/go/runtime/runtime2.go#L329-L354 type g struct { _panic uintptr _defer uintptr m uintptr syscallsp uintptr syscallpc uintptr param uintptr atomicstatus uint32 goid int64 // Here it is! } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/runtime_go1.23.go000066400000000000000000000011041465714455400253370ustar00rootroot00000000000000//go:build gc && go1.23 // +build gc,go1.23 package goid type stack struct { lo uintptr hi uintptr } type gobuf struct { sp uintptr pc uintptr g uintptr ctxt uintptr ret uintptr lr uintptr bp uintptr } type g struct { stack stack stackguard0 uintptr stackguard1 uintptr _panic uintptr _defer uintptr m uintptr sched gobuf syscallsp uintptr syscallpc uintptr syscallbp uintptr stktopsp uintptr param uintptr atomicstatus uint32 stackLock uint32 goid int64 // Here it is! } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/runtime_go1.5.go000066400000000000000000000026151465714455400252670ustar00rootroot00000000000000// Copyright 2016 Peter Mattis. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. //go:build go1.5 && !go1.6 // +build go1.5,!go1.6 package goid // Just enough of the structs from runtime/runtime2.go to get the offset to goid. // See https://github.com/golang/go/blob/release-branch.go1.5/src/runtime/runtime2.go type stack struct { lo uintptr hi uintptr } type gobuf struct { sp uintptr pc uintptr g uintptr ctxt uintptr ret uintptr lr uintptr bp uintptr } type g struct { stack stack stackguard0 uintptr stackguard1 uintptr _panic uintptr _defer uintptr m uintptr stackAlloc uintptr sched gobuf syscallsp uintptr syscallpc uintptr stkbar []uintptr stkbarPos uintptr param uintptr atomicstatus uint32 stackLock uint32 goid int64 // Here it is! } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/runtime_go1.6.go000066400000000000000000000014521465714455400252660ustar00rootroot00000000000000//go:build gc && go1.6 && !go1.9 // +build gc,go1.6,!go1.9 package goid // Just enough of the structs from runtime/runtime2.go to get the offset to goid. // See https://github.com/golang/go/blob/release-branch.go1.6/src/runtime/runtime2.go type stack struct { lo uintptr hi uintptr } type gobuf struct { sp uintptr pc uintptr g uintptr ctxt uintptr ret uintptr lr uintptr bp uintptr } type g struct { stack stack stackguard0 uintptr stackguard1 uintptr _panic uintptr _defer uintptr m uintptr stackAlloc uintptr sched gobuf syscallsp uintptr syscallpc uintptr stkbar []uintptr stkbarPos uintptr stktopsp uintptr param uintptr atomicstatus uint32 stackLock uint32 goid int64 // Here it is! } golang-github-petermattis-goid-0.0~git20240813.4fcff4a/runtime_go1.9.go000066400000000000000000000010771465714455400252740ustar00rootroot00000000000000//go:build gc && go1.9 && !go1.23 // +build gc,go1.9,!go1.23 package goid type stack struct { lo uintptr hi uintptr } type gobuf struct { sp uintptr pc uintptr g uintptr ctxt uintptr ret uintptr lr uintptr bp uintptr } type g struct { stack stack stackguard0 uintptr stackguard1 uintptr _panic uintptr _defer uintptr m uintptr sched gobuf syscallsp uintptr syscallpc uintptr stktopsp uintptr param uintptr atomicstatus uint32 stackLock uint32 goid int64 // Here it is! }