Skip to content

Commit

Permalink
Merge pull request shadowsocks#168 from mcfans/develop
Browse files Browse the repository at this point in the history
the ping process thread-safe now
  • Loading branch information
qinyuhang authored Jun 9, 2017
2 parents 9eeb023 + 2d331b2 commit 15833de
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions ShadowsocksX-NG/PingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,25 @@ class PingServers:NSObject{
if let latency = $0{
self.SerMgr.profiles[k].latency = String(latency)

synchronized(lock: result as AnyObject){
result.append(k,latency)
}
DispatchQueue.main.async {
// do the UI update HERE
(NSApplication.shared().delegate as! AppDelegate).updateServersMenu()
(NSApplication.shared().delegate as! AppDelegate).updateRunningModeMenu()
}
}



})
}
// after two seconds ,time out
delay(3){
DispatchQueue.main.async {

for k in 0..<self.SerMgr.profiles.count {
if let late = self.SerMgr.profiles[k].latency{
if let latency = Double(late){
result.append((k,latency))
}
}
}


(NSApplication.shared().delegate as! AppDelegate).updateServersMenu()
(NSApplication.shared().delegate as! AppDelegate).updateRunningModeMenu()

// do the UI update HERE
if let min = result.min(by: {$0.1 < $1.1}){
self.fastest = String(describing: min.1)
Expand Down Expand Up @@ -218,11 +220,7 @@ typealias Task = (_ cancel : Bool) -> Void

}

func synchronized(lock: AnyObject, closure: () -> ()) {
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}

func cancel(_ task: Task?) {
task?(true)
}
Expand Down

0 comments on commit 15833de

Please sign in to comment.