Skip to content

Commit ecd59a9

Browse files
authored
Swift iOS Controller Async updates (#4620)
1 parent 4c01988 commit ecd59a9

File tree

1 file changed

+52
-55
lines changed

1 file changed

+52
-55
lines changed

swift/test/ios/TestDriverApp/ControllerI.swift

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ struct ProcessI: CommonProcess {
1313
_helper = helper
1414
}
1515

16-
func waitReady(timeout: Int32, current _: Ice.Current) throws {
17-
try _helper.waitReady(timeout: timeout)
16+
func waitReady(timeout: Int32, current _: Ice.Current) async throws {
17+
try await _helper.waitReady(timeout: timeout)
18+
1819
}
1920

20-
func waitSuccess(timeout: Int32, current _: Ice.Current) throws -> Int32 {
21-
return try _helper.waitSuccess(timeout: timeout)
21+
func waitSuccess(timeout: Int32, current _: Ice.Current) async throws -> Int32 {
22+
return try await _helper.waitSuccess(timeout: timeout)
2223
}
2324

2425
func terminate(current: Ice.Current) throws -> String {
@@ -134,31 +135,28 @@ class ControllerI {
134135
class ControllerHelperI: ControllerHelper, TextWriter, @unchecked Sendable {
135136
private let _view: ViewController
136137
private let _args: [String]
137-
private var _ready: Bool
138-
private var _completed: Bool
139-
private var _status: Int32
140-
private var _out: String
141-
private var _communicator: Ice.Communicator!
142-
private let _semaphore: DispatchSemaphore
138+
private var _communicator: Ice.Communicator? = nil
143139
private let _exe: String
144140
private let _testName: String
141+
private var _out: String = ""
142+
143+
private struct State {
144+
var isReady = false
145+
var completedStatus: Int32?
146+
}
147+
private let _lock = Mutex(State())
145148

146149
public init(view: ViewController, testName: String, args: [String], exe: String) {
147150
_view = view
148151
_testName = testName
149152
_args = args
150-
_ready = false
151-
_completed = false
152-
_status = 1
153-
_out = ""
154-
_communicator = nil
155153
_exe = exe
156-
_semaphore = DispatchSemaphore(value: 0)
157154
}
158155

159156
public func serverReady() {
160-
_ready = true
161-
_semaphore.signal()
157+
_lock.withLock {
158+
$0.isReady = true
159+
}
162160
}
163161

164162
public func communicatorInitialized(communicator: Ice.Communicator) {
@@ -177,10 +175,10 @@ class ControllerHelperI: ControllerHelper, TextWriter, @unchecked Sendable {
177175
write("\(msg)\n")
178176
}
179177

180-
public func completed(status: Int32) {
181-
_completed = true
182-
_status = status
183-
_semaphore.signal()
178+
public func completed(status: Int32) async {
179+
_lock.withLock {
180+
$0.completedStatus = status
181+
}
184182
}
185183

186184
public func run() {
@@ -193,10 +191,10 @@ class ControllerHelperI: ControllerHelper, TextWriter, @unchecked Sendable {
193191
testHelper.setControllerHelper(controllerHelper: self)
194192
testHelper.setWriter(writer: self)
195193
try await testHelper.run(args: self._args)
196-
self.completed(status: 0)
194+
await completed(status: 0)
197195
} catch {
198196
self.writeLine("Error: \(error)")
199-
self.completed(status: 1)
197+
await completed(status: 1)
200198
}
201199
}
202200
}
@@ -208,47 +206,46 @@ class ControllerHelperI: ControllerHelper, TextWriter, @unchecked Sendable {
208206
communicator.shutdown()
209207
}
210208

211-
public func waitReady(timeout: Int32) throws {
212-
var ex: Error?
213-
do {
214-
while !_ready, !_completed {
215-
if _semaphore.wait(timeout: .now() + Double(timeout)) == .timedOut {
216-
throw CommonProcessFailedException(
217-
reason: "timed out waiting for the process to be ready")
218-
}
209+
public func waitReady(timeout: Int32) async throws {
210+
let deadline = ContinuousClock.now + .seconds(Int(timeout))
211+
212+
while ContinuousClock.now < deadline {
213+
// Check current state
214+
let state = _lock.withLock { $0 }
215+
216+
if state.isReady {
217+
return
219218
}
220219

221-
if _completed, _status != 0 {
222-
throw CommonProcessFailedException(reason: _out)
220+
if let status = state.completedStatus {
221+
if status == 0 {
222+
return
223+
} else {
224+
throw CommonProcessFailedException(reason: self._out)
225+
}
223226
}
224-
} catch {
225-
ex = error
226-
}
227-
if let ex = ex {
228-
throw ex
227+
228+
// Brief sleep to avoid busy waiting
229+
try await Task.sleep(for: .milliseconds(100))
229230
}
231+
232+
throw CommonProcessFailedException(reason: "timed out waiting for the process to be ready")
230233
}
231234

232-
public func waitSuccess(timeout: Int32) throws -> Int32 {
233-
var ex: Error?
234-
do {
235-
while !_completed {
236-
if _semaphore.wait(timeout: .now() + Double(timeout)) == .timedOut {
237-
throw CommonProcessFailedException(reason: "timed out waiting for the process to succeed")
238-
}
239-
}
235+
public func waitSuccess(timeout: Int32) async throws -> Int32 {
236+
let deadline = ContinuousClock.now + .seconds(Int(timeout))
240237

241-
if _completed, _status != 0 {
242-
throw CommonProcessFailedException(reason: _out)
238+
while ContinuousClock.now < deadline {
239+
// Check if completed
240+
if let status = _lock.withLock({ $0.completedStatus }) {
241+
return status
243242
}
244-
} catch {
245-
ex = error
246-
}
247243

248-
if let ex = ex {
249-
throw ex
244+
// Brief sleep to avoid busy waiting
245+
try await Task.sleep(for: .milliseconds(100))
250246
}
251-
return _status
247+
248+
throw CommonProcessFailedException(reason: "timed out waiting for the process to succeed")
252249
}
253250

254251
public func getOutput() -> String {

0 commit comments

Comments
 (0)