mirror of
https://github.com/golang/go
synced 2024-11-26 15:46:54 -07:00
ef0dedce87
In a C thread, it's necessary to acquire an extra M by using needm while invoking a Go function from C. But, needm and dropm are heavy costs due to the signal-related syscalls.
So, we change to not dropm while returning back to C, which means binding the extra M to the C thread until it exits, to avoid needm and dropm on each C to Go call.
Instead, we only dropm while the C thread exits, so the extra M won't leak.
When invoking a Go function from C:
Allocate a pthread variable using pthread_key_create, only once per shared object, and register a thread-exit-time destructor.
And store the g0 of the current m into the thread-specified value of the pthread key, only once per C thread, so that the destructor will put the extra M back onto the extra M list while the C thread exits.
When returning back to C:
Skip dropm in cgocallback, when the pthread variable has been created, so that the extra M will be reused the next time invoke a Go function from C.
This is purely a performance optimization. The old version, in which needm & dropm happen on each cgo call, is still correct too, and we have to keep the old version on systems with cgo but without pthreads, like Windows.
This optimization is significant, and the specific value depends on the OS system and CPU, but in general, it can be considered as 10x faster, for a simple Go function call from a C thread.
For the newly added BenchmarkCGoInCThread, some benchmark results:
1. it's 28x faster, from 3395 ns/op to 121 ns/op, in darwin OS & Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
2. it's 6.5x faster, from 1495 ns/op to 230 ns/op, in Linux OS & Intel(R) Xeon(R) CPU E5-2630 0 @ 2.30GHz
Fixes #51676
Change-Id: I380702fe2f9b6b401b2d6f04b0aba990f4b9ee6c
GitHub-Last-Rev: 93dc64ad98
GitHub-Pull-Request: golang/go#51679
Reviewed-on: https://go-review.googlesource.com/c/go/+/392854
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: thepudds <thepudds1460@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
60 lines
1.1 KiB
C
60 lines
1.1 KiB
C
// Copyright 2013 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.
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <windows.h>
|
|
#include <process.h>
|
|
#include "_cgo_export.h"
|
|
|
|
__stdcall
|
|
static unsigned int
|
|
addThread(void *p)
|
|
{
|
|
int i, max;
|
|
|
|
max = *(int*)p;
|
|
for(i=0; i<max; i++)
|
|
Add(i);
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
doAdd(int max, int nthread)
|
|
{
|
|
enum { MaxThread = 20 };
|
|
int i;
|
|
uintptr_t thread_id[MaxThread];
|
|
|
|
if(nthread > MaxThread)
|
|
nthread = MaxThread;
|
|
for(i=0; i<nthread; i++)
|
|
thread_id[i] = _beginthreadex(0, 0, addThread, &max, 0, 0);
|
|
for(i=0; i<nthread; i++) {
|
|
WaitForSingleObject((HANDLE)thread_id[i], INFINITE);
|
|
CloseHandle((HANDLE)thread_id[i]);
|
|
}
|
|
}
|
|
|
|
__stdcall
|
|
static unsigned int
|
|
goDummyCallbackThread(void* p)
|
|
{
|
|
int i, max;
|
|
|
|
max = *(int*)p;
|
|
for(i=0; i<max; i++)
|
|
goDummy();
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
callGoInCThread(int max)
|
|
{
|
|
uintptr_t thread_id;
|
|
thread_id = _beginthreadex(0, 0, goDummyCallbackThread, &max, 0, 0);
|
|
WaitForSingleObject((HANDLE)thread_id, INFINITE);
|
|
CloseHandle((HANDLE)thread_id);
|
|
return max;
|
|
}
|