Skip to content

Commit

Permalink
Merge pull request #2 from rickymohk/fight_with_github_actions
Browse files Browse the repository at this point in the history
Fight with GitHub actions
  • Loading branch information
rickymohk authored Sep 27, 2023
2 parents 5c417d0 + 74dea62 commit 02aa39c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Swift

on:
push:
branches: [ "main" ]
branches: [ "main", "fight_with_github_actions" ]
pull_request:
branches: [ "main" ]
branches: [ "main", "fight_with_github_actions" ]

jobs:
build:
Expand Down
43 changes: 27 additions & 16 deletions Sources/SwiftTaskQueue/SwiftTaskQueue.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Foundation

public class TaskQueue{
private class PendingTask{
let label:String?
Expand Down Expand Up @@ -36,6 +38,8 @@ public class TaskQueue{

private var scope: Task<Void,Never>?

private let initTaskDispatchQueue = DispatchQueue(label: "initTask")

private var initTask: Task<Void,Error>?

private var preInitPendingTasks: [PendingTask] = []
Expand Down Expand Up @@ -114,18 +118,25 @@ public class TaskQueue{
initTask = Task{
await withCheckedContinuation{ initScope(initContinuation: $0) }
//pendingTasks should be available since here
if let pendingTasksContinuation = pendingTasksContinuation
{
for pendingTask in preInitPendingTasks
// print("initScope done. pendingTasksContinuation=\(pendingTasksContinuation)")
try initTaskDispatchQueue.sync {
if let pendingTasksContinuation = pendingTasksContinuation
{

// print("yield preInitPendingTasks start \(preInitPendingTasks.count)")
for pendingTask in preInitPendingTasks
{
pendingTasksContinuation.yield(pendingTask)
}
// print("yield preInitPendingTasks done \(preInitPendingTasks.count)")

}
else
{
pendingTasksContinuation.yield(pendingTask)
throw fatalError("pendingTasksContinuation not available after init")
}
}
else
{
throw fatalError("pendingTasksContinuation not available after init")
}

// print("initTask done")
initTask = nil
}
}
Expand All @@ -141,18 +152,18 @@ public class TaskQueue{

public func dispatch(label:String?=nil,block: @escaping () async throws -> Void)
{
if let pendingTasksContinuation = pendingTasksContinuation
if initTask == nil, let pendingTasksContinuation = pendingTasksContinuation
{
// print("yield directly \(label)")
pendingTasksContinuation.yield(AsyncTask(label: label, continuation: nil, block: block))
}
else
{
preInitPendingTasks.append(AsyncTask(label: label, continuation: nil, block: block))
// Task{
// await initTask.value
// pendingTasksContinuation?.yield(AsyncTask(label: label, continuation: nil, block: block))
// }

initTaskDispatchQueue.sync {
// print("append preInitPendingTasks \(label) start \(preInitPendingTasks.count)")
preInitPendingTasks.append(AsyncTask(label: label, continuation: nil, block: block))
// print("append preInitPendingTasks \(label) done \(preInitPendingTasks.count)")
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions Tests/SwiftTaskQueueTests/SwiftTaskQueueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ final class SwiftTaskQueueTests: XCTestCase {
let taskQueue = TaskQueue()

({
taskQueue.dispatch {
taskQueue.dispatch(label: "task1") {
await result.append("1")
}
taskQueue.dispatch {
taskQueue.dispatch(label: "task2") {
await result.append("2")
}
taskQueue.dispatch {
taskQueue.dispatch(label: "task3") {
await result.append("3")
}
taskQueue.dispatch {
taskQueue.dispatch(label: "task4") {
await result.append("4")
}
}())
Expand Down Expand Up @@ -178,7 +178,7 @@ final class SwiftTaskQueueTests: XCTestCase {
}
}

try? await Task.sleep(nanoseconds: 5000000000)
try? await Task.sleep(nanoseconds: 1000000000)
try? await taskQueue.dispatch {
print("ensure unwaited task1 is executed")
}
Expand Down

0 comments on commit 02aa39c

Please sign in to comment.