-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: keyangxie <[email protected]>
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package nvml | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sync" | ||
) | ||
|
||
var ( | ||
nvmlInitCounter int | ||
mux sync.Mutex | ||
) | ||
|
||
func InitCounter() (cleanup func(), err error) { | ||
mux.Lock() | ||
if nvmlInitCounter < 0 { | ||
count := fmt.Sprintf("%d", nvmlInitCounter) | ||
err = fmt.Errorf("ShutdownCounter() is called %s times, before InitCounter()", count[1:]) | ||
} | ||
if nvmlInitCounter == 0 { | ||
err = Init() | ||
} | ||
nvmlInitCounter += 1 | ||
mux.Unlock() | ||
|
||
return func() { | ||
if err := ShutdownCounter(); err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to shutdown DCGM with error: `%v`", err) | ||
} | ||
}, err | ||
} | ||
|
||
func ShutdownCounter() (err error) { | ||
mux.Lock() | ||
if nvmlInitCounter <= 0 { | ||
err = fmt.Errorf("Init() needs to be called before Shutdown()") | ||
} | ||
if nvmlInitCounter == 1 { | ||
err = Shutdown() | ||
} | ||
nvmlInitCounter -= 1 | ||
mux.Unlock() | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters