Reviewers: bradfitz, minux, rsc,
Message:
Hello [email protected], [email protected], [email protected] (cc:
[email protected]),
I'd like you to review this change to
https://code.google.com/p/go
Description:
net: align deadline fields on 8 byte boundaries
This is a workaround for issue 599. Assuming the test in
fd_unix_test.go passes, then 64bit atomics should be safe
to use on the deadline fields, nee CL 6855110.
sizeof netFD on 386 is 112 bytes.
Please review this at https://codereview.appspot.com/6842127/
Affected files:
M src/pkg/net/fd_unix.go
M src/pkg/net/fd_unix_test.go
Index: src/pkg/net/fd_unix.go
===================================================================
--- a/src/pkg/net/fd_unix.go
+++ b/src/pkg/net/fd_unix.go
@@ -17,6 +17,20 @@
// Network file descriptor.
type netFD struct {
+ // 64 bit atomic operations require the value to be aligned on an
+ // 8 byte boundary. Placing these fields at the top of the struct
+ // guarantees they will be aligned so long as the heap allocator
+ // aligns to 8 byte boundaries. The length of the struct must
+ // also be a multiple of 8 to ensure that an array of netFD
+ // structs is correctly aligned. A test in fd_unix_test.go ensures
+ // this holds true on all architectures.
+ //
+ // TODO(dfc) move these fields back to their original position
+ // when 599 is fixed.
+
+ // read and write deadlines
+ rdeadline, wdeadline int64 // nsec since 1970, 0 if not set
+
// locking/lifetime of sysfd
sysmu sync.Mutex
sysref int
@@ -37,11 +51,8 @@
laddr Addr
raddr Addr
- // owned by client
- rdeadline int64
- rio sync.Mutex
- wdeadline int64
- wio sync.Mutex
+ // serialize access to Read and Write methods
+ rio, wio sync.Mutex
// owned by fd wait server
ncr, ncw int
Index: src/pkg/net/fd_unix_test.go
===================================================================
--- a/src/pkg/net/fd_unix_test.go
+++ b/src/pkg/net/fd_unix_test.go
@@ -10,6 +10,7 @@
"io"
"syscall"
"testing"
+ "unsafe"
)
// Issue 3590. netFd.AddFD should return an error
@@ -104,3 +105,17 @@
}
}
}
+
+// see comment in fd_unix.go
+func TestDeadlineFieldAlignment(t *testing.T) {
+ var fd netFD
+ if size := unsafe.Sizeof(fd) % 8; size != 0 {
+ t.Errorf("size of fd %% 8 is %d not 0", size)
+ }
+ if offset := unsafe.Offsetof(fd.rdeadline) % 8; offset != 0 {
+ t.Errorf("offset of fd.rdeadline %% 8 is %d not 0", offset)
+ }
+ if offset := unsafe.Offsetof(fd.wdeadline) % 8; offset != 0 {
+ t.Errorf("offset of fd.wdeadline %% 8 is %d not 0", offset)
+ }
+}