Skip to content

Instantly share code, notes, and snippets.

View aarzilli's full-sized avatar

Alessandro Arzilli aarzilli

View GitHub Profile

Under the assumption that the range-over-function proposal is accepted and that the following code:

for x := range seq(3) {
	fmt.Println(x)
}

compiles equivalently to:

seq(3)(func(x int) bool {

fmt.Println(x)

package main
import (
"fmt"
"reflect"
)
type Bstruct struct {
n float64
}
#include <stdio.h>
#include <pthread.h>
int N = 100000;
//int N = 20;
pthread_mutex_t ping_lock = PTHREAD_MUTEX_INITIALIZER, pong_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t ping_cond, pong_cond;
pthread_t ping_thread, pong_thread;
int can_ping = 1, can_pong = 0;
package main
import (
"runtime"
"runtime/pprof"
_ "runtime/trace"
"fmt"
"time"
"os"
)
append.go:29 0x454e8d main._Append
append.go:29 0x454e95 main._Append
append.go:29 0x4550ad main._Append
append.go:29 0x4550b5 main._Append
chans.go:412 0x48c4e2 main.TestReadAll
chans.go:412 0x48c4e4 main.TestReadAll
chans.go:412 0x48c7c9 main.TestMerge
chans.go:412 0x48c7ce main.TestMerge
chans.go:412 0x48c912 main.TestMerge
chans.go:412 0x48c914 main.TestMerge

Proposal: runtime: allow N goroutines to be simultaneously locked to the same OS thread

Summary

The aim of this proposal is to extend the LockOSThread mechanism so that it is possible to create a N:1 association between N goroutines and 1 OS thread, by adding the following function to the runtime package

func LockToRecvOSThread[T any](ch <- chan T)

When LockToRecvOSThread is called the calling goroutine will be locked to the same thread as the goroutine receiving on ch, which is assumed to be already locked to a OS thread.

package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
// This requires tip of Go master branch for 1.18; `gotip` is a great tool to
// use.
//
// Run benchmark with
// $ gotip test -bench=. <filename>
package main
import (
"math/rand"
package main
import (
"fmt"
"runtime"
)
var globalvar1 = 0
var globalvar2 = 0
VSCodeGoLand
Version VSCode 1.34.0, VSCode-go 0.10.2 GoLand 2019.2 EAP (192.4205.56) Vim-go 1.20 (vim 8.1)