This patch to libgo replaces runtime/runtime1.goc with Go and C code. This is a step toward eliminating goc2c.
This drops the exported parfor code; it was needed for tests in the past, but no longer is. The Go 1.7 runtime no longer uses parfor. Bootstrapped and ran Go testsuite on x86_64-pc-linux-gnu. Committed to mainline. Ian
Index: gcc/go/gofrontend/MERGE =================================================================== --- gcc/go/gofrontend/MERGE (revision 242494) +++ gcc/go/gofrontend/MERGE (working copy) @@ -1,4 +1,4 @@ -31ff8c31d33c3e77cae4fd55445f12825eb92af5 +d9189ebc139ff739af956094626ccc5eb92c3091 The first line of this file holds the git revision number of the last merge done from the gofrontend repository. Index: libgo/Makefile.am =================================================================== --- libgo/Makefile.am (revision 242060) +++ libgo/Makefile.am (working copy) @@ -485,7 +485,6 @@ runtime_files = \ runtime/yield.c \ $(rtems_task_variable_add_file) \ malloc.c \ - runtime1.c \ $(runtime_getncpu_file) goc2c.$(OBJEXT): runtime/goc2c.c @@ -498,10 +497,6 @@ malloc.c: $(srcdir)/runtime/malloc.goc g ./goc2c $< > $@.tmp mv -f $@.tmp $@ -runtime1.c: $(srcdir)/runtime/runtime1.goc goc2c - ./goc2c $< > $@.tmp - mv -f $@.tmp $@ - %.c: $(srcdir)/runtime/%.goc goc2c ./goc2c $< > $@.tmp mv -f $@.tmp $@ Index: libgo/go/runtime/debug.go =================================================================== --- libgo/go/runtime/debug.go (revision 241341) +++ libgo/go/runtime/debug.go (working copy) @@ -4,6 +4,11 @@ package runtime +import ( + "runtime/internal/atomic" + "unsafe" +) + // GOMAXPROCS sets the maximum number of CPUs that can be executing // simultaneously and returns the previous setting. If n < 1, it does not // change the current setting. @@ -19,10 +24,18 @@ func GOMAXPROCS(n int) int func NumCPU() int // NumCgoCall returns the number of cgo calls made by the current process. -func NumCgoCall() int64 +func NumCgoCall() int64 { + var n int64 + for mp := (*m)(atomic.Loadp(unsafe.Pointer(allm()))); mp != nil; mp = mp.alllink { + n += int64(mp.ncgocall) + } + return n +} // NumGoroutine returns the number of goroutines that currently exist. -func NumGoroutine() int +func NumGoroutine() int { + return int(gcount()) +} // Get field tracking information. Only fields with a tag go:"track" // are tracked. This function will add every such field that is Index: libgo/go/runtime/error.go =================================================================== --- libgo/go/runtime/error.go (revision 241341) +++ libgo/go/runtime/error.go (working copy) @@ -133,7 +133,10 @@ type stringer interface { String() string } -func typestring(interface{}) string +func typestring(x interface{}) string { + e := efaceOf(&x) + return *e._type.string +} // For calling from C. // Prints an argument passed to panic. Index: libgo/go/runtime/export_test.go =================================================================== --- libgo/go/runtime/export_test.go (revision 241427) +++ libgo/go/runtime/export_test.go (working copy) @@ -21,11 +21,10 @@ import ( //var F64toint = f64toint //var Sqrt = sqrt -func golockedOSThread() bool - var Entersyscall = entersyscall var Exitsyscall = exitsyscall -var LockedOSThread = golockedOSThread + +// var LockedOSThread = lockedOSThread // var Xadduintptr = xadduintptr @@ -44,29 +43,6 @@ func LFStackPop(head *uint64) *LFNode { return (*LFNode)(unsafe.Pointer(lfstackpop(head))) } -type ParFor struct { - body func(*ParFor, uint32) - done uint32 - Nthr uint32 - thrseq uint32 - Cnt uint32 - wait bool -} - -func newParFor(nthrmax uint32) *ParFor -func parForSetup(desc *ParFor, nthr, n uint32, wait bool, body func(*ParFor, uint32)) -func parForDo(desc *ParFor) -func parForIters(desc *ParFor, tid uintptr) (uintptr, uintptr) - -var NewParFor = newParFor -var ParForSetup = parForSetup -var ParForDo = parForDo - -func ParForIters(desc *ParFor, tid uint32) (uint32, uint32) { - begin, end := parForIters(desc, uintptr(tid)) - return uint32(begin), uint32(end) -} - func GCMask(x interface{}) (ret []byte) { return nil } Index: libgo/go/runtime/extern.go =================================================================== --- libgo/go/runtime/extern.go (revision 241341) +++ libgo/go/runtime/extern.go (working copy) @@ -274,13 +274,11 @@ func SetFinalizer(obj interface{}, final // the actual system call. func KeepAlive(interface{}) -func getgoroot() string - // GOROOT returns the root of the Go tree. // It uses the GOROOT environment variable, if set, // or else the root used during the Go build. func GOROOT() string { - s := getgoroot() + s := gogetenv("GOROOT") if s != "" { return s } Index: libgo/go/runtime/stubs.go =================================================================== --- libgo/go/runtime/stubs.go (revision 242060) +++ libgo/go/runtime/stubs.go (working copy) @@ -501,6 +501,7 @@ func needm() func dropm() func sigprof() func mcount() int32 +func gcount() int32 // Signal trampoline, written in C. func sigtramp() Index: libgo/runtime/proc.c =================================================================== --- libgo/runtime/proc.c (revision 242060) +++ libgo/runtime/proc.c (working copy) @@ -2535,15 +2535,19 @@ runtime_Gosched(void) // Implementation of runtime.GOMAXPROCS. // delete when scheduler is even stronger -int32 -runtime_gomaxprocsfunc(int32 n) + +intgo runtime_GOMAXPROCS(intgo) + __asm__(GOSYM_PREFIX "runtime.GOMAXPROCS"); + +intgo +runtime_GOMAXPROCS(intgo n) { - int32 ret; + intgo ret; if(n > _MaxGomaxprocs) n = _MaxGomaxprocs; runtime_lock(&runtime_sched); - ret = runtime_gomaxprocs; + ret = (intgo)runtime_gomaxprocs; if(n <= 0 || n == ret) { runtime_unlock(&runtime_sched); return ret; @@ -2553,7 +2557,7 @@ runtime_gomaxprocsfunc(int32 n) runtime_acquireWorldsema(); g->m->gcing = 1; runtime_stopTheWorldWithSema(); - newprocs = n; + newprocs = (int32)n; g->m->gcing = 0; runtime_releaseWorldsema(); runtime_startTheWorldWithSema(); @@ -3499,6 +3503,58 @@ runtime_setmaxthreads(intgo in) return out; } +static intgo +procPin() +{ + M *mp; + + mp = runtime_m(); + mp->locks++; + return (intgo)(((P*)mp->p)->id); +} + +static void +procUnpin() +{ + runtime_m()->locks--; +} + +intgo sync_runtime_procPin(void) + __asm__ (GOSYM_PREFIX "sync.runtime_procPin"); + +intgo +sync_runtime_procPin() +{ + return procPin(); +} + +void sync_runtime_procUnpin(void) + __asm__ (GOSYM_PREFIX "sync.runtime_procUnpin"); + +void +sync_runtime_procUnpin() +{ + procUnpin(); +} + +intgo sync_atomic_runtime_procPin(void) + __asm__ (GOSYM_PREFIX "sync_atomic.runtime_procPin"); + +intgo +sync_atomic_runtime_procPin() +{ + return procPin(); +} + +void sync_atomic_runtime_procUnpin(void) + __asm__ (GOSYM_PREFIX "sync_atomic.runtime_procUnpin"); + +void +sync_atomic_runtime_procUnpin() +{ + procUnpin(); +} + void runtime_proc_scan(struct Workbuf** wbufp, void (*enqueue1)(struct Workbuf**, Obj)) { @@ -3589,3 +3645,11 @@ runtime_go_allgs() s.__capacity = allgcap; return s; } + +intgo NumCPU(void) __asm__ (GOSYM_PREFIX "runtime.NumCPU"); + +intgo +NumCPU() +{ + return (intgo)(runtime_ncpu); +} Index: libgo/runtime/runtime.h =================================================================== --- libgo/runtime/runtime.h (revision 242060) +++ libgo/runtime/runtime.h (working copy) @@ -315,7 +315,8 @@ void runtime_mprofinit(void); #define runtime_getcallersp(p) __builtin_frame_address(0) int32 runtime_mcount(void) __asm__ (GOSYM_PREFIX "runtime.mcount"); -int32 runtime_gcount(void); +int32 runtime_gcount(void) + __asm__ (GOSYM_PREFIX "runtime.gcount"); void runtime_mcall(void(*)(G*)); uint32 runtime_fastrand1(void) __asm__ (GOSYM_PREFIX "runtime.fastrand1"); int32 runtime_timediv(int64, int32, int32*) @@ -512,7 +513,6 @@ void runtime_semacquire(uint32 volatile __asm__ (GOSYM_PREFIX "runtime.semacquire"); void runtime_semrelease(uint32 volatile *) __asm__ (GOSYM_PREFIX "runtime.semrelease"); -int32 runtime_gomaxprocsfunc(int32 n); void runtime_procyield(uint32) __asm__(GOSYM_PREFIX "runtime.procyield"); void runtime_osyield(void) Index: libgo/runtime/runtime1.goc =================================================================== --- libgo/runtime/runtime1.goc (revision 241341) +++ libgo/runtime/runtime1.goc (working copy) @@ -1,82 +0,0 @@ -// Copyright 2010 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. - -package runtime -#include "runtime.h" -#include "arch.h" -#include "go-type.h" - -func GOMAXPROCS(n int) (ret int) { - ret = runtime_gomaxprocsfunc(n); -} - -func NumCPU() (ret int) { - ret = runtime_ncpu; -} - -func NumCgoCall() (ret int64) { - M *mp; - - ret = 0; - for(mp=runtime_atomicloadp(&runtime_allm); mp; mp=mp->alllink) - ret += mp->ncgocall; -} - -func newParFor(nthrmax uint32) (desc *ParFor) { - desc = runtime_parforalloc(nthrmax); -} - -func parForSetup(desc *ParFor, nthr uint32, n uint32, wait bool, body *byte) { - runtime_parforsetup(desc, nthr, n, wait, (const FuncVal*) body); -} - -func parForDo(desc *ParFor) { - runtime_parfordo(desc); -} - -func parForIters(desc *ParFor, tid uintptr) (start uintptr, end uintptr) { - runtime_parforiters(desc, tid, &start, &end); -} - -func typestring(e Eface) (s String) { - s = *((Type*)e._type)->__reflection; -} - -func golockedOSThread() (ret bool) { - ret = runtime_lockedOSThread(); -} - -func NumGoroutine() (ret int) { - ret = runtime_gcount(); -} - -func getgoroot() (out String) { - out = runtime_getenv("GOROOT"); -} - -func sync.runtime_procPin() (p int) { - M *mp; - - mp = runtime_m(); - // Disable preemption. - mp->locks++; - p = ((P*)mp->p)->id; -} - -func sync.runtime_procUnpin() { - runtime_m()->locks--; -} - -func sync_atomic.runtime_procPin() (p int) { - M *mp; - - mp = runtime_m(); - // Disable preemption. - mp->locks++; - p = ((P*)mp->p)->id; -} - -func sync_atomic.runtime_procUnpin() { - runtime_m()->locks--; -}