Skip to content

Commit

Permalink
gc: fix missing pthread registration causing unknown thread error
Browse files Browse the repository at this point in the history
- Use `GC_pthread_create` instead of `pthread_create` when GC is
  enabled.
- Remove the `// +build` constraint, which has been deprecated since Go
  1.17.
  • Loading branch information
aofei committed Aug 15, 2024
1 parent 4501519 commit fe871f4
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 21 deletions.
1 change: 0 additions & 1 deletion c/c_default.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !linux
// +build !linux

/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
Expand Down
1 change: 0 additions & 1 deletion c/c_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build linux
// +build linux

/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
Expand Down
11 changes: 11 additions & 0 deletions c/pthread/_pthread/pthread_gc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#define GC_THREADS
#include <gc.h>
#include <pthread.h>

int llgoPthreadCreate(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) {
return GC_pthread_create(thread, attr, start_routine, arg);
}

int llgoPthreadJoin(pthread_t thread, void **retval) {
return GC_pthread_join(thread, retval);
}
9 changes: 9 additions & 0 deletions c/pthread/_pthread/pthread_nogc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <pthread.h>

int llgoPthreadCreate(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) {
return pthread_create(thread, attr, start_routine, arg);
}

int llgoPthreadJoin(pthread_t thread, void **retval) {
return pthread_join(thread, retval);
}
8 changes: 2 additions & 6 deletions c/pthread/pthread.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import (
"github.com/goplus/llgo/c"
)

const (
LLGoPackage = "decl"
)

func __noop__() c.Int {
return 0
}
Expand Down Expand Up @@ -65,7 +61,7 @@ type Thread = *aThread
//
// See https://man7.org/linux/man-pages/man3/pthread_create.3.html
//
//go:linkname Create C.pthread_create
//go:linkname Create C.llgoPthreadCreate
func Create(pthread *Thread, attr *Attr, routine func(c.Pointer) c.Pointer, arg c.Pointer) c.Int

// The pthread_join() function waits for the thread specified by
Expand All @@ -86,7 +82,7 @@ func Create(pthread *Thread, attr *Attr, routine func(c.Pointer) c.Pointer, arg
//
// See https://man7.org/linux/man-pages/man3/pthread_join.3.html
//
//go:linkname Join C.pthread_join
//go:linkname Join C.llgoPthreadJoin
func Join(thread Thread, retval *c.Pointer) c.Int

// The pthread_exit() function terminates the calling thread and
Expand Down
24 changes: 24 additions & 0 deletions c/pthread/pthread_gc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build !nogc

/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* 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.
*/

package pthread

const (
LLGoFiles = "$(pkg-config --cflags bdw-gc): _pthread/pthread_gc.c"
LLGoPackage = "link: $(pkg-config --libs bdw-gc); -lgc"
)
24 changes: 24 additions & 0 deletions c/pthread/pthread_nogc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build nogc

/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* 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.
*/

package pthread

const (
LLGoFiles = "_pthread/pthread_nogc.c"
LLGoPackage = "link"
)
4 changes: 2 additions & 2 deletions cl/_testgo/goroutine/out.ll
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ _llgo_0:
%15 = getelementptr inbounds { { ptr, ptr }, %"github.com/goplus/llgo/internal/runtime.String" }, ptr %13, i32 0, i32 1
store %"github.com/goplus/llgo/internal/runtime.String" %12, ptr %15, align 8
%16 = alloca i8, i64 8, align 1
%17 = call i32 @pthread_create(ptr %16, ptr null, ptr @"main._llgo_routine$1", ptr %13)
%17 = call i32 @llgoPthreadCreate(ptr %16, ptr null, ptr @"main._llgo_routine$1", ptr %13)
br label %_llgo_3

_llgo_1: ; preds = %_llgo_3
Expand Down Expand Up @@ -104,7 +104,7 @@ _llgo_0:

declare void @free(ptr)

declare i32 @pthread_create(ptr, ptr, ptr, ptr)
declare i32 @llgoPthreadCreate(ptr, ptr, ptr, ptr)

declare void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String")

Expand Down
1 change: 0 additions & 1 deletion cpp/std/std_gc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !nogc
// +build !nogc

/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
Expand Down
1 change: 0 additions & 1 deletion cpp/std/std_nogc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build nogc
// +build nogc

/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
Expand Down
1 change: 0 additions & 1 deletion internal/aliases/aliases_go121.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// license that can be found in the LICENSE file.

//go:build !go1.22
// +build !go1.22

package aliases

Expand Down
1 change: 0 additions & 1 deletion internal/aliases/aliases_go122.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// license that can be found in the LICENSE file.

//go:build go1.22
// +build go1.22

package aliases

Expand Down
7 changes: 4 additions & 3 deletions internal/runtime/goarch/endian_big.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//go:build 386 || amd64 || arm || arm64 || ppc64le || mips64le || mipsle || riscv64 || wasm
// +build 386 amd64 arm arm64 ppc64le mips64le mipsle riscv64 wasm

package goarch

const BigEndian = true
const LittleEndian = false
const (
BigEndian = true
LittleEndian = false
)
1 change: 0 additions & 1 deletion internal/runtime/goarch/endian_little.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build ppc64 || s390x || mips || mips64
// +build ppc64 s390x mips mips64

package goarch

Expand Down
1 change: 0 additions & 1 deletion internal/runtime/z_gc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !nogc
// +build !nogc

/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
Expand Down
1 change: 0 additions & 1 deletion internal/runtime/z_nogc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build nogc
// +build nogc

/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion ssa/goroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (p Program) tyPthreadCreate() *types.Signature {
}

func (b Builder) pthreadCreate(pp, attr, routine, arg Expr) Expr {
fn := b.Pkg.cFunc("pthread_create", b.Prog.tyPthreadCreate())
fn := b.Pkg.cFunc("llgoPthreadCreate", b.Prog.tyPthreadCreate())
return b.Call(fn, pp, attr, routine, arg)
}

Expand Down

0 comments on commit fe871f4

Please sign in to comment.