Skip to content

Commit be2b903

Browse files
committed
refactor: lazy close amd card fd
Signed-off-by: thxCode <thxcode0824@gmail.com>
1 parent c612f20 commit be2b903

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

binding/amdgpu/library_device.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ func (l *AMDGPU) Open(cardID uint32) (Device, Return) {
1919
if err != nil {
2020
return Device{}, ERROR_CARD_NOTFOUND
2121
}
22-
defer func() {
23-
_ = devFile.Close()
24-
}()
2522

2623
var (
2724
majorVersion uint32
@@ -31,15 +28,17 @@ func (l *AMDGPU) Open(cardID uint32) (Device, Return) {
3128

3229
ret := Return(amdgpuDeviceInitialize(int32(devFile.Fd()), &majorVersion, &minorVersion, &handle))
3330
if !ret.IsSuccess() {
31+
_ = devFile.Close()
3432
return Device{}, ret
3533
}
3634

37-
return Device{handle: handle, so: l.so}, SUCCESS
35+
return Device{devFile: devFile, handle: handle, so: l.so}, SUCCESS
3836
}
3937

4038
type Device struct {
41-
handle amdgpuDevice
42-
so binding.Library
39+
devFile *os.File
40+
handle amdgpuDevice
41+
so binding.Library
4342
}
4443

4544
// Free releases the device handle and any resources it holds.
@@ -48,6 +47,12 @@ func (l Device) Free() Return {
4847
return ERROR_FUNCTION_NOT_FOUND
4948
}
5049

50+
defer func() {
51+
if l.devFile != nil {
52+
_ = l.devFile.Close()
53+
}
54+
}()
55+
5156
ret := Return(amdgpuDeviceDeinitialize(l.handle))
5257
return ret
5358
}

0 commit comments

Comments
 (0)