Skip to content

cgo: processes started from exported function are not scheduled correctly. #1560

@rogpeppe

Description

@rogpeppe
Unpack the attached archive and run gotest.
I would expect the output to be around 1000, as both
sleeps should sleep concurrently.
Instead it prints around 2000 for me - the sleeps execute
sequentially.

If GOMAXPROCS > 0, it works as expected.

6g macos d9ff478c4ed3+ tip

Here is the salient code from the archive:

package tst

//#include <unistd.h>
// extern void BackgroundSleep(int);
// void twoSleeps(int n){
//  BackgroundSleep(n);
//  sleep(n);
// }
import "C"

var sleepDone = make(chan bool)

func ParSleep(n int){
    C.twoSleeps(C.int(n))
    <-sleepDone
}

//export BackgroundSleep
func BackgroundSleep(n int){
    // This function should start running immediately,
    // but it actually starts after twoSleeps has returned.
    go func(){
        C.sleep(C.uint(n))
        sleepDone <- true
    }()
}


func TestParSleep(t *testing.T) {
    t0 := time.Nanoseconds()
    ParSleep(1)
    fmt.Printf("%d\n", (time.Nanoseconds() - t0) / 1e6)
}

Attachments:

  1. tstcgo.tgz (659 bytes)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions