Skip to content

Commit b94596a

Browse files
committed
Move cdata logic to assembly in Go 1.4+.
Fixes go-qml#106.
1 parent f0a005b commit b94596a

File tree

6 files changed

+73
-12
lines changed

6 files changed

+73
-12
lines changed

cdata/cdata.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// This package supports the implementation of the qml package,
2-
// and must not be used by itself.
1+
// Package cdata supports the implementation of the qml package.
32
package cdata
43

54
func Ref() uintptr

cdata/cdata.c renamed to cdata/cdata12.c

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !go1.4
2+
13
#include "runtime.h"
24

35
void ·Ref(uintptr ref) {

cdata/cdata14_386.s

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// +build go1.4
2+
3+
#include "textflag.h"
4+
5+
TEXT ·Ref(SB),NOSPLIT,$4-4
6+
CALL runtime·acquirem(SB)
7+
MOVL 0(SP), AX
8+
MOVL AX, ret+0(FP)
9+
CALL runtime·releasem(SB)
10+
RET
11+
12+
TEXT ·Addrs(SB),NOSPLIT,$0-8
13+
MOVL $runtime·main(SB), AX
14+
MOVL AX, ret+0(FP)
15+
MOVL $runtime·main_main(SB), AX
16+
MOVL AX, ret+8(FP)
17+
RET

cdata/cdata14_amd64.s

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// +build go1.4
2+
3+
#include "textflag.h"
4+
5+
TEXT ·Ref(SB),NOSPLIT,$8-8
6+
CALL runtime·acquirem(SB)
7+
MOVQ 0(SP), AX
8+
MOVQ AX, ret+0(FP)
9+
CALL runtime·releasem(SB)
10+
RET
11+
12+
TEXT ·Addrs(SB),NOSPLIT,$0-16
13+
MOVQ $runtime·main(SB), AX
14+
MOVQ AX, ret+0(FP)
15+
MOVQ $runtime·main_main(SB), AX
16+
MOVQ AX, ret+8(FP)
17+
RET

cdata/cdata14_arm.s

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// +build go1.4
2+
3+
#include "textflag.h"
4+
5+
TEXT ·Ref(SB),NOSPLIT,$4-4
6+
BL runtime·acquirem(SB)
7+
MOVW 4(R13), R0
8+
MOVW R0, ret+0(FP)
9+
MOVW R0, 4(R13)
10+
BL runtime·releasem(SB)
11+
RET
12+
13+
TEXT ·Addrs(SB),NOSPLIT,$0-8
14+
MOVW $runtime·main(SB), R0
15+
MOVW R0, ret+0(FP)
16+
MOVW $runtime·main_main(SB), R0
17+
MOVW R0, ret+4(FP)
18+
RET

cdata/cdata_test.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,37 @@ import (
66
"testing"
77
)
88

9+
type refPair struct {
10+
ref1, ref2 uintptr
11+
}
12+
913
func TestRef(t *testing.T) {
1014
const N = 10
1115
runtime.LockOSThread()
16+
exit := sync.WaitGroup{}
17+
exit.Add(1)
18+
defer exit.Done()
1219
wg := sync.WaitGroup{}
1320
wg.Add(N)
14-
ch := make(chan uintptr)
21+
ch := make(chan refPair)
1522
for i := 0; i < N; i++ {
1623
go func() {
1724
runtime.LockOSThread()
1825
wg.Done()
19-
ch <- Ref()
20-
wg.Wait()
26+
ch <- refPair{Ref(), Ref()}
27+
exit.Wait()
2128
}()
2229
}
2330
wg.Wait()
24-
refs := []uintptr{Ref()}
31+
refs := make(map[uintptr]bool)
2532
for i := 0; i < N; i++ {
26-
chref := <-ch
27-
for _, ref := range refs {
28-
if chref == ref {
29-
t.Fatalf("found duplicated ref: %d == %d", chref, ref)
30-
}
33+
pair := <-ch
34+
if pair.ref1 != pair.ref2 {
35+
t.Fatalf("found inconsistent ref: %d != %d", pair.ref1, pair.ref2)
36+
}
37+
if refs[pair.ref1] {
38+
t.Fatalf("found duplicated ref: %d", pair.ref1)
3139
}
32-
refs = append(refs, chref)
40+
refs[pair.ref1] = true
3341
}
3442
}

0 commit comments

Comments
 (0)