提交 fceff4ff 编写于 作者: Y Yanzhan Yang 提交者: GitHub

add nslock to load and clear method of runner (#1623)

上级 17fa2ebd
...@@ -40,6 +40,8 @@ import Foundation ...@@ -40,6 +40,8 @@ import Foundation
public let net: Net public let net: Net
let device: MTLDevice? let device: MTLDevice?
let numel: Int let numel: Int
private static let loadLock = NSLock()
private static let clearLock = NSLock()
/// 初始化函数 /// 初始化函数
/// ///
...@@ -60,10 +62,20 @@ import Foundation ...@@ -60,10 +62,20 @@ import Foundation
numel = net.inputDim.numel() numel = net.inputDim.numel()
} }
/// load 模型, 返回 true 可进行预测 /// load 模型, 返回 true 可进行预测,公共方法,保证线程安全
/// ///
/// - Returns: load 成功或失败 /// - Returns: load 成功或失败
@objc public func load() -> Bool { @objc public func load() -> Bool {
Runner.loadLock.lock()
let success = unSafeLoad()
Runner.loadLock.unlock()
return success
}
/// load 模型, 返回 true 可进行预测,不保证线程安全
///
/// - Returns: load 成功或失败
private func unSafeLoad() -> Bool {
guard let inDevice = device, let inQueue = queue else { guard let inDevice = device, let inQueue = queue else {
print(" paddle mobile gpu load error, need MTLCommandQueue") print(" paddle mobile gpu load error, need MTLCommandQueue")
return false return false
...@@ -139,9 +151,11 @@ import Foundation ...@@ -139,9 +151,11 @@ import Foundation
/// 清理内存, 调用此函数后, 不能再使用, 需重新 load /// 清理内存, 调用此函数后, 不能再使用, 需重新 load
@objc public func clear() { @objc public func clear() {
Runner.clearLock.lock()
executor?.clear() executor?.clear()
executor = nil executor = nil
program = nil program = nil
Runner.clearLock.unlock()
} }
/// 获取 texture, 对 texture 进行预处理, 预测时使用 /// 获取 texture, 对 texture 进行预处理, 预测时使用
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册