Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

os: refactor to use runtime.Addcleanup #75039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cuiweixie wants to merge 1 commit into golang:master from cuiweixie:AddCleanup_os
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/os/file_unix.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type file struct {
nonblock bool // whether we set nonblocking mode
stdoutOrErr bool // whether this is stdout or stderr
appendMode bool // whether file is opened for appending
cleanup runtime.Cleanup
}

// fd is the Unix implementation of Fd.
Expand Down Expand Up @@ -221,7 +222,9 @@ func newFile(fd int, name string, kind newFileKind, nonBlocking bool) *File {
}
}

runtime.SetFinalizer(f.file, (*file).close)
f.file.cleanup = runtime.AddCleanup(f, func(f *file) {
f.close()
}, f.file)
return f
}

Expand Down Expand Up @@ -319,7 +322,7 @@ func (file *file) close() error {
}

// no need for a finalizer anymore
runtime.SetFinalizer(file, nil)
file.cleanup.Stop()
return err
}

Expand Down
7 changes: 5 additions & 2 deletions src/os/file_windows.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type file struct {
name string
dirinfo atomic.Pointer[dirInfo] // nil unless directory being read
appendMode bool // whether file is opened for appending
cleanup runtime.Cleanup
}

// fd is the Windows implementation of Fd.
Expand Down Expand Up @@ -68,7 +69,9 @@ func newFile(h syscall.Handle, name string, kind string, nonBlocking bool) *File
},
name: name,
}}
runtime.SetFinalizer(f.file, (*file).close)
f.file.cleanup = runtime.AddCleanup(f, func(f *file) {
f.close()
}, f.file)

// Ignore initialization errors.
// Assume any problems will show up in later I/O.
Expand Down Expand Up @@ -144,7 +147,7 @@ func (file *file) close() error {
}

// no need for a finalizer anymore
runtime.SetFinalizer(file, nil)
file.cleanup.Stop()
return err
}

Expand Down
11 changes: 6 additions & 5 deletions src/os/root_openat.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ type root struct {
// refs is incremented while an operation is using fd.
// closed is set when Close is called.
// fd is closed when closed is true and refs is 0.
mu sync.Mutex
fd sysfdType
refs int // number of active operations
closed bool // set when closed
mu sync.Mutex
fd sysfdType
refs int // number of active operations
closed bool // set when closed
cleanup runtime.Cleanup
}

func (r *root) Close() error {
Expand All @@ -35,7 +36,7 @@ func (r *root) Close() error {
syscall.Close(r.fd)
}
r.closed = true
runtime.SetFinalizer(r, nil) // no need for a finalizer any more
r.cleanup.Stop()
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion src/os/root_unix.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func newRoot(fd int, name string) (*Root, error) {
fd: fd,
name: name,
}}
runtime.SetFinalizer(r.root, (*root).Close)
r.root.cleanup = runtime.AddCleanup(r.root, func(_ int) {
r.root.Close()
}, 0)
return r, nil
}

Expand Down

AltStyle によって変換されたページ (->オリジナル) /